diff --git a/common/net/destination.go b/common/net/destination.go index 13adb8da4..57510e5d9 100644 --- a/common/net/destination.go +++ b/common/net/destination.go @@ -17,8 +17,15 @@ type Destination interface { IsUDP() bool // True if destination is reachable via UDP } -func TCPDestinationFromAddr(addr *net.TCPAddr) Destination { - return TCPDestination(IPAddress(addr.IP), Port(addr.Port)) +func DestinationFromAddr(addr net.Addr) Destination { + switch addr := addr.(type) { + case *net.TCPAddr: + return TCPDestination(IPAddress(addr.IP), Port(addr.Port)) + case *net.UDPAddr: + return UDPDestination(IPAddress(addr.IP), Port(addr.Port)) + default: + panic("Unknown address type.") + } } // TCPDestination creates a TCP destination with given address @@ -26,10 +33,6 @@ func TCPDestination(address Address, port Port) Destination { return &tcpDestination{address: address, port: port} } -func UDPDestinationFromAddr(addr *net.UDPAddr) Destination { - return UDPDestination(IPAddress(addr.IP), Port(addr.Port)) -} - // UDPDestination creates a UDP destination with given address func UDPDestination(address Address, port Port) Destination { return &udpDestination{address: address, port: port} diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index e39cf5b3c..7d38d9f51 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -1,7 +1,6 @@ package dokodemo import ( - "net" "sync" "github.com/v2ray/v2ray-core/app" @@ -151,7 +150,7 @@ func (this *DokodemoDoor) HandleTCPConnection(conn internet.Connection) { log.Info("Dokodemo: Handling request to ", dest) ray := this.packetDispatcher.DispatchToOutbound(this.meta, &proxy.SessionInfo{ - Source: v2net.TCPDestinationFromAddr(conn.RemoteAddr().(*net.TCPAddr)), + Source: v2net.DestinationFromAddr(conn.RemoteAddr()), Destination: dest, }) defer ray.InboundOutput().Release() diff --git a/proxy/http/server.go b/proxy/http/server.go index 1b3699b12..82ad004c3 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -120,7 +120,7 @@ func (this *Server) handleConnection(conn internet.Connection) { } log.Access(conn.RemoteAddr(), request.URL, log.AccessAccepted, "") session := &proxy.SessionInfo{ - Source: v2net.TCPDestinationFromAddr(conn.RemoteAddr().(*net.TCPAddr)), + Source: v2net.DestinationFromAddr(conn.RemoteAddr()), Destination: dest, } if strings.ToUpper(request.Method) == "CONNECT" { diff --git a/proxy/shadowsocks/server.go b/proxy/shadowsocks/server.go index d453397b6..12cace63b 100644 --- a/proxy/shadowsocks/server.go +++ b/proxy/shadowsocks/server.go @@ -4,7 +4,6 @@ package shadowsocks import ( "crypto/rand" "io" - "net" "sync" "github.com/v2ray/v2ray-core/app" @@ -206,7 +205,7 @@ func (this *Server) handleConnection(conn internet.Connection) { log.Info("Shadowsocks: Tunnelling request to ", dest) ray := this.packetDispatcher.DispatchToOutbound(this.meta, &proxy.SessionInfo{ - Source: v2net.TCPDestinationFromAddr(conn.RemoteAddr().(*net.TCPAddr)), + Source: v2net.DestinationFromAddr(conn.RemoteAddr()), Destination: dest, }) defer ray.InboundOutput().Release() diff --git a/proxy/socks/server.go b/proxy/socks/server.go index 2ed187e29..dae731589 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -3,7 +3,6 @@ package socks import ( "errors" "io" - "net" "sync" "time" @@ -120,7 +119,7 @@ func (this *Server) handleConnection(connection internet.Connection) { return } - clientAddr := v2net.TCPDestinationFromAddr(connection.RemoteAddr().(*net.TCPAddr)) + clientAddr := v2net.DestinationFromAddr(connection.RemoteAddr()) if err != nil && err == protocol.Socks4Downgrade { this.handleSocks4(clientAddr, reader, writer, auth4) } else { diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 7dd2b3661..85a0b9de4 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -2,7 +2,6 @@ package inbound import ( "io" - "net" "sync" "github.com/v2ray/v2ray-core/app" @@ -165,7 +164,7 @@ func (this *VMessInboundHandler) HandleConnection(connection internet.Connection connection.SetReusable(request.Option.Has(protocol.RequestOptionConnectionReuse)) ray := this.packetDispatcher.DispatchToOutbound(this.meta, &proxy.SessionInfo{ - Source: v2net.TCPDestinationFromAddr(connection.RemoteAddr().(*net.TCPAddr)), + Source: v2net.DestinationFromAddr(connection.RemoteAddr()), Destination: request.Destination(), }) input := ray.InboundInput()