Skip to content

Commit

Permalink
proxy: support chain more than 2 protocols in server mode
Browse files Browse the repository at this point in the history
  • Loading branch information
nadoo committed May 5, 2021
1 parent 70cf30b commit ceb8b82
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 29 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,12 @@ Examples:
listen=tls://:443?cert=crtFilePath&key=keyFilePath,http://
```
- Chain protocols in listener: http over smux over websocket proxy server
``` bash
listen=ws://:10000,smux://,http://
```
</details>
## Links
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ require (
github.com/nadoo/ipset v0.3.0
github.com/tjfoc/gmsm v1.4.0 // indirect
github.com/xtaci/kcp-go/v5 v5.6.1
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 // indirect
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887 // indirect
golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125 // indirect
golang.org/x/sys v0.0.0-20210503173754-0981d6026fa6 // indirect
)

// Replace dependency modules with local developing copy
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ golang.org/x/crypto v0.0.0-20191219195013-becbf705a915/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e h1:8foAy0aoO5GkqCvAEJ4VC4P3zksTg4X4aJCDpZzmgQI=
golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
Expand All @@ -128,8 +128,8 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0=
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125 h1:Ugb8sMTWuWRC3+sz5WeN/4kejDx9BvIwnPUiJBjJE+8=
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -154,8 +154,8 @@ golang.org/x/sys v0.0.0-20201101102859-da207088b7d1/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201112073958-5cba982894dd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887 h1:dXfMednGJh/SUUFjTLsWJz3P+TQt9qnR11GgeI3vWKs=
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210503173754-0981d6026fa6 h1:cdsMqa2nXzqlgs183pHxtvoVwU7CyzaCTAUOg94af4c=
golang.org/x/sys v0.0.0-20210503173754-0981d6026fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
11 changes: 7 additions & 4 deletions proxy/kcp/kcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,18 @@ func NewKCPDialer(s string, d proxy.Dialer) (proxy.Dialer, error) {

// NewKCPServer returns a kcp proxy server.
func NewKCPServer(s string, p proxy.Proxy) (proxy.Server, error) {
transport := strings.Split(s, ",")
server, chain := s, ""
if idx := strings.IndexByte(s, ','); idx != -1 {
server, chain = s[:idx], s[idx+1:]
}

k, err := NewKCP(transport[0], nil, p)
k, err := NewKCP(server, nil, p)
if err != nil {
return nil, err
}

if len(transport) > 1 {
k.server, err = proxy.ServerFromURL(transport[1], p)
if chain != "" {
k.server, err = proxy.ServerFromURL(chain, p)
if err != nil {
return nil, err
}
Expand Down
11 changes: 7 additions & 4 deletions proxy/smux/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ func init() {

// NewSmuxServer returns a smux transport layer before the real server.
func NewSmuxServer(s string, p proxy.Proxy) (proxy.Server, error) {
transport := strings.Split(s, ",")
server, chain := s, ""
if idx := strings.IndexByte(s, ','); idx != -1 {
server, chain = s[:idx], s[idx+1:]
}

u, err := url.Parse(transport[0])
u, err := url.Parse(server)
if err != nil {
log.F("[smux] parse url err: %s", err)
return nil, err
Expand All @@ -37,8 +40,8 @@ func NewSmuxServer(s string, p proxy.Proxy) (proxy.Server, error) {
addr: u.Host,
}

if len(transport) > 1 {
m.server, err = proxy.ServerFromURL(transport[1], p)
if chain != "" {
m.server, err = proxy.ServerFromURL(chain, p)
if err != nil {
return nil, err
}
Expand Down
11 changes: 7 additions & 4 deletions proxy/tls/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ func NewTLSDialer(s string, d proxy.Dialer) (proxy.Dialer, error) {

// NewTLSServer returns a tls transport layer before the real server.
func NewTLSServer(s string, p proxy.Proxy) (proxy.Server, error) {
transport := strings.Split(s, ",")
server, chain := s, ""
if idx := strings.IndexByte(s, ','); idx != -1 {
server, chain = s[:idx], s[idx+1:]
}

t, err := NewTLS(transport[0], nil, p)
t, err := NewTLS(server, nil, p)
if err != nil {
return nil, err
}
Expand All @@ -104,8 +107,8 @@ func NewTLSServer(s string, p proxy.Proxy) (proxy.Server, error) {
MinVersion: stdtls.VersionTLS12,
}

if len(transport) > 1 {
t.server, err = proxy.ServerFromURL(transport[1], p)
if chain != "" {
t.server, err = proxy.ServerFromURL(chain, p)
if err != nil {
return nil, err
}
Expand Down
11 changes: 7 additions & 4 deletions proxy/unix/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ func init() {

// NewUnixServer returns a unix domain socket server.
func NewUnixServer(s string, p proxy.Proxy) (proxy.Server, error) {
transport := strings.Split(s, ",")
server, chain := s, ""
if idx := strings.IndexByte(s, ','); idx != -1 {
server, chain = s[:idx], s[idx+1:]
}

unix, err := NewUnix(transport[0], nil, p)
unix, err := NewUnix(server, nil, p)
if err != nil {
return nil, err
}

if len(transport) > 1 {
unix.server, err = proxy.ServerFromURL(transport[1], p)
if chain != "" {
unix.server, err = proxy.ServerFromURL(chain, p)
if err != nil {
return nil, err
}
Expand Down
11 changes: 7 additions & 4 deletions proxy/ws/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ func init() {

// NewWSServer returns a ws transport server.
func NewWSServer(s string, p proxy.Proxy) (proxy.Server, error) {
transport := strings.Split(s, ",")
server, chain := s, ""
if idx := strings.IndexByte(s, ','); idx != -1 {
server, chain = s[:idx], s[idx+1:]
}

w, err := NewWS(transport[0], nil, p)
w, err := NewWS(server, nil, p)
if err != nil {
return nil, err
}

if len(transport) > 1 {
w.server, err = proxy.ServerFromURL(transport[1], p)
if chain != "" {
w.server, err = proxy.ServerFromURL(chain, p)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit ceb8b82

Please sign in to comment.