Skip to content

Commit

Permalink
feat: add android
Browse files Browse the repository at this point in the history
  • Loading branch information
Mythologyli committed Nov 2, 2023
1 parent 52998a7 commit f97d62e
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 20 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ jobs:
goarch: riscv64
- goos: windows
goarch: arm64
- goos: android
goarch: arm64
# BEGIN MIPS
- goos: linux
goarch: mips64
Expand Down Expand Up @@ -97,10 +99,41 @@ jobs:
run: go mod download

- name: Build
if: matrix.goos != 'android'
run: |
mkdir -p build_assets
go build -v -o build_assets/zju-connect -trimpath -ldflags "-s -w -buildid=" .
- name: Build Android AAR
if: matrix.goos == 'android'
run: |
mkdir -p build_assets
sudo apt update && sudo apt install openjdk-17-jdk
export NDK_LTS_VERSION=23.2.8568313
export SDK_TOOLS_VERSION=10406996
export ANDROID_PLATFORM_VERSION=24
export ANDROID_HOME="/home/runner/android-sdk"
export ANDROID_SDK_ROOT=$ANDROID_HOME
export CMDLINE_TOOLS_ROOT="${ANDROID_HOME}/cmdline-tools/latest/bin"
export ADB_INSTALL_TIMEOUT=120
export PATH="${ANDROID_HOME}/emulator:${ANDROID_HOME}/cmdline-tools/latest/bin:${ANDROID_HOME}/tools:${ANDROID_HOME}/tools/bin:${ANDROID_HOME}/platform-tools:${ANDROID_HOME}/platform-tools/bin:${PATH}"
export ANDROID_NDK_HOME="/home/runner/android-sdk/ndk/${NDK_LTS_VERSION}"
export ANDROID_NDK_ROOT="${ANDROID_NDK_HOME}"
mkdir -p ${ANDROID_HOME}/cmdline-tools
mkdir ${ANDROID_HOME}/platforms
mkdir ${ANDROID_HOME}/ndk
wget -O /tmp/cmdline-tools.zip -t 5 --no-verbose "https://dl.google.com/android/repository/commandlinetools-linux-${SDK_TOOLS_VERSION}_latest.zip"
unzip -q /tmp/cmdline-tools.zip -d ${ANDROID_HOME}/cmdline-tools
rm /tmp/cmdline-tools.zip
mv ${ANDROID_HOME}/cmdline-tools/cmdline-tools ${ANDROID_HOME}/cmdline-tools/latest
echo y | ${CMDLINE_TOOLS_ROOT}/sdkmanager "build-tools;${ANDROID_PLATFORM_VERSION}.0.0"
echo y | ${CMDLINE_TOOLS_ROOT}/sdkmanager "platforms;android-${ANDROID_PLATFORM_VERSION}"
echo y | ${CMDLINE_TOOLS_ROOT}/sdkmanager "ndk;${NDK_LTS_VERSION}"
sudo apt install -y --no-install-recommends g++ libc6-dev
go install golang.org/x/mobile/cmd/gomobile@latest
gomobile init
gomobile bind -target=android -o build_assets/zju-connect.aar ./mobile
- name: Rename Windows zju-connect
if: matrix.goos == 'windows'
run: |
Expand Down
19 changes: 7 additions & 12 deletions client/rvpn_conn.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"errors"
"github.com/mythologyli/zju-connect/log"
"io"
)
Expand All @@ -15,7 +16,6 @@ type RvpnConn struct {
recvErrCount int
}

