xjasonlyu
3 years ago
10 changed files with 116 additions and 378 deletions
@ -1,6 +1,8 @@ |
|||
package core |
|||
|
|||
// Handler is a TCP/UDP connection handler that implements
|
|||
// HandleTCPConn and HandleUDPConn methods.
|
|||
type Handler interface { |
|||
Add(TCPConn) |
|||
AddPacket(UDPPacket) |
|||
HandleTCPConn(TCPConn) |
|||
HandleUDPConn(UDPConn) |
|||
} |
|||
|
@ -0,0 +1,25 @@ |
|||
package tunnel |
|||
|
|||
import ( |
|||
"net" |
|||
"strconv" |
|||
) |
|||
|
|||
// parseAddr parses net.Addr to IP and port.
|
|||
func parseAddr(addr net.Addr) (net.IP, uint16) { |
|||
switch v := addr.(type) { |
|||
case *net.TCPAddr: |
|||
return v.IP, uint16(v.Port) |
|||
case *net.UDPAddr: |
|||
return v.IP, uint16(v.Port) |
|||
default: |
|||
return parseAddrString(addr.String()) |
|||
} |
|||
} |
|||
|
|||
// parseAddrString parses address string to IP and port.
|
|||
func parseAddrString(addr string) (net.IP, uint16) { |
|||
host, port, _ := net.SplitHostPort(addr) |
|||
portInt, _ := strconv.ParseUint(port, 10, 16) |
|||
return net.ParseIP(host), uint16(portInt) |
|||
} |
@ -1,55 +1,36 @@ |
|||
package tunnel |
|||
|
|||
import ( |
|||
"runtime" |
|||
|
|||
"github.com/xjasonlyu/tun2socks/v2/core" |
|||
"github.com/xjasonlyu/tun2socks/v2/log" |
|||
) |
|||
|
|||
const ( |
|||
// maxUDPQueueSize is the max number of UDP packets
|
|||
// could be buffered. if queue is full, upcoming packets
|
|||
// would be dropped util queue is ready again.
|
|||
maxUDPQueueSize = 1 << 9 |
|||
) |
|||
|
|||
// Unbuffered TCP/UDP queues.
|
|||
var ( |
|||
_tcpQueue = make(chan core.TCPConn) /* unbuffered */ |
|||
_udpQueue = make(chan core.UDPPacket, maxUDPQueueSize) |
|||
_numUDPWorkers = max(runtime.GOMAXPROCS(0), 4 /* at least 4 workers */) |
|||
_tcpQueue = make(chan core.TCPConn) |
|||
_udpQueue = make(chan core.UDPConn) |
|||
) |
|||
|
|||
func init() { |
|||
go process() |
|||
} |
|||
|
|||
// Add adds tcpConn to tcpQueue.
|
|||
func Add(conn core.TCPConn) { |
|||
_tcpQueue <- conn |
|||
// TCPIn return fan-in TCP queue.
|
|||
func TCPIn() chan<- core.TCPConn { |
|||
return _tcpQueue |
|||
} |
|||
|
|||
// AddPacket adds udpPacket to udpQueue.
|
|||
func AddPacket(packet core.UDPPacket) { |
|||
select { |
|||
case _udpQueue <- packet: |
|||
default: |
|||
log.Warnf("queue is currently full, packet will be dropped") |
|||
packet.Drop() |
|||
} |
|||
// UDPIn return fan-in UDP queue.
|
|||
func UDPIn() chan<- core.UDPConn { |
|||
return _udpQueue |
|||
} |
|||
|
|||
func process() { |
|||
for i := 0; i < _numUDPWorkers; i++ { |
|||
queue := _udpQueue |
|||
go func() { |
|||
for packet := range queue { |
|||
handleUDP(packet) |
|||
} |
|||
}() |
|||
} |
|||
|
|||
for conn := range _tcpQueue { |
|||
go handleTCP(conn) |
|||
for { |
|||
select { |
|||
case conn := <-_tcpQueue: |
|||
go handleTCPConn(conn) |
|||
case conn := <-_udpQueue: |
|||
go handleUDPConn(conn) |
|||
} |
|||
} |
|||
} |
|||
|
@ -1,20 +0,0 @@ |
|||
package tunnel |
|||
|
|||
import ( |
|||
"net" |
|||
"strconv" |
|||
) |
|||
|
|||
func max(a, b int) int { |
|||
if a > b { |
|||
return a |
|||
} |
|||
return b |
|||
} |
|||
|
|||
// parseAddr parses address to IP and port.
|
|||
func parseAddr(addr string) (net.IP, uint16) { |
|||
host, portStr, _ := net.SplitHostPort(addr) |
|||
portInt, _ := strconv.ParseUint(portStr, 10, 16) |
|||
return net.ParseIP(host), uint16(portInt) |
|||
} |
Loading…
Reference in new issue