|
@@ -3,6 +3,7 @@ package tun
|
|
|
import (
|
|
import (
|
|
|
"context"
|
|
"context"
|
|
|
|
|
|
|
|
|
|
+ tun_net "github.com/v2fly/v2ray-core/v5/app/tun/net"
|
|
|
"github.com/v2fly/v2ray-core/v5/common/buf"
|
|
"github.com/v2fly/v2ray-core/v5/common/buf"
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net/packetaddr"
|
|
"github.com/v2fly/v2ray-core/v5/common/net/packetaddr"
|
|
@@ -35,7 +36,7 @@ func (c *udpConn) ID() *stack.TransportEndpointID {
|
|
|
return &c.id
|
|
return &c.id
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func HandleUDP(handle func(UDPConn)) StackOption {
|
|
|
|
|
|
|
+func HandleUDP(handle func(tun_net.UDPConn)) StackOption {
|
|
|
return func(s *stack.Stack) error {
|
|
return func(s *stack.Stack) error {
|
|
|
udpForwarder := gvisor_udp.NewForwarder(s, func(r *gvisor_udp.ForwarderRequest) {
|
|
udpForwarder := gvisor_udp.NewForwarder(s, func(r *gvisor_udp.ForwarderRequest) {
|
|
|
wg := new(waiter.Queue)
|
|
wg := new(waiter.Queue)
|
|
@@ -57,7 +58,7 @@ func HandleUDP(handle func(UDPConn)) StackOption {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (h *UDPHandler) HandleQueue(ch chan UDPConn) {
|
|
|
|
|
|
|
+func (h *UDPHandler) HandleQueue(ch chan tun_net.UDPConn) {
|
|
|
for {
|
|
for {
|
|
|
select {
|
|
select {
|
|
|
case <-h.ctx.Done():
|
|
case <-h.ctx.Done():
|
|
@@ -70,7 +71,9 @@ func (h *UDPHandler) HandleQueue(ch chan UDPConn) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (h *UDPHandler) Handle(conn UDPConn) error {
|
|
|
|
|
|
|
+func (h *UDPHandler) Handle(conn tun_net.UDPConn) error {
|
|
|
|
|
+ defer conn.Close()
|
|
|
|
|
+ id := conn.ID()
|
|
|
ctx := session.ContextWithInbound(h.ctx, &session.Inbound{Tag: h.config.Tag})
|
|
ctx := session.ContextWithInbound(h.ctx, &session.Inbound{Tag: h.config.Tag})
|
|
|
packetConn := conn.(net.PacketConn)
|
|
packetConn := conn.(net.PacketConn)
|
|
|
|
|
|
|
@@ -83,10 +86,13 @@ func (h *UDPHandler) Handle(conn UDPConn) error {
|
|
|
udpDispatcherConstructor = packetAddrDispatcherFactory.NewPacketAddrDispatcher
|
|
udpDispatcherConstructor = packetAddrDispatcherFactory.NewPacketAddrDispatcher
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ dest := net.UDPDestination(tun_net.AddressFromTCPIPAddr(id.LocalAddress), net.Port(id.LocalPort))
|
|
|
|
|
+ src := net.UDPDestination(tun_net.AddressFromTCPIPAddr(id.RemoteAddress), net.Port(id.RemotePort))
|
|
|
|
|
+
|
|
|
udpServer := udpDispatcherConstructor(h.dispatcher, func(ctx context.Context, packet *udp_proto.Packet) {
|
|
udpServer := udpDispatcherConstructor(h.dispatcher, func(ctx context.Context, packet *udp_proto.Packet) {
|
|
|
if _, err := packetConn.WriteTo(packet.Payload.Bytes(), &net.UDPAddr{
|
|
if _, err := packetConn.WriteTo(packet.Payload.Bytes(), &net.UDPAddr{
|
|
|
- IP: packet.Source.Address.IP(),
|
|
|
|
|
- Port: int(packet.Source.Port),
|
|
|
|
|
|
|
+ IP: src.Address.IP(),
|
|
|
|
|
+ Port: int(src.Port),
|
|
|
}); err != nil {
|
|
}); err != nil {
|
|
|
newError("failed to write UDP packet").Base(err).WriteToLog()
|
|
newError("failed to write UDP packet").Base(err).WriteToLog()
|
|
|
}
|
|
}
|
|
@@ -98,13 +104,13 @@ func (h *UDPHandler) Handle(conn UDPConn) error {
|
|
|
return nil
|
|
return nil
|
|
|
default:
|
|
default:
|
|
|
var buffer [2048]byte
|
|
var buffer [2048]byte
|
|
|
- n, addr, err := packetConn.ReadFrom(buffer[:])
|
|
|
|
|
|
|
+ n, _, err := packetConn.ReadFrom(buffer[:])
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return newError("failed to read UDP packet").Base(err)
|
|
return newError("failed to read UDP packet").Base(err)
|
|
|
}
|
|
}
|
|
|
currentPacketCtx := ctx
|
|
currentPacketCtx := ctx
|
|
|
|
|
|
|
|
- udpServer.Dispatch(currentPacketCtx, net.DestinationFromAddr(addr), buf.FromBytes(buffer[:n]))
|
|
|
|
|
|
|
+ udpServer.Dispatch(currentPacketCtx, dest, buf.FromBytes(buffer[:n]))
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|