1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05: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
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316
4 changed files with 52 additions and 33 deletions

View File

@ -1,31 +1,18 @@
package tun
import (
"github.com/v2fly/v2ray-core/v5/common/net"
"gvisor.dev/gvisor/pkg/tcpip/stack"
tun_net "github.com/v2fly/v2ray-core/v5/app/tun/net"
)
var (
tcpQueue = make(chan TCPConn)
udpQueue = make(chan UDPConn)
tcpQueue = make(chan tun_net.TCPConn)
udpQueue = make(chan tun_net.UDPConn)
)
type TCPConn interface {
net.Conn
ID() *stack.TransportEndpointID
}
type UDPConn interface {
net.Conn
ID() *stack.TransportEndpointID
}
func handleTCP(conn TCPConn) {
func handleTCP(conn tun_net.TCPConn) {
tcpQueue <- conn
}
func handleUDP(conn UDPConn) {
func handleUDP(conn tun_net.UDPConn) {
udpQueue <- conn
}

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"
"github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/log"
@ -41,7 +42,7 @@ type TCPHandler struct {
stack *stack.Stack
}
func HandleTCP(handle func(TCPConn)) StackOption {
func HandleTCP(handle func(tun_net.TCPConn)) StackOption {
return func(s *stack.Stack) error {
tcpForwarder := tcp.NewForwarder(s, rcvWnd, maxInFlight, func(r *tcp.ForwarderRequest) {
wg := new(waiter.Queue)
@ -80,7 +81,7 @@ func HandleTCP(handle func(TCPConn)) StackOption {
}
}
func (h *TCPHandler) HandleQueue(ch chan TCPConn) {
func (h *TCPHandler) HandleQueue(ch chan tun_net.TCPConn) {
for {
select {
case conn := <-ch:
@ -93,15 +94,16 @@ func (h *TCPHandler) HandleQueue(ch chan TCPConn) {
}
}
func (h *TCPHandler) Handle(conn TCPConn) error {
func (h *TCPHandler) Handle(conn tun_net.TCPConn) error {
defer conn.Close()
id := conn.ID()
ctx := session.ContextWithInbound(h.ctx, &session.Inbound{Tag: h.config.Tag})
sessionPolicy := h.policyManager.ForLevel(h.config.UserLevel)
addr := conn.RemoteAddr()
dest := net.DestinationFromAddr(addr)
ctx = log.ContextWithAccessMessage(h.ctx, &log.AccessMessage{
From: addr,
dest := net.TCPDestination(tun_net.AddressFromTCPIPAddr(id.LocalAddress), net.Port(id.LocalPort))
src := net.TCPDestination(tun_net.AddressFromTCPIPAddr(id.RemoteAddress), net.Port(id.RemotePort))
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
From: src, // Parse IpAddr to Destination
To: dest,
Status: log.AccessAccepted,
Reason: "",

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]))
}
}
}

24
app/tun/net/net.go Normal file
View File

@ -0,0 +1,24 @@
package net
import (
"github.com/v2fly/v2ray-core/v5/common/net"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/stack"
)
type TCPConn interface {
net.Conn
ID() *stack.TransportEndpointID
}
type UDPConn interface {
net.Conn
ID() *stack.TransportEndpointID
}
func AddressFromTCPIPAddr(addr tcpip.Address) net.Address {
return net.IPAddress(addr.AsSlice())
}