Skip to content

Commit

Permalink
separate code from main module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ginder-Singh committed Aug 26, 2024
1 parent ce54674 commit a94d094
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 34 deletions.
18 changes: 13 additions & 5 deletions build_android.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file modified build_desktop.sh
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion build_ios.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 8 additions & 16 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
//"C"
"github.com/Windscribe/wstunnel/cli"
"github.com/spf13/cobra"
"os"
//_ "runtime/cgo"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions client_test.go → cli/client_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cli

import (
"fmt"
Expand All @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion common.go → cli/common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cli

const (
// BufferSize is the size of the intermediate buffer for network packets
Expand Down
2 changes: 1 addition & 1 deletion echo_server.go → cli/echo_server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cli

import (
"github.com/gorilla/websocket"
Expand Down
11 changes: 10 additions & 1 deletion httpClient.go → cli/httpClient.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cli

import (
"context"
Expand All @@ -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.
// //////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion logger.go → cli/logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cli

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion stunnelbidirection.go → cli/stunnelbidirection.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cli

import (
tls "github.com/refraction-networking/utls"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package main
package cli

import (
"github.com/gorilla/websocket"
"io"
"net"
"os"
"time"

"github.com/gorilla/websocket"
)

// WebSocketBiDirection
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module wstunnel
module github.com/Windscribe/wstunnel

go 1.18

Expand Down

0 comments on commit a94d094

Please sign in to comment.