1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-04 16:37:12 -05:00

fix udp in transparent proxy

This commit is contained in:
Darien Raymond 2017-10-27 17:13:43 +02:00
parent 27c099dd37
commit 5901192a58

View File

@ -172,6 +172,11 @@ func (*udpConn) SetWriteDeadline(time.Time) error {
return nil return nil
} }
type connId struct {
src net.Destination
dest net.Destination
}
type udpWorker struct { type udpWorker struct {
sync.RWMutex sync.RWMutex
@ -185,39 +190,43 @@ type udpWorker struct {
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
activeConn map[net.Destination]*udpConn activeConn map[connId]*udpConn
} }
func (w *udpWorker) getConnection(src net.Destination) (*udpConn, bool) { func (w *udpWorker) getConnection(id connId) (*udpConn, bool) {
w.Lock() w.Lock()
defer w.Unlock() defer w.Unlock()
if conn, found := w.activeConn[src]; found { if conn, found := w.activeConn[id]; found {
return conn, true return conn, true
} }
conn := &udpConn{ conn := &udpConn{
input: make(chan *buf.Buffer, 32), input: make(chan *buf.Buffer, 32),
output: func(b []byte) (int, error) { output: func(b []byte) (int, error) {
return w.hub.WriteTo(b, src) return w.hub.WriteTo(b, id.src)
}, },
remote: &net.UDPAddr{ remote: &net.UDPAddr{
IP: src.Address.IP(), IP: id.src.Address.IP(),
Port: int(src.Port), Port: int(id.src.Port),
}, },
local: &net.UDPAddr{ local: &net.UDPAddr{
IP: w.address.IP(), IP: w.address.IP(),
Port: int(w.port), Port: int(w.port),
}, },
} }
w.activeConn[src] = conn w.activeConn[id] = conn
conn.updateActivity() conn.updateActivity()
return conn, false return conn, false
} }
func (w *udpWorker) callback(b *buf.Buffer, source net.Destination, originalDest net.Destination) { func (w *udpWorker) callback(b *buf.Buffer, source net.Destination, originalDest net.Destination) {
conn, existing := w.getConnection(source) id := connId{
src: source,
dest: originalDest,
}
conn, existing := w.getConnection(id)
select { select {
case conn.input <- b: case conn.input <- b:
default: default:
@ -240,20 +249,20 @@ func (w *udpWorker) callback(b *buf.Buffer, source net.Destination, originalDest
if err := w.proxy.Process(ctx, net.Network_UDP, conn, w.dispatcher); err != nil { if err := w.proxy.Process(ctx, net.Network_UDP, conn, w.dispatcher); err != nil {
log.Trace(newError("connection ends").Base(err)) log.Trace(newError("connection ends").Base(err))
} }
w.removeConn(source) w.removeConn(id)
cancel() cancel()
}() }()
} }
} }
func (w *udpWorker) removeConn(src net.Destination) { func (w *udpWorker) removeConn(id connId) {
w.Lock() w.Lock()
delete(w.activeConn, src) delete(w.activeConn, id)
w.Unlock() w.Unlock()
} }
func (w *udpWorker) Start() error { func (w *udpWorker) Start() error {
w.activeConn = make(map[net.Destination]*udpConn) w.activeConn = make(map[connId]*udpConn, 16)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
w.ctx = ctx w.ctx = ctx
w.cancel = cancel w.cancel = cancel