mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-30 05:56:54 -05:00
Add packet addr dispatcher
This commit is contained in:
parent
75a2448c48
commit
ec9b64e5ec
57
transport/internet/udp/dispatcher_packetaddr.go
Normal file
57
transport/internet/udp/dispatcher_packetaddr.go
Normal file
@ -0,0 +1,57 @@
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue
Block a user