Skip to content
This repository has been archived by the owner on Apr 27, 2020. It is now read-only.

Commit

Permalink
use small buf, disable no delay and tfo in local connections
Browse files Browse the repository at this point in the history
  • Loading branch information
IrineSistiana committed Jan 26, 2020
1 parent 241982a commit 6727e3d
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 32 deletions.
6 changes: 4 additions & 2 deletions local.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package main

import (
"context"
"crypto/tls"
"net"
"sync"
Expand Down Expand Up @@ -57,7 +58,8 @@ func doLocal() {
wssURL = "wss://" + *serverName + *path
}

listener, err := net.Listen("tcp", *bindAddr)
listenConfig := net.ListenConfig{Control: getControlFunc(defaultLeftTCPConfig)}
listener, err := listenConfig.Listen(context.Background(), "tcp", *bindAddr)
if err != nil {
logrus.Fatalf("net.Listen: %v", err)
}
Expand All @@ -79,7 +81,7 @@ func doLocal() {
func newRightConn() (net.Conn, error) {
var rightConn net.Conn
d := &net.Dialer{
Control: getControlFunc(),
Control: getControlFunc(defaultRightTCPConfig),
Timeout: handShakeTimeout,
}

Expand Down
44 changes: 43 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ var (

//mux config
defaultSmuxConfig *smux.Config

//tcp config
defaultLeftTCPConfig *tcpConfig
defaultRightTCPConfig *tcpConfig
)

const (
Expand Down Expand Up @@ -167,6 +171,44 @@ func main() {
MaxStreamBuffer: 512 * 1024,
}

localTCPConfig := &tcpConfig{
tfo: false,
noDelay: false,
mss: 0,
sndBuf: 64 * 1024,
rcvBuf: 64 * 1024,
}

defaultTCPConfig := &tcpConfig{
tfo: *enableTFO,
noDelay: *enableTCPNoDelay,
mss: *mss,
sndBuf: tcp_SO_SNDBUF,
rcvBuf: tcp_SO_RCVBUF,
}

// bind
addr, err := net.ResolveTCPAddr("tcp", *bindAddr)
if err != nil {
logrus.Fatalf("bind addr invalid, %v", err)
}
if addr.IP.IsLoopback() {
defaultLeftTCPConfig = localTCPConfig
} else {
defaultLeftTCPConfig = defaultTCPConfig
}

// remote
addr, err = net.ResolveTCPAddr("tcp", *remoteAddr)
if err != nil {
logrus.Fatalf("remote addr invalid, %v", err)
}
if addr.IP.IsLoopback() {
defaultRightTCPConfig = localTCPConfig
} else {
defaultRightTCPConfig = defaultTCPConfig
}

buffPool = &sync.Pool{New: func() interface{} {
return make([]byte, ioCopyBuffSize)
}}
Expand All @@ -186,7 +228,7 @@ func main() {
net.DefaultResolver.PreferGo = true
net.DefaultResolver.Dial = func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{}
d.Control = getControlFunc()
d.Control = getControlFunc(defaultRightTCPConfig)
return d.DialContext(ctx, "tcp", *fallbackDNS)
}
}
Expand Down
5 changes: 3 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func doServer() {
tlsConfig.Certificates = []tls.Certificate{cer}
}

listenConfig := net.ListenConfig{Control: getControlFunc()}
listenConfig := net.ListenConfig{Control: getControlFunc(defaultLeftTCPConfig)}
innerListener, err := listenConfig.Listen(context.Background(), "tcp", *bindAddr)
if err != nil {
logrus.Fatalf("tls inner Listener: %v", err)
Expand Down Expand Up @@ -94,7 +94,8 @@ func doServer() {

func handleLeftConn(leftConn net.Conn) {
defer leftConn.Close()
rightConn, err := net.Dial("tcp", *remoteAddr)
d := net.Dialer{Control: getControlFunc(defaultRightTCPConfig)}
rightConn, err := d.Dial("tcp", *remoteAddr)
if err != nil {
logrus.Errorf("tcp failed to dial, %v", err)
return
Expand Down
9 changes: 9 additions & 0 deletions tcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

type tcpConfig struct {
tfo bool
noDelay bool
mss int
sndBuf int
rcvBuf int
}
4 changes: 2 additions & 2 deletions tcp_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import (
"golang.org/x/sys/unix"
)

func getControlFunc() func(network, address string, c syscall.RawConn) error {
func getControlFunc(conf *tcpConfig) func(network, address string, c syscall.RawConn) error {
return func(network, address string, c syscall.RawConn) error {
if *vpnMode {
if err := c.Control(sendFdToBypass); err != nil {
return err
}
}
return c.Control(setSockOpt)
return c.Control(conf.setSockOpt)
}
}

Expand Down
16 changes: 8 additions & 8 deletions tcp_android_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,37 @@ import (
)

//TCP_MAXSEG TCP_NODELAY SO_SND/RCVBUF etc..
func setSockOpt(uintFd uintptr) {
func (c *tcpConfig) setSockOpt(uintFd uintptr) {
fd := int(uintFd)

if *enableTFO {
if c.tfo {
err := unix.SetsockoptInt(fd, unix.IPPROTO_TCP, unix.TCP_FASTOPEN_CONNECT, 1)
if err != nil {
logrus.Errorf("setsockopt TCP_FASTOPEN_CONNECT, %v", err)
}
}

if *enableTCPNoDelay {
if c.noDelay {
err := unix.SetsockoptInt(fd, unix.IPPROTO_TCP, unix.TCP_NODELAY, 1)
if err != nil {
logrus.Errorf("setsockopt TCP_NODELAY, %v", err)
}
}

if *mss > 0 {
if c.mss > 0 {
err := unix.SetsockoptInt(fd, unix.IPPROTO_TCP, unix.TCP_MAXSEG, *mss)
if err != nil {
logrus.Errorf("setsockopt TCP_MAXSEG, %v", err)
}
}
if tcp_SO_SNDBUF > 0 {
err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_SNDBUF, tcp_SO_SNDBUF)
if c.sndBuf > 0 {
err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_SNDBUF, c.sndBuf)
if err != nil {
logrus.Errorf("setsockopt SO_SNDBUF, %v", err)
}
}
if tcp_SO_RCVBUF > 0 {
err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_RCVBUF, tcp_SO_RCVBUF)
if c.rcvBuf > 0 {
err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_RCVBUF, c.rcvBuf)
if err != nil {
logrus.Errorf("setsockopt SO_RCVBUF, %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tcp_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ import (
"syscall"
)

func getControlFunc() func(network, address string, c syscall.RawConn) error {
func getControlFunc(conf *tcpConfig) func(network, address string, c syscall.RawConn) error {
return nil
}
4 changes: 2 additions & 2 deletions tcp_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ package main

import "syscall"

func getControlFunc() func(network, address string, c syscall.RawConn) error {
func getControlFunc(conf *tcpConfig) func(network, address string, c syscall.RawConn) error {
return func(network, address string, c syscall.RawConn) error {
return c.Control(setSockOpt)
return c.Control(conf.setSockOpt)
}
}
22 changes: 8 additions & 14 deletions tcp_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,32 @@ import (
"golang.org/x/sys/windows"
)

func getControlFunc() func(network, address string, c syscall.RawConn) error {
func getControlFunc(conf *tcpConfig) func(network, address string, c syscall.RawConn) error {
return func(network, address string, c syscall.RawConn) error {
return c.Control(setSockOpt)
return c.Control(conf.setSockOpt)
}
}

//TCP_MAXSEG TCP_NODELAY SO_SND/RCVBUF etc..
func setSockOpt(uintptrFd uintptr) {
func (c *tcpConfig) setSockOpt(uintptrFd uintptr) {
fd := windows.Handle(uintptrFd)
var err error

if *enableTCPNoDelay {
if c.noDelay {
err = windows.SetsockoptInt(fd, windows.IPPROTO_TCP, windows.TCP_NODELAY, 1)
if err != nil {
logrus.Errorf("setsockopt TCP_NODELAY, %v", err)
}
}

// can't set TCP_MAXSEG on windows

// if *mss > 0 {
// windows.SetsockoptInt(fd, windows.IPPROTO_TCP, windows.TCP_MAXSEG, *mss)
// }

if tcp_SO_SNDBUF > 0 {
err := windows.SetsockoptInt(fd, windows.SOL_SOCKET, windows.SO_SNDBUF, tcp_SO_SNDBUF)
if c.sndBuf > 0 {
err := windows.SetsockoptInt(fd, windows.SOL_SOCKET, windows.SO_SNDBUF, c.sndBuf)
if err != nil {
logrus.Errorf("setsockopt SO_SNDBUF, %v", err)
}
}
if tcp_SO_RCVBUF > 0 {
err := windows.SetsockoptInt(fd, windows.SOL_SOCKET, windows.SO_RCVBUF, tcp_SO_RCVBUF)
if c.rcvBuf > 0 {
err := windows.SetsockoptInt(fd, windows.SOL_SOCKET, windows.SO_RCVBUF, c.rcvBuf)
if err != nil {
logrus.Errorf("setsockopt SO_RCVBUF, %v", err)
}
Expand Down

0 comments on commit 6727e3d

Please sign in to comment.