1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-02-20 23:47:21 -05:00
v2fly/transport/internet/udp/dispatcher.go

198 lines
4.5 KiB
Go
Raw Normal View History

2016-06-14 22:54:08 +02:00
package udp
2016-02-01 15:36:33 +00:00
import (
"context"
2019-01-05 21:43:22 +01:00
"io"
2016-02-01 15:36:33 +00:00
"sync"
2017-11-15 00:36:14 +01:00
"time"
2016-02-01 15:36:33 +00:00
2021-02-17 04:31:50 +08:00
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/buf"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/protocol/udp"
"github.com/v2fly/v2ray-core/v4/common/session"
"github.com/v2fly/v2ray-core/v4/common/signal"
"github.com/v2fly/v2ray-core/v4/common/signal/done"
"github.com/v2fly/v2ray-core/v4/features/routing"
"github.com/v2fly/v2ray-core/v4/transport"
2016-02-01 15:36:33 +00:00
)
2019-01-05 21:43:22 +01:00
type ResponseCallback func(ctx context.Context, packet *udp.Packet)
2016-02-01 15:36:33 +00:00
2017-11-15 00:36:14 +01:00
type connEntry struct {
2018-11-03 12:36:29 +01:00
link *transport.Link
2018-04-17 00:31:10 +02:00
timer signal.ActivityUpdater
cancel context.CancelFunc
2017-11-15 00:36:14 +01:00
}
2017-01-27 14:45:16 +01:00
type Dispatcher struct {
2016-02-01 15:36:33 +00:00
sync.RWMutex
2017-11-15 00:36:14 +01:00
conns map[net.Destination]*connEntry
dispatcher routing.Dispatcher
callback ResponseCallback
2016-02-01 15:36:33 +00:00
}
func NewDispatcher(dispatcher routing.Dispatcher, callback ResponseCallback) *Dispatcher {
2017-01-27 14:45:16 +01:00
return &Dispatcher{
2017-11-15 00:36:14 +01:00
conns: make(map[net.Destination]*connEntry),
dispatcher: dispatcher,
callback: callback,
2016-02-01 15:36:33 +00:00
}
}
func (v *Dispatcher) RemoveRay(dest net.Destination) {
2016-11-27 21:39:09 +01:00
v.Lock()
defer v.Unlock()
2017-02-04 21:55:59 +01:00
if conn, found := v.conns[dest]; found {
2018-04-17 00:31:10 +02:00
common.Close(conn.link.Reader)
common.Close(conn.link.Writer)
2017-02-04 21:55:59 +01:00
delete(v.conns, dest)
2016-02-01 15:36:33 +00:00
}
}
func (v *Dispatcher) getInboundRay(ctx context.Context, dest net.Destination) *connEntry {
2017-01-06 11:40:59 +01:00
v.Lock()
defer v.Unlock()
2017-02-04 21:55:59 +01:00
if entry, found := v.conns[dest]; found {
2017-11-15 00:36:14 +01:00
return entry
2017-01-06 11:40:59 +01:00
}
2017-12-19 21:28:12 +01:00
newError("establishing new connection for ", dest).WriteToLog()
2017-11-15 00:36:14 +01:00
ctx, cancel := context.WithCancel(ctx)
2017-11-15 00:36:14 +01:00
removeRay := func() {
cancel()
v.RemoveRay(dest)
}
timer := signal.CancelAfterInactivity(ctx, removeRay, time.Second*4)
2018-04-17 00:31:10 +02:00
link, _ := v.dispatcher.Dispatch(ctx, dest)
2017-11-15 00:36:14 +01:00
entry := &connEntry{
2018-04-17 00:31:10 +02:00
link: link,
timer: timer,
cancel: removeRay,
2017-11-15 00:36:14 +01:00
}
v.conns[dest] = entry
2019-01-05 21:43:22 +01:00
go handleInput(ctx, entry, dest, v.callback)
2017-11-15 00:36:14 +01:00
return entry
2017-01-06 11:40:59 +01:00
}
func (v *Dispatcher) Dispatch(ctx context.Context, destination net.Destination, payload *buf.Buffer) {
2016-08-14 17:08:01 +02:00
// TODO: Add user to destString
newError("dispatch request to: ", destination).AtDebug().WriteToLog(session.ExportIDToError(ctx))
conn := v.getInboundRay(ctx, destination)
2018-04-17 00:31:10 +02:00
outputStream := conn.link.Writer
2016-05-12 17:20:07 -07:00
if outputStream != nil {
2018-11-16 11:08:12 +01:00
if err := outputStream.WriteMultiBuffer(buf.MultiBuffer{payload}); err != nil {
newError("failed to write first UDP payload").Base(err).WriteToLog(session.ExportIDToError(ctx))
2017-11-15 00:36:14 +01:00
conn.cancel()
return
}
2016-05-12 17:20:07 -07:00
}
2016-02-01 15:36:33 +00:00
}
2019-01-05 21:43:22 +01:00
func handleInput(ctx context.Context, conn *connEntry, dest net.Destination, callback ResponseCallback) {
defer conn.cancel()
2018-04-17 00:31:10 +02:00
input := conn.link.Reader
2017-11-15 00:36:14 +01:00
timer := conn.timer
2016-04-18 18:44:10 +02:00
for {
2017-11-15 00:36:14 +01:00
select {
case <-ctx.Done():
return
default:
}
2017-11-09 22:33:15 +01:00
mb, err := input.ReadMultiBuffer()
2016-04-18 18:44:10 +02:00
if err != nil {
newError("failed to handle UDP input").Base(err).WriteToLog(session.ExportIDToError(ctx))
2017-11-15 00:36:14 +01:00
return
2016-04-18 18:44:10 +02:00
}
2017-11-15 00:36:14 +01:00
timer.Update()
2017-04-15 21:07:23 +02:00
for _, b := range mb {
2019-01-05 21:43:22 +01:00
callback(ctx, &udp.Packet{
Payload: b,
Source: dest,
})
2017-04-15 21:07:23 +02:00
}
2016-02-01 15:36:33 +00:00
}
}
2019-01-05 21:43:22 +01:00
type dispatcherConn struct {
dispatcher *Dispatcher
cache chan *udp.Packet
done *done.Instance
}
func DialDispatcher(ctx context.Context, dispatcher routing.Dispatcher) (net.PacketConn, error) {
c := &dispatcherConn{
cache: make(chan *udp.Packet, 16),
done: done.New(),
}
d := NewDispatcher(dispatcher, c.callback)
c.dispatcher = d
return c, nil
}
func (c *dispatcherConn) callback(ctx context.Context, packet *udp.Packet) {
select {
case <-c.done.Wait():
packet.Payload.Release()
return
case c.cache <- packet:
default:
packet.Payload.Release()
return
}
}
func (c *dispatcherConn) ReadFrom(p []byte) (int, net.Addr, error) {
select {
case <-c.done.Wait():
return 0, nil, io.EOF
case packet := <-c.cache:
n := copy(p, packet.Payload.Bytes())
return n, &net.UDPAddr{
IP: packet.Source.Address.IP(),
Port: int(packet.Source.Port),
}, nil
}
}
func (c *dispatcherConn) WriteTo(p []byte, addr net.Addr) (int, error) {
buffer := buf.New()
raw := buffer.Extend(buf.Size)
n := copy(raw, p)
buffer.Resize(0, int32(n))
ctx := context.Background()
c.dispatcher.Dispatch(ctx, net.DestinationFromAddr(addr), buffer)
return n, nil
}
func (c *dispatcherConn) Close() error {
return c.done.Close()
}
func (c *dispatcherConn) LocalAddr() net.Addr {
return &net.UDPAddr{
IP: []byte{0, 0, 0, 0},
Port: 0,
}
}
func (c *dispatcherConn) SetDeadline(t time.Time) error {
return nil
}
func (c *dispatcherConn) SetReadDeadline(t time.Time) error {
return nil
}
func (c *dispatcherConn) SetWriteDeadline(t time.Time) error {
return nil
}