Skip to content

Commit

Permalink
Split sources
Browse files Browse the repository at this point in the history
  • Loading branch information
coyove committed Jan 1, 2018
1 parent eea6b63 commit 9d838d2
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 85 deletions.
85 changes: 0 additions & 85 deletions proxy/httpmux.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package proxy

import (
"encoding/binary"
"errors"
"strconv"
"syscall"
"time"
"unsafe"

"github.com/coyove/goflyway/pkg/fd"
"github.com/coyove/goflyway/pkg/logg"
"github.com/coyove/tcpmux"

Expand Down Expand Up @@ -59,82 +53,3 @@ CONTINUE:
return wrapper, err
}
}

func vpnDial(address string) (net.Conn, error) {
sock, err := fd.Socket(syscall.AF_INET)
if err != nil {
return nil, err
}

if err := protectFD(sock); err != nil {
return nil, err
}

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

// cmsghdr.len may be uint64 or uint32, depending on the platform
ln := syscall.SizeofCmsghdr + strconv.IntSize/8
h := (*[8]byte)(unsafe.Pointer(&cmsg.Len))

if _little_endian {
h[0] = byte(ln)
} else {
var i interface{} = cmsg.Len
switch i.(type) {
case uint64:
binary.BigEndian.PutUint64(h[:8], uint64(ln))
case uint32:
binary.BigEndian.PutUint32(h[:4], uint32(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)
}
88 changes: 88 additions & 0 deletions proxy/ssvpn_interop.go
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)
}
7 changes: 7 additions & 0 deletions proxy/ssvpn_windows.go
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")
}

0 comments on commit 9d838d2

Please sign in to comment.