Skip to content

Commit

Permalink
refactor: move Go backend code from tun2socks to app (#496)
Browse files Browse the repository at this point in the history
- **Relocated Go backend code**: I moved all the Go backend code from the separate `tun2socks` module (`tun2socks/intra`) into the main `app` module (`app/src/main/go/intra`), because a separate `tun2socks` module wasn't providing any benefits and was making dependency management more complex.
- **Upgraded Gradle binary from `7.0.2` to `7.2`**: This upgrade might fix a NPE bug that was found in the DEX compiler.
- **Used a fixed `GOTOOLCHAIN` version**: This will avoid a `cgo` bug that was introduced in `go1.22`.
  • Loading branch information
jyyi1 authored Jan 17, 2024
1 parent d355484 commit f8a2737
Show file tree
Hide file tree
Showing 35 changed files with 299 additions and 239 deletions.
70 changes: 68 additions & 2 deletions Android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,31 @@ try {
keystoreProperties['storePassword'] = ''
}

// Go backend build constants
def goSourceDir = "${projectDir}/src/go"
def goSourcePackages = ["${goSourceDir}/intra",
"${goSourceDir}/intra/android",
"${goSourceDir}/intra/doh",
"${goSourceDir}/intra/split",
"${goSourceDir}/intra/protect"]
def goBuildDir = file("${buildDir}/go")
def goBackendAAR = file("${goBuildDir}/backend.aar")

// gomobile won't use the Go version in go.mod. So we need to manually read it from go.mod and
// explicitly set the GOTOOLCHAIN environment variable later.
def goModLines = new File("${rootDir}/../go.mod").readLines()
def goToolchain = goModLines.find { it.startsWith('toolchain ') }
if (goToolchain == null) {
goToolchain = goModLines.find { it.startsWith('go ') }
if (goToolchain == null) {
throw new GradleException('cannot locate Go toolchain version in go.mod')
}
}
// goToolchain: ['toolchain go1.x.y' or 'go 1.x.y'] -> 'go1.x.y'
goToolchain = goToolchain.replace("toolchain", "").replace(" ", "")
println "using Go toolchain ${goToolchain}"

// Standard Android build definition
android {
signingConfigs {
config {
Expand Down Expand Up @@ -106,10 +131,51 @@ dependencies {
implementation 'com.google.firebase:firebase-crashlytics:18.2.6'
implementation 'com.google.firebase:firebase-crashlytics-ndk:18.2.6'
implementation 'com.google.firebase:firebase-config:21.0.1'
// For go-tun2socks
implementation project(path: ':tun2socks', configuration: 'aarBinary')

// Go backend (use fileTree instead of files to prevent Android Studio sync errors)
implementation fileTree(goBuildDir) {
include '*.aar'
builtBy 'compileGoBackend'
}
}

// For Firebase Analytics
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.firebase-perf'

// Go backend build tasks
tasks.register('compileGoBackend', Exec) {
dependsOn 'ensureGoMobile'

// invoke gomobile to compile backend code
inputs.dir(goSourceDir)
outputs.file(goBackendAAR)

environment 'GOTOOLCHAIN', goToolchain
environment 'ANDROID_HOME', android.sdkDirectory
environment 'PATH', goBuildDir.getPath() +
System.getProperty('path.separator') +
System.getenv('PATH')

commandLine("${goBuildDir}/gomobile", 'bind',
'-ldflags=-s -w',
'-target=android',
"-androidapi=${android.defaultConfig.minSdk}",
'-o', goBackendAAR,
*goSourcePackages)
}

tasks.register('ensureGoMobile', Exec) {
// install gomobile and gobind into the build folder
outputs.file("${goBuildDir}/gomobile")
outputs.file("${goBuildDir}/gobind")

doFirst { goBuildDir.mkdirs() }

environment 'GOTOOLCHAIN', goToolchain

commandLine('go', 'build',
'-o', goBuildDir,
'golang.org/x/mobile/cmd/gomobile',
'golang.org/x/mobile/cmd/gobind')
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"os"
"strings"

"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra"
"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra/doh"
"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra/protect"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/doh"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/protect"
"github.com/Jigsaw-Code/outline-sdk/network"
)

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
"sync"
"time"

"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra/doh/ipmap"
"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra/split"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/doh/ipmap"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/split"
"github.com/eycorsican/go-tun2socks/common/log"
"golang.org/x/net/dns/dnsmessage"
)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"sync/atomic"
"time"

"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra/doh"
"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra/protect"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/doh"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/protect"
"github.com/Jigsaw-Code/outline-sdk/network"
"github.com/Jigsaw-Code/outline-sdk/transport"
)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"sync"
"time"

"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra/doh"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/doh"
"github.com/Jigsaw-Code/choir"
"github.com/eycorsican/go-tun2socks/common/log"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (

"golang.org/x/net/dns/dnsmessage"

"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra/doh"
"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra/split"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/doh"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/split"
)

type qfunc func(q []byte) ([]byte, error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"net"
"os"

"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra/split"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/split"
)

func main() {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"sync/atomic"
"time"

"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra/doh"
"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra/protect"
"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra/split"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/doh"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/protect"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/split"
"github.com/Jigsaw-Code/outline-sdk/transport"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"sync/atomic"
"time"

"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra/split"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/split"
"github.com/Jigsaw-Code/outline-sdk/transport"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"os"
"strings"

"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra/doh"
"github.com/Jigsaw-Code/Intra/Android/tun2socks/intra/protect"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/doh"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/protect"
"github.com/Jigsaw-Code/outline-sdk/network"
"github.com/Jigsaw-Code/outline-sdk/network/lwip2transport"
)
Expand Down
File renamed without changes.
5 changes: 4 additions & 1 deletion Android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# Increase gradle heap size to 2G to prevent OutOfMemoryError
org.gradle.jvmargs=-Xmx2048m
# Other values include:
# -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
Expand Down
Binary file modified Android/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 1 addition & 2 deletions Android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Fri Jan 29 10:30:35 EST 2021
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
Loading

0 comments on commit f8a2737

Please sign in to comment.