1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 11:51:02 -05:00
v2fly/transport/internet/udp/dispatcher_packetaddr.go
2022-01-03 15:55:18 +00:00

58 lines
1.6 KiB
Go

package udp
import (
"context"
"github.com/v2fly/v2ray-core/v4/common/buf"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/net/packetaddr"
"github.com/v2fly/v2ray-core/v4/common/protocol/udp"
"github.com/v2fly/v2ray-core/v4/features/routing"
)
type PacketAddrDispatcher struct {
conn net.PacketConn
callback ResponseCallback
ctx context.Context
}
func (p PacketAddrDispatcher) Close() error {
return p.conn.Close()
}
func (p PacketAddrDispatcher) Dispatch(ctx context.Context, destination net.Destination, payload *buf.Buffer) {
if destination.Network != net.Network_UDP {
return
}
p.conn.WriteTo(payload.Bytes(), &net.UDPAddr{IP: destination.Address.IP(), Port: int(destination.Port.Value())})
}
func (p PacketAddrDispatcher) readWorker() {
for {
readBuf := buf.New()
n, addr, err := p.conn.ReadFrom(readBuf.Extend(2048))
if err != nil {
return
}
readBuf.Resize(0, int32(n))
p.callback(p.ctx, &udp.Packet{Payload: readBuf, Source: net.DestinationFromAddr(addr)})
}
}
type PacketAddrDispatcherCreator struct {
ctx context.Context
}
func NewPacketAddrDispatcherCreator(ctx context.Context) PacketAddrDispatcherCreator {
return PacketAddrDispatcherCreator{ctx: ctx}
}
func (pdc *PacketAddrDispatcherCreator) NewPacketAddrDispatcher(
dispatcher routing.Dispatcher, callback ResponseCallback) (DispatcherI, error) {
packetConn, err := packetaddr.CreatePacketAddrConn(pdc.ctx, dispatcher, false)
if err != nil {
return nil, err
}
pd := &PacketAddrDispatcher{conn: packetConn, callback: callback, ctx: pdc.ctx}
go pd.readWorker()
return pd, nil
}