Skip to content

Commit

Permalink
Merge branch 'jni'
Browse files Browse the repository at this point in the history
  • Loading branch information
madeye committed Jul 26, 2014
2 parents c2a1dac + 796d6f3 commit 9e07215
Show file tree
Hide file tree
Showing 15 changed files with 353 additions and 115 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ src/main/libs
src/main/assets/armeabi
src/main/assets/armeabi-v7a
src/main/assets/x86
src/main/assets/api-16/
src/main/jni
jni/Application.mk
obj
libs
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: scala
scala:
- 2.10.3
- 2.10.4
before_install:
- sudo apt-get update
- sudo apt-get install libc6-dev-i386
Expand All @@ -17,10 +17,9 @@ before_install:
- tar xf android-ndk-r9d-linux-${ARCH}.tar.bz2
- export ANDROID_NDK=`pwd`/android-ndk-r9d
- export ANDROID_HOME=`pwd`/android-sdk-linux
- export PATH=${PATH}:${ANDROID_NDK}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools
- export PATH=${PATH}:${ANDROID_NDK_HOME}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools
- echo "y" | android update sdk --filter tools,platform-tools,build-tools-20.0.0,android-19,extra-google-m2repository --no-ui --no-https -a
- echo "y" | android update sdk --filter extra-android-m2repository --no-ui --no-https -a
- ln -s $ANDROID_HOME/build-tools/20.0.0/zipalign $ANDROID_HOME/tools/
install:
- gem install --version 0.8.9 faraday
- gem install travis-artifacts
Expand Down
26 changes: 25 additions & 1 deletion build-ndk.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
#!/bin/bash

# Target 9
cp jni/Application.mk.9 jni/Application.mk
ndk-build clean
ndk-build
rm -rf src/main/assets/armeabi
rm -rf src/main/assets/armeabi-v7a
rm -rf src/main/assets/x86
mkdir -p src/main/assets/armeabi
mkdir -p src/main/assets/armeabi-v7a
mkdir -p src/main/assets/x86
for app in ip6tables iptables pdnsd redsocks ss-local ss-tunnel tun2socks
for app in pdnsd redsocks
do
mv libs/armeabi/$app src/main/assets/armeabi/
mv libs/armeabi-v7a/$app src/main/assets/armeabi-v7a/
mv libs/x86/$app src/main/assets/x86/
done
rm -rf src/main/jni
mv libs src/main/jni

# Target 16
cp jni/Application.mk.16 jni/Application.mk
ndk-build clean
ndk-build
rm -rf src/main/assets/api-16/armeabi
rm -rf src/main/assets/api-16/armeabi-v7a
rm -rf src/main/assets/api-16/x86
mkdir -p src/main/assets/api-16/armeabi
mkdir -p src/main/assets/api-16/armeabi-v7a
mkdir -p src/main/assets/api-16/x86
for app in pdnsd redsocks
do
mv libs/armeabi/$app src/main/assets/api-16/armeabi/
mv libs/armeabi-v7a/$app src/main/assets/api-16/armeabi-v7a/
mv libs/x86/$app src/main/assets/api-16/x86/
done

76 changes: 56 additions & 20 deletions jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,30 @@ LOCAL_CFLAGS := -Wall -O2 -I$(LOCAL_PATH)/pdnsd
include $(BUILD_EXECUTABLE)

########################################################
## shadowsocks local
## pdnsd library
########################################################

include $(CLEAR_VARS)

