Optimization: socks UDP & fix typo (#261)
This commit is contained in:
parent
92f3d9ae5b
commit
5431e51554
3 changed files with 23 additions and 11 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
@ -14,3 +14,9 @@ bin/*
|
||||||
|
|
||||||
# dep
|
# dep
|
||||||
vendor
|
vendor
|
||||||
|
|
||||||
|
# GoLand
|
||||||
|
.idea/*
|
||||||
|
|
||||||
|
# macOS file
|
||||||
|
.DS_Store
|
||||||
|
|
|
@ -116,7 +116,7 @@ func (ss *Socks5) DialUDP(metadata *C.Metadata) (_ C.PacketConn, _ net.Addr, err
|
||||||
pc.Close()
|
pc.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return newPacketConn(&socksUDPConn{PacketConn: pc, rAddr: targetAddr}, ss), addr, nil
|
return newPacketConn(&socksUDPConn{PacketConn: pc, rAddr: targetAddr, tcpConn: c}, ss), addr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSocks5(option Socks5Option) *Socks5 {
|
func NewSocks5(option Socks5Option) *Socks5 {
|
||||||
|
@ -146,7 +146,8 @@ func NewSocks5(option Socks5Option) *Socks5 {
|
||||||
|
|
||||||
type socksUDPConn struct {
|
type socksUDPConn struct {
|
||||||
net.PacketConn
|
net.PacketConn
|
||||||
rAddr net.Addr
|
rAddr net.Addr
|
||||||
|
tcpConn net.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uc *socksUDPConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
func (uc *socksUDPConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
||||||
|
@ -166,3 +167,8 @@ func (uc *socksUDPConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||||
copy(b, payload)
|
copy(b, payload)
|
||||||
return n - len(addr) - 3, a, e
|
return n - len(addr) - 3, a, e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (uc *socksUDPConn) Close() error {
|
||||||
|
uc.tcpConn.Close()
|
||||||
|
return uc.PacketConn.Close()
|
||||||
|
}
|
||||||
|
|
|
@ -161,16 +161,16 @@ func (t *Tunnel) handleConn(localConn C.ServerAdapter) {
|
||||||
func (t *Tunnel) handleUDPConn(localConn C.ServerAdapter, metadata *C.Metadata, proxy C.Proxy, rule C.Rule) {
|
func (t *Tunnel) handleUDPConn(localConn C.ServerAdapter, metadata *C.Metadata, proxy C.Proxy, rule C.Rule) {
|
||||||
pc, addr := natTable.Get(localConn.RemoteAddr())
|
pc, addr := natTable.Get(localConn.RemoteAddr())
|
||||||
if pc == nil {
|
if pc == nil {
|
||||||
rawpc, naddr, err := proxy.DialUDP(metadata)
|
rawPc, nAddr, err := proxy.DialUDP(metadata)
|
||||||
addr = naddr
|
addr = nAddr
|
||||||
pc = rawpc
|
pc = rawPc
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnln("dial %s error: %s", proxy.Name(), err.Error())
|
log.Warnln("dial %s error: %s", proxy.Name(), err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if rule != nil {
|
if rule != nil {
|
||||||
log.Infoln("%s --> %v match %s using %s", metadata.SrcIP.String(), metadata.String(), rule.RuleType().String(), rawpc.Chains().String())
|
log.Infoln("%s --> %v match %s using %s", metadata.SrcIP.String(), metadata.String(), rule.RuleType().String(), rawPc.Chains().String())
|
||||||
} else {
|
} else {
|
||||||
log.Infoln("%s --> %v doesn't match any rule using DIRECT", metadata.SrcIP.String(), metadata.String())
|
log.Infoln("%s --> %v doesn't match any rule using DIRECT", metadata.SrcIP.String(), metadata.String())
|
||||||
}
|
}
|
||||||
|
@ -183,24 +183,24 @@ func (t *Tunnel) handleUDPConn(localConn C.ServerAdapter, metadata *C.Metadata,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tunnel) handleTCPConn(localConn C.ServerAdapter, metadata *C.Metadata, proxy C.Proxy, rule C.Rule) {
|
func (t *Tunnel) handleTCPConn(localConn C.ServerAdapter, metadata *C.Metadata, proxy C.Proxy, rule C.Rule) {
|
||||||
remoConn, err := proxy.Dial(metadata)
|
remoteConn, err := proxy.Dial(metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnln("dial %s error: %s", proxy.Name(), err.Error())
|
log.Warnln("dial %s error: %s", proxy.Name(), err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer remoConn.Close()
|
defer remoteConn.Close()
|
||||||
|
|
||||||
if rule != nil {
|
if rule != nil {
|
||||||
log.Infoln("%s --> %v match %s using %s", metadata.SrcIP.String(), metadata.String(), rule.RuleType().String(), remoConn.Chains().String())
|
log.Infoln("%s --> %v match %s using %s", metadata.SrcIP.String(), metadata.String(), rule.RuleType().String(), remoteConn.Chains().String())
|
||||||
} else {
|
} else {
|
||||||
log.Infoln("%s --> %v doesn't match any rule using DIRECT", metadata.SrcIP.String(), metadata.String())
|
log.Infoln("%s --> %v doesn't match any rule using DIRECT", metadata.SrcIP.String(), metadata.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
switch adapter := localConn.(type) {
|
switch adapter := localConn.(type) {
|
||||||
case *InboundAdapter.HTTPAdapter:
|
case *InboundAdapter.HTTPAdapter:
|
||||||
t.handleHTTP(adapter, remoConn)
|
t.handleHTTP(adapter, remoteConn)
|
||||||
case *InboundAdapter.SocketAdapter:
|
case *InboundAdapter.SocketAdapter:
|
||||||
t.handleSocket(adapter, remoConn)
|
t.handleSocket(adapter, remoteConn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue