-
Notifications
You must be signed in to change notification settings - Fork 654
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
95 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// +build darwin dragonfly freebsd linux netbsd openbsd solaris | ||
|
||
package proxy | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
"strconv" | ||
"syscall" | ||
"unsafe" | ||
|
||
"github.com/coyove/goflyway/pkg/fd" | ||
) | ||
|
||
func vpnDial(address string) (net.Conn, error) { | ||
sock, err := fd.Socket(syscall.AF_INET) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Send our conn fd to shadowsocks vpn thread for protection | ||
if err := protectFD(sock); err != nil { | ||
return nil, err | ||
} | ||
|
||
// If succeeded, this fd will be closed while we still need it. | ||
// So we dial a new conn, replace its fd with this one | ||
return fd.DialWithFD(sock, address) | ||
} | ||
|
||
func protectFD(fd int) error { | ||
sock, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM, 0) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var addr syscall.SockaddrUnix | ||
addr.Name = "protect_path" | ||
|
||
if err := (syscall.Connect(sock, &addr)); err != nil { | ||
return err | ||
} | ||
|
||
if err := sendFD(sock, fd); err != nil { | ||
return err | ||
} | ||
|
||
ret := []byte{9} | ||
if n, err := syscall.Read(sock, ret); err != nil { | ||
return err | ||
} else if n != 1 { | ||
return errors.New("protecting failed") | ||
} | ||
|
||
syscall.Close(sock) | ||
|
||
if ret[0] != 0 { | ||
return errors.New("protecting failed") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
var _int_value_one int = 1 | ||
var _little_endian = *(*byte)(unsafe.Pointer(&_int_value_one)) == 1 | ||
|
||
func sendFD(sock int, fd int) error { | ||
cmsg := &syscall.Cmsghdr{ | ||
Level: syscall.SOL_SOCKET, | ||
Type: syscall.SCM_RIGHTS, | ||
} | ||
|
||
ln := byte(syscall.SizeofCmsghdr + strconv.IntSize/8) | ||
h := (*[8]byte)(unsafe.Pointer(&cmsg.Len)) | ||
|
||
if _little_endian { | ||
h[0] = ln | ||
} else { | ||
h[3+4*(^cmsg.Len<<32>>63)] = ln | ||
} | ||
|
||
buffer := make([]byte, cmsg.Len) | ||
|
||
copy(buffer, (*[syscall.SizeofCmsghdr]byte)(unsafe.Pointer(cmsg))[:]) | ||
*(*int)(unsafe.Pointer(&buffer[syscall.SizeofCmsghdr])) = fd | ||
|
||
return syscall.Sendmsg(sock, []byte{'!'}, buffer, nil, 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package proxy | ||
|
||
import "net" | ||
|
||
func vpnDial(address string) (net.Conn, error) { | ||
panic("not on Windows") | ||
} |