Skip to content

Commit

Permalink
修复超时时间为0时,连接会直接断开
Browse files Browse the repository at this point in the history
  • Loading branch information
iGwkang committed Nov 21, 2020
1 parent 50ff94b commit f38306f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
13 changes: 9 additions & 4 deletions client/tcp_client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"time"
"SocketProxy/common"
. "SocketProxy/logger"
"bytes"
Expand All @@ -10,6 +9,7 @@ import (
"errors"
"io"
"net"
"time"
)

type TcpClient struct {
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions common/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")},
Expand Down Expand Up @@ -50,4 +50,3 @@ func IsIntranetAddress(ipv4 string) bool {

return false
}

2 changes: 1 addition & 1 deletion server/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
8 changes: 6 additions & 2 deletions server/tcp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,21 @@ 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)
conn.Close()
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 {
Expand Down

0 comments on commit f38306f

Please sign in to comment.