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 594a3b4
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 20 deletions.
89 changes: 89 additions & 0 deletions .github/workflows/build-android.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: Build Android

on:
release:
types: [ published ]
push:
paths:
- "**/*.go"
- "go.mod"
- "go.sum"
- ".github/workflows/*.yml"
pull_request:
types:
- opened
paths:
- "**/*.go"
- "go.mod"
- "go.sum"

permissions: write-all

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout codebase
uses: actions/checkout@v3

- name: Set up Go
run: |
wget https://go.dev/dl/go1.21.3.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.21.3.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
- name: Get project dependencies
run: go mod download

- name: Build Android AAR
run: |
go install golang.org/x/mobile/cmd/gomobile@latest
export PATH="/home/runner/go/bin:${PATH}"
gomobile init
mkdir -p build_assets
sudo apt update && sudo apt install openjdk-17-jdk
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
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
gomobile bind -target=android -o build_assets/zju-connect.aar ./mobile
- name: Upload artifact
if: github.event_name != 'release'
uses: actions/upload-artifact@v3
with:
name: zju-connect-android-aar
path: build_assets/*

- name: Create ZIP archive
if: github.event_name == 'release'
run: |
pushd build_assets || exit 1
zip -9vr ../zju-connect-android-aar.zip .
popd || exit 1
- name: Upload release binary
if: github.event_name == 'release'
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
gh release upload ${{ github.event.release.tag_name }} zju-connect-android-aar.zip
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 594a3b4

Please sign in to comment.