From 46a8f244008ec63211cde023e39854f3b47af61c Mon Sep 17 00:00:00 2001 From: zakuwaki <79925675+zakuwaki@users.noreply.github.com> Date: Wed, 7 Sep 2022 23:01:57 +0800 Subject: [PATCH] Optional proxyproto header --- common/proxyproto/listener.go | 14 +++++++++----- inbound/default_tcp.go | 2 +- option/inbound.go | 15 ++++++++------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/common/proxyproto/listener.go b/common/proxyproto/listener.go index e70e097e..068e32ef 100644 --- a/common/proxyproto/listener.go +++ b/common/proxyproto/listener.go @@ -13,6 +13,7 @@ import ( type Listener struct { net.Listener + AcceptNoHeader bool } func (l *Listener) Accept() (net.Conn, error) { @@ -22,7 +23,7 @@ func (l *Listener) Accept() (net.Conn, error) { } bufReader := std_bufio.NewReader(conn) header, err := proxyproto.Read(bufReader) - if err != nil { + if err != nil && !(l.AcceptNoHeader && err == proxyproto.ErrNoProxyProtocol) { return nil, err } if bufReader.Buffered() > 0 { @@ -33,8 +34,11 @@ func (l *Listener) Accept() (net.Conn, error) { } conn = bufio.NewCachedConn(conn, cache) } - return &bufio.AddrConn{Conn: conn, Metadata: M.Metadata{ - Source: M.SocksaddrFromNet(header.SourceAddr), - Destination: M.SocksaddrFromNet(header.DestinationAddr), - }}, nil + if header != nil { + return &bufio.AddrConn{Conn: conn, Metadata: M.Metadata{ + Source: M.SocksaddrFromNet(header.SourceAddr), + Destination: M.SocksaddrFromNet(header.DestinationAddr), + }}, nil + } + return conn, nil } diff --git a/inbound/default_tcp.go b/inbound/default_tcp.go index b0d9d9e5..0f5bc0fc 100644 --- a/inbound/default_tcp.go +++ b/inbound/default_tcp.go @@ -29,7 +29,7 @@ func (a *myInboundAdapter) ListenTCP() (net.Listener, error) { } if a.listenOptions.ProxyProtocol { a.logger.Debug("proxy protocol enabled") - tcpListener = &proxyproto.Listener{Listener: tcpListener} + tcpListener = &proxyproto.Listener{Listener: tcpListener, AcceptNoHeader: a.listenOptions.ProxyProtocolAcceptNoHeader} } a.tcpListener = tcpListener return tcpListener, err diff --git a/option/inbound.go b/option/inbound.go index f05317f4..6145bced 100644 --- a/option/inbound.go +++ b/option/inbound.go @@ -111,12 +111,13 @@ type InboundOptions struct { } type ListenOptions struct { - Listen ListenAddress `json:"listen"` - ListenPort uint16 `json:"listen_port,omitempty"` - TCPFastOpen bool `json:"tcp_fast_open,omitempty"` - UDPFragment bool `json:"udp_fragment,omitempty"` - UDPTimeout int64 `json:"udp_timeout,omitempty"` - ProxyProtocol bool `json:"proxy_protocol,omitempty"` - Detour string `json:"detour,omitempty"` + Listen ListenAddress `json:"listen"` + ListenPort uint16 `json:"listen_port,omitempty"` + TCPFastOpen bool `json:"tcp_fast_open,omitempty"` + UDPFragment bool `json:"udp_fragment,omitempty"` + UDPTimeout int64 `json:"udp_timeout,omitempty"` + ProxyProtocol bool `json:"proxy_protocol,omitempty"` + ProxyProtocolAcceptNoHeader bool `json:"proxy_protocol_accept_no_header,omitempty"` + Detour string `json:"detour,omitempty"` InboundOptions }