1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-04-14 01:29:17 -04:00

fix: wrong source and destination

This commit is contained in:
AkinoKaede
2023-05-28 22:22:59 +08:00
committed by Shelikhoo
parent 54b605ba4c
commit 4b5f788b2f
4 changed files with 52 additions and 33 deletions

View File

@@ -3,6 +3,7 @@ package tun
import (
"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/net"
"github.com/v2fly/v2ray-core/v5/common/net/packetaddr"
@@ -35,7 +36,7 @@ func (c *udpConn) ID() *stack.TransportEndpointID {
return &c.id
}
func HandleUDP(handle func(UDPConn)) StackOption {
func HandleUDP(handle func(tun_net.UDPConn)) StackOption {
return func(s *stack.Stack) error {
udpForwarder := gvisor_udp.NewForwarder(s, func(r *gvisor_udp.ForwarderRequest) {
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 {
select {
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})
packetConn := conn.(net.PacketConn)
@@ -83,10 +86,13 @@ func (h *UDPHandler) Handle(conn UDPConn) error {
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) {
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 {
newError("failed to write UDP packet").Base(err).WriteToLog()
}
@@ -98,13 +104,13 @@ func (h *UDPHandler) Handle(conn UDPConn) error {
return nil
default:
var buffer [2048]byte
n, addr, err := packetConn.ReadFrom(buffer[:])
n, _, err := packetConn.ReadFrom(buffer[:])
if err != nil {
return newError("failed to read UDP packet").Base(err)
}
currentPacketCtx := ctx
udpServer.Dispatch(currentPacketCtx, net.DestinationFromAddr(addr), buf.FromBytes(buffer[:n]))
udpServer.Dispatch(currentPacketCtx, dest, buf.FromBytes(buffer[:n]))
}
}
}