1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-16 09:26:21 -05:00

remove use of sysFd

This commit is contained in:
Darien Raymond 2017-08-25 23:02:54 +02:00
parent 896bff2195
commit 2b9d9e1ae3
2 changed files with 19 additions and 52 deletions

View File

@ -3,27 +3,35 @@
package tcp package tcp
import ( import (
"net"
"syscall" "syscall"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/internal"
) )
const SO_ORIGINAL_DST = 80 const SO_ORIGINAL_DST = 80
func GetOriginalDestination(conn internet.Connection) (v2net.Destination, error) { func GetOriginalDestination(conn internet.Connection) (v2net.Destination, error) {
fd, err := internal.GetSysFd(conn.(net.Conn)) sysrawconn, f := conn.(syscall.Conn)
if !f {
return v2net.Destination{}, newError("unable to get syscall.Conn")
}
rawConn, err := sysrawconn.SyscallConn()
if err != nil {
return v2net.Destination{}, newError("failed to get sys fd").Base(err)
}
var dest v2net.Destination
err := rawConn.Control(func(fd uintptr) {
addr, err := syscall.GetsockoptIPv6Mreq(int(fd), syscall.IPPROTO_IP, SO_ORIGINAL_DST)
if err != nil {
return v2net.Destination{}, newError("failed to call getsockopt").Base(err)
}
ip := v2net.IPAddress(addr.Multiaddr[4:8])
port := uint16(addr.Multiaddr[2])<<8 + uint16(addr.Multiaddr[3])
addr = v2net.TCPDestination(ip, v2net.Port(port))
})
if err != nil { if err != nil {
return v2net.Destination{}, newError("failed to get original destination").Base(err) return v2net.Destination{}, newError("failed to get original destination").Base(err)
} }
return dest, nil
addr, err := syscall.GetsockoptIPv6Mreq(fd, syscall.IPPROTO_IP, SO_ORIGINAL_DST)
if err != nil {
return v2net.Destination{}, newError("failed to call getsockopt").Base(err)
}
ip := v2net.IPAddress(addr.Multiaddr[4:8])
port := uint16(addr.Multiaddr[2])<<8 + uint16(addr.Multiaddr[3])
return v2net.TCPDestination(ip, v2net.Port(port)), nil
} }

View File

@ -1,41 +0,0 @@
// +build linux
package udp_test
import (
"os"
"syscall"
"testing"
"v2ray.com/core/common/buf"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
"v2ray.com/core/transport/internet/internal"
. "v2ray.com/core/transport/internet/udp"
)
func TestHubSocksOption(t *testing.T) {
assert := assert.On(t)
if os.Geteuid() != 0 {
// This test case requires root permission.
return
}
hub, err := ListenUDP(v2net.LocalHostIP, v2net.Port(0), ListenOption{
Callback: func(*buf.Buffer, v2net.Destination, v2net.Destination) {},
ReceiveOriginalDest: true,
})
assert.Error(err).IsNil()
conn := hub.Connection()
fd, err := internal.GetSysFd(conn)
assert.Error(err).IsNil()
val, err := syscall.GetsockoptInt(fd, syscall.SOL_IP, syscall.IP_TRANSPARENT)
assert.Error(err).IsNil()
assert.Int(val).Equals(1)
val, err = syscall.GetsockoptInt(fd, syscall.SOL_IP, syscall.IP_RECVORIGDSTADDR)
assert.Error(err).IsNil()
assert.Int(val).Equals(1)
}