Skip to content

Commit

Permalink
support for android 9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
isayan committed Feb 3, 2019
1 parent 1623972 commit c9ccf6d
Show file tree
Hide file tree
Showing 19 changed files with 116 additions and 110 deletions.
4 changes: 2 additions & 2 deletions Readme-ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TunProxyアプリを起動すると以下の画面が起動します。
![Tun Proxy](images/TunProxy.png)

* Proxy address (host:port)
* 接続先のプロキシサーバを ** IPアドレス:ポート番号 ** の形式で指定します。
* 接続先のプロキシサーバを **IPアドレス:ポート番号** の形式で指定します。
IPアドレスはIPv4形式で記載する必要があります。

* [Start] ボタン
Expand All @@ -35,7 +35,7 @@ TunProxyアプリを起動すると以下の画面が起動します。

VPNの接続設定を行います。

![Menu Settings](images/Menu-Settings.png)![Menu Settings](images/Menu-Settings-sub.png)
![Menu Settings](images/Menu-Settings.png)![Menu Settings](images/Menu-Settings-app.png)

Disallow Application と Allow Application の2つのモードがありますが、同時に指定することはできません。
このためどちらのモードで動作させたいかを選択する必要があります。
Expand Down
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ When you start the TunProxy application, the following screen will be launched.
![Tun Proxy](images/TunProxy.png)

* Proxy address (host:port)
* Specify the destination proxy server in the format ** IP address:port number **.
* Specify the destination proxy server in the format **IP address:port number**.
The IP address must be described in IPv4 format.

* [Start] button
Expand All @@ -35,7 +35,7 @@ Application settings can be made from the menu icon (![Menu](images/Menu.png)) a

Configure VPN service settings.

![Menu Settings](images/Menu-Settings.png) => ![Menu Settings](images/Menu-Settings-sub.png)
![Menu Settings](images/Menu-Settings.png) => ![Menu Settings](images/Menu-Settings-app.png)

