Skip to content

Commit

Permalink
Merge pull request #30 from so898/Pre-App
Browse files Browse the repository at this point in the history
Pre-App Support

 感谢 👍
  • Loading branch information
dawei101 authored May 6, 2017
2 parents 4b1e217 + b46a9fe commit 6a7869a
Show file tree
Hide file tree
Showing 15 changed files with 553 additions and 25 deletions.
9 changes: 6 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ buildscript {
}

android {
compileSdkVersion 19
compileSdkVersion 25
buildToolsVersion "23.0.1"
useLibrary 'org.apache.http.legacy'

defaultConfig {
applicationId "com.vm.shadowsocks"
minSdkVersion 14
targetSdkVersion 19
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.1"
}
Expand All @@ -44,6 +45,8 @@ dependencies {
compile 'com.embarkmobile:zxing-android-integration:2.0.0@aar'
compile 'com.google.zxing:core:3.0.1'
compile 'org.bouncycastle:bcprov-jdk15on:1.52'
compile 'com.futuremind.recyclerfastscroll:fastscroll:0.2.5'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile('com.googlecode.json-simple:json-simple:1.1.1') {
exclude group: 'junit', module: 'junit'
exclude group: 'org.hamcrest', module: 'hamcrest-core'
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
</intent-filter>
</activity>

<activity
android:name="com.vm.shadowsocks.ui.AppManager"
android:label="@string/proxied_apps"
android:excludeFromRecents="true"
android:launchMode="singleTask"/>

<service
android:name="com.vm.shadowsocks.core.LocalVpnService"
android:permission="android.permission.BIND_VPN_SERVICE">
Expand Down
36 changes: 36 additions & 0 deletions app/src/main/java/com/vm/shadowsocks/core/AppInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.vm.shadowsocks.core;

import android.graphics.drawable.Drawable;

public class AppInfo {
private Drawable appIcon;
private String appLabel;
private String pkgName;

public AppInfo() {
}

public Drawable getAppIcon() {
return this.appIcon;
}

public String getAppLabel() {
return this.appLabel;
}

public String getPkgName() {
return this.pkgName;
}

public void setAppIcon(Drawable var1) {
this.appIcon = var1;
}

public void setAppLabel(String var1) {
this.appLabel = var1;
}

public void setPkgName(String var1) {
this.pkgName = var1;
}
}
102 changes: 102 additions & 0 deletions app/src/main/java/com/vm/shadowsocks/core/AppProxyManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.vm.shadowsocks.core;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Build;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.MODE_PRIVATE;

public class AppProxyManager {
public static boolean isLollipopOrAbove = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;

public static AppProxyManager Instance;
private static final String PROXY_APPS = "PROXY_APPS";
private Context mContext;

public List<AppInfo> mlistAppInfo = new ArrayList<AppInfo>();
public List<AppInfo> proxyAppInfo = new ArrayList<AppInfo>();

public AppProxyManager(Context context){
this.mContext = context;
Instance = this;
readProxyAppsList();
}

public void removeProxyApp(String pkg){
for (AppInfo app : this.proxyAppInfo) {
if (app.getPkgName().equals(pkg)){
proxyAppInfo.remove(app);
break;
}
}
writeProxyAppsList();
}

public void addProxyApp(String pkg){
for (AppInfo app : this.mlistAppInfo) {
if (app.getPkgName().equals(pkg)){
proxyAppInfo.add(app);
break;
}
}
writeProxyAppsList();
}

public boolean isAppProxy(String pkg){
for (AppInfo app : this.proxyAppInfo) {
if (app.getPkgName().equals(pkg)){
return true;
}
}
return false;
}

private void readProxyAppsList() {
SharedPreferences preferences = mContext.getSharedPreferences("shadowsocksProxyUrl", MODE_PRIVATE);
String tmpString = preferences.getString(PROXY_APPS, "");
try {
if (proxyAppInfo != null){
proxyAppInfo.clear();
}
if (tmpString.isEmpty()){
return;
}
JSONArray jsonArray = new JSONArray(tmpString);
for (int i = 0; i < jsonArray.length() ; i++){
JSONObject object = jsonArray.getJSONObject(i);
AppInfo appInfo = new AppInfo();
appInfo.setAppLabel(object.getString("label"));
appInfo.setPkgName(object.getString("pkg"));
proxyAppInfo.add(appInfo);
}
} catch (Exception e){
e.printStackTrace();
}
}

private void writeProxyAppsList() {
SharedPreferences preferences = mContext.getSharedPreferences("shadowsocksProxyUrl", MODE_PRIVATE);
try {
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < proxyAppInfo.size() ; i++){
JSONObject object = new JSONObject();
AppInfo appInfo = proxyAppInfo.get(i);
object.put("label", appInfo.getAppLabel());
object.put("pkg", appInfo.getPkgName());
jsonArray.put(object);
}
SharedPreferences.Editor editor = preferences.edit();
editor.putString(PROXY_APPS, jsonArray.toString());
editor.apply();
} catch (Exception e){
e.printStackTrace();
}
}
}
26 changes: 24 additions & 2 deletions app/src/main/java/com/vm/shadowsocks/core/LocalVpnService.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ String getAppInstallID() {
appInstallID = UUID.randomUUID().toString();
Editor editor = preferences.edit();
editor.putString("AppInstallID", appInstallID);
editor.commit();
editor.apply();
}
return appInstallID;
}
Expand Down Expand Up @@ -379,12 +379,34 @@ private ParcelFileDescriptor establishVPN() throws Exception {
String value = (String) method.invoke(null, name);
if (value != null && !"".equals(value) && !servers.contains(value)) {
servers.add(value);
builder.addRoute(value, 32);
if (value.replaceAll("\\d", "").length() == 3){//防止IPv6地址导致问题
builder.addRoute(value, 32);
} else {
builder.addRoute(value, 128);
}
if (ProxyConfig.IS_DEBUG)
System.out.printf("%s=%s\n", name, value);
}
}

if (AppProxyManager.isLollipopOrAbove){
if (AppProxyManager.Instance.proxyAppInfo.size() == 0){
writeLog("Proxy All Apps");
}
for (AppInfo app : AppProxyManager.Instance.proxyAppInfo){
builder.addAllowedApplication("com.vm.shadowsocks");//需要把自己加入代理,不然会无法进行网络连接
try{
builder.addAllowedApplication(app.getPkgName());
writeLog("Proxy App: " + app.getAppLabel());
} catch (Exception e){
e.printStackTrace();
writeLog("Proxy App Fail: " + app.getAppLabel());
}
}
} else {
writeLog("No Pre-App proxy, due to low Android version.");
}

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setConfigureIntent(pendingIntent);
Expand Down
Loading

0 comments on commit 6a7869a

Please sign in to comment.