You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.0 KiB

4 years ago
package tunnel
import (
"runtime"
"github.com/xjasonlyu/tun2socks/v2/core"
"github.com/xjasonlyu/tun2socks/v2/log"
4 years ago
)
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
4 years ago
)
var (
_tcpQueue = make(chan core.TCPConn) /* unbuffered */
_udpQueue = make(chan core.UDPPacket, maxUDPQueueSize)
_numUDPWorkers = max(runtime.GOMAXPROCS(0), 4 /* at least 4 workers */)
4 years ago
)
func init() {
go process()
}
// Add adds tcpConn to tcpQueue.
func Add(conn core.TCPConn) {
_tcpQueue <- conn
4 years ago
}
// AddPacket adds udpPacket to udpQueue.
func AddPacket(packet core.UDPPacket) {
4 years ago
select {
case _udpQueue <- packet:
4 years ago
default:
log.Warnf("queue is currently full, packet will be dropped")
packet.Drop()
}
}
func process() {
for i := 0; i < _numUDPWorkers; i++ {
queue := _udpQueue
4 years ago
go func() {
for packet := range queue {
handleUDP(packet)
}
}()
}
for conn := range _tcpQueue {
4 years ago
go handleTCP(conn)
}
}