diff options
author | zeripath <art27@cantab.net> | 2022-08-21 19:20:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-21 19:20:43 +0100 |
commit | 943753f560fab8bb01946618b16c694bc2032827 (patch) | |
tree | d4faa0f8bde73d61ee00d7252ceb79f8334088b1 /modules/private | |
parent | 0b4c166e8a90beeb1e71ee2fc16b3a240517c82d (diff) | |
download | gitea-943753f560fab8bb01946618b16c694bc2032827.tar.gz gitea-943753f560fab8bb01946618b16c694bc2032827.zip |
Support Proxy protocol (#12527)
This PR adds functionality to allow Gitea to sit behind an
HAProxy and HAProxy protocolled connections directly.
Fix #7508
Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'modules/private')
-rw-r--r-- | modules/private/internal.go | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/modules/private/internal.go b/modules/private/internal.go index a77a990627..2ea516ba80 100644 --- a/modules/private/internal.go +++ b/modules/private/internal.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/modules/httplib" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/proxyprotocol" "code.gitea.io/gitea/modules/setting" ) @@ -50,7 +51,32 @@ func newInternalRequest(ctx context.Context, url, method string) *httplib.Reques req.SetTransport(&http.Transport{ DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { var d net.Dialer - return d.DialContext(ctx, "unix", setting.HTTPAddr) + conn, err := d.DialContext(ctx, "unix", setting.HTTPAddr) + if err != nil { + return conn, err + } + if setting.LocalUseProxyProtocol { + if err = proxyprotocol.WriteLocalHeader(conn); err != nil { + _ = conn.Close() + return nil, err + } + } + return conn, err + }, + }) + } else if setting.LocalUseProxyProtocol { + req.SetTransport(&http.Transport{ + DialContext: func(ctx context.Context, network, address string) (net.Conn, error) { + var d net.Dialer + conn, err := d.DialContext(ctx, network, address) + if err != nil { + return conn, err + } + if err = proxyprotocol.WriteLocalHeader(conn); err != nil { + _ = conn.Close() + return nil, err + } + return conn, err }, }) } |