forked from riobard/go-shadowsocks2
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add -tcp-keep-alive and -tcp-fast-open flags
- Loading branch information
1 parent
71a35d3
commit bcd29b4
Showing
11 changed files
with
231 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3 h1:f/FNXud6gA3MNr8meMVVGxhp+QBTqY91tM8HjEuMjGg= | ||
github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3/go.mod h1:HgjTstvQsPGkxUsCd2KWxErBblirPizecHcpD3ffK+s= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 h1:/ZScEX8SfEmUGRHs0gxpqteO5nfNW6axyZbBdw9A12g= | ||
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= | ||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= | ||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY= | ||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= | ||
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c= | ||
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= | ||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= | ||
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-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20220721230656-c6bc011c0c49 h1:TMjZDarEwf621XDryfitp/8awEhiZNiwgphKlTMGRIg= | ||
golang.org/x/sys v0.0.0-20220721230656-c6bc011c0c49/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s= | ||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,33 @@ | ||
// +build !linux,!darwin | ||
//go:build !linux && !darwin && !windows | ||
// +build !linux,!darwin,!windows | ||
|
||
package main | ||
|
||
import ( | ||
"net" | ||
"runtime" | ||
) | ||
|
||
func redirLocal(addr, server string, shadow func(net.Conn) net.Conn) { | ||
logf("TCP redirect not supported") | ||
logf("TCP redirect not supported on %s-%s", runtime.GOOS, runtime.GOARCH) | ||
} | ||
|
||
func redir6Local(addr, server string, shadow func(net.Conn) net.Conn) { | ||
logf("TCP6 redirect not supported") | ||
logf("TCP6 redirect not supported on %s-%s", runtime.GOOS, runtime.GOARCH) | ||
} | ||
|
||
// tcpSetListenOpts sets listening socket options. | ||
func tcpSetListenOpts(fd uintptr) error { | ||
if config.TCPFastOpen { | ||
return fmt.Errorf("tcp-fast-open is not supported on %s-%s", runtime.GOOS, runtime.GOARCH) | ||
} | ||
return nil | ||
} | ||
|
||
// tcpSetDialOpts sets dialing socket options. | ||
func tcpSetDialOpts(fd uintptr) error { | ||
if config.TCPFastOpen { | ||
return fmt.Errorf("tcp-fast-open is not supported on %s-%s", runtime.GOOS, runtime.GOARCH) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
const ( | ||
// https://github.com/shadowsocks/shadowsocks-libev/blob/89b5f987d6a5329de9713704615581d363f0cfed/src/winsock.h#L82 | ||
TCP_FASTOPEN = 15 | ||
) | ||
|
||
func redirLocal(addr, server string, shadow func(net.Conn) net.Conn) { | ||
logf("TCP redirect not supported on %s-%s", runtime.GOOS, runtime.GOARCH) | ||
} | ||
|
||
func redir6Local(addr, server string, shadow func(net.Conn) net.Conn) { | ||
logf("TCP6 redirect not supported on %s-%s", runtime.GOOS, runtime.GOARCH) | ||
} | ||
|
||
// tcpSetListenOpts sets listening socket options. | ||
func tcpSetListenOpts(fd uintptr) error { | ||
if config.TCPFastOpen { | ||
if err := windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_TCP, TCP_FASTOPEN, 1); err != nil { | ||
return fmt.Errorf("failed to set TCP_FASTOPEN: %s", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// tcpSetDialOpts sets dialing socket options. | ||
func tcpSetDialOpts(fd uintptr) error { | ||
if config.TCPFastOpen { | ||
if err := windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_TCP, TCP_FASTOPEN, 1); err != nil { | ||
return fmt.Errorf("failed to set TCP_FASTOPEN: %s", err) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"syscall" | ||
) | ||
|
||
// tcpListen binds a listening socket | ||
func tcpListen(addr string) (net.Listener, error) { | ||
var rawConn syscall.RawConn | ||
lc := net.ListenConfig{ | ||
KeepAlive: config.TCPKeepAlive, | ||
Control: func(network, address string, c syscall.RawConn) error { | ||
rawConn = c | ||
return nil | ||
}, | ||
} | ||
|
||
l, err := lc.Listen(context.Background(), "tcp", addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// On MacOS we have to call Control() after the bind() and listen() are complete, | ||
// otherwise setsockopt(TCP_FASTOPEN) fails with EINVAL (invalid argument). | ||
// See https://github.com/h2o/h2o/commit/ec58f59f5e9a6c6a8a38087eb87fdc4b1763f080 | ||
var ctrlErr error | ||
if err := rawConn.Control(func(fd uintptr) { ctrlErr = tcpSetListenOpts(fd) }); err != nil { | ||
l.Close() | ||
return nil, err | ||
} | ||
|
||
if ctrlErr != nil { | ||
logf("failed to set up listening socket: %s", ctrlErr) | ||
} | ||
|
||
return l, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//go:build !darwin | ||
// +build !darwin | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"syscall" | ||
) | ||
|
||
// tcpListen binds a listening socket | ||
func tcpListen(addr string) (net.Listener, error) { | ||
lc := net.ListenConfig{ | ||
KeepAlive: config.TCPKeepAlive, | ||
Control: func(network, address string, c syscall.RawConn) error { | ||
var ctrlErr error | ||
if err := c.Control(func(fd uintptr) { ctrlErr = tcpSetListenOpts(fd) }); err != nil { | ||
return err | ||
} | ||
if ctrlErr != nil { | ||
logf("failed to set up listening socket: %s", ctrlErr) | ||
} | ||
return nil | ||
}, | ||
} | ||
return lc.Listen(context.Background(), "tcp", addr) | ||
} |