From f38306f40f503f16f6a678560fa788e8f08ff1bf Mon Sep 17 00:00:00 2001 From: Gwkang <975500206@qq.com> Date: Sat, 21 Nov 2020 19:15:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B6=85=E6=97=B6=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=B8=BA0=E6=97=B6,=E8=BF=9E=E6=8E=A5=E4=BC=9A?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E6=96=AD=E5=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/tcp_client.go | 13 +++++++++---- common/ip.go | 3 +-- server/conf.go | 2 +- server/tcp_server.go | 8 ++++++-- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/client/tcp_client.go b/client/tcp_client.go index d7f149d..ff385a3 100644 --- a/client/tcp_client.go +++ b/client/tcp_client.go @@ -1,7 +1,6 @@ package main import ( - "time" "SocketProxy/common" . "SocketProxy/logger" "bytes" @@ -10,6 +9,7 @@ import ( "errors" "io" "net" + "time" ) type TcpClient struct { @@ -98,14 +98,19 @@ func (c *TcpClient) TcpClientHandle(conn net.Conn) { defer func() { serverConn.Close() }() - - serverConn.SetDeadline(time.Now().Add(ClientConfig.Timeout)) + + if ClientConfig.Timeout != 0 { + serverConn.SetDeadline(time.Now().Add(ClientConfig.Timeout)) + } + serverConn, cipherType, err := c.Handshake(serverConn, addr) if err != nil { Logger.Warn(err) return } - serverConn.SetDeadline(time.Time{}) + if ClientConfig.Timeout != 0 { + serverConn.SetDeadline(time.Time{}) + } Logger.Debugf("Use cipherType: %#v, start relay %s <--> %s <--> %s", cipherType, conn.RemoteAddr(), serverConn.RemoteAddr(), ip+":"+port) common.Relay(serverConn, conn) } diff --git a/common/ip.go b/common/ip.go index 860414e..6952963 100644 --- a/common/ip.go +++ b/common/ip.go @@ -8,7 +8,7 @@ type IPRange struct { } // 内网地址范围 -var intranetAddr = []IPRange{ +var intranetAddr = [...]IPRange{ {Begin: Inet_addr_h("127.0.0.0"), End: Inet_addr_h("127.255.255.255")}, {Begin: Inet_addr_h("10.0.0.0"), End: Inet_addr_h("10.255.255.255")}, {Begin: Inet_addr_h("169.254.0.0"), End: Inet_addr_h("169.254.255.255")}, @@ -50,4 +50,3 @@ func IsIntranetAddress(ipv4 string) bool { return false } - diff --git a/server/conf.go b/server/conf.go index d28e1b5..fadd16e 100644 --- a/server/conf.go +++ b/server/conf.go @@ -3,12 +3,12 @@ package main import ( "SocketProxy/common" . "SocketProxy/logger" + "crypto/sha1" "crypto/tls" "encoding/json" "flag" "io/ioutil" "time" - "crypto/sha1" ) var configPath = flag.String("config", "config.json", "Config File Path.") diff --git a/server/tcp_server.go b/server/tcp_server.go index 73fa29c..2e52e82 100644 --- a/server/tcp_server.go +++ b/server/tcp_server.go @@ -55,9 +55,9 @@ func TcpServerListenAddr(addr string) error { func TcpServerHandle(conn net.Conn) { if ServerConfig.Timeout != 0 { _ = conn.(*net.TCPConn).SetKeepAlivePeriod(ServerConfig.Timeout) + conn.SetDeadline(time.Now().Add(ServerConfig.Timeout)) } - conn.SetDeadline(time.Now().Add(ServerConfig.Timeout)) newConn, ip, port, cipherType, err := Handshake(conn) if err != nil { Logger.Warn("Remote Addr: ", conn.RemoteAddr(), " Handshake error: ", err) @@ -65,7 +65,11 @@ func TcpServerHandle(conn net.Conn) { return } defer newConn.Close() - conn.SetDeadline(time.Time{}) + + if ServerConfig.Timeout != 0 { + conn.SetDeadline(time.Time{}) + } + // 访问目标地址 dstConn, err := net.DialTimeout("tcp", ip+":"+port, ServerConfig.Timeout) if err != nil {