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/core"
"github.com/xjasonlyu/tun2socks/filter"
"github.com/xjasonlyu/tun2socks/proxy/socks"
"github.com/xjasonlyu/tun2socks/proxy"
"github.com/xjasonlyu/tun2socks/tun"
// init logger
@@ -149,8 +149,8 @@ func main() {
}
proxyHost := proxyAddr.IP.String()
proxyPort := uint16(proxyAddr.Port)
core.RegisterTCPConnHandler(socks.NewTCPHandler(proxyHost, proxyPort, fakeDns, sessionStater))
core.RegisterUDPConnHandler(socks.NewUDPHandler(proxyHost, proxyPort, *args.UdpTimeout, fakeDns, sessionStater))
core.RegisterTCPConnHandler(proxy.NewTCPHandler(proxyHost, proxyPort, 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
// 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
// is also referencing another repo: https://github.com/shadowsocks/go-shadowsocks2
package socks
package proxy
import (
"errors"

View File

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

View File

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

View File

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