There are two modes, Disallow Application and Allow Application, but you can not specify them at the same time.
Because of this you will have to choose whether you want to run in either mode.
Expand Down
2 changes: 1 addition & 1 deletion android_app/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {
applicationId "tun.proxy"
minSdkVersion 21
targetSdkVersion 27
versionCode 100007
versionCode 100008
versionName VERSION_NAME
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
Expand Down
3 changes: 2 additions & 1 deletion android_app/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="tun.proxy">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand Down
1 change: 0 additions & 1 deletion android_app/app/src/main/java/tun/proxy/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ public void run() {
protected void onPause() {
super.onPause();
statusHandler.removeCallbacks(statusRunnable);

unbindService(serviceConnection);
}

Expand Down
46 changes: 31 additions & 15 deletions android_app/app/src/main/java/tun/proxy/MyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@
import java.util.Set;

public class MyApplication extends Application {
private static Context context;
private static MyApplication instance;

public static Context getContext() {
return context;
public static MyApplication getInstance() {
return instance;
}

@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
instance = this;
}

// public byte [] getTrustCA() {
// try {
// X509Certificate cert = CertificateUtil.getCACertificate("/sdcard/", "");
Expand All @@ -40,22 +41,37 @@ public void onCreate() {
// return null;
// }

public int getVPNMode() {
public enum VPNMode {DISALLOW, ALLOW};

// public final String vpn_mode_key[] = {VPNMode.DISALLOW.name(), VPNMode.ALLOW.name()};

private final String pref_key[] = {"vpn_disallowed_application", "vpn_allowed_application"};

public VPNMode loadVPNMode() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String vpn_connection_mode = sharedPreferences.getString("vpn_connection_mode", String.valueOf(SimplePreferenceFragment.PackageListPreferenceFragment.VPNMode.DISALLOW.ordinal()));
return Integer.parseInt( vpn_connection_mode );
String vpn_mode = sharedPreferences.getString("vpn_connection_mode", MyApplication.VPNMode.DISALLOW.name());
return VPNMode.valueOf ( vpn_mode );
}

public Set<String> getAllowedApplication() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Set<String> allowed_preference = sharedPreferences.getStringSet("vpn_allowed_application", new HashSet<String>() );
return allowed_preference;
public void storeVPNMode(VPNMode mode) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
final SharedPreferences.Editor editor = prefs.edit();
editor.putString("vpn_connection_mode", mode.name());
return;
}

public Set<String> getDisallowedApplication() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Set<String> disallowed_preference = sharedPreferences.getStringSet("vpn_disallowed_application", new HashSet<String>() );
return disallowed_preference;
public Set<String> loadVPNApplication(VPNMode mode) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Set<String> preference = prefs.getStringSet(pref_key[mode.ordinal()], new HashSet<String>() );
return preference;
}

public void storeVPNApplication(VPNMode mode, final Set<String> set) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
final SharedPreferences.Editor editor = prefs.edit();
editor.putStringSet(pref_key[mode.ordinal()], set);
editor.commit();
return ;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,54 @@
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.preference.*;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import tun.utils.CertificateUtil;

public class SimplePreferenceFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener {

public static final String VPN_CONNECTION_MODE = "vpn_connection_mode";
public static final String VPN_DISALLOWED_APPLICATION_LIST = "vpn_disallowed_application_list";
public static final String VPN_ALLOWED_APPLICATION_LIST = "vpn_allowed_application_list";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
setHasOptionsMenu(true);

/* Allowed / Disallowed Application */
final ListPreference pkg_selection = (ListPreference) this.findPreference("vpn_connection_mode");
final ListPreference pkg_selection = (ListPreference) this.findPreference(VPN_CONNECTION_MODE);
pkg_selection.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue((String) value);

PreferenceScreen disallow = (PreferenceScreen) findPreference("disallowed_application_list");
PreferenceScreen allow = (PreferenceScreen) findPreference("allowed_application_list");
disallow.setEnabled(index == 0);
allow.setEnabled(index != 0);
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue((String) value);

PreferenceScreen disallow = (PreferenceScreen) findPreference(VPN_DISALLOWED_APPLICATION_LIST);
PreferenceScreen allow = (PreferenceScreen) findPreference(VPN_ALLOWED_APPLICATION_LIST);
disallow.setEnabled(index == 0);
allow.setEnabled(index != 0);

// Set the summary to reflect the new value.
preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
// Set the summary to reflect the new value.
preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);

}
return true;
}
return true;
}
});
pkg_selection.setSummary(pkg_selection.getEntry());
PreferenceScreen disallow = (PreferenceScreen) findPreference("disallowed_application_list");
PreferenceScreen allow = (PreferenceScreen) findPreference("allowed_application_list");
disallow.setEnabled(Integer.parseInt(pkg_selection.getValue()) == 0);
allow.setEnabled(Integer.parseInt(pkg_selection.getValue()) != 0);
PreferenceScreen disallow = (PreferenceScreen) findPreference(VPN_DISALLOWED_APPLICATION_LIST);
PreferenceScreen allow = (PreferenceScreen) findPreference(VPN_ALLOWED_APPLICATION_LIST);
disallow.setEnabled(MyApplication.VPNMode.DISALLOW.name().equals(pkg_selection.getValue()));
allow.setEnabled(MyApplication.VPNMode.ALLOW.name().equals(pkg_selection.getValue()));

findPreference("allowed_application_list").setOnPreferenceClickListener(this);
findPreference("disallowed_application_list").setOnPreferenceClickListener(this);
findPreference(VPN_DISALLOWED_APPLICATION_LIST).setOnPreferenceClickListener(this);
findPreference(VPN_ALLOWED_APPLICATION_LIST).setOnPreferenceClickListener(this);

}

