From f1bd6f41713b136efc65d1bbd97efc3c852aa9cc Mon Sep 17 00:00:00 2001 From: pagefault Date: Sat, 21 Mar 2020 12:11:47 +0800 Subject: [PATCH] fix windows compilation --- proxy/server.go | 21 +++++++-------------- proxy/tcp_option.go | 22 ++++++++++++++++++++++ proxy/tcp_option_stub.go | 12 ++++++++++++ 3 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 proxy/tcp_option.go create mode 100644 proxy/tcp_option_stub.go diff --git a/proxy/server.go b/proxy/server.go index aa8a19592..2bdf2f195 100644 --- a/proxy/server.go +++ b/proxy/server.go @@ -10,7 +10,6 @@ import ( "github.com/p4gefau1t/trojan-go/protocol/direct" "github.com/p4gefau1t/trojan-go/protocol/trojan" "github.com/p4gefau1t/trojan-go/stat" - "github.com/valyala/tcplisten" "github.com/xtaci/smux" ) @@ -135,19 +134,13 @@ func (s *Server) Run() error { var listener net.Listener var err error if s.config.TCP.ReusePort || s.config.TCP.FastOpen || s.config.TCP.NoDelay { - cfg := tcplisten.Config{ - ReusePort: s.config.TCP.ReusePort, - FastOpen: s.config.TCP.FastOpen, - DeferAccept: s.config.TCP.NoDelay, - } - network := "tcp6" - if s.config.LocalIP.To4() != nil { - network = "tcp4" - } - listener, err = cfg.NewListener(network, s.config.LocalAddr.String()) - if err != nil { - return err - } + listener, err = ListenWithTCPOption( + s.config.TCP.FastOpen, + s.config.TCP.ReusePort, + s.config.TCP.NoDelay, + s.config.LocalIP, + s.config.LocalAddr.String(), + ) } else { listener, err = net.Listen("tcp", s.config.LocalAddr.String()) if err != nil { diff --git a/proxy/tcp_option.go b/proxy/tcp_option.go new file mode 100644 index 000000000..e8cb3d731 --- /dev/null +++ b/proxy/tcp_option.go @@ -0,0 +1,22 @@ +// +build !windows + +package proxy + +import ( + "net" + + "github.com/valyala/tcplisten" +) + +func ListenWithTCPOption(fastOpen, reusePort, noDelay bool, ip net.IP, addr string) (net.Listener, error) { + cfg := tcplisten.Config{ + ReusePort: reusePort, + FastOpen: fastOpen, + DeferAccept: noDelay, + } + network := "tcp6" + if ip.To4() != nil { + network = "tcp4" + } + return cfg.NewListener(network, addr) +} diff --git a/proxy/tcp_option_stub.go b/proxy/tcp_option_stub.go new file mode 100644 index 000000000..fdb48583c --- /dev/null +++ b/proxy/tcp_option_stub.go @@ -0,0 +1,12 @@ +// +build windows + +package proxy + +import ( + "net" +) + +func ListenWithTCPOption(fastOpen, reusePort, noDelay bool, ip net.IP, addr string) (net.Listener, error) { + panic("os not support tcp option") + return nil, nil +}