Skip to content

Commit

Permalink
feat:first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AiMuC committed Jul 14, 2023
0 parents commit fe08dc1
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BASE_URL=https://steamcommunity.com
ADDR=localhost:9797
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.idea
*.vscode
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Golang实现的简易反代服务器
- 默认代理[Steam](https://steamcommunity.com)
- 默认访问地址[localhost](http://localhost:9797/)
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module steam

go 1.20

require (
github.com/joho/godotenv v1.5.1
github.com/valyala/fasthttp v1.48.0
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/klauspost/compress v1.16.3 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.9.0 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.16.3 h1:XuJt9zzcnaz6a16/OU53ZjWp/v7/42WcR5t2a0PcNQY=
github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.48.0 h1:oJWvHb9BIZToTQS3MuQ2R3bJZiNSa2KiNdeI8A+79Tc=
github.com/valyala/fasthttp v1.48.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
57 changes: 57 additions & 0 deletions proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
_ "github.com/joho/godotenv/autoload"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/reuseport"
"log"
"net/url"
"steam/support"
)

func main() {
Addr := support.Env("ADDR", "localhost:9797").(string)
log.Println("代理服务器已启动,监听在 " + Addr)
ln, err := reuseport.Listen("tcp4", Addr)
if err != nil {
log.Fatal("无法监听端口:", err)
}
server := &fasthttp.Server{
Handler: proxyHandler,
}
if err := server.Serve(ln); err != nil {
log.Fatal("无法启动服务器:", err)
}
}

func proxyHandler(ctx *fasthttp.RequestCtx) {
BaseUrl := support.Env("BASE_URL", "https://steamcommunity.com").(string)
targetURL, err := url.Parse(BaseUrl)
if err != nil {
log.Fatal("无法解析目标URL:", err)
}
client := &fasthttp.Client{}
targetURL.Path = "/" + string(ctx.Path())
targetURL.RawQuery = string(ctx.QueryArgs().QueryString())
req := fasthttp.AcquireRequest()
req.SetRequestURI(targetURL.String())
req.Header.SetHost(targetURL.Host)
req.Header.SetMethodBytes(ctx.Method())
req.Header.SetReferer(BaseUrl)
req.Header.Set("Access-Control-Allow-Origin", "*")
req.Header.Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
req.Header.Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
req.SetBody(ctx.Request.Body())
resp := fasthttp.AcquireResponse()
if err := client.Do(req, resp); err != nil {
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
resp.Header.VisitAll(func(key, value []byte) {
ctx.Response.Header.SetBytesKV(key, value)
})
ctx.Response.SetStatusCode(resp.StatusCode())
ctx.Response.SetBody(resp.Body())
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
}
36 changes: 36 additions & 0 deletions support/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package support

import (
"os"
"regexp"
"strconv"
"strings"
)

// Env 获取配置文件(未获取到则使用第二个参数作为默认值)
func Env(name string, value ...any) any {
var env any
if len(value) == 1 {
env = value[0]
}
if localEnv := os.Getenv(name); localEnv != "" {
//处理Bool类型
if strings.ToUpper(localEnv) == "TRUE" {
env = true
return env
} else if strings.ToUpper(localEnv) == "FALSE" {
env = false
return env
}
//处理int类型
reg := regexp.MustCompile("^(?:[0-9]|[1-9][0-9]{1,3}|10000)$") // 匹配1-10000以内的全部数字
match := reg.MatchString(localEnv)
if match {
num, _ := strconv.Atoi(localEnv)
env = num
return env
}
env = localEnv
}
return env
}

0 comments on commit fe08dc1

Please sign in to comment.