Expand All @@ -81,11 +74,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
public boolean onPreferenceClick(Preference preference) {
// keyを見てクリックされたPreferenceを特定
switch (preference.getKey()) {
case "allowed_application_list":
transitionFragment(PackageListPreferenceFragment.newInstance(PackageListPreferenceFragment.VPNMode.ALLOW));
case VPN_DISALLOWED_APPLICATION_LIST:
transitionFragment(PackageListPreferenceFragment.newInstance(MyApplication.VPNMode.DISALLOW));
break;
case "disallowed_application_list":
transitionFragment(PackageListPreferenceFragment.newInstance(PackageListPreferenceFragment.VPNMode.DISALLOW));
case VPN_ALLOWED_APPLICATION_LIST:
transitionFragment(PackageListPreferenceFragment.newInstance(MyApplication.VPNMode.ALLOW));
break;
}
return false;
Expand All @@ -102,13 +95,11 @@ private void transitionFragment(PreferenceFragment nextPreferenceFragment) {

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class PackageListPreferenceFragment extends PreferenceFragment {
enum VPNMode {DISALLOW, ALLOW};
private VPNMode mode;
private MyApplication.VPNMode mode;
private PreferenceScreen mRootPreferenceScreen;

private String pref_key[] = {"vpn_disallowed_application", "vpn_allowed_application"};

public static PackageListPreferenceFragment newInstance(VPNMode mode) {
public static PackageListPreferenceFragment newInstance(MyApplication.VPNMode mode) {
PackageListPreferenceFragment fragment = new PackageListPreferenceFragment();
fragment.mode = mode;
return fragment;
Expand All @@ -122,6 +113,12 @@ public void onCreate(Bundle savedInstanceState) {
setPreferenceScreen(mRootPreferenceScreen);
}

@Override
public void onPause() {
super.onPause();
storeSelectedPackageSet(this.getSelectedPackageSet());
}

@Override
public void onResume() {
super.onResume();
Expand All @@ -135,7 +132,7 @@ private void removeAllPreferenceScreen() {
}

private void buildPackagesPreferences() {
Context context = MyApplication.getContext().getApplicationContext();
Context context = MyApplication.getInstance().getApplicationContext();
PackageManager pm = context.getPackageManager();
List<PackageInfo> installedPackages = pm.getInstalledPackages(PackageManager.GET_META_DATA);
for (final PackageInfo pi : installedPackages) {
Expand All @@ -157,7 +154,8 @@ private Set<String> getSelectedPackageSet() {
Preference pref = this.mRootPreferenceScreen.getPreference(i);
if ((pref instanceof CheckBoxPreference)) {
CheckBoxPreference pref_check = (CheckBoxPreference) pref;
if (pref_check.isChecked()) selected.add(pref_check.getSummary().toString());
if (pref_check.isChecked())
selected.add(pref_check.getSummary().toString());
}
}
return selected;
Expand All @@ -175,24 +173,21 @@ private void setSelectedPackageSet(Set<String> selected) {
}

private void loadSelectedPackage() {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( getActivity());
this.getArguments();
Set<String> selected = prefs.getStringSet( pref_key[mode.ordinal()], new HashSet<String>());
mode = MyApplication.getInstance().loadVPNMode();
Set<String> selected = MyApplication.getInstance().loadVPNApplication(mode);
setSelectedPackageSet(selected);
}

private void storeSelectedPackageSet(final Set<String> set) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
final SharedPreferences.Editor editor = prefs.edit();
editor.putStringSet(pref_key[mode.ordinal()], set);
editor.commit();
MyApplication.getInstance().storeVPNMode(mode);
MyApplication.getInstance().storeVPNApplication(mode, set);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
storeSelectedPackageSet(this.getSelectedPackageSet());
startActivity(new Intent(getActivity(), SimplePreferenceActivity.class));
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,17 @@ private Builder getBuilder() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
MyApplication app = (MyApplication) this.getApplication();
if (app.getVPNMode() == 0) {
Set<String> disallow = app.getDisallowedApplication();
if (app.loadVPNMode() == MyApplication.VPNMode.DISALLOW) {
Set<String> disallow = app.loadVPNApplication(MyApplication.VPNMode.DISALLOW);
Log.d(TAG, "disallowed:" + disallow.size());
builder.addDisallowedApplication(Arrays.asList(disallow.toArray(new String[0])));
} else {
Set<String> allow = app.getAllowedApplication();
Set<String> allow = app.loadVPNApplication(MyApplication.VPNMode.ALLOW);
Log.d(TAG, "allowed:" + allow.size());
builder.addAllowedApplication(Arrays.asList(allow.toArray(new String[0])));
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
Log.e( TAG, e.getMessage(), e);
}
}

Expand Down
Loading

0 comments on commit c9ccf6d

Please sign in to comment.