// always success or panic
func (r *RvpnConn) Read(p []byte) (n int, err error) {
for n, err = r.recvConn.Read(p); err != nil && r.recvErrCount < 5; {
log.Printf("Error occurred while receiving, retrying: %v", err)
Expand All @@ -24,18 +24,16 @@ func (r *RvpnConn) Read(p []byte) (n int, err error) {
_ = r.recvConn.Close()
r.recvConn, err = r.easyConnectClient.RecvConn()
if err != nil {
// TODO graceful shutdown
panic(err)
return 0, err
}
r.recvErrCount++
if r.recvErrCount >= 5 {
panic("recv retry limit exceeded.")
return 0, errors.New("recv error count exceeded")
}
}
return
}

// always success or panic
func (r *RvpnConn) Write(p []byte) (n int, err error) {
for n, err = r.sendConn.Write(p); err != nil && r.sendErrCount < 5; {
log.Printf("Error occurred while sending, retrying: %v", err)
Expand All @@ -44,12 +42,11 @@ func (r *RvpnConn) Write(p []byte) (n int, err error) {
_ = r.sendConn.Close()
r.sendConn, err = r.easyConnectClient.SendConn()
if err != nil {
// TODO graceful shutdown
panic(err)
return 0, err
}
r.sendErrCount++
if r.sendErrCount >= 5 {
panic("send retry limit exceeded.")
return 0, errors.New("send error count exceeded")
}
}
return
Expand All @@ -75,14 +72,12 @@ func NewRvpnConn(ec *EasyConnectClient) (*RvpnConn, error) {
var err error
c.sendConn, err = ec.SendConn()
if err != nil {
log.Printf("Error occurred while creating sendConn: %v", err)
panic(err)
return nil, err
}

c.recvConn, err = ec.RecvConn()
if err != nil {
log.Printf("Error occurred while creating recvConn: %v", err)
panic(err)
return nil, err
}
return c, nil
}
45 changes: 45 additions & 0 deletions mobile/mobile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package mobile

import (
"github.com/mythologyli/zju-connect/client"
"github.com/mythologyli/zju-connect/log"
"github.com/mythologyli/zju-connect/stack/tun"
)

var vpnClient *client.EasyConnectClient

func Login(server string, username string, password string) string {
log.Init()

vpnClient = client.NewEasyConnectClient(
server,
username,
password,
"",
false,
false,
)
err := vpnClient.Setup()
if err != nil {
return ""
}

log.Printf("EasyConnect client started")

clientIP, err := vpnClient.IP()
if err != nil {
return ""
}

return clientIP.String()
}

func StartStack(fd int) {
vpnTUNStack, err := tun.NewStack(vpnClient, "")
if err != nil {
return
}

vpnTUNStack.SetupTun(fd)
vpnTUNStack.Run()
}
18 changes: 14 additions & 4 deletions stack/gvisor/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ func (ep *Endpoint) WritePackets(list stack.PacketBufferList) (int, tcpip.Error)
}

if ep.rvpnConn != nil {
n, _ := ep.rvpnConn.Write(buf)
n, err := ep.rvpnConn.Write(buf)
if err != nil {
panic(err)
}

log.DebugPrintf("Send: wrote %d bytes", n)
log.DebugDumpHex(buf[:n])
Expand Down Expand Up @@ -133,13 +136,20 @@ func NewStack(easyConnectClient *client.EasyConnectClient) (*Stack, error) {
}

func (s *Stack) Run() {

s.endpoint.rvpnConn, _ = client.NewRvpnConn(s.endpoint.easyConnectClient)
var err error
s.endpoint.rvpnConn, err = client.NewRvpnConn(s.endpoint.easyConnectClient)
if err != nil {
log.Printf("Error occurred while creating sendConn: %v", err)
panic(err)
}

// Read from VPN server and send to gVisor stack
for {
buf := make([]byte, 1500)
n, _ := s.endpoint.rvpnConn.Read(buf)
n, err := s.endpoint.rvpnConn.Read(buf)
if err != nil {
panic(err)
}

log.DebugPrintf("Recv: read %d bytes", n)
log.DebugDumpHex(buf[:n])
Expand Down
21 changes: 17 additions & 4 deletions stack/tun/stack.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !android

package tun

import (
Expand All @@ -14,18 +16,26 @@ type Stack struct {
}

func (s *Stack) Run() {
s.rvpnConn, _ = client.NewRvpnConn(s.endpoint.easyConnectClient)
var err error
s.rvpnConn, err = client.NewRvpnConn(s.endpoint.easyConnectClient)
if err != nil {
log.Printf("Error occurred while creating sendConn: %v", err)
panic(err)
}

// Read from VPN server and send to TUN stack
go func() {
for {
buf := make([]byte, 1500)
n, _ := s.rvpnConn.Read(buf)
n, err := s.rvpnConn.Read(buf)
if err != nil {
panic(err)
}

log.DebugPrintf("Recv: read %d bytes", n)
log.DebugDumpHex(buf[:n])

err := s.endpoint.Write(buf[:n])
err = s.endpoint.Write(buf[:n])
if err != nil {
log.Printf("Error occurred while writing to TUN stack: %v", err)
panic(err)
Expand Down Expand Up @@ -57,7 +67,10 @@ func (s *Stack) Run() {
continue
}

_, _ = s.rvpnConn.Write(buf[:n])
_, err = s.rvpnConn.Write(buf[:n])
if err != nil {
panic(err)
}

log.DebugPrintf("Send: wrote %d bytes", n)
log.DebugDumpHex(buf[:n])
Expand Down
Loading

0 comments on commit f97d62e

Please sign in to comment.