瀏覽代碼

unified tproxy option

Darien Raymond 7 年之前
父節點
當前提交
383b84e7dd
共有 66 個文件被更改,包括 503 次插入335 次删除
  1. 6 4
      app/commander/config.pb.go
  2. 5 3
      app/dispatcher/config.pb.go
  3. 6 4
      app/dns/config.pb.go
  4. 6 3
      app/log/command/config.pb.go
  5. 6 4
      app/log/config.pb.go
  6. 5 3
      app/policy/config.pb.go
  7. 9 6
      app/proxyman/command/command.pb.go
  8. 8 6
      app/proxyman/config.pb.go
  9. 17 5
      app/proxyman/inbound/always.go
  10. 10 1
      app/proxyman/inbound/dynamic.go
  11. 3 2
      app/proxyman/inbound/worker.go
  12. 6 4
      app/router/config.pb.go
  13. 6 3
      app/stats/command/command.pb.go
  14. 5 3
      app/stats/config.pb.go
  15. 5 3
      common/log/log.pb.go
  16. 5 3
      common/net/address.pb.go
  17. 5 3
      common/net/destination.pb.go
  18. 5 3
      common/net/network.pb.go
  19. 5 3
      common/net/port.pb.go
  20. 7 5
      common/protocol/command.pb.go
  21. 5 3
      common/protocol/headers.pb.go
  22. 6 4
      common/protocol/server_spec.pb.go
  23. 6 4
      common/protocol/user.pb.go
  24. 5 3
      common/serial/typed_message.pb.go
  25. 7 5
      config.pb.go
  26. 6 4
      proxy/blackhole/config.pb.go
  27. 6 4
      proxy/dokodemo/config.pb.go
  28. 9 4
      proxy/dokodemo/dokodemo.go
  29. 6 4
      proxy/freedom/config.pb.go
  30. 5 3
      proxy/http/config.pb.go
  31. 6 4
      proxy/mtproto/config.pb.go
  32. 7 5
      proxy/shadowsocks/config.pb.go
  33. 7 5
      proxy/socks/config.pb.go
  34. 6 4
      proxy/vmess/account.pb.go
  35. 6 4
      proxy/vmess/inbound/config.pb.go
  36. 6 4
      proxy/vmess/outbound/config.pb.go
  37. 6 4
      transport/config.pb.go
  38. 4 0
      transport/internet/config.go
  39. 91 44
      transport/internet/config.pb.go
  40. 14 1
      transport/internet/config.proto
  41. 12 0
      transport/internet/context.go
  42. 5 3
      transport/internet/domainsocket/config.pb.go
  43. 5 3
      transport/internet/headers/http/config.pb.go
  44. 5 3
      transport/internet/headers/noop/config.pb.go
  45. 5 3
      transport/internet/headers/srtp/config.pb.go
  46. 5 3
      transport/internet/headers/tls/config.pb.go
  47. 5 3
      transport/internet/headers/utp/config.pb.go
  48. 5 3
      transport/internet/headers/wechat/config.pb.go
  49. 5 3
      transport/internet/headers/wireguard/config.pb.go
  50. 5 3
      transport/internet/http/config.pb.go
  51. 6 4
      transport/internet/kcp/config.pb.go
  52. 1 1
      transport/internet/kcp/listener.go
  53. 6 0
      transport/internet/sockopt_darwin.go
  54. 33 2
      transport/internet/sockopt_linux.go
  55. 6 0
      transport/internet/sockopt_other.go
  56. 6 0
      transport/internet/sockopt_windows.go
  57. 6 0
      transport/internet/system_dialer.go
  58. 6 4
      transport/internet/tcp/config.pb.go
  59. 5 3
      transport/internet/tls/config.pb.go
  60. 5 3
      transport/internet/udp/config.pb.go
  61. 17 24
      transport/internet/udp/hub.go
  62. 0 10
      transport/internet/udp/hub_linux.go
  63. 0 4
      transport/internet/udp/hub_other.go
  64. 0 11
      transport/internet/udp/source_forging.go
  65. 0 55
      transport/internet/udp/source_forging_linux.go
  66. 5 3
      transport/internet/websocket/config.pb.go

+ 6 - 4
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

+ 5 - 3
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

+ 6 - 4
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

+ 6 - 3
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"

+ 6 - 4
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

+ 5 - 3
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

+ 9 - 6
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"

+ 8 - 6
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

+ 17 - 5
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)
 		}

+ 10 - 1
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()

+ 3 - 2
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
 	}

+ 6 - 4
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

