Skip to content

Commit

Permalink
fix: wrong source and destination
Browse files Browse the repository at this point in the history
  • Loading branch information
AkinoKaede committed May 28, 2023
1 parent 22c909a commit d6c52f7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
23 changes: 5 additions & 18 deletions app/tun/handler.go
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 10 additions & 8 deletions app/tun/handler_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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: "",
Expand Down
20 changes: 13 additions & 7 deletions app/tun/handler_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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():
Expand All @@ -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)

Expand All @@ -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()
}
Expand All @@ -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 changes: 24 additions & 0 deletions app/tun/net/net.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit d6c52f7

Please sign in to comment.