Skip to content

Commit

Permalink
feat: support cusom proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Jan 7, 2022
1 parent cb4d222 commit 39c0c5c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ openssl req -new -x509 -sha256 -key server.key -out server.pem -days 3650
forward --tls-cert-file=server.pem --tls-key-file=server.key http://example.com
```

2. 自定义代理请求

```bash
# 代理 https://github.com
forward https://github.com
# 发起请求
curl http://0.0.0.0:80/api # 实际请求 https://github.com/api
# 发起自定义代理
curl -H "X-Proxy-Target: https://www.google.com" http://0.0.0.0/api # 实际请求 https://www.google.com/api
```

### 开源许可

The [MIT License](LICENSE)
11 changes: 11 additions & 0 deletions README_en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ openssl req -new -x509 -sha256 -key server.key -out server.pem -days 3650
forward --tls-cert-file=server.pem --tls-key-file=server.key http://example.com
```

2. Custom proxy

```bash
# proxy https://github.com
forward https://github.com
# send request
curl http://0.0.0.0:80/api # request https://github.com/api
# send custom request
curl -H "X-Proxy-Target: https://www.google.com" http://0.0.0.0/api # request https://www.google.com/api
```

### License

The [MIT License](LICENSE)
25 changes: 22 additions & 3 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ var (
regIntegrity = regexp.MustCompile(`\sintegrity="[^"]+"`)
)

const (
headerXProxyTarget = "X-Proxy-Target"
headerXOriginHost = "X-Origin-Host"
headerXProxyClient = "X-Proxy-Client"
)

type ProxyServer struct {
*ProxyServerOptions
proxy *httputil.ReverseProxy
Expand Down Expand Up @@ -149,9 +155,22 @@ func (p *ProxyServer) modifyRequest(req *http.Request) {
target = *u
}
}
} else if req.Header.Get(headerXProxyTarget) != "" {
targetUrl := req.Header.Get(headerXProxyTarget)

if u, err := url.Parse(targetUrl); err == nil {
if u.Scheme == "" {
if p.UseSSL {
u.Scheme = "https"
} else {
u.Scheme = "http"
}
}
target = *u
}
}

req.Header.Set("X-Origin-Host", req.Host)
req.Header.Set(headerXOriginHost, req.Host)
req.Host = target.Host
if isProxyUrl {
req.URL = &target
Expand Down Expand Up @@ -205,7 +224,7 @@ func (p *ProxyServer) modifyResponse(res *http.Response) error {
}
}

proxyHost := res.Request.Header.Get("X-Origin-Host") // localhost:8080 or localhost
proxyHost := res.Request.Header.Get(headerXOriginHost) // localhost:8080 or localhost

var hostName string

Expand All @@ -221,7 +240,7 @@ func (p *ProxyServer) modifyResponse(res *http.Response) error {
hostName = proxyHost
}

res.Header.Set("X-Proxy-Client", "Forward-Cli")
res.Header.Set(headerXProxyClient, "Forward-Cli")
res.Header.Del("Expect-CT")

// disable cache
Expand Down

0 comments on commit 39c0c5c

Please # to comment.