+ 6 - 3
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"

+ 5 - 3
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

+ 5 - 3
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

+ 5 - 3
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

+ 5 - 3
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

+ 5 - 3
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

+ 5 - 3
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

+ 7 - 5
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

+ 5 - 3
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

+ 6 - 4
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

+ 6 - 4
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

+ 5 - 3
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

+ 7 - 5
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

+ 6 - 4
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

+ 6 - 4
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

+ 9 - 4
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}
 			}
 		}
 

+ 6 - 4
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

+ 5 - 3
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

+ 6 - 4
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

+ 7 - 5
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

+ 7 - 5
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

+ 6 - 4
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

+ 6 - 4
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

+ 6 - 4
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

+ 6 - 4
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

+ 4 - 0
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
+}

+ 91 - 44
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, 0xc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x8f, 0xa0,
-	0xfd, 0x9e, 0x2d, 0x4b, 0xa3, 0xc8, 0xd7, 0x89, 0x57, 0x49, 0x12, 0xa1, 0x3a, 0x46, 0xd0, 0x9d,
-	0xf1, 0x9c, 0x66, 0x45, 0x95, 0x6b, 0x4c, 0x2e, 0xe1, 0x7e, 0xca, 0xf3, 0xfd, 0xc8, 0x91, 0x77,
-	0xd5, 0xb2, 0xf1, 0xd7, 0xda, 0xc9, 0xbb, 0x30, 0xa6, 0x5b, 0x32, 0xd5, 0xb5, 0xce, 0x16, 0x39,
-	0xab, 0xf2, 0xcb, 0xa6, 0x79, 0xce, 0x4f, 0x7e, 0x06, 0x00, 0x00, 0xff, 0xff, 0xb9, 0x1d, 0xbb,
-	0xfd, 0x9d, 0x04, 0x00, 0x00,
+	// 607 bytes 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, 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72, 0x76, 0xbd, 0x40, 0xd7, 0x22, 0x6d, 0xd8, 0x19,
+	0xa5, 0xd2, 0x7c, 0xd8, 0x7d, 0x0f, 0xe0, 0x97, 0x1e, 0xb2, 0x03, 0xce, 0x78, 0x36, 0x2b, 0xf0,
+	0x45, 0xda, 0xb5, 0x48, 0x07, 0x5a, 0x21, 0x4e, 0x53, 0x81, 0x89, 0x72, 0xed, 0x27, 0x57, 0x70,
+	0xf0, 0xdb, 0x3b, 0xd0, 0x7d, 0xd1, 0x30, 0x70, 0xb7, 0x74, 0xf0, 0x7e, 0x14, 0xb8, 0x96, 0x1e,
+	0x7d, 0xf1, 0x76, 0x18, 0xb8, 0x36, 0xd9, 0x83, 0xdd, 0x8f, 0x78, 0x5d, 0x6c, 0xc0, 0x75, 0x74,
+	0xe1, 0x4d, 0x14, 0x05, 0x6e, 0x83, 0xb8, 0xd0, 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,
 }

+ 14 - 1
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;
 }

+ 12 - 0
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{}
+}

+ 5 - 3
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

+ 5 - 3
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

+ 5 - 3
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

+ 5 - 3
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

+ 5 - 3
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

+ 5 - 3
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

+ 5 - 3
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

+ 5 - 3
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

+ 5 - 3
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

+ 6 - 4
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

+ 1 - 1
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
 	}

+ 6 - 0
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
+}

+ 33 - 2
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
 }

+ 6 - 0
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
+}

+ 6 - 0
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
+}

+ 6 - 0
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))
+					}
+				}
 			})
 		}
 	}

+ 6 - 4
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

+ 5 - 3
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

+ 5 - 3
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

+ 17 - 24
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,22 +45,21 @@ func ListenUDP(address net.Address, port net.Port, options ...HubOption) (*Hub,
 		opt(hub)
 	}
 
-	hub.cache = make(chan *Payload, hub.capacity)
+	streamSettings := internet.StreamSettingsFromContext(ctx)
+	if streamSettings != nil && streamSettings.SocketSettings != nil && streamSettings.SocketSettings.ReceiveOriginalDestAddress {
+		hub.recvOrigDest = true
+	}
 
-	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)
-		}
+	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

+ 0 - 10
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 {

+ 0 - 4
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{}
 }

+ 0 - 11
transport/internet/udp/source_forging.go

@@ -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()
-}

+ 0 - 55
transport/internet/udp/source_forging_linux.go

@@ -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
-}

+ 5 - 3
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