This commit is contained in:
Jason
2019-08-13 17:26:32 +08:00
parent 573fba0941
commit 640389a80c
5 changed files with 15 additions and 16 deletions

View File

@@ -16,7 +16,7 @@ import (
"github.com/xjasonlyu/tun2socks/common/stats" "github.com/xjasonlyu/tun2socks/common/stats"
"github.com/xjasonlyu/tun2socks/core" "github.com/xjasonlyu/tun2socks/core"
"github.com/xjasonlyu/tun2socks/filter" "github.com/xjasonlyu/tun2socks/filter"
"github.com/xjasonlyu/tun2socks/proxy/socks" "github.com/xjasonlyu/tun2socks/proxy"
"github.com/xjasonlyu/tun2socks/tun" "github.com/xjasonlyu/tun2socks/tun"
// init logger // init logger
@@ -149,8 +149,8 @@ func main() {
} }
proxyHost := proxyAddr.IP.String() proxyHost := proxyAddr.IP.String()
proxyPort := uint16(proxyAddr.Port) proxyPort := uint16(proxyAddr.Port)
core.RegisterTCPConnHandler(socks.NewTCPHandler(proxyHost, proxyPort, fakeDns, sessionStater)) core.RegisterTCPConnHandler(proxy.NewTCPHandler(proxyHost, proxyPort, fakeDns, sessionStater))
core.RegisterUDPConnHandler(socks.NewUDPHandler(proxyHost, proxyPort, *args.UdpTimeout, fakeDns, sessionStater)) core.RegisterUDPConnHandler(proxy.NewUDPHandler(proxyHost, proxyPort, *args.UdpTimeout, fakeDns, sessionStater))
// Register an output callback to write packets output from lwip stack to tun // Register an output callback to write packets output from lwip stack to tun
// device, output function should be set before input any packets. // device, output function should be set before input any packets.

View File

@@ -1,7 +1,7 @@
// Code in this file are grabbed from https://github.com/nadoo/glider, which // Code in this file are grabbed from https://github.com/nadoo/glider, which
// is also referencing another repo: https://github.com/shadowsocks/go-shadowsocks2 // is also referencing another repo: https://github.com/shadowsocks/go-shadowsocks2
package socks package proxy
import ( import (
"errors" "errors"

View File

@@ -1,4 +1,4 @@
package socks package proxy
import ( import (
"net" "net"
@@ -12,7 +12,6 @@ import (
"github.com/xjasonlyu/tun2socks/common/lsof" "github.com/xjasonlyu/tun2socks/common/lsof"
"github.com/xjasonlyu/tun2socks/common/stats" "github.com/xjasonlyu/tun2socks/common/stats"
"github.com/xjasonlyu/tun2socks/core" "github.com/xjasonlyu/tun2socks/core"
. "github.com/xjasonlyu/tun2socks/proxy"
) )
type tcpHandler struct { type tcpHandler struct {
@@ -71,12 +70,12 @@ func (h *tcpHandler) Handle(localConn net.Conn, target *net.TCPAddr) error {
} }
// set keepalive // set keepalive
TCPKeepAlive(localConn) tcpKeepAlive(localConn)
TCPKeepAlive(remoteConn) tcpKeepAlive(remoteConn)
go func() { go func() {
// relay connections // relay connections
TCPRelay(localConn, remoteConn) tcpRelay(localConn, remoteConn)
// remove session // remove session
if h.sessionStater != nil { if h.sessionStater != nil {

View File

@@ -1,4 +1,4 @@
package socks package proxy
import ( import (
"bytes" "bytes"

View File

@@ -15,26 +15,26 @@ type duplexConn interface {
CloseWrite() error CloseWrite() error
} }
func TCPCloseRead(conn net.Conn) { func tcpCloseRead(conn net.Conn) {
if c, ok := conn.(duplexConn); ok { if c, ok := conn.(duplexConn); ok {
c.CloseRead() c.CloseRead()
} }
} }
func TCPCloseWrite(conn net.Conn) { func tcpCloseWrite(conn net.Conn) {
if c, ok := conn.(duplexConn); ok { if c, ok := conn.(duplexConn); ok {
c.CloseWrite() c.CloseWrite()
} }
} }
func TCPKeepAlive(conn net.Conn) { func tcpKeepAlive(conn net.Conn) {
if tcp, ok := conn.(*net.TCPConn); ok { if tcp, ok := conn.(*net.TCPConn); ok {
tcp.SetKeepAlive(true) tcp.SetKeepAlive(true)
tcp.SetKeepAlivePeriod(30 * time.Second) tcp.SetKeepAlivePeriod(30 * time.Second)
} }
} }
func TCPRelay(localConn, remoteConn net.Conn) { func tcpRelay(localConn, remoteConn net.Conn) {
var once sync.Once var once sync.Once
closeOnce := func() { closeOnce := func() {
once.Do(func() { once.Do(func() {
@@ -59,7 +59,7 @@ func TCPRelay(localConn, remoteConn net.Conn) {
} else { } else {
localConn.SetDeadline(time.Now()) localConn.SetDeadline(time.Now())
remoteConn.SetDeadline(time.Now()) remoteConn.SetDeadline(time.Now())
TCPCloseRead(remoteConn) tcpCloseRead(remoteConn)
} }
wg.Done() wg.Done()
}() }()
@@ -71,7 +71,7 @@ func TCPRelay(localConn, remoteConn net.Conn) {
} else { } else {
localConn.SetDeadline(time.Now()) localConn.SetDeadline(time.Now())
remoteConn.SetDeadline(time.Now()) remoteConn.SetDeadline(time.Now())
TCPCloseRead(localConn) tcpCloseRead(localConn)
} }
pool.BufPool.Put(buf[:cap(buf)]) pool.BufPool.Put(buf[:cap(buf)])