Xray-core/transport/internet/udp/dispatcher.go

231 lines
4.9 KiB
Go
Raw Normal View History

2020-11-25 14:01:53 +03:00
package udp
import (
"context"
2022-06-01 06:11:53 +03:00
"errors"
2020-11-25 14:01:53 +03:00
"io"
"sync"
"time"
2020-12-04 04:36:16 +03:00
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/protocol/udp"
"github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/common/signal"
"github.com/xtls/xray-core/common/signal/done"
2020-12-04 04:36:16 +03:00
"github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/transport"
2020-11-25 14:01:53 +03:00
)
type ResponseCallback func(ctx context.Context, packet *udp.Packet)
type connEntry struct {
link *transport.Link
timer signal.ActivityUpdater
cancel context.CancelFunc
}
type Dispatcher struct {
sync.RWMutex
conn *connEntry
2020-11-25 14:01:53 +03:00
dispatcher routing.Dispatcher
callback ResponseCallback
callClose func() error
2020-11-25 14:01:53 +03:00
}
func NewDispatcher(dispatcher routing.Dispatcher, callback ResponseCallback) *Dispatcher {
return &Dispatcher{
dispatcher: dispatcher,
callback: callback,
}
}
func (v *Dispatcher) RemoveRay() {
2020-11-25 14:01:53 +03:00
v.Lock()
defer v.Unlock()
if v.conn != nil {
common.Interrupt(v.conn.link.Reader)
common.Close(v.conn.link.Writer)
v.conn = nil
2020-11-25 14:01:53 +03:00
}
}
2023-01-20 14:42:49 +03:00
func (v *Dispatcher) getInboundRay(ctx context.Context, dest net.Destination) (*connEntry, error) {
2020-11-25 14:01:53 +03:00
v.Lock()
defer v.Unlock()
if v.conn != nil {
return v.conn, nil
2020-11-25 14:01:53 +03:00
}
newError("establishing new connection for ", dest).WriteToLog()
ctx, cancel := context.WithCancel(ctx)
removeRay := func() {
cancel()
v.RemoveRay()
2020-11-25 14:01:53 +03:00
}
timer := signal.CancelAfterInactivity(ctx, removeRay, time.Minute)
2023-01-20 14:42:49 +03:00
link, err := v.dispatcher.Dispatch(ctx, dest)
if err != nil {
return nil, newError("failed to dispatch request to ", dest).Base(err)
}
2020-11-25 14:01:53 +03:00
entry := &connEntry{
link: link,
timer: timer,
cancel: removeRay,
}
v.conn = entry
go handleInput(ctx, entry, dest, v.callback, v.callClose)
2023-01-20 14:42:49 +03:00
return entry, nil
2020-11-25 14:01:53 +03:00
}
func (v *Dispatcher) Dispatch(ctx context.Context, destination net.Destination, payload *buf.Buffer) {
// TODO: Add user to destString
newError("dispatch request to: ", destination).AtDebug().WriteToLog(session.ExportIDToError(ctx))
2023-01-20 14:42:49 +03:00
conn, err := v.getInboundRay(ctx, destination)
if err != nil {
newError("failed to get inbound").Base(err).WriteToLog(session.ExportIDToError(ctx))
return
}
2020-11-25 14:01:53 +03:00
outputStream := conn.link.Writer
if outputStream != nil {
if err := outputStream.WriteMultiBuffer(buf.MultiBuffer{payload}); err != nil {
newError("failed to write first UDP payload").Base(err).WriteToLog(session.ExportIDToError(ctx))
conn.cancel()
return
}
}
}
func handleInput(ctx context.Context, conn *connEntry, dest net.Destination, callback ResponseCallback, callClose func() error) {
defer func() {
conn.cancel()
if callClose != nil {
callClose()
}
}()
2020-11-25 14:01:53 +03:00
input := conn.link.Reader
timer := conn.timer
for {
select {
case <-ctx.Done():
return
default:
}
mb, err := input.ReadMultiBuffer()
if err != nil {
2022-06-01 06:11:53 +03:00
if !errors.Is(err, io.EOF) {
newError("failed to handle UDP input").Base(err).WriteToLog(session.ExportIDToError(ctx))
}
2020-11-25 14:01:53 +03:00
return
}
timer.Update()
for _, b := range mb {
if b.UDP != nil {
dest = *b.UDP
}
2020-11-25 14:01:53 +03:00
callback(ctx, &udp.Packet{
Payload: b,
Source: dest,
})
}
}
}
type dispatcherConn struct {
dispatcher *Dispatcher
cache chan *udp.Packet
done *done.Instance
2023-05-06 06:18:08 +03:00
ctx context.Context
2020-11-25 14:01:53 +03:00
}
func DialDispatcher(ctx context.Context, dispatcher routing.Dispatcher) (net.PacketConn, error) {
c := &dispatcherConn{
cache: make(chan *udp.Packet, 16),
done: done.New(),
2023-05-06 06:18:08 +03:00
ctx: ctx,
2020-11-25 14:01:53 +03:00
}
d := &Dispatcher{
dispatcher: dispatcher,
callback: c.callback,
callClose: c.Close,
}
2020-11-25 14:01:53 +03:00
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) {
var packet *udp.Packet
s:
2020-11-25 14:01:53 +03:00
select {
case <-c.done.Wait():
select {
case packet = <-c.cache:
break s
default:
return 0, nil, io.EOF
}
case packet = <-c.cache:
2020-11-25 14:01:53 +03:00
}
return copy(p, packet.Payload.Bytes()), &net.UDPAddr{
IP: packet.Source.Address.IP(),
Port: int(packet.Source.Port),
}, nil
2020-11-25 14:01:53 +03:00
}
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))
destination := net.DestinationFromAddr(addr)
buffer.UDP = &destination
c.dispatcher.Dispatch(c.ctx, destination, buffer)
2020-11-25 14:01:53 +03:00
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
}