Optional proxyproto header

This commit is contained in:
zakuwaki 2022-09-07 23:01:57 +08:00 committed by 世界
parent 87bc292296
commit 46a8f24400
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 18 additions and 13 deletions

View file

@ -13,6 +13,7 @@ import (
type Listener struct { type Listener struct {
net.Listener net.Listener
AcceptNoHeader bool
} }
func (l *Listener) Accept() (net.Conn, error) { func (l *Listener) Accept() (net.Conn, error) {
@ -22,7 +23,7 @@ func (l *Listener) Accept() (net.Conn, error) {
} }
bufReader := std_bufio.NewReader(conn) bufReader := std_bufio.NewReader(conn)
header, err := proxyproto.Read(bufReader) header, err := proxyproto.Read(bufReader)
if err != nil { if err != nil && !(l.AcceptNoHeader && err == proxyproto.ErrNoProxyProtocol) {
return nil, err return nil, err
} }
if bufReader.Buffered() > 0 { if bufReader.Buffered() > 0 {
@ -33,8 +34,11 @@ func (l *Listener) Accept() (net.Conn, error) {
} }
conn = bufio.NewCachedConn(conn, cache) conn = bufio.NewCachedConn(conn, cache)
} }
if header != nil {
return &bufio.AddrConn{Conn: conn, Metadata: M.Metadata{ return &bufio.AddrConn{Conn: conn, Metadata: M.Metadata{
Source: M.SocksaddrFromNet(header.SourceAddr), Source: M.SocksaddrFromNet(header.SourceAddr),
Destination: M.SocksaddrFromNet(header.DestinationAddr), Destination: M.SocksaddrFromNet(header.DestinationAddr),
}}, nil }}, nil
}
return conn, nil
} }

View file

@ -29,7 +29,7 @@ func (a *myInboundAdapter) ListenTCP() (net.Listener, error) {
} }
if a.listenOptions.ProxyProtocol { if a.listenOptions.ProxyProtocol {
a.logger.Debug("proxy protocol enabled") a.logger.Debug("proxy protocol enabled")
tcpListener = &proxyproto.Listener{Listener: tcpListener} tcpListener = &proxyproto.Listener{Listener: tcpListener, AcceptNoHeader: a.listenOptions.ProxyProtocolAcceptNoHeader}
} }
a.tcpListener = tcpListener a.tcpListener = tcpListener
return tcpListener, err return tcpListener, err

View file

@ -117,6 +117,7 @@ type ListenOptions struct {
UDPFragment bool `json:"udp_fragment,omitempty"` UDPFragment bool `json:"udp_fragment,omitempty"`
UDPTimeout int64 `json:"udp_timeout,omitempty"` UDPTimeout int64 `json:"udp_timeout,omitempty"`
ProxyProtocol bool `json:"proxy_protocol,omitempty"` ProxyProtocol bool `json:"proxy_protocol,omitempty"`
ProxyProtocolAcceptNoHeader bool `json:"proxy_protocol_accept_no_header,omitempty"`
Detour string `json:"detour,omitempty"` Detour string `json:"detour,omitempty"`
InboundOptions InboundOptions
} }