diff --git a/app/commander/config.pb.go b/app/commander/config.pb.go index fc1fec8b1..74ecbd6d3 100644 --- a/app/commander/config.pb.go +++ b/app/commander/config.pb.go @@ -1,9 +1,11 @@ package commander -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import serial "v2ray.com/core/common/serial" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + serial "v2ray.com/core/common/serial" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/app/dispatcher/config.pb.go b/app/dispatcher/config.pb.go index 4cacaec01..3c991d756 100644 --- a/app/dispatcher/config.pb.go +++ b/app/dispatcher/config.pb.go @@ -1,8 +1,10 @@ package dispatcher -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/app/dns/config.pb.go b/app/dns/config.pb.go index ca29ee81b..e069d9b6c 100644 --- a/app/dns/config.pb.go +++ b/app/dns/config.pb.go @@ -1,9 +1,11 @@ package dns -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import net "v2ray.com/core/common/net" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + net "v2ray.com/core/common/net" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/app/log/command/config.pb.go b/app/log/command/config.pb.go index 37d151078..c1181aea2 100644 --- a/app/log/command/config.pb.go +++ b/app/log/command/config.pb.go @@ -1,8 +1,11 @@ package command -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + math "math" + + proto "github.com/golang/protobuf/proto" +) import ( "context" diff --git a/app/log/config.pb.go b/app/log/config.pb.go index 609c58eaa..90ec9424e 100644 --- a/app/log/config.pb.go +++ b/app/log/config.pb.go @@ -1,9 +1,11 @@ package log -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import log "v2ray.com/core/common/log" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + log "v2ray.com/core/common/log" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/app/policy/config.pb.go b/app/policy/config.pb.go index f55efb9b5..4986c4be8 100644 --- a/app/policy/config.pb.go +++ b/app/policy/config.pb.go @@ -1,8 +1,10 @@ package policy -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/app/proxyman/command/command.pb.go b/app/proxyman/command/command.pb.go index 66e03ae99..03f7660f2 100644 --- a/app/proxyman/command/command.pb.go +++ b/app/proxyman/command/command.pb.go @@ -1,11 +1,14 @@ package command -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import core "v2ray.com/core" -import protocol "v2ray.com/core/common/protocol" -import serial "v2ray.com/core/common/serial" +import ( + fmt "fmt" + math "math" + + proto "github.com/golang/protobuf/proto" + core "v2ray.com/core" + protocol "v2ray.com/core/common/protocol" + serial "v2ray.com/core/common/serial" +) import ( "context" diff --git a/app/proxyman/config.pb.go b/app/proxyman/config.pb.go index 3aeb062bb..92469b32f 100644 --- a/app/proxyman/config.pb.go +++ b/app/proxyman/config.pb.go @@ -1,11 +1,13 @@ package proxyman -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import net "v2ray.com/core/common/net" -import serial "v2ray.com/core/common/serial" -import internet "v2ray.com/core/transport/internet" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + net "v2ray.com/core/common/net" + serial "v2ray.com/core/common/serial" + internet "v2ray.com/core/transport/internet" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/app/proxyman/inbound/always.go b/app/proxyman/inbound/always.go index ecb537b2a..24998d102 100644 --- a/app/proxyman/inbound/always.go +++ b/app/proxyman/inbound/always.go @@ -69,13 +69,25 @@ func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig * if address == nil { address = net.AnyIP } + + mss, err := internet.ToMemoryStreamConfig(receiverConfig.StreamSettings) + if err != nil { + return nil, newError("failed to parse stream config").Base(err).AtWarning() + } + + if receiverConfig.ReceiveOriginalDestination { + if mss.SocketSettings == nil { + mss.SocketSettings = &internet.SocketConfig{} + } + if mss.SocketSettings.Tproxy == internet.SocketConfig_Off { + mss.SocketSettings.Tproxy = internet.SocketConfig_Redirect + } + mss.SocketSettings.ReceiveOriginalDestAddress = true + } + for port := pr.From; port <= pr.To; port++ { if nl.HasNetwork(net.Network_TCP) { newError("creating stream worker on ", address, ":", port).AtDebug().WriteToLog() - mss, err := internet.ToMemoryStreamConfig(receiverConfig.StreamSettings) - if err != nil { - return nil, newError("failed to parse stream config").Base(err).AtWarning() - } worker := &tcpWorker{ address: address, @@ -98,10 +110,10 @@ func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig * proxy: p, address: address, port: net.Port(port), - recvOrigDest: receiverConfig.ReceiveOriginalDestination, dispatcher: h.mux, uplinkCounter: uplinkCounter, downlinkCounter: downlinkCounter, + stream: mss, } h.workers = append(h.workers, worker) } diff --git a/app/proxyman/inbound/dynamic.go b/app/proxyman/inbound/dynamic.go index d00cd7ea4..1ff48872f 100644 --- a/app/proxyman/inbound/dynamic.go +++ b/app/proxyman/inbound/dynamic.go @@ -45,6 +45,15 @@ func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *p if err != nil { return nil, newError("failed to parse stream settings").Base(err).AtWarning() } + if receiverConfig.ReceiveOriginalDestination { + if mss.SocketSettings == nil { + mss.SocketSettings = &internet.SocketConfig{} + } + if mss.SocketSettings.Tproxy == internet.SocketConfig_Off { + mss.SocketSettings.Tproxy = internet.SocketConfig_Redirect + } + mss.SocketSettings.ReceiveOriginalDestAddress = true + } h.streamSettings = mss @@ -139,10 +148,10 @@ func (h *DynamicInboundHandler) refresh() error { proxy: p, address: address, port: port, - recvOrigDest: h.receiverConfig.ReceiveOriginalDestination, dispatcher: h.mux, uplinkCounter: uplinkCounter, downlinkCounter: downlinkCounter, + stream: h.streamSettings, } if err := worker.Start(); err != nil { newError("failed to create UDP worker").Base(err).AtWarning().WriteToLog() diff --git a/app/proxyman/inbound/worker.go b/app/proxyman/inbound/worker.go index 46d7f55d9..bf00c3534 100644 --- a/app/proxyman/inbound/worker.go +++ b/app/proxyman/inbound/worker.go @@ -204,8 +204,8 @@ type udpWorker struct { hub *udp.Hub address net.Address port net.Port - recvOrigDest bool tag string + stream *internet.MemoryStreamConfig dispatcher core.Dispatcher uplinkCounter core.StatCounter downlinkCounter core.StatCounter @@ -322,7 +322,8 @@ func (w *udpWorker) clean() error { func (w *udpWorker) Start() error { w.activeConn = make(map[connID]*udpConn, 16) - h, err := udp.ListenUDP(w.address, w.port, udp.HubReceiveOriginalDestination(w.recvOrigDest), udp.HubCapacity(256)) + ctx := internet.ContextWithStreamSettings(context.Background(), w.stream) + h, err := udp.ListenUDP(ctx, w.address, w.port, udp.HubCapacity(256)) if err != nil { return err } diff --git a/app/router/config.pb.go b/app/router/config.pb.go index e262cf3fe..48f0c4985 100644 --- a/app/router/config.pb.go +++ b/app/router/config.pb.go @@ -1,9 +1,11 @@ package router -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import net "v2ray.com/core/common/net" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + net "v2ray.com/core/common/net" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/app/stats/command/command.pb.go b/app/stats/command/command.pb.go index b5ed65566..a8f9332d0 100644 --- a/app/stats/command/command.pb.go +++ b/app/stats/command/command.pb.go @@ -1,8 +1,11 @@ package command -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + math "math" + + proto "github.com/golang/protobuf/proto" +) import ( "context" diff --git a/app/stats/config.pb.go b/app/stats/config.pb.go index ff08d17e8..a90bf1adb 100644 --- a/app/stats/config.pb.go +++ b/app/stats/config.pb.go @@ -1,8 +1,10 @@ package stats -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/common/log/log.pb.go b/common/log/log.pb.go index 763a4debe..db0850903 100644 --- a/common/log/log.pb.go +++ b/common/log/log.pb.go @@ -1,8 +1,10 @@ package log -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/common/net/address.pb.go b/common/net/address.pb.go index 5e158cd75..9ff19fc45 100644 --- a/common/net/address.pb.go +++ b/common/net/address.pb.go @@ -1,8 +1,10 @@ package net -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/common/net/destination.pb.go b/common/net/destination.pb.go index f97cf71ff..79c8ab560 100644 --- a/common/net/destination.pb.go +++ b/common/net/destination.pb.go @@ -1,8 +1,10 @@ package net -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/common/net/network.pb.go b/common/net/network.pb.go index 4f9cd57e6..7252aaa53 100644 --- a/common/net/network.pb.go +++ b/common/net/network.pb.go @@ -1,8 +1,10 @@ package net -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/common/net/port.pb.go b/common/net/port.pb.go index 037579682..3db91c5f7 100644 --- a/common/net/port.pb.go +++ b/common/net/port.pb.go @@ -1,8 +1,10 @@ package net -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/common/protocol/command.pb.go b/common/protocol/command.pb.go index 7f8856797..a489f0361 100644 --- a/common/protocol/command.pb.go +++ b/common/protocol/command.pb.go @@ -1,10 +1,12 @@ package protocol -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import serial "v2ray.com/core/common/serial" -import internet "v2ray.com/core/transport/internet" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + serial "v2ray.com/core/common/serial" + internet "v2ray.com/core/transport/internet" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/common/protocol/headers.pb.go b/common/protocol/headers.pb.go index 30d5a9262..2f94da1ce 100644 --- a/common/protocol/headers.pb.go +++ b/common/protocol/headers.pb.go @@ -1,8 +1,10 @@ package protocol -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/common/protocol/server_spec.pb.go b/common/protocol/server_spec.pb.go index c43d36130..8c6931db7 100644 --- a/common/protocol/server_spec.pb.go +++ b/common/protocol/server_spec.pb.go @@ -1,9 +1,11 @@ package protocol -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import net "v2ray.com/core/common/net" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + net "v2ray.com/core/common/net" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/common/protocol/user.pb.go b/common/protocol/user.pb.go index 9137f99cd..dbc4d23e2 100644 --- a/common/protocol/user.pb.go +++ b/common/protocol/user.pb.go @@ -1,9 +1,11 @@ package protocol -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import serial "v2ray.com/core/common/serial" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + serial "v2ray.com/core/common/serial" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/common/serial/typed_message.pb.go b/common/serial/typed_message.pb.go index 90193a51a..ba303b0cd 100644 --- a/common/serial/typed_message.pb.go +++ b/common/serial/typed_message.pb.go @@ -1,8 +1,10 @@ package serial -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/config.pb.go b/config.pb.go index 9de0a7a28..2c34f74bb 100644 --- a/config.pb.go +++ b/config.pb.go @@ -1,10 +1,12 @@ package core -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import serial "v2ray.com/core/common/serial" -import transport "v2ray.com/core/transport" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + serial "v2ray.com/core/common/serial" + transport "v2ray.com/core/transport" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/proxy/blackhole/config.pb.go b/proxy/blackhole/config.pb.go index 9c69ea502..5ebb01a93 100644 --- a/proxy/blackhole/config.pb.go +++ b/proxy/blackhole/config.pb.go @@ -1,9 +1,11 @@ package blackhole -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import serial "v2ray.com/core/common/serial" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + serial "v2ray.com/core/common/serial" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/proxy/dokodemo/config.pb.go b/proxy/dokodemo/config.pb.go index 5ce9edd22..296da878f 100644 --- a/proxy/dokodemo/config.pb.go +++ b/proxy/dokodemo/config.pb.go @@ -1,9 +1,11 @@ package dokodemo -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import net "v2ray.com/core/common/net" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + net "v2ray.com/core/common/net" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index cf382fcfe..cc612519d 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -15,7 +15,6 @@ import ( "v2ray.com/core/common/task" "v2ray.com/core/proxy" "v2ray.com/core/transport/internet" - "v2ray.com/core/transport/internet/udp" "v2ray.com/core/transport/pipe" ) @@ -111,12 +110,18 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in if !d.config.FollowRedirect { writer = &buf.SequentialWriter{Writer: conn} } else { - srca := net.UDPAddr{IP: dest.Address.IP(), Port: int(dest.Port.Value())} - origsend, err := udp.TransmitSocket(&srca, conn.RemoteAddr()) + tCtx := internet.ContextWithBindAddress(context.Background(), dest) + tCtx = internet.ContextWithStreamSettings(tCtx, &internet.MemoryStreamConfig{ + ProtocolName: "udp", + SocketSettings: &internet.SocketConfig{ + Tproxy: internet.SocketConfig_TProxy, + }, + }) + tConn, err := internet.DialSystem(tCtx, nil, net.DestinationFromAddr(conn.RemoteAddr())) if err != nil { return err } - writer = &buf.SequentialWriter{Writer: origsend} + writer = &buf.SequentialWriter{Writer: tConn} } } diff --git a/proxy/freedom/config.pb.go b/proxy/freedom/config.pb.go index cc264a024..8991a0892 100644 --- a/proxy/freedom/config.pb.go +++ b/proxy/freedom/config.pb.go @@ -1,9 +1,11 @@ package freedom -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import protocol "v2ray.com/core/common/protocol" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + protocol "v2ray.com/core/common/protocol" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/proxy/http/config.pb.go b/proxy/http/config.pb.go index 9c44d2707..47dedd3a1 100644 --- a/proxy/http/config.pb.go +++ b/proxy/http/config.pb.go @@ -1,8 +1,10 @@ package http -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/proxy/mtproto/config.pb.go b/proxy/mtproto/config.pb.go index 34e58130d..0d2d6c18c 100644 --- a/proxy/mtproto/config.pb.go +++ b/proxy/mtproto/config.pb.go @@ -1,9 +1,11 @@ package mtproto -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import protocol "v2ray.com/core/common/protocol" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + protocol "v2ray.com/core/common/protocol" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/proxy/shadowsocks/config.pb.go b/proxy/shadowsocks/config.pb.go index 30e1a5b7c..ef728c2aa 100644 --- a/proxy/shadowsocks/config.pb.go +++ b/proxy/shadowsocks/config.pb.go @@ -1,10 +1,12 @@ package shadowsocks -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import net "v2ray.com/core/common/net" -import protocol "v2ray.com/core/common/protocol" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + net "v2ray.com/core/common/net" + protocol "v2ray.com/core/common/protocol" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/proxy/socks/config.pb.go b/proxy/socks/config.pb.go index 7f9bf54e8..bf6c70498 100644 --- a/proxy/socks/config.pb.go +++ b/proxy/socks/config.pb.go @@ -1,10 +1,12 @@ package socks -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import net "v2ray.com/core/common/net" -import protocol "v2ray.com/core/common/protocol" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + net "v2ray.com/core/common/net" + protocol "v2ray.com/core/common/protocol" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/proxy/vmess/account.pb.go b/proxy/vmess/account.pb.go index e37c44130..4a1b1b26e 100755 --- a/proxy/vmess/account.pb.go +++ b/proxy/vmess/account.pb.go @@ -1,9 +1,11 @@ package vmess -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import protocol "v2ray.com/core/common/protocol" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + protocol "v2ray.com/core/common/protocol" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/proxy/vmess/inbound/config.pb.go b/proxy/vmess/inbound/config.pb.go index 046d225e5..8abd4c30d 100644 --- a/proxy/vmess/inbound/config.pb.go +++ b/proxy/vmess/inbound/config.pb.go @@ -1,9 +1,11 @@ package inbound -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import protocol "v2ray.com/core/common/protocol" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + protocol "v2ray.com/core/common/protocol" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/proxy/vmess/outbound/config.pb.go b/proxy/vmess/outbound/config.pb.go index 6775c59d1..aa697c754 100644 --- a/proxy/vmess/outbound/config.pb.go +++ b/proxy/vmess/outbound/config.pb.go @@ -1,9 +1,11 @@ package outbound -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import protocol "v2ray.com/core/common/protocol" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + protocol "v2ray.com/core/common/protocol" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/transport/config.pb.go b/transport/config.pb.go index a1fa20979..00b62a2d0 100644 --- a/transport/config.pb.go +++ b/transport/config.pb.go @@ -1,9 +1,11 @@ package transport -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import internet "v2ray.com/core/transport/internet" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + internet "v2ray.com/core/transport/internet" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/transport/internet/config.go b/transport/internet/config.go index e665da8b9..2f8968486 100644 --- a/transport/internet/config.go +++ b/transport/internet/config.go @@ -130,3 +130,7 @@ func ApplyGlobalTransportSettings(settings []*TransportConfig) error { func (c *ProxyConfig) HasTag() bool { return c != nil && len(c.Tag) > 0 } + +func (m SocketConfig_TProxyMode) IsEnabled() bool { + return m != SocketConfig_Off +} diff --git a/transport/internet/config.pb.go b/transport/internet/config.pb.go index 7d31a7553..8491987a0 100644 --- a/transport/internet/config.pb.go +++ b/transport/internet/config.pb.go @@ -1,9 +1,11 @@ package internet -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import serial "v2ray.com/core/common/serial" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + serial "v2ray.com/core/common/serial" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal @@ -84,6 +86,37 @@ func (SocketConfig_TCPFastOpenState) EnumDescriptor() ([]byte, []int) { return fileDescriptor_91dbc815c3d97a05, []int{3, 0} } +type SocketConfig_TProxyMode int32 + +const ( + // TProxy is off. + SocketConfig_Off SocketConfig_TProxyMode = 0 + // TProxy mode. + SocketConfig_TProxy SocketConfig_TProxyMode = 1 + // Redirect mode. + SocketConfig_Redirect SocketConfig_TProxyMode = 2 +) + +var SocketConfig_TProxyMode_name = map[int32]string{ + 0: "Off", + 1: "TProxy", + 2: "Redirect", +} + +var SocketConfig_TProxyMode_value = map[string]int32{ + "Off": 0, + "TProxy": 1, + "Redirect": 2, +} + +func (x SocketConfig_TProxyMode) String() string { + return proto.EnumName(SocketConfig_TProxyMode_name, int32(x)) +} + +func (SocketConfig_TProxyMode) EnumDescriptor() ([]byte, []int) { + return fileDescriptor_91dbc815c3d97a05, []int{3, 1} +} + type TransportConfig struct { // Type of network that this settings supports. // Deprecated. Use the string form below. @@ -270,10 +303,12 @@ type SocketConfig struct { // TFO is the state of TFO settings. Tfo SocketConfig_TCPFastOpenState `protobuf:"varint,2,opt,name=tfo,proto3,enum=v2ray.core.transport.internet.SocketConfig_TCPFastOpenState" json:"tfo,omitempty"` // TProxy is for enabling TProxy socket option. - Tproxy bool `protobuf:"varint,3,opt,name=tproxy,proto3" json:"tproxy,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + Tproxy SocketConfig_TProxyMode `protobuf:"varint,3,opt,name=tproxy,proto3,enum=v2ray.core.transport.internet.SocketConfig_TProxyMode" json:"tproxy,omitempty"` + // ReceiveOriginalDestAddress is for enabling IP_RECVORIGDSTADDR socket option. + ReceiveOriginalDestAddress bool `protobuf:"varint,4,opt,name=receive_original_dest_address,json=receiveOriginalDestAddress,proto3" json:"receive_original_dest_address,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *SocketConfig) Reset() { *m = SocketConfig{} } @@ -314,10 +349,17 @@ func (m *SocketConfig) GetTfo() SocketConfig_TCPFastOpenState { return SocketConfig_AsIs } -func (m *SocketConfig) GetTproxy() bool { +func (m *SocketConfig) GetTproxy() SocketConfig_TProxyMode { if m != nil { return m.Tproxy } + return SocketConfig_Off +} + +func (m *SocketConfig) GetReceiveOriginalDestAddress() bool { + if m != nil { + return m.ReceiveOriginalDestAddress + } return false } @@ -328,6 +370,7 @@ func init() { proto.RegisterType((*SocketConfig)(nil), "v2ray.core.transport.internet.SocketConfig") proto.RegisterEnum("v2ray.core.transport.internet.TransportProtocol", TransportProtocol_name, TransportProtocol_value) proto.RegisterEnum("v2ray.core.transport.internet.SocketConfig_TCPFastOpenState", SocketConfig_TCPFastOpenState_name, SocketConfig_TCPFastOpenState_value) + proto.RegisterEnum("v2ray.core.transport.internet.SocketConfig_TProxyMode", SocketConfig_TProxyMode_name, SocketConfig_TProxyMode_value) } func init() { @@ -335,39 +378,43 @@ func init() { } var fileDescriptor_91dbc815c3d97a05 = []byte{ - // 533 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x92, 0xd1, 0x6b, 0x13, 0x4f, - 0x10, 0xc7, 0x7b, 0xb9, 0x24, 0x4d, 0x26, 0x69, 0xba, 0xd9, 0x87, 0x1f, 0xe1, 0x07, 0xc5, 0x18, - 0x41, 0x82, 0xc2, 0x5e, 0x39, 0xf1, 0xcd, 0x17, 0x93, 0x28, 0x16, 0x6d, 0x7b, 0x5c, 0x4e, 0x85, - 0x82, 0x84, 0xcd, 0xb9, 0x0d, 0x47, 0x73, 0xb7, 0x61, 0x77, 0x15, 0xf3, 0x2f, 0xf9, 0xec, 0x3f, - 0xe0, 0x9b, 0x7f, 0x96, 0xec, 0xde, 0xed, 0x12, 0xaa, 0xc4, 0x8a, 0x6f, 0x73, 0x37, 0x33, 0xdf, - 0xf9, 0x7e, 0x66, 0x16, 0xc8, 0xe7, 0x50, 0xd0, 0x2d, 0x49, 0x79, 0x1e, 0xa4, 0x5c, 0xb0, 0x40, - 0x09, 0x5a, 0xc8, 0x0d, 0x17, 0x2a, 0xc8, 0x0a, 0xc5, 0x44, 0xc1, 0x54, 0x90, 0xf2, 0xe2, 0x3a, - 0x5b, 0x91, 0x8d, 0xe0, 0x8a, 0xe3, 0x13, 0x5b, 0x2f, 0x18, 0x71, 0xb5, 0xc4, 0xd6, 0xfe, 0x7f, - 0x7a, 0x4b, 0x2e, 0xe5, 0x79, 0xce, 0x8b, 0x40, 0x32, 0x91, 0xd1, 0x75, 0xa0, 0xb6, 0x1b, 0xf6, - 0x71, 0x91, 0x33, 0x29, 0xe9, 0x8a, 0x95, 0x82, 0xa3, 0x1f, 0x1e, 0x1c, 0x27, 0x56, 0x68, 0x6a, - 0x46, 0xe1, 0x37, 0xd0, 0x32, 0xc9, 0x94, 0xaf, 0x07, 0xde, 0xd0, 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of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x53, 0xdd, 0x6e, 0xd3, 0x4c, + 0x10, 0xad, 0xed, 0x34, 0x4d, 0x27, 0x69, 0xea, 0xee, 0x55, 0x54, 0xa9, 0xfa, 0xfa, 0x05, 0x09, + 0x45, 0x20, 0xad, 0x2b, 0x23, 0xb8, 0xe2, 0xa6, 0x4d, 0x40, 0x54, 0xd0, 0xc6, 0x72, 0x0c, 0x48, + 0x95, 0x90, 0xb5, 0x75, 0x26, 0x91, 0xd5, 0xd8, 0x1b, 0xed, 0x2e, 0x15, 0x79, 0x25, 0xae, 0x79, + 0x08, 0x5e, 0x86, 0x77, 0x40, 0xbb, 0xfe, 0x21, 0x2a, 0x28, 0xb4, 0xe2, 0x6e, 0x3c, 0x73, 0xe6, + 0xcc, 0x39, 0x33, 0x5e, 0xa0, 0xb7, 0xbe, 0x60, 0x2b, 0x9a, 0xf0, 0xcc, 0x4b, 0xb8, 0x40, 0x4f, + 0x09, 0x96, 0xcb, 0x25, 0x17, 0xca, 0x4b, 0x73, 0x85, 0x22, 0x47, 0xe5, 0x25, 0x3c, 0x9f, 0xa5, + 0x73, 0xba, 0x14, 0x5c, 0x71, 0x72, 0x54, 0xe1, 0x05, 0xd2, 0x1a, 0x4b, 0x2b, 0xec, 0xe1, 0xc9, + 0x1d, 0xba, 0x84, 0x67, 0x19, 0xcf, 0x3d, 0x89, 0x22, 0x65, 0x0b, 0x4f, 0xad, 0x96, 0x38, 0x8d, + 0x33, 0x94, 0x92, 0xcd, 0xb1, 0x20, 0xec, 0x7f, 0xb7, 0x60, 0x3f, 0xaa, 0x88, 0x86, 0x66, 0x14, + 0x79, 0x07, 0x2d, 0x53, 0x4c, 0xf8, 0xa2, 0x67, 0x1d, 0x5b, 0x83, 0xae, 0x7f, 0x42, 0x37, 0xce, + 0xa5, 0x35, 0x43, 0x50, 0xf6, 0x85, 0x35, 0x03, 0x79, 0x04, 0x7b, 0x55, 0x1c, 0xe7, 0x2c, 0xc3, + 0x9e, 0x73, 0x6c, 0x0d, 0x76, 0xc3, 0x4e, 0x95, 0xbc, 0x64, 0x19, 0x92, 0x33, 0x68, 0x49, 0x54, + 0x2a, 0xcd, 0xe7, 0xb2, 0x67, 0x1f, 0x5b, 0x83, 0xb6, 0xff, 0x78, 0x7d, 0x64, 0xe1, 0x83, 0x16, + 0x3e, 0x68, 0xa4, 0x7d, 0x5c, 0x14, 0x36, 0xc2, 0xba, 0xaf, 0xff, 0xcd, 0x81, 0xce, 0x44, 0x09, + 0x64, 0x59, 0xe9, 0x23, 0xf8, 0x77, 0x1f, 0x67, 0x76, 0xcf, 0xda, 0xe4, 0x65, 0xfb, 0x0f, 0x5e, + 0x3e, 0x01, 0xa9, 0xa9, 0xe3, 0x35, 0x57, 0xce, 0xa0, 0xed, 0xd3, 0xfb, 0x0a, 0x28, 0x2c, 0x84, + 0x07, 0x35, 0x66, 0x52, 0x12, 0x69, 0x0d, 0x12, 0x93, 0xcf, 0x22, 0x55, 0xab, 0x58, 0x5f, 0xb4, + 0xda, 0x67, 0x95, 0xd4, 0xdb, 0x21, 0x13, 0x38, 0xa8, 0x41, 0xb5, 0x84, 0x86, 0x91, 0x70, 0xdf, + 0xc5, 0xba, 0x15, 0x41, 0x3d, 0x39, 0x82, 0x7d, 0xc9, 0x93, 0x1b, 0x5c, 0x73, 0xd5, 0x34, 0xb7, + 0x7a, 0xfa, 0x17, 0x57, 0x13, 0xd3, 0x55, 0x5a, 0xea, 0x16, 0x1c, 0x15, 0x6b, 0xff, 0x3f, 0x68, + 0x07, 0x82, 0x7f, 0x59, 0x95, 0x47, 0x73, 0xc1, 0x51, 0x6c, 0x6e, 0xee, 0xb5, 0x1b, 0xea, 0xb0, + 0xff, 0xc3, 0x86, 0xce, 0x3a, 0x03, 0x21, 0xd0, 0xc8, 0x98, 0xb8, 0x31, 0x98, 0xed, 0xd0, 0xc4, + 0xe4, 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xd0, 0x19, 0xf1, 0x8c, 0xa5, 0x79, 0x59, 0xdb, 0x3e, + 0x1b, 0xc3, 0xff, 0x09, 0xcf, 0x36, 0xef, 0x32, 0xb0, 0xae, 0x5a, 0x55, 0xfc, 0xd5, 0x3e, 0xfa, + 0xe0, 0x87, 0x6c, 0x45, 0x87, 0x1a, 0x5b, 0xcb, 0xa2, 0xe7, 0x65, 0xfd, 0xba, 0x69, 0x9e, 0xde, + 0xb3, 0x9f, 0x01, 0x00, 0x00, 0xff, 0xff, 0xf2, 0x99, 0xd5, 0x37, 0x49, 0x05, 0x00, 0x00, } diff --git a/transport/internet/config.proto b/transport/internet/config.proto index a01814af8..27d5a772a 100644 --- a/transport/internet/config.proto +++ b/transport/internet/config.proto @@ -68,6 +68,19 @@ message SocketConfig { // TFO is the state of TFO settings. TCPFastOpenState tfo = 2; + enum TProxyMode { + // TProxy is off. + Off = 0; + // TProxy mode. + TProxy = 1; + // Redirect mode. + Redirect = 2; + } + // TProxy is for enabling TProxy socket option. - bool tproxy = 3; + TProxyMode tproxy = 3; + + // ReceiveOriginalDestAddress is for enabling IP_RECVORIGDSTADDR socket option. + // This option is for UDP only. + bool receive_original_dest_address = 4; } diff --git a/transport/internet/context.go b/transport/internet/context.go index 9d24dee23..6b059a6c4 100644 --- a/transport/internet/context.go +++ b/transport/internet/context.go @@ -11,6 +11,7 @@ type key int const ( streamSettingsKey key = iota dialerSrcKey + bindAddrKey ) func ContextWithStreamSettings(ctx context.Context, streamSettings *MemoryStreamConfig) context.Context { @@ -35,3 +36,14 @@ func DialerSourceFromContext(ctx context.Context) net.Address { } return net.AnyIP } + +func ContextWithBindAddress(ctx context.Context, dest net.Destination) context.Context { + return context.WithValue(ctx, bindAddrKey, dest) +} + +func BindAddressFromContext(ctx context.Context) net.Destination { + if addr, ok := ctx.Value(bindAddrKey).(net.Destination); ok { + return addr + } + return net.Destination{} +} diff --git a/transport/internet/domainsocket/config.pb.go b/transport/internet/domainsocket/config.pb.go index 178eb3c65..c1ad33527 100644 --- a/transport/internet/domainsocket/config.pb.go +++ b/transport/internet/domainsocket/config.pb.go @@ -1,8 +1,10 @@ package domainsocket -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/transport/internet/headers/http/config.pb.go b/transport/internet/headers/http/config.pb.go index 6fdd9beab..ad9984bf7 100644 --- a/transport/internet/headers/http/config.pb.go +++ b/transport/internet/headers/http/config.pb.go @@ -1,8 +1,10 @@ package http -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/transport/internet/headers/noop/config.pb.go b/transport/internet/headers/noop/config.pb.go index 9f0449de5..2783b144d 100644 --- a/transport/internet/headers/noop/config.pb.go +++ b/transport/internet/headers/noop/config.pb.go @@ -1,8 +1,10 @@ package noop -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/transport/internet/headers/srtp/config.pb.go b/transport/internet/headers/srtp/config.pb.go index d85016f0f..c10534b57 100644 --- a/transport/internet/headers/srtp/config.pb.go +++ b/transport/internet/headers/srtp/config.pb.go @@ -1,8 +1,10 @@ package srtp -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/transport/internet/headers/tls/config.pb.go b/transport/internet/headers/tls/config.pb.go index d35342f82..f61d92a0b 100644 --- a/transport/internet/headers/tls/config.pb.go +++ b/transport/internet/headers/tls/config.pb.go @@ -1,8 +1,10 @@ package tls -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/transport/internet/headers/utp/config.pb.go b/transport/internet/headers/utp/config.pb.go index 488ad2aff..53e396ac1 100644 --- a/transport/internet/headers/utp/config.pb.go +++ b/transport/internet/headers/utp/config.pb.go @@ -1,8 +1,10 @@ package utp -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/transport/internet/headers/wechat/config.pb.go b/transport/internet/headers/wechat/config.pb.go index edcc8bc9d..42808b049 100644 --- a/transport/internet/headers/wechat/config.pb.go +++ b/transport/internet/headers/wechat/config.pb.go @@ -1,8 +1,10 @@ package wechat -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/transport/internet/headers/wireguard/config.pb.go b/transport/internet/headers/wireguard/config.pb.go index d5590504d..744c5ac6f 100644 --- a/transport/internet/headers/wireguard/config.pb.go +++ b/transport/internet/headers/wireguard/config.pb.go @@ -1,8 +1,10 @@ package wireguard -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/transport/internet/http/config.pb.go b/transport/internet/http/config.pb.go index 8a7744a14..5cd03f128 100644 --- a/transport/internet/http/config.pb.go +++ b/transport/internet/http/config.pb.go @@ -1,8 +1,10 @@ package http -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/transport/internet/kcp/config.pb.go b/transport/internet/kcp/config.pb.go index 71f4c0430..bfad94d25 100644 --- a/transport/internet/kcp/config.pb.go +++ b/transport/internet/kcp/config.pb.go @@ -1,9 +1,11 @@ package kcp -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import serial "v2ray.com/core/common/serial" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + serial "v2ray.com/core/common/serial" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/transport/internet/kcp/listener.go b/transport/internet/kcp/listener.go index be8ef4f83..e9c22c8f6 100644 --- a/transport/internet/kcp/listener.go +++ b/transport/internet/kcp/listener.go @@ -61,7 +61,7 @@ func NewListener(ctx context.Context, address net.Address, port net.Port, addCon l.tlsConfig = config.GetTLSConfig() } - hub, err := udp.ListenUDP(address, port, udp.HubCapacity(1024)) + hub, err := udp.ListenUDP(ctx, address, port, udp.HubCapacity(1024)) if err != nil { return nil, err } diff --git a/transport/internet/sockopt_darwin.go b/transport/internet/sockopt_darwin.go index 3db792caf..4a2117af4 100644 --- a/transport/internet/sockopt_darwin.go +++ b/transport/internet/sockopt_darwin.go @@ -2,6 +2,8 @@ package internet import ( "syscall" + + "v2ray.com/core/common/net" ) const ( @@ -43,3 +45,7 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) return nil } + +func bindAddr(fd uintptr, address net.Address, port net.Port) error { + return nil +} diff --git a/transport/internet/sockopt_linux.go b/transport/internet/sockopt_linux.go index 8673308d5..35063c3f5 100644 --- a/transport/internet/sockopt_linux.go +++ b/transport/internet/sockopt_linux.go @@ -2,6 +2,8 @@ package internet import ( "syscall" + + "v2ray.com/core/common/net" ) const ( @@ -11,6 +13,29 @@ const ( TCP_FASTOPEN_CONNECT = 30 ) +func bindAddr(fd uintptr, address net.Address, port net.Port) error { + var sockaddr syscall.Sockaddr + + switch address.Family() { + case net.AddressFamilyIPv4: + a4 := &syscall.SockaddrInet4{ + Port: int(port), + } + copy(a4.Addr[:], address.IP()) + sockaddr = a4 + case net.AddressFamilyIPv6: + a6 := &syscall.SockaddrInet6{ + Port: int(port), + } + copy(a6.Addr[:], address.IP()) + sockaddr = a6 + default: + return newError("unsupported address family: ", address.Family()) + } + + return syscall.Bind(int(fd), sockaddr) +} + func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error { if config.Mark != 0 { if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(config.Mark)); err != nil { @@ -31,7 +56,7 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf } } - if config.Tproxy { + if config.Tproxy.IsEnabled() { if err := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_TRANSPARENT, 1); err != nil { return newError("failed to set IP_TRANSPARENT").Base(err) } @@ -54,11 +79,17 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) } } - if config.Tproxy { + if config.Tproxy.IsEnabled() { if err := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_TRANSPARENT, 1); err != nil { return newError("failed to set IP_TRANSPARENT").Base(err) } } + if config.ReceiveOriginalDestAddress && isUDPSocket(network) { + if err := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_RECVORIGDSTADDR, 1); err != nil { + return err + } + } + return nil } diff --git a/transport/internet/sockopt_other.go b/transport/internet/sockopt_other.go index 8a3b03326..acdd6b422 100644 --- a/transport/internet/sockopt_other.go +++ b/transport/internet/sockopt_other.go @@ -2,6 +2,8 @@ package internet +import "v2ray.com/core/common/net" + func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error { return nil } @@ -9,3 +11,7 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error { return nil } + +func bindAddr(fd uintptr, address net.Address, port net.Port) error { + return nil +} diff --git a/transport/internet/sockopt_windows.go b/transport/internet/sockopt_windows.go index b41b0793f..28ba91f4e 100644 --- a/transport/internet/sockopt_windows.go +++ b/transport/internet/sockopt_windows.go @@ -2,6 +2,8 @@ package internet import ( "syscall" + + "v2ray.com/core/common/net" ) const ( @@ -42,3 +44,7 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) return nil } + +func bindAddr(fd uintptr, address net.Address, port net.Port) error { + return nil +} diff --git a/transport/internet/system_dialer.go b/transport/internet/system_dialer.go index 31f2ab9a2..bff0e7e16 100644 --- a/transport/internet/system_dialer.go +++ b/transport/internet/system_dialer.go @@ -37,11 +37,17 @@ func (DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest net.D sockopts := getSocketSettings(ctx) if sockopts != nil { + bindAddress := BindAddressFromContext(ctx) dialer.Control = func(network, address string, c syscall.RawConn) error { return c.Control(func(fd uintptr) { if err := applyOutboundSocketOptions(network, address, fd, sockopts); err != nil { newError("failed to apply socket options").Base(err).WriteToLog(session.ExportIDToError(ctx)) } + if dest.Network == net.Network_UDP && bindAddress.IsValid() { + if err := bindAddr(fd, bindAddress.Address, bindAddress.Port); err != nil { + newError("failed to bind source address to ", bindAddress).Base(err).WriteToLog(session.ExportIDToError(ctx)) + } + } }) } } diff --git a/transport/internet/tcp/config.pb.go b/transport/internet/tcp/config.pb.go index c537653f8..7b28b886e 100644 --- a/transport/internet/tcp/config.pb.go +++ b/transport/internet/tcp/config.pb.go @@ -1,9 +1,11 @@ package tcp -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import serial "v2ray.com/core/common/serial" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" + serial "v2ray.com/core/common/serial" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/transport/internet/tls/config.pb.go b/transport/internet/tls/config.pb.go index 816372496..de79f174e 100644 --- a/transport/internet/tls/config.pb.go +++ b/transport/internet/tls/config.pb.go @@ -1,8 +1,10 @@ package tls -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/transport/internet/udp/config.pb.go b/transport/internet/udp/config.pb.go index 58f7899ae..6260350c8 100644 --- a/transport/internet/udp/config.pb.go +++ b/transport/internet/udp/config.pb.go @@ -1,8 +1,10 @@ package udp -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal diff --git a/transport/internet/udp/hub.go b/transport/internet/udp/hub.go index 627e749e5..9ac25db33 100644 --- a/transport/internet/udp/hub.go +++ b/transport/internet/udp/hub.go @@ -1,8 +1,11 @@ package udp import ( + "context" + "v2ray.com/core/common/buf" "v2ray.com/core/common/net" + "v2ray.com/core/transport/internet" ) // Payload represents a single UDP payload. @@ -33,17 +36,8 @@ type Hub struct { recvOrigDest bool } -func ListenUDP(address net.Address, port net.Port, options ...HubOption) (*Hub, error) { - udpConn, err := net.ListenUDP("udp", &net.UDPAddr{ - IP: address.IP(), - Port: int(port), - }) - if err != nil { - return nil, err - } - newError("listening UDP on ", address, ":", port).WriteToLog() +func ListenUDP(ctx context.Context, address net.Address, port net.Port, options ...HubOption) (*Hub, error) { hub := &Hub{ - conn: udpConn, capacity: 256, recvOrigDest: false, } @@ -51,23 +45,22 @@ func ListenUDP(address net.Address, port net.Port, options ...HubOption) (*Hub, opt(hub) } - hub.cache = make(chan *Payload, hub.capacity) - - if hub.recvOrigDest { - rawConn, err := udpConn.SyscallConn() - if err != nil { - return nil, newError("failed to get fd").Base(err) - } - err = rawConn.Control(func(fd uintptr) { - if err := SetOriginalDestOptions(int(fd)); err != nil { - newError("failed to set socket options").Base(err).WriteToLog() - } - }) - if err != nil { - return nil, newError("failed to control socket").Base(err) - } + streamSettings := internet.StreamSettingsFromContext(ctx) + if streamSettings != nil && streamSettings.SocketSettings != nil && streamSettings.SocketSettings.ReceiveOriginalDestAddress { + hub.recvOrigDest = true } + udpConn, err := internet.ListenSystemPacket(ctx, &net.UDPAddr{ + IP: address.IP(), + Port: int(port), + }) + if err != nil { + return nil, err + } + newError("listening UDP on ", address, ":", port).WriteToLog() + hub.conn = udpConn.(*net.UDPConn) + hub.cache = make(chan *Payload, hub.capacity) + go hub.start() return hub, nil } diff --git a/transport/internet/udp/hub_linux.go b/transport/internet/udp/hub_linux.go index 00ede31c9..e7f4d9231 100644 --- a/transport/internet/udp/hub_linux.go +++ b/transport/internet/udp/hub_linux.go @@ -8,16 +8,6 @@ import ( "v2ray.com/core/common/net" ) -func SetOriginalDestOptions(fd int) error { - if err := syscall.SetsockoptInt(fd, syscall.SOL_IP, syscall.IP_TRANSPARENT, 1); err != nil { - return err - } - if err := syscall.SetsockoptInt(fd, syscall.SOL_IP, syscall.IP_RECVORIGDSTADDR, 1); err != nil { - return err - } - return nil -} - func RetrieveOriginalDest(oob []byte) net.Destination { msgs, err := syscall.ParseSocketControlMessage(oob) if err != nil { diff --git a/transport/internet/udp/hub_other.go b/transport/internet/udp/hub_other.go index 7a107c478..8ce8cc0ea 100644 --- a/transport/internet/udp/hub_other.go +++ b/transport/internet/udp/hub_other.go @@ -6,10 +6,6 @@ import ( "v2ray.com/core/common/net" ) -func SetOriginalDestOptions(fd int) error { - return nil -} - func RetrieveOriginalDest(oob []byte) net.Destination { return net.Destination{} } diff --git a/transport/internet/udp/source_forging.go b/transport/internet/udp/source_forging.go deleted file mode 100644 index caa935343..000000000 --- a/transport/internet/udp/source_forging.go +++ /dev/null @@ -1,11 +0,0 @@ -// +build !linux - -package udp - -import ( - "net" -) - -func TransmitSocket(src net.Addr, dst net.Addr) (net.Conn, error) { - return nil, newError("forging source address is not supported on non-Linux platform.").AtWarning() -} diff --git a/transport/internet/udp/source_forging_linux.go b/transport/internet/udp/source_forging_linux.go deleted file mode 100644 index 238519b37..000000000 --- a/transport/internet/udp/source_forging_linux.go +++ /dev/null @@ -1,55 +0,0 @@ -// +build linux - -package udp - -import ( - "net" - "os" - "syscall" -) - -//Currently, Only IPv4 Forge is supported -func TransmitSocket(src net.Addr, dst net.Addr) (net.Conn, error) { - var fd int - var err error - fd, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0) - if err != nil { - return nil, newError("failed to create fd").Base(err).AtWarning() - } - err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1) - if err != nil { - return nil, newError("failed to set resuse_addr").Base(err).AtWarning() - } - - err = syscall.SetsockoptInt(fd, syscall.SOL_IP, syscall.IP_TRANSPARENT, 1) - if err != nil { - return nil, newError("failed to set transparent").Base(err).AtWarning() - } - - ip := src.(*net.UDPAddr).IP.To4() - var ip2 [4]byte - copy(ip2[:], ip) - srcaddr := syscall.SockaddrInet4{} - srcaddr.Addr = ip2 - srcaddr.Port = src.(*net.UDPAddr).Port - err = syscall.Bind(fd, &srcaddr) - if err != nil { - return nil, newError("failed to bind source address").Base(err).AtWarning() - } - ipd := dst.(*net.UDPAddr).IP.To4() - var ip2d [4]byte - copy(ip2d[:], ipd) - dstaddr := syscall.SockaddrInet4{} - dstaddr.Addr = ip2d - dstaddr.Port = dst.(*net.UDPAddr).Port - err = syscall.Connect(fd, &dstaddr) - if err != nil { - return nil, newError("failed to connect to source address").Base(err).AtWarning() - } - fdf := os.NewFile(uintptr(fd), "/dev/udp/") - c, err := net.FileConn(fdf) - if err != nil { - return nil, newError("failed to create file conn").Base(err).AtWarning() - } - return c, nil -} diff --git a/transport/internet/websocket/config.pb.go b/transport/internet/websocket/config.pb.go index 689196ffe..9012a5f84 100644 --- a/transport/internet/websocket/config.pb.go +++ b/transport/internet/websocket/config.pb.go @@ -1,8 +1,10 @@ package websocket -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal