Skip to content

Commit

Permalink
Add PROXY protocol support to tcp inbound (#103)
Browse files Browse the repository at this point in the history
* Add  PROXY protocol support to tcp inbound

* add settings for PROXY protocol

* Adjust hub.go

Co-authored-by: RPRX <63339210+rprx@users.noreply.github.com>
  • Loading branch information
lucifer9 and RPRX committed Aug 21, 2020
1 parent 9362e2b commit 5df1733
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 20 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -7,6 +7,7 @@ require (
github.com/google/go-cmp v0.5.1
github.com/gorilla/websocket v1.4.2
github.com/miekg/dns v1.1.31
github.com/pires/go-proxyproto v0.1.3
github.com/seiflotfy/cuckoofilter v0.0.0-20200511222245-56093a4d3841
github.com/stretchr/testify v1.6.1
github.com/xiaokangwang/VSign v0.0.0-20200704130305-63f4b4d7a751
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -54,6 +54,8 @@ github.com/miekg/dns v1.1.31 h1:sJFOl9BgwbYAWOGEwr61FU28pqsBNdpRBnhGXtO06Oo=
github.com/miekg/dns v1.1.31/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2 h1:JhzVVoYvbOACxoUmOs6V/G4D5nPVUW73rKvXxP4XUJc=
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
github.com/pires/go-proxyproto v0.1.3 h1:2XEuhsQluSNA5QIQkiUv8PfgZ51sNYIQkq/yFquiSQM=
github.com/pires/go-proxyproto v0.1.3/go.mod h1:Odh9VFOZJCf9G8cLW5o435Xf1J95Jw9Gw5rnCjcwzAY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down
7 changes: 5 additions & 2 deletions infra/conf/transport_internet.go
Expand Up @@ -109,7 +109,8 @@ func (c *KCPConfig) Build() (proto.Message, error) {
}

type TCPConfig struct {
HeaderConfig json.RawMessage `json:"header"`
HeaderConfig json.RawMessage `json:"header"`
AcceptProxyProtocol bool `json:"acceptProxyProtocol"`
}

// Build implements Buildable.
Expand All @@ -126,7 +127,9 @@ func (c *TCPConfig) Build() (proto.Message, error) {
}
config.HeaderSettings = serial.ToTypedMessage(ts)
}

if c.AcceptProxyProtocol {
config.AcceptProxyProtocol = c.AcceptProxyProtocol
}
return config, nil
}

Expand Down
45 changes: 31 additions & 14 deletions transport/internet/tcp/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions transport/internet/tcp/config.proto
Expand Up @@ -11,4 +11,5 @@ import "v2ray.com/core/common/serial/typed_message.proto";
message Config {
reserved 1;
v2ray.core.common.serial.TypedMessage header_settings = 2;
bool accept_proxy_protocol = 3;
}
23 changes: 19 additions & 4 deletions transport/internet/tcp/hub.go
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"time"

"github.com/pires/go-proxyproto"

"v2ray.com/core/common"
"v2ray.com/core/common/net"
"v2ray.com/core/common/session"
Expand Down Expand Up @@ -36,10 +38,22 @@ func ListenTCP(ctx context.Context, address net.Address, port net.Port, streamSe
newError("listening TCP on ", address, ":", port).WriteToLog(session.ExportIDToError(ctx))

tcpSettings := streamSettings.ProtocolSettings.(*Config)
l := &Listener{
listener: listener,
config: tcpSettings,
addConn: handler,
var l *Listener

if tcpSettings.AcceptProxyProtocol {
policyFunc := func(upstream net.Addr) (proxyproto.Policy, error) { return proxyproto.REQUIRE, nil }
l = &Listener{
listener: &proxyproto.Listener{Listener: listener, Policy: policyFunc},
config: tcpSettings,
addConn: handler,
}
newError("Accepting PROXY protocol").AtWarning().WriteToLog(session.ExportIDToError(ctx))
} else {
l = &Listener{
listener: listener,
config: tcpSettings,
addConn: handler,
}
}

if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
Expand All @@ -57,6 +71,7 @@ func ListenTCP(ctx context.Context, address net.Address, port net.Port, streamSe
}
l.authConfig = auth
}

go l.keepAccepting()
return l, nil
}
Expand Down

0 comments on commit 5df1733

Please sign in to comment.