PDNSD_SOURCES := $(wildcard $(LOCAL_PATH)/pdnsd/src/*.c) main-jni.cpp

LOCAL_MODULE := pdnsd-jni
LOCAL_SRC_FILES := $(PDNSD_SOURCES:$(LOCAL_PATH)/%=%)
LOCAL_CFLAGS := -Wall -O2 -DPDNSD_JNI -I$(LOCAL_PATH)/pdnsd

include $(BUILD_SHARED_LIBRARY)

########################################################
## shadowsocks local library
########################################################

include $(CLEAR_VARS)

SHADOWSOCKS_SOURCES := local.c cache.c udprelay.c encrypt.c utils.c json.c jconf.c acl.c

LOCAL_MODULE := ss-local
LOCAL_SRC_FILES := $(addprefix shadowsocks/src/, $(SHADOWSOCKS_SOURCES))
LOCAL_CFLAGS := -Wall -O2 -fno-strict-aliasing -DUDPRELAY_LOCAL \
LOCAL_MODULE := ss-local-jni
LOCAL_SRC_FILES := $(addprefix shadowsocks/src/, $(SHADOWSOCKS_SOURCES)) main-jni.cpp
LOCAL_CFLAGS := -Wall -O2 -fno-strict-aliasing -DUDPRELAY_LOCAL -DSSLOCAL_JNI \
-DUSE_CRYPTO_OPENSSL -DANDROID -DHAVE_CONFIG_H \
-I$(LOCAL_PATH)/libev/ \
-I$(LOCAL_PATH)/openssl/include \
Expand All @@ -163,28 +177,49 @@ LOCAL_STATIC_LIBRARIES := libev libcrypto libipset libcork

LOCAL_LDLIBS := -llog

include $(BUILD_EXECUTABLE)
include $(BUILD_SHARED_LIBRARY)

########################################################
## shadowsocks tunnel
########################################################

# include $(CLEAR_VARS)
#
# SHADOWSOCKS_SOURCES := tunnel.c cache.c udprelay.c encrypt.c utils.c json.c jconf.c
#
# LOCAL_MODULE := ss-tunnel
# LOCAL_SRC_FILES := $(addprefix shadowsocks/src/, $(SHADOWSOCKS_SOURCES))
# LOCAL_CFLAGS := -Wall -O2 -fno-strict-aliasing -DUDPRELAY_LOCAL -DUDPRELAY_TUNNEL \
# -DUSE_CRYPTO_OPENSSL -DANDROID -DHAVE_CONFIG_H \
# -I$(LOCAL_PATH)/libev/ \
# -I$(LOCAL_PATH)/openssl/include
#
# LOCAL_STATIC_LIBRARIES := libev libcrypto
#
# LOCAL_LDLIBS := -llog
#
# include $(BUILD_EXECUTABLE)

########################################################
## shadowsocks tunnel library
########################################################

include $(CLEAR_VARS)

SHADOWSOCKS_SOURCES := tunnel.c cache.c udprelay.c encrypt.c utils.c json.c jconf.c

LOCAL_MODULE := ss-tunnel
LOCAL_SRC_FILES := $(addprefix shadowsocks/src/, $(SHADOWSOCKS_SOURCES))
LOCAL_MODULE := ss-tunnel-jni
LOCAL_SRC_FILES := $(addprefix shadowsocks/src/, $(SHADOWSOCKS_SOURCES)) main-jni.cpp
LOCAL_CFLAGS := -Wall -O2 -fno-strict-aliasing -DUDPRELAY_LOCAL -DUDPRELAY_TUNNEL \
-DUSE_CRYPTO_OPENSSL -DANDROID -DHAVE_CONFIG_H \
-DUSE_CRYPTO_OPENSSL -DANDROID -DHAVE_CONFIG_H -DSSTUNNEL_JNI \
-I$(LOCAL_PATH)/libev/ \
-I$(LOCAL_PATH)/openssl/include

LOCAL_STATIC_LIBRARIES := libev libcrypto

LOCAL_LDLIBS := -llog

include $(BUILD_EXECUTABLE)
include $(BUILD_SHARED_LIBRARY)

########################################################
## system
Expand All @@ -204,7 +239,7 @@ LOCAL_STATIC_LIBRARIES := cpufeatures
include $(BUILD_SHARED_LIBRARY)

########################################################
## tun2socks
## tun2socks-library
########################################################

include $(CLEAR_VARS)
Expand All @@ -214,6 +249,7 @@ LOCAL_CFLAGS += -DBADVPN_THREADWORK_USE_PTHREAD -DBADVPN_LINUX -DBADVPN_BREACTOR
LOCAL_CFLAGS += -DBADVPN_USE_SELFPIPE -DBADVPN_USE_EPOLL
LOCAL_CFLAGS += -DBADVPN_LITTLE_ENDIAN -DBADVPN_THREAD_SAFE
LOCAL_CFLAGS += -DNDEBUG -DANDROID
LOCAL_CFLAGS += -DTUN2SOCKS_JNI

LOCAL_C_INCLUDES:= \
$(LOCAL_PATH)/badvpn/lwip/src/include/ipv4 \
Expand Down Expand Up @@ -283,13 +319,13 @@ TUN2SOCKS_SOURCES := \
tun2socks/SocksUdpGwClient.c \
udpgw_client/UdpGwClient.c

LOCAL_MODULE := tun2socks
LOCAL_MODULE := tun2socks-jni

LOCAL_LDLIBS := -ldl -llog

LOCAL_SRC_FILES := $(addprefix badvpn/, $(TUN2SOCKS_SOURCES))
LOCAL_SRC_FILES := $(addprefix badvpn/, $(TUN2SOCKS_SOURCES)) main-jni.cpp

include $(BUILD_EXECUTABLE)
include $(BUILD_SHARED_LIBRARY)

# OpenSSL
openssl_subdirs := $(addprefix $(LOCAL_PATH)/openssl/,$(addsuffix /Android.mk, \
Expand All @@ -299,13 +335,13 @@ openssl_subdirs := $(addprefix $(LOCAL_PATH)/openssl/,$(addsuffix /Android.mk, \
include $(openssl_subdirs)

# Iptables
LOCAL_PATH := $(ROOT_PATH)
iptables_subdirs := $(addprefix $(LOCAL_PATH)/iptables/,$(addsuffix /Android.mk, \
iptables \
extensions \
libiptc \
))
include $(iptables_subdirs)
# LOCAL_PATH := $(ROOT_PATH)
# iptables_subdirs := $(addprefix $(LOCAL_PATH)/iptables/,$(addsuffix /Android.mk, \
# iptables \
# extensions \
# libiptc \
# ))
# include $(iptables_subdirs)

# Import cpufeatures
$(call import-module,android/cpufeatures)
4 changes: 4 additions & 0 deletions jni/Application.mk.16
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
APP_ABI := armeabi armeabi-v7a x86
APP_PLATFORM := android-16
APP_STL := stlport_static
NDK_TOOLCHAIN_VERSION := 4.8
File renamed without changes.
84 changes: 84 additions & 0 deletions jni/main-jni.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <jni.h>
#include <string.h>
#include <unistd.h>

typedef unsigned short char16_t;

class String8 {
public:
String8() {
mString = 0;
}

~String8() {
if (mString) {
free(mString);
}
}

void set(const char16_t* o, size_t numChars) {
if (mString) {
free(mString);
}
mString = (char*) malloc(numChars + 1);
if (!mString) {
return;
}
for (size_t i = 0; i < numChars; i++) {
mString[i] = (char) o[i];
}
mString[numChars] = '\0';
}

const char* string() {
return mString;
}
private:
char* mString;
};

extern "C" {

int main(int argc, char* args[]);

#if defined(PDNSD_JNI)
jint Java_com_github_shadowsocks_Core_pdnsd(JNIEnv *env, jobject thiz, jobjectArray argv) {
#elif defined(SSLOCAL_JNI)
jint Java_com_github_shadowsocks_Core_sslocal(JNIEnv *env, jobject thiz, jobjectArray argv) {
#elif defined(SSTUNNEL_JNI)
jint Java_com_github_shadowsocks_Core_sstunnel(JNIEnv *env, jobject thiz, jobjectArray argv) {
#elif defined(TUN2SOCKS_JNI)
jint Java_com_github_shadowsocks_Core_tun2socks(JNIEnv *env, jobject thiz, jobjectArray argv) {
#else
#error "invalid header"
#endif

int argc = argv ? env->GetArrayLength(argv) : 0;
char **args = NULL;
String8 tmp_8;

if (argc > 0) {
args = (char **)malloc((argc+1)*sizeof(char *));
for (int i = 0; i < argc; ++i) {
jstring arg = reinterpret_cast<jstring>(env->GetObjectArrayElement(argv, i));
const jchar *str = env->GetStringCritical(arg, 0);
tmp_8.set(str, env->GetStringLength(arg));
env->ReleaseStringCritical(arg, str);
args[i] = strdup(tmp_8.string());
}
args[argc] = NULL;

pid_t pid;
pid = fork();
if (pid == 0) {
int ret = main(argc, args);
return ret;
}

if (args != NULL) free(args);
}

return 0;
}

}
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=0.12.3
sbt.version=0.13.5
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resolvers += Resolver.url("scalasbt releases", new URL("http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-snapshots"))(Resolver.ivyStylePatterns)

addSbtPlugin("com.hanhuy.sbt" % "android-sdk-plugin" % "1.2.16")
addSbtPlugin("com.hanhuy.sbt" % "android-sdk-plugin" % "1.2.20")

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
4 changes: 2 additions & 2 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.github.shadowsocks"
android:versionCode="67"
android:versionName="2.1.6">
android:versionCode="68"
android:versionName="2.2.0">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/github/shadowsocks/Core.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Shadowsocks - A shadowsocks client for Android
* Copyright (C) 2012 <max.c.lv@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* ___====-_ _-====___
* _--^^^#####// \\#####^^^--_
* _-^##########// ( ) \\##########^-_
* -############// |\^^/| \\############-
* _/############// (@::@) \\############\_
* /#############(( \\// ))#############\
* -###############\\ (oo) //###############-
* -#################\\ / VV \ //#################-
* -###################\\/ \//###################-
* _#/|##########/\######( /\ )######/\##########|\#_
* |/ |#/\#/\#/\/ \#/\##\ | | /##/\#/ \/\#/\#/\#| \|
* ` |/ V V ` V \#\| | | |/#/ V ' V V \| '
* ` ` ` ` / | | | | \ ' ' ' '
* ( | | | | )
* __\ | | | | /__
* (vvv(VVV)(VVV)vvv)
*
* HERE BE DRAGONS
*
*/

package com.github.shadowsocks;

public class Core {
static {
java.lang.System.loadLibrary("ss-local-jni");
java.lang.System.loadLibrary("ss-tunnel-jni");
java.lang.System.loadLibrary("tun2socks-jni");
java.lang.System.loadLibrary("pdnsd-jni");
}

public static native void sslocal(String[] cmd);
public static native void sstunnel(String[] cmd);
public static native void tun2socks(String[] cmd);
public static native void pdnsd(String[] cmd);
}
Loading

0 comments on commit 9e07215

Please sign in to comment.