mirror of https://github.com/v2fly/v2ray-core.git
fix: wrong source and destination
This commit is contained in:
parent
54b605ba4c
commit
4b5f788b2f
|
@ -1,31 +1,18 @@
|
||||||
package tun
|
package tun
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/v2fly/v2ray-core/v5/common/net"
|
tun_net "github.com/v2fly/v2ray-core/v5/app/tun/net"
|
||||||
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tcpQueue = make(chan TCPConn)
|
tcpQueue = make(chan tun_net.TCPConn)
|
||||||
udpQueue = make(chan UDPConn)
|
udpQueue = make(chan tun_net.UDPConn)
|
||||||
)
|
)
|
||||||
|
|
||||||
type TCPConn interface {
|
func handleTCP(conn tun_net.TCPConn) {
|
||||||
net.Conn
|
|
||||||
|
|
||||||
ID() *stack.TransportEndpointID
|
|
||||||
}
|
|
||||||
|
|
||||||
type UDPConn interface {
|
|
||||||
net.Conn
|
|
||||||
|
|
||||||
ID() *stack.TransportEndpointID
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleTCP(conn TCPConn) {
|
|
||||||
tcpQueue <- conn
|
tcpQueue <- conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleUDP(conn UDPConn) {
|
func handleUDP(conn tun_net.UDPConn) {
|
||||||
udpQueue <- conn
|
udpQueue <- conn
|
||||||
}
|
}
|
||||||
|
|
|
@ -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"
|
"github.com/v2fly/v2ray-core/v5/common"
|
||||||
"github.com/v2fly/v2ray-core/v5/common/buf"
|
"github.com/v2fly/v2ray-core/v5/common/buf"
|
||||||
"github.com/v2fly/v2ray-core/v5/common/log"
|
"github.com/v2fly/v2ray-core/v5/common/log"
|
||||||
|
@ -41,7 +42,7 @@ type TCPHandler struct {
|
||||||
stack *stack.Stack
|
stack *stack.Stack
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleTCP(handle func(TCPConn)) StackOption {
|
func HandleTCP(handle func(tun_net.TCPConn)) StackOption {
|
||||||
return func(s *stack.Stack) error {
|
return func(s *stack.Stack) error {
|
||||||
tcpForwarder := tcp.NewForwarder(s, rcvWnd, maxInFlight, func(r *tcp.ForwarderRequest) {
|
tcpForwarder := tcp.NewForwarder(s, rcvWnd, maxInFlight, func(r *tcp.ForwarderRequest) {
|
||||||
wg := new(waiter.Queue)
|
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 {
|
for {
|
||||||
select {
|
select {
|
||||||
case conn := <-ch:
|
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})
|
ctx := session.ContextWithInbound(h.ctx, &session.Inbound{Tag: h.config.Tag})
|
||||||
sessionPolicy := h.policyManager.ForLevel(h.config.UserLevel)
|
sessionPolicy := h.policyManager.ForLevel(h.config.UserLevel)
|
||||||
|
|
||||||
addr := conn.RemoteAddr()
|
dest := net.TCPDestination(tun_net.AddressFromTCPIPAddr(id.LocalAddress), net.Port(id.LocalPort))
|
||||||
|
src := net.TCPDestination(tun_net.AddressFromTCPIPAddr(id.RemoteAddress), net.Port(id.RemotePort))
|
||||||
dest := net.DestinationFromAddr(addr)
|
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
|
||||||
ctx = log.ContextWithAccessMessage(h.ctx, &log.AccessMessage{
|
From: src, // Parse IpAddr to Destination
|
||||||
From: addr,
|
|
||||||
To: dest,
|
To: dest,
|
||||||
Status: log.AccessAccepted,
|
Status: log.AccessAccepted,
|
||||||
Reason: "",
|
Reason: "",
|
||||||
|
|
|
@ -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(),
|
IP: src.Address.IP(),
|
||||||
Port: int(packet.Source.Port),
|
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]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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())
|
||||||
|
}
|
Loading…
Reference in New Issue