Skip to content

Commit

Permalink
Drop gotfo, use go1.11 net.Dialer for VPN protector callback. Fix cbe…
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeuw committed Sep 18, 2018
1 parent 2489b50 commit 3874601
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
34 changes: 13 additions & 21 deletions cmd/gq-client/gq-client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build go1.8,!go1.10
// +build go1.11

package main

Expand All @@ -13,7 +13,6 @@ import (

"github.com/cbeuw/GoQuiet/gqclient"
"github.com/cbeuw/GoQuiet/gqclient/TLS"
"github.com/cbeuw/gotfo"
)

var version string
Expand Down Expand Up @@ -85,25 +84,19 @@ func initSequence(ssConn net.Conn, sta *gqclient.State) {
}
data = data[:i]

d := net.Dialer{Control: protector}

var remoteConn net.Conn
clientHello := TLS.ComposeInitHandshake(sta)
if sta.FastOpen {
remoteConn, err = gotfo.Dial(sta.SS_REMOTE_HOST+":"+sta.SS_REMOTE_PORT, true, clientHello)
if err != nil {
log.Printf("Connecting and sending ClientHello to remote: %v\n", err)
return
}
} else {
remoteConn, err = gotfo.Dial(sta.SS_REMOTE_HOST+":"+sta.SS_REMOTE_PORT, false, nil)
if err != nil {
log.Printf("Connecting to remote: %v\n", err)
return
}
_, err = remoteConn.Write(clientHello)
if err != nil {
log.Printf("Sending ClientHello: %v\n", err)
return
}
remoteConn, err = d.Dial("tcp", sta.SS_REMOTE_HOST+":"+sta.SS_REMOTE_PORT)
if err != nil {
log.Printf("Connecting to remote: %v\n", err)
return
}
_, err = remoteConn.Write(clientHello)
if err != nil {
log.Printf("Sending ClientHello: %v\n", err)
return
}

// Three discarded messages: ServerHello, ChangeCipherSpec and Finished
Expand Down Expand Up @@ -153,7 +146,6 @@ func main() {

// These two functions do nothing for non-android
log_init()
protect()

if os.Getenv("SS_LOCAL_HOST") != "" {
localHost = os.Getenv("SS_LOCAL_HOST")
Expand Down Expand Up @@ -212,7 +204,7 @@ func main() {
}

sta.SetAESKey()
listener, err := gotfo.Listen(sta.SS_LOCAL_HOST+":"+sta.SS_LOCAL_PORT, sta.FastOpen)
listener, err := net.Listen("tcp", sta.SS_LOCAL_HOST+":"+sta.SS_LOCAL_PORT)
if err != nil {
log.Fatal(err)
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/gq-client/protector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

package main

func protect() {
import "syscall"

func protector(string, string, syscall.RawConn) error {
return nil
}
28 changes: 12 additions & 16 deletions cmd/gq-client/protector_android.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// +build android
package main

// Stolen from https://github.com/shadowsocks/overture/blob/shadowsocks/core/utils/utils_android.go

/*
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -65,7 +67,6 @@ set_timeout(int sock)
import "C"

import (
"github.com/cbeuw/gotfo"
"log"
"syscall"
)
Expand All @@ -76,27 +77,18 @@ import (
//
// The Android system provides an API VpnService.protect(int socketFD)
// This tells the system to bypass the socket around the VPN.
//
// Unfortunately it's extremely complicated to access this API through normal syscalls,
// the only plausible way is to somehow let the Android app know our socketFD and access this API
// through JVM. Shadowsocks app provides an interface for this and we need to pass our socketFD
// using a local socket to the shadowsocks app, which is what the C code is for.
func protect() {
func protector(network string, address string, c syscall.RawConn) error {
log.Println("Using Android VPN mode.")
fn := func(s uintptr) {
fd := int(s)
path := "protect_path"

path := "protect_path"

// There is no exported method to fetch the socket's system file descriptor in either
// standard lib "net" or "gotfo" package. This callback function is used to get the socket's
// file descriptor.
//
// Note that the callback function is not supported in the standard lib "net".
callback := func(fd int) {
socket, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
if err != nil {
log.Println(err)
return
}

defer syscall.Close(socket)

C.set_timeout(C.int(socket))
Expand All @@ -121,5 +113,9 @@ func protect() {
}
}

gotfo.SetFdCallback(callback)
if err := c.Control(fn); err != nil {
return err
}

return nil
}

0 comments on commit 3874601

Please sign in to comment.