转发请求时上报反向代理信息
This commit is contained in:
@@ -24,7 +24,6 @@ make gitea-pages
|
||||
|
||||
具体配置可查看 [`config.yaml`](./config.yaml)。
|
||||
|
||||
|
||||
### Page Config
|
||||
|
||||
在项目的 `gh-pages` 分支创建 `.pages.yaml`,填入如下内容
|
||||
@@ -47,6 +46,8 @@ ignore: .git/**,.pages.yaml
|
||||
- [x] CNAME 自定义域名
|
||||
- [x] 模板渲染
|
||||
- [x] 反向代理请求
|
||||
- [ ] 支持跨域
|
||||
- [ ] 支持自定义缓存策略 (http cache-control)
|
||||
- [ ] OAuth2 授权访问私有页面
|
||||
- [ ] ~~http01 自动签发证书~~: 交由 Caddy 完成
|
||||
- [ ] ~~Web 钩子触发更新~~: 对实时性需求不大
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
@@ -137,8 +138,16 @@ func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error
|
||||
request.RequestURI = request.URL.RequestURI()
|
||||
proxy := httputil.NewSingleHostReverseProxy(u)
|
||||
proxy.Transport = s.options.HttpClient.Transport
|
||||
|
||||
if host, _, err := net.SplitHostPort(request.RemoteAddr); err == nil {
|
||||
request.Header.Set("X-Real-IP", host)
|
||||
}
|
||||
request.Header.Set("X-Page-IP", utils.GetRemoteIP(request))
|
||||
request.Header.Set("X-Page-Refer", fmt.Sprintf("%s/%s/%s", meta.Owner, meta.Repo, meta.Path))
|
||||
request.Header.Set("X-Page-Host", request.Host)
|
||||
zap.L().Debug("命中反向代理", zap.Any("prefix", prefix), zap.Any("backend", backend),
|
||||
zap.Any("path", proxyPath), zap.Any("target", fmt.Sprintf("%s%s", u, targetPath)))
|
||||
// todo(security): 处理 websocket
|
||||
proxy.ServeHTTP(writer, request)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user