Skip to content

Commit

Permalink
Rewritten in Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
Mygod committed Apr 10, 2018
1 parent c693f28 commit 9d33635
Show file tree
Hide file tree
Showing 35 changed files with 525 additions and 223 deletions.
105 changes: 10 additions & 95 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,96 +1,11 @@
# Created by .ignore support plugin (hsz.mobi)
### Scala template
*.class
*.log

# sbt specific
.cache
.history
.lib/
dist/*
target
lib_managed/
src_managed/
project/boot/
project/plugins/project/

# Scala-IDE specific
.scala_dependencies
.worksheet

# ENSIME specific
.ensime_cache/
.ensime
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
### SBT template
# Simple Build Tool
# http://www.scala-sbt.org/release/docs/Getting-Started/Directories.html#configuring-version-control

### Android template
# Built application files
*.apk
*.ap_

# Files for the ART/Dalvik VM
*.dex

# Java class files

# Generated files
bin/
gen/
out/
src/.deps

# Gradle files
.gradle/
build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Log Files

# Android Studio Navigation editor temp files
.navigation/

# Android Studio captures folder
captures/

# Intellij
.idea/
proguard-sbt.txt

# Keystore files
*.jks

# External native build folder generated in Android Studio 2.2 and later
*.iml
.gradle
/.idea
/local.properties
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
.DS_Store
/build
/captures
.externalNativeBuild

/.deps/
/src/main/libs
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[submodule "src/go"]
path = src/go
path = app/src/go
url = https://github.com/shadowsocks/go.git
branch = shadowsocks
[submodule "src/kcptun"]
path = src/kcptun
path = app/src/kcptun
url = https://github.com/shadowsocks/kcptun.git
branch = shadowsocks
6 changes: 6 additions & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/build
# no tests written yet
/src/androidTest
/src/test
/src/bin
/.deps
78 changes: 78 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import org.apache.tools.ant.taskdefs.condition.Os

import java.util.regex.Matcher
import java.util.regex.Pattern

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

def getCurrentFlavor() {
String task = getGradle().getStartParameter().getTaskRequests().toString()
Matcher matcher = Pattern.compile("(assemble|generate)\\w*(Release|Debug)").matcher(task)
if (matcher.find()) return matcher.group(2).toLowerCase() else {
println "Warning: No match found for $task"
return "debug"
}
}

android {
compileSdkVersion rootProject.sdkVersion
defaultConfig {
applicationId "com.github.shadowsocks.plugin.kcptun"
minSdkVersion rootProject.minSdkVersion
targetSdkVersion rootProject.sdkVersion
versionCode 10000
versionName "0.1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets.main.jniLibs.srcDirs += new File(projectDir, "src/bin")
}

task goBuild(type: Exec) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
println "Warning: Building on Windows is not supported"
} else {
executable "sh"
args "-c", "src/make.bash " + minSdkVersion
}
}

task goClean(type: Exec) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
println "Warning: Building on Windows is not supported"
} else {
executable "sh"
args "-c", "src/clean.bash"
}
}

tasks.whenTaskAdded { task ->
if ((task.name == 'javaPreCompileDebug' ||
task.name == 'javaPreCompileRelease')) {
task.dependsOn(goBuild)
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion"
implementation 'com.github.shadowsocks:plugin:0.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

ext.abiCodes = ['armeabi-v7a': 1, 'arm64-v8a': 2, x86: 3]
if (getCurrentFlavor() == 'release') android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def offset = project.ext.abiCodes.get(output.getFilter(OutputFile.ABI))
if (offset != null) output.versionCodeOverride = variant.versionCode + offset
}
}
22 changes: 22 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
-keepattributes SourceFile,LineNumberTable
-dontobfuscate

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
10 changes: 10 additions & 0 deletions app/src/clean.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
DEPS=$DIR/.deps

rm -rf $DEPS
rm -rf $DIR/go/bin
rm -rf $DIR/bin

echo "Successfully clean kcptun"
1 change: 1 addition & 0 deletions app/src/go
Submodule go added at 486515
1 change: 1 addition & 0 deletions app/src/kcptun
Submodule kcptun added at 4bfc48
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.github.shadowsocks.plugin.kcptun">
<uses-feature android:name="android.hardware.touchscreen"
android:required="false"/>
<uses-sdk android:minSdkVersion="19"
android:targetSdkVersion="26"/>
<application android:allowBackup="false"
android:label="kcptun"
android:icon="@mipmap/ic_launcher">
<provider android:name=".BinaryProvider"
android:exported="true"
android:authorities="com.github.shadowsocks.plugin.kcptun.BinaryProvider">
android:authorities="com.github.shadowsocks.plugin.kcptun.BinaryProvider"
tools:ignore="ExportedContentProvider">
<intent-filter>
<action android:name="com.github.shadowsocks.plugin.ACTION_NATIVE_PLUGIN"/>
</intent-filter>
Expand All @@ -25,7 +25,7 @@
android:value="crypt=none;mode=normal;mtu=1200;nocomp;dscp=46;parityshard=0"/>
</provider>
<activity android:name=".HelpCallback"
android:theme="@android:style/Theme.Translucent.NoTitleBar">
android:theme="@style/Theme.AppCompat.Translucent">
<intent-filter>
<action android:name="com.github.shadowsocks.plugin.ACTION_HELP"/>
<category android:name="android.intent.category.DEFAULT"/>
Expand Down
File renamed without changes
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.shadowsocks.plugin.kcptun

import android.net.Uri
import android.os.ParcelFileDescriptor
import com.github.shadowsocks.plugin.NativePluginProvider
import com.github.shadowsocks.plugin.PathProvider
import java.io.File
import java.io.FileNotFoundException

class BinaryProvider : NativePluginProvider() {
override fun populateFiles(provider: PathProvider) {
provider.addPath("kcptun", "755")
}
override fun getExecutable() = context.applicationInfo.nativeLibraryDir + "/libkcptun.so"
override fun openFile(uri: Uri?): ParcelFileDescriptor = when (uri?.path) {
"/kcptun" -> ParcelFileDescriptor.open(File(getExecutable()), ParcelFileDescriptor.MODE_READ_ONLY)
else -> throw FileNotFoundException()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.shadowsocks.plugin.kcptun

import com.github.shadowsocks.plugin.PluginOptions

class HelpCallback : com.github.shadowsocks.plugin.HelpCallback() {
override fun produceHelpMessage(options: PluginOptions): CharSequence =
ProcessBuilder(applicationInfo.nativeLibraryDir + "/libkcptun.so", "--help")
.redirectErrorStream(true)
.start()
.inputStream.bufferedReader().useLines {
it.dropWhile { it != "GLOBAL OPTIONS:" }
.drop(1)
.takeWhile { it.length > 3 }
.filter {
!it.startsWith(" --localaddr ") &&
!it.startsWith(" --remoteaddr ") &&
!it.startsWith(" -c ") &&
!it.startsWith(" -V ") &&
!it.startsWith(" --fast-open ") &&
!it.startsWith(" --help,") &&
!it.startsWith(" --version,")
}
.joinToString("\n")
.replace(Regex(" {2,}"), "\n")
.replace("--", "")
.replace(" value", "=value")
.substring(1) // remove 1st \n
}
}
Binary file added app/src/main/libs/arm64-v8a/libkcptun.so
Binary file not shown.
Binary file added app/src/main/libs/armeabi-v7a/libkcptun.so
Binary file not shown.
Binary file added app/src/main/libs/x86/libkcptun.so
Binary file not shown.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
58 changes: 36 additions & 22 deletions src/make.bash → app/src/make.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ function try () {

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
MIN_API=$1
TARGET=$DIR/bin
DEPS=$(pwd)/.deps

ANDROID_ARM_TOOLCHAIN=$DEPS/android-toolchain-${MIN_API}-arm
ANDROID_ARM64_TOOLCHAIN=$DEPS/android-toolchain-21-arm64
ANDROID_ARM64_TOOLCHAIN=$DEPS/android-toolchain-${MIN_API}-arm64
ANDROID_X86_TOOLCHAIN=$DEPS/android-toolchain-${MIN_API}-x86

ANDROID_ARM_CC=$ANDROID_ARM_TOOLCHAIN/bin/arm-linux-androideabi-clang
Expand All @@ -22,7 +24,7 @@ ANDROID_ARM64_STRIP=$ANDROID_ARM64_TOOLCHAIN/bin/aarch64-linux-android-strip
ANDROID_X86_CC=$ANDROID_X86_TOOLCHAIN/bin/i686-linux-android-clang
ANDROID_X86_STRIP=$ANDROID_X86_TOOLCHAIN/bin/i686-linux-android-strip

try mkdir -p $DEPS $DIR/main/libs/armeabi-v7a $DIR/main/libs/x86 $DIR/main/libs/arm64-v8a
try mkdir -p $DEPS $TARGET/armeabi-v7a $TARGET/x86 $TARGET/arm64-v8a

if [ ! -d "$ANDROID_ARM_TOOLCHAIN" ]; then
echo "Make standalone toolchain for ARM arch"
Expand Down Expand Up @@ -58,25 +60,37 @@ export PATH=$GOROOT/bin:$PATH

pushd $DIR/kcptun/client

echo "Get dependences for kcptun"
go get -u github.com/xtaci/kcp-go
go get -u github.com/xtaci/smux
go get

echo "Cross compile kcptun for arm"
try env CGO_ENABLED=1 CC=$ANDROID_ARM_CC GOOS=android GOARCH=arm GOARM=7 go build -ldflags="-s -w"
try $ANDROID_ARM_STRIP client
try mv client $DIR/main/libs/armeabi-v7a/libkcptun.so

echo "Cross compile kcptun for arm64"
try env CGO_ENABLED=1 CC=$ANDROID_ARM64_CC GOOS=android GOARCH=arm64 go build -ldflags="-s -w"
try $ANDROID_ARM64_STRIP client
try mv client $DIR/main/libs//arm64-v8a/libkcptun.so

echo "Cross compile kcptun for x86"
try env CGO_ENABLED=1 CC=$ANDROID_X86_CC GOOS=android GOARCH=386 go build -ldflags="-s -w"
try $ANDROID_X86_STRIP client
try mv client $DIR/main/libs/x86/libkcptun.so
popd
if [ ! -f "$TARGET/armeabi-v7a/libkcptun.so" ] || [ ! -f "$TARGET/arm64-v8a/libkcptun.so" ] ||
[ ! -f "$TARGET/x86/libkcptun.so" ]; then

echo "Get dependences for kcptun"
go get -u github.com/xtaci/kcp-go
go get -u github.com/xtaci/smux
go get

echo "Cross compile kcptun for arm"
if [ ! -f "$TARGET/armeabi-v7a/liboverture.so" ]; then
try env CGO_ENABLED=1 CC=$ANDROID_ARM_CC GOOS=android GOARCH=arm GOARM=7 go build -ldflags="-s -w"
try $ANDROID_ARM_STRIP client
try mv client $TARGET/armeabi-v7a/libkcptun.so
fi

echo "Cross compile kcptun for arm64"
if [ ! -f "$TARGET/arm64-v8a/liboverture.so" ]; then
try env CGO_ENABLED=1 CC=$ANDROID_ARM64_CC GOOS=android GOARCH=arm64 go build -ldflags="-s -w"
try $ANDROID_ARM64_STRIP client
try mv client $TARGET/arm64-v8a/libkcptun.so
fi

echo "Cross compile kcptun for x86"
if [ ! -f "$TARGET/x86/liboverture.so" ]; then
try env CGO_ENABLED=1 CC=$ANDROID_X86_CC GOOS=android GOARCH=386 go build -ldflags="-s -w"
try $ANDROID_X86_STRIP client
try mv client $TARGET/x86/libkcptun.so
fi

popd

fi

echo "Successfully build kcptun"
Loading

0 comments on commit 9d33635

Please sign in to comment.