diff --git a/build_android.sh b/build_android.sh index 2fdcc6fd..d8b13dd8 100755 --- a/build_android.sh +++ b/build_android.sh @@ -7,34 +7,42 @@ mkdir -p build/android/x86 mkdir -p build/android/x86_64 export CGO_ENABLED=1 export CGO_CFLAGS="-fstack-protector-strong" + +if [[ "$(uname)" == "Darwin" ]]; then + PLATFORM="darwin" +elif [[ "$(uname)" == "Linux" ]]; then + PLATFORM="linux" +else + PLATFORM="unknown" +fi # shellcheck disable=SC2016 -buildCommand='go build -ldflags "-s -w" -buildmode=c-shared -o "$output_dir/libproxy.so" cli.go logger.go httpClient.go stunnelbidirection.go websocketbidirConnection.go common.go' +buildCommand='go build -ldflags "-s -w" -buildmode=c-shared -o "$output_dir/libproxy.so" cli.go' echo "$buildCommand" # For ARM64 output_dir="./build/android/arm64-v8a" -TOOLCHAIN=("$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang") +TOOLCHAIN=("$ANDROID_NDK/toolchains/llvm/prebuilt/$PLATFORM-x86_64/bin/aarch64-linux-android21-clang") # shellcheck disable=SC2086 GOOS=android GOARCH=arm64 CC="${TOOLCHAIN[0]}" output_dir="$output_dir" sh -c "$buildCommand" rm $output_dir/libproxy.h ## For ARMv7 output_dir="./build/android/armeabi-v7a" -TOOLCHAIN=("$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi21-clang") +TOOLCHAIN=("$ANDROID_NDK/toolchains/llvm/prebuilt/$PLATFORM-x86_64/bin/armv7a-linux-androideabi21-clang") # shellcheck disable=SC2086 GOOS=android GOARCH=arm CC="${TOOLCHAIN[0]}" output_dir="$output_dir" sh -c "$buildCommand" rm $output_dir/libproxy.h ## For x86 output_dir="./build/android/x86" -TOOLCHAIN=("$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/i686-linux-android21-clang") +TOOLCHAIN=("$ANDROID_NDK/toolchains/llvm/prebuilt/$PLATFORM-x86_64/bin/i686-linux-android21-clang") # shellcheck disable=SC2086 GOOS=android GOARCH=386 CC="${TOOLCHAIN[0]}" output_dir="$output_dir" sh -c "$buildCommand" rm $output_dir/libproxy.h ## For x86_64 output_dir="./build/android/x86_64" -TOOLCHAIN=("$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android21-clang") +TOOLCHAIN=("$ANDROID_NDK/toolchains/llvm/prebuilt/$PLATFORM-x86_64/bin/x86_64-linux-android21-clang") # shellcheck disable=SC2086 GOOS=android GOARCH=amd64 CC="${TOOLCHAIN[0]}" output_dir="$output_dir" sh -c "$buildCommand" rm $output_dir/libproxy.h diff --git a/build_desktop.sh b/build_desktop.sh old mode 100644 new mode 100755 diff --git a/build_ios.sh b/build_ios.sh index aa8a65d5..870d8530 100755 --- a/build_ios.sh +++ b/build_ios.sh @@ -19,7 +19,7 @@ build() { output_dir="./build/${sdk}/arm64" rm -rf "$output_dir" mkdir -p "$output_dir" - go build -buildmode=c-archive -o "$output_dir/proxy.a" cli.go logger.go httpclient.go stunnelbidirection.go websocketbidirConnection.go common.go + go build -buildmode=c-archive -o "$output_dir/proxy.a" cli.go } # Build for Apple TVOS diff --git a/cli.go b/cli.go index 4ebe64ba..4f4839a7 100644 --- a/cli.go +++ b/cli.go @@ -2,6 +2,7 @@ package main import ( //"C" + "github.com/Windscribe/wstunnel/cli" "github.com/spf13/cobra" "os" //_ "runtime/cgo" @@ -47,30 +48,21 @@ func main() { } } -//export Channel is used by host app to send events to http client. -var channel = make(chan string) - //export Callback is used by http client to send events to host app var primaryListenerSocketFd int = -1 -//export WSTunnel wraps OpenVPN tcp traffic in to Websocket -const WSTunnel = 1 - -//export Stunnel wraps OpenVPN tcp traffic in to regular tcp. -const Stunnel = 2 - //export Initialise func Initialise(development bool, logFilePath string) { - InitLogger(development, logFilePath) + cli.InitLogger(development, logFilePath) } //export StartProxy func StartProxy(listenAddress string, remoteAddress string, tunnelType int, mtu int, extraPadding bool) bool { - Logger.Infof("Starting proxy with listenAddress: %s remoteAddress %s tunnelType: %d mtu %d", listenAddress, remoteAddress, tunnelType, mtu) - err := NewHTTPClient(listenAddress, remoteAddress, tunnelType, mtu, func(fd int) { + cli.Logger.Infof("Starting proxy with listenAddress: %s remoteAddress %s tunnelType: %d mtu %d", listenAddress, remoteAddress, tunnelType, mtu) + err := cli.NewHTTPClient(listenAddress, remoteAddress, tunnelType, mtu, func(fd int) { primaryListenerSocketFd = fd - Logger.Info("Socket ready to protect.") - }, channel, extraPadding).Run() + cli.Logger.Info("Socket ready to protect.") + }, cli.Channel, extraPadding).Run() if err != nil { return false } @@ -79,8 +71,8 @@ func StartProxy(listenAddress string, remoteAddress string, tunnelType int, mtu //export Stop func Stop() { - Logger.Info("Disconnect signal from host app.") - channel <- "done" + cli.Logger.Info("Disconnect signal from host app.") + cli.Channel <- "done" } //export GetPrimaryListenerSocketFd diff --git a/client_test.go b/cli/client_test.go similarity index 95% rename from client_test.go rename to cli/client_test.go index fc361bcb..7428ce7e 100644 --- a/client_test.go +++ b/cli/client_test.go @@ -1,4 +1,4 @@ -package main +package cli import ( "fmt" @@ -22,7 +22,7 @@ func TestEndToEndConnection(t *testing.T) { go func() { err := NewHTTPClient(tcpServerAddress, webSocketServerAddress, 1, 1600, func(fd int) { t.Log(fd) - }, channel, false).Run() + }, Channel, false).Run() if err != nil { t.Fail() return @@ -43,7 +43,7 @@ func TestEndToEndConnection(t *testing.T) { } //Exit time.Sleep(time.Millisecond * 100) - channel <- "done" + Channel <- "done" //Client 3 _, client3Err := mockClientConnection() if client3Err == nil { diff --git a/common.go b/cli/common.go similarity index 94% rename from common.go rename to cli/common.go index 92af6b16..b767985e 100644 --- a/common.go +++ b/cli/common.go @@ -1,4 +1,4 @@ -package main +package cli const ( // BufferSize is the size of the intermediate buffer for network packets diff --git a/echo_server.go b/cli/echo_server.go similarity index 98% rename from echo_server.go rename to cli/echo_server.go index 99a95e52..dfc8d331 100644 --- a/echo_server.go +++ b/cli/echo_server.go @@ -1,4 +1,4 @@ -package main +package cli import ( "github.com/gorilla/websocket" diff --git a/httpClient.go b/cli/httpClient.go similarity index 95% rename from httpClient.go rename to cli/httpClient.go index 1ef53b3f..085d4b68 100644 --- a/httpClient.go +++ b/cli/httpClient.go @@ -1,4 +1,4 @@ -package main +package cli import ( "context" @@ -14,6 +14,15 @@ import ( "time" ) +//export WSTunnel wraps OpenVPN tcp traffic in to Websocket +const WSTunnel = 1 + +//export Stunnel wraps OpenVPN tcp traffic in to regular tcp. +const Stunnel = 2 + +//export Channel is used by host app to send events to http client. +var Channel = make(chan string) + // httpClient // sets up tcp server and remote connections. // ////////////////////////////////////////////////////////////////////////////// diff --git a/logger.go b/cli/logger.go similarity index 98% rename from logger.go rename to cli/logger.go index 12246124..07ca64e5 100644 --- a/logger.go +++ b/cli/logger.go @@ -1,4 +1,4 @@ -package main +package cli import ( "fmt" diff --git a/stunnelbidirection.go b/cli/stunnelbidirection.go similarity index 99% rename from stunnelbidirection.go rename to cli/stunnelbidirection.go index 158eaae8..655ba4c3 100644 --- a/stunnelbidirection.go +++ b/cli/stunnelbidirection.go @@ -1,4 +1,4 @@ -package main +package cli import ( tls "github.com/refraction-networking/utls" diff --git a/websocketbidirConnection.go b/cli/websocketbidirConnection.go similarity index 99% rename from websocketbidirConnection.go rename to cli/websocketbidirConnection.go index e4b7aa16..7c81ecd4 100644 --- a/websocketbidirConnection.go +++ b/cli/websocketbidirConnection.go @@ -1,12 +1,11 @@ -package main +package cli import ( + "github.com/gorilla/websocket" "io" "net" "os" "time" - - "github.com/gorilla/websocket" ) // WebSocketBiDirection diff --git a/go.mod b/go.mod index 9bef4615..8f0b4fc3 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module wstunnel +module github.com/Windscribe/wstunnel go 1.18