From 400b758d6b0ce35c28de2c29b05a0d678ac664b2 Mon Sep 17 00:00:00 2001 From: isayan Date: Sun, 22 Sep 2019 20:18:54 +0900 Subject: [PATCH] bugfix pref --- Readme-ja.md | 2 +- android_app/app/build.gradle | 2 +- .../src/main/java/tun/proxy/MainActivity.java | 4 +- .../main/java/tun/proxy/MyApplication.java | 9 +- .../tun/proxy/SimplePreferenceFragment.java | 151 +++++++++++------- .../tun/proxy/service/Tun2HttpVpnService.java | 4 +- .../main/java/tun/utils/CertificateUtil.java | 116 +++++++------- .../src/main/java/tun/utils/PackageUtil.java | 21 +-- android_app/gradle.properties | 2 +- 9 files changed, 170 insertions(+), 141 deletions(-) diff --git a/Readme-ja.md b/Readme-ja.md index 60f8029..6f3c151 100644 --- a/Readme-ja.md +++ b/Readme-ja.md @@ -58,7 +58,7 @@ Disallow Application と Allow Application の2つのモードがあります 画面上部の検索アイコン(![Menu](images/Search.png))から、アプリケーションを絞り込めます。 アプリケーション名に指定したキーワードを含む、アプリケーションのみが表示されます。 -プリケーションリストは、画面上部のメニューアイコン(![メニュー](images / Menu.png))からソートできます。 +プリケーションリストは、画面上部のメニューアイコン(![Menu](images/Menu.png))からソートできます。 * アプリ名で並べ替え * アプリケーション名でアプリケーションリストを並べ替えます。 diff --git a/android_app/app/build.gradle b/android_app/app/build.gradle index 4d7c799..5e72359 100644 --- a/android_app/app/build.gradle +++ b/android_app/app/build.gradle @@ -6,7 +6,7 @@ android { applicationId "tun.proxy" minSdkVersion 21 targetSdkVersion 27 - versionCode 100011 + versionCode 100012 versionName VERSION_NAME testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" externalNativeBuild { diff --git a/android_app/app/src/main/java/tun/proxy/MainActivity.java b/android_app/app/src/main/java/tun/proxy/MainActivity.java index 4eef299..55d9ce5 100644 --- a/android_app/app/src/main/java/tun/proxy/MainActivity.java +++ b/android_app/app/src/main/java/tun/proxy/MainActivity.java @@ -66,7 +66,7 @@ public void onClick(View v) { // if (intent != null) { // startActivityForResult(intent, REQUEST_CERT); // } else { - startVpn(); + startVpn(); // } // } } @@ -272,7 +272,7 @@ public void onRequestPermissionsResult(int requestCode, String permissions[], in switch (requestCode) { case 8000: { if (grantResults.length > 0 - && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + && grantResults[0] == PackageManager.PERMISSION_GRANTED) { } else { requestPermission(); } diff --git a/android_app/app/src/main/java/tun/proxy/MyApplication.java b/android_app/app/src/main/java/tun/proxy/MyApplication.java index a76659d..19a117e 100644 --- a/android_app/app/src/main/java/tun/proxy/MyApplication.java +++ b/android_app/app/src/main/java/tun/proxy/MyApplication.java @@ -4,6 +4,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; + import tun.utils.CertificateUtil; import java.security.cert.CertificateEncodingException; @@ -45,14 +46,12 @@ public enum VPNMode {DISALLOW, ALLOW}; public enum AppSortBy {APPNAME, PKGNAME}; -// 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_mode = sharedPreferences.getString("vpn_connection_mode", MyApplication.VPNMode.DISALLOW.name()); - return VPNMode.valueOf ( vpn_mode ); + return VPNMode.valueOf(vpn_mode); } public void storeVPNMode(VPNMode mode) { @@ -64,7 +63,7 @@ public void storeVPNMode(VPNMode mode) { public Set loadVPNApplication(VPNMode mode) { final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); - Set preference = prefs.getStringSet(pref_key[mode.ordinal()], new HashSet() ); + Set preference = prefs.getStringSet(pref_key[mode.ordinal()], new HashSet()); return preference; } @@ -73,7 +72,7 @@ public void storeVPNApplication(VPNMode mode, final Set set) { final SharedPreferences.Editor editor = prefs.edit(); editor.putStringSet(pref_key[mode.ordinal()], set); editor.commit(); - return ; + return; } } diff --git a/android_app/app/src/main/java/tun/proxy/SimplePreferenceFragment.java b/android_app/app/src/main/java/tun/proxy/SimplePreferenceFragment.java index 4dfa0dd..640a6e6 100644 --- a/android_app/app/src/main/java/tun/proxy/SimplePreferenceFragment.java +++ b/android_app/app/src/main/java/tun/proxy/SimplePreferenceFragment.java @@ -3,26 +3,28 @@ import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.preference.*; -import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; -import android.view.View; import android.widget.*; import java.util.Collections; import java.util.Comparator; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; -public class SimplePreferenceFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener { +import static android.preference.Preference.*; + +public class SimplePreferenceFragment extends PreferenceFragment + implements 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"; @@ -34,8 +36,8 @@ public void onCreate(Bundle savedInstanceState) { setHasOptionsMenu(true); /* Allowed / Disallowed Application */ - final ListPreference pkg_selection = (ListPreference) this.findPreference(VPN_CONNECTION_MODE); - pkg_selection.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + final ListPreference prefPackage = (ListPreference) this.findPreference(VPN_CONNECTION_MODE); + prefPackage.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object value) { if (preference instanceof ListPreference) { @@ -54,23 +56,23 @@ public boolean onPreferenceChange(Preference preference, Object value) { return true; } }); - pkg_selection.setSummary(pkg_selection.getEntry()); + prefPackage.setSummary(prefPackage.getEntry()); 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())); + disallow.setEnabled(MyApplication.VPNMode.DISALLOW.name().equals(prefPackage.getValue())); + allow.setEnabled(MyApplication.VPNMode.ALLOW.name().equals(prefPackage.getValue())); findPreference(VPN_DISALLOWED_APPLICATION_LIST).setOnPreferenceClickListener(this); findPreference(VPN_ALLOWED_APPLICATION_LIST).setOnPreferenceClickListener(this); - } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); - if (id == android.R.id.home) { - startActivity(new Intent(getActivity(), MainActivity.class)); - return true; + switch (id) { + case android.R.id.home: + startActivity(new Intent(getActivity(), MainActivity.class)); + return true; } return super.onOptionsItemSelected(item); } @@ -99,12 +101,14 @@ private void transitionFragment(PreferenceFragment nextPreferenceFragment) { .commit(); } - @TargetApi(Build.VERSION_CODES.HONEYCOMB) - public static class PackageListPreferenceFragment extends PreferenceFragment implements SearchView.OnQueryTextListener, SearchView.OnCloseListener { + public static class PackageListPreferenceFragment extends PreferenceFragment + implements SearchView.OnQueryTextListener, SearchView.OnCloseListener { + final private Map mAllPackageInfoMap = new HashMap(); + private MyApplication.VPNMode mode = MyApplication.VPNMode.DISALLOW; private MyApplication.AppSortBy appSortBy = MyApplication.AppSortBy.APPNAME; - private PreferenceScreen mRootPreferenceScreen; + private PreferenceScreen mFilterPreferenceScreen; public static PackageListPreferenceFragment newInstance(MyApplication.VPNMode mode) { final PackageListPreferenceFragment fragment = new PackageListPreferenceFragment(); @@ -116,8 +120,8 @@ public static PackageListPreferenceFragment newInstance(MyApplication.VPNMode mo public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); - mRootPreferenceScreen = getPreferenceManager().createPreferenceScreen(getActivity()); - setPreferenceScreen(mRootPreferenceScreen); + mFilterPreferenceScreen = getPreferenceManager().createPreferenceScreen(getActivity()); + setPreferenceScreen(mFilterPreferenceScreen); } private String searchFilter = ""; @@ -130,13 +134,15 @@ protected void filter(String filter) { protected void filter(String filter, final MyApplication.AppSortBy sortBy) { if (filter == null) { filter = searchFilter; - } - else { + } else { searchFilter = filter; } - removeAllPreferenceScreen(); - buildPackagesPreferences(filter, sortBy); - loadSelectedPackage(); + + Set selected = this.getAllSelectedPackageSet(); + storeSelectedPackageSet(selected); + + this.removeAllPreferenceScreen(); + this.filterPackagesPreferences(filter, sortBy); } @Override @@ -147,72 +153,91 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { final MenuItem menuItem = menu.findItem(R.id.menu_search_item); - this.searchView = (SearchView)menuItem.getActionView(); + this.searchView = (SearchView) menuItem.getActionView(); this.searchView.setOnQueryTextListener(this); this.searchView.setOnCloseListener(this); this.searchView.setSubmitButtonEnabled(false); + } @Override - public void onPause() { + public void onPause() { super.onPause(); - storeSelectedPackageSet(this.getSelectedPackageSet()); + Set selected = this.getAllSelectedPackageSet(); + storeSelectedPackageSet(selected); } @Override public void onResume() { super.onResume(); + Set loadMap = MyApplication.getInstance().loadVPNApplication(mode); + for (String pkgName : loadMap) { + this.mAllPackageInfoMap.put(pkgName, loadMap.contains(pkgName)); + } filter(null); } private void removeAllPreferenceScreen() { - mRootPreferenceScreen.removeAll(); + mFilterPreferenceScreen.removeAll(); } - private void buildPackagesPreferences(String filter, final MyApplication.AppSortBy sortBy) { + private void filterPackagesPreferences(String filter, final MyApplication.AppSortBy sortBy) { final Context context = MyApplication.getInstance().getApplicationContext(); final PackageManager pm = context.getPackageManager(); final List installedPackages = pm.getInstalledPackages(PackageManager.GET_META_DATA); Collections.sort(installedPackages, new Comparator() { @Override public int compare(PackageInfo o1, PackageInfo o2) { - if (sortBy == MyApplication.AppSortBy.APPNAME) { - String t1 = o1.applicationInfo.loadLabel(pm).toString(); - String t2 = o2.applicationInfo.loadLabel(pm).toString(); - return t1.compareTo(t2); - } - else { - String t1 = o1.packageName; - String t2 = o2.packageName; - return t1.compareTo(t2); + String t1 = ""; + String t2 = ""; + switch (sortBy) { + case APPNAME: + t1 = o1.applicationInfo.loadLabel(pm).toString(); + t2 = o2.applicationInfo.loadLabel(pm).toString(); + break; + case PKGNAME: + t1 = o1.packageName; + t2 = o2.packageName; + break; } + return t1.compareTo(t2); } }); for (final PackageInfo pi : installedPackages) { String t1 = pi.applicationInfo.loadLabel(pm).toString(); if (filter.trim().isEmpty() || t1.toLowerCase().contains(filter.toLowerCase())) { final Preference preference = buildPackagePreferences(pm, pi); - mRootPreferenceScreen.addPreference(preference); + mFilterPreferenceScreen.addPreference(preference); } } } private Preference buildPackagePreferences(final PackageManager pm, final PackageInfo pi) { - final CheckBoxPreference p = new CheckBoxPreference(getActivity()); - p.setIcon(pi.applicationInfo.loadIcon(pm)); - p.setTitle(pi.applicationInfo.loadLabel(pm).toString()); - p.setSummary(pi.packageName); - return p; + final CheckBoxPreference prefCheck = new CheckBoxPreference(getActivity()); + prefCheck.setIcon(pi.applicationInfo.loadIcon(pm)); + prefCheck.setTitle(pi.applicationInfo.loadLabel(pm).toString()); + prefCheck.setSummary(pi.packageName); + boolean ckecked = this.mAllPackageInfoMap.containsKey(pi.packageName) ? this.mAllPackageInfoMap.get(pi.packageName) : false; + prefCheck.setChecked(ckecked); + OnPreferenceClickListener click = new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + mAllPackageInfoMap.put(prefCheck.getSummary().toString(), prefCheck.isChecked()); + return false; + } + }; + prefCheck.setOnPreferenceClickListener(click); + return prefCheck; } - private Set getSelectedPackageSet() { - Set selected = new HashSet<>(); - for (int i = 0; i < this.mRootPreferenceScreen.getPreferenceCount(); i++) { - Preference pref = this.mRootPreferenceScreen.getPreference(i); + private Set getFilterSelectedPackageSet() { + final Set selected = new HashSet<>(); + for (int i = 0; i < this.mFilterPreferenceScreen.getPreferenceCount(); i++) { + Preference pref = this.mFilterPreferenceScreen.getPreference(i); if ((pref instanceof CheckBoxPreference)) { - CheckBoxPreference pref_check = (CheckBoxPreference) pref; - if (pref_check.isChecked()) { - selected.add(pref_check.getSummary().toString()); + CheckBoxPreference prefCheck = (CheckBoxPreference) pref; + if (prefCheck.isChecked()) { + selected.add(prefCheck.getSummary().toString()); } } } @@ -220,22 +245,23 @@ private Set getSelectedPackageSet() { } private void setSelectedPackageSet(Set selected) { - for (int i = 0; i < this.mRootPreferenceScreen.getPreferenceCount(); i++) { - Preference pref = this.mRootPreferenceScreen.getPreference(i); + for (int i = 0; i < this.mFilterPreferenceScreen.getPreferenceCount(); i++) { + Preference pref = this.mFilterPreferenceScreen.getPreference(i); if ((pref instanceof CheckBoxPreference)) { - CheckBoxPreference pref_check = (CheckBoxPreference) pref; - if (selected.contains(pref_check.getSummary())) { - pref_check.setChecked(true); + CheckBoxPreference prefCheck = (CheckBoxPreference) pref; + if (selected.contains((prefCheck.getSummary()))) { + prefCheck.setChecked(true); } } } } - private void loadSelectedPackage() { - this.getArguments(); - mode = MyApplication.getInstance().loadVPNMode(); - Set selected = MyApplication.getInstance().loadVPNApplication(mode); - setSelectedPackageSet(selected); + private Set getAllSelectedPackageSet() { + Set selected = this.getFilterSelectedPackageSet(); + for (Map.Entry value : this.mAllPackageInfoMap.entrySet()) { + if (value.getValue()) selected.add(value.getKey()); + } + return selected; } private void storeSelectedPackageSet(final Set set) { @@ -270,8 +296,7 @@ public boolean onQueryTextSubmit(String query) { if (!query.trim().isEmpty()) { filter(query); return true; - } - else { + } else { filter(""); return true; } @@ -284,6 +309,8 @@ public boolean onQueryTextChange(String newText) { @Override public boolean onClose() { + Set selected = this.getAllSelectedPackageSet(); + storeSelectedPackageSet(selected); filter(""); return false; } diff --git a/android_app/app/src/main/java/tun/proxy/service/Tun2HttpVpnService.java b/android_app/app/src/main/java/tun/proxy/service/Tun2HttpVpnService.java index 7a9c291..fee4110 100644 --- a/android_app/app/src/main/java/tun/proxy/service/Tun2HttpVpnService.java +++ b/android_app/app/src/main/java/tun/proxy/service/Tun2HttpVpnService.java @@ -152,7 +152,7 @@ private Builder getBuilder() { MyApplication app = (MyApplication) this.getApplication(); if (app.loadVPNMode() == MyApplication.VPNMode.DISALLOW) { Set disallow = app.loadVPNApplication(MyApplication.VPNMode.DISALLOW); - Log.d(TAG, "disallowed:" + disallow.size()); + Log.d(TAG, "disallowed:" + disallow.size()); builder.addDisallowedApplication(Arrays.asList(disallow.toArray(new String[0]))); } else { Set allow = app.loadVPNApplication(MyApplication.VPNMode.ALLOW); @@ -160,7 +160,7 @@ private Builder getBuilder() { builder.addAllowedApplication(Arrays.asList(allow.toArray(new String[0]))); } } catch (PackageManager.NameNotFoundException e) { - Log.e( TAG, e.getMessage(), e); + Log.e(TAG, e.getMessage(), e); } } diff --git a/android_app/app/src/main/java/tun/utils/CertificateUtil.java b/android_app/app/src/main/java/tun/utils/CertificateUtil.java index a541703..3e86052 100644 --- a/android_app/app/src/main/java/tun/utils/CertificateUtil.java +++ b/android_app/app/src/main/java/tun/utils/CertificateUtil.java @@ -19,10 +19,11 @@ public class CertificateUtil { private static final String TAG = "CertificateManager"; - public enum CertificateInstallType {SYSTEM, USER} - private final static Pattern CA_COMMON_NAME = Pattern.compile( "CN=([^,]+),?.*$" ); - private final static Pattern CA_ORGANIZATION = Pattern.compile( "O=([^,]+),?.*$" ); + public enum CertificateInstallType {SYSTEM, USER}; + + private final static Pattern CA_COMMON_NAME = Pattern.compile("CN=([^,]+),?.*$"); + private final static Pattern CA_ORGANIZATION = Pattern.compile("O=([^,]+),?.*$"); public static boolean findCAStore(String caName) { boolean found = false; @@ -42,14 +43,14 @@ public static boolean findCAStore(String caName) { break; } } - } catch(IOException e){ - Log.e( TAG, e.getMessage(), e); - } catch(KeyStoreException e){ - Log.e( TAG, e.getMessage(), e); - } catch(NoSuchAlgorithmException e){ - Log.e( TAG, e.getMessage(), e); - } catch(CertificateException e){ - Log.e( TAG, e.getMessage(), e); + } catch (IOException e) { + Log.e(TAG, e.getMessage(), e); + } catch (KeyStoreException e) { + Log.e(TAG, e.getMessage(), e); + } catch (NoSuchAlgorithmException e) { + Log.e(TAG, e.getMessage(), e); + } catch (CertificateException e) { + Log.e(TAG, e.getMessage(), e); } return found; } @@ -58,28 +59,28 @@ public static boolean findCAStore(String caName) { public static List getRootCAStore() { final List rootCAList = new ArrayList<>(); try { - KeyStore ks = KeyStore.getInstance( "AndroidCAStore" ); + KeyStore ks = KeyStore.getInstance("AndroidCAStore"); if (ks == null) return null; - ks.load( null, null ); + ks.load(null, null); X509Certificate rootCACert = null; Enumeration aliases = ks.aliases(); boolean found = false; while (aliases.hasMoreElements()) { String alias = (String) aliases.nextElement(); - X509Certificate cert = (X509Certificate) ks.getCertificate( alias ); - System.out.println( alias + "/" + cert.getIssuerX500Principal().getName() ); - rootCAList.add( cert ); + X509Certificate cert = (X509Certificate) ks.getCertificate(alias); + System.out.println(alias + "/" + cert.getIssuerX500Principal().getName()); + rootCAList.add(cert); } } catch (IOException e) { - Log.e( TAG, e.getMessage(), e); + Log.e(TAG, e.getMessage(), e); } catch (KeyStoreException e) { - Log.e( TAG, e.getMessage(), e); + Log.e(TAG, e.getMessage(), e); } catch (NoSuchAlgorithmException e) { - Log.e( TAG, e.getMessage(), e); + Log.e(TAG, e.getMessage(), e); } catch (CertificateException e) { - Log.e( TAG, e.getMessage(), e); + Log.e(TAG, e.getMessage(), e); } return rootCAList; } @@ -87,82 +88,83 @@ public static List getRootCAStore() { public static Map getRootCAMap(EnumSet type) { final Map rootCAMap = new HashMap<>(); try { - KeyStore ks = KeyStore.getInstance( "AndroidCAStore" ); + KeyStore ks = KeyStore.getInstance("AndroidCAStore"); if (ks == null) return null; - ks.load( null, null ); + ks.load(null, null); X509Certificate rootCACert = null; Enumeration aliases = ks.aliases(); List certList = new ArrayList<>(); while (aliases.hasMoreElements()) { String alias = (String) aliases.nextElement(); - X509Certificate cert = (X509Certificate) ks.getCertificate( alias ); - if (type.contains(CertificateInstallType.SYSTEM) && alias.startsWith("system:")) { - certList.add( cert ); + X509Certificate cert = (X509Certificate) ks.getCertificate(alias); + if (type.contains(CertificateInstallType.SYSTEM) && alias.startsWith("system:")) { + certList.add(cert); } if (type.contains(CertificateInstallType.USER) && alias.startsWith("user:")) { - certList.add( cert ); + certList.add(cert); } } - Collections.sort( certList, new Comparator() { + Collections.sort(certList, new Comparator() { @Override public int compare(X509Certificate t1, X509Certificate t2) { - String t1cn = CertificateUtil.getCommonName( t1.getIssuerX500Principal().getName() ); - String t2cn = CertificateUtil.getCommonName( t2.getIssuerX500Principal().getName() ); - return t1cn.compareToIgnoreCase( t2cn ); + String t1cn = CertificateUtil.getCommonName(t1.getIssuerX500Principal().getName()); + String t2cn = CertificateUtil.getCommonName(t2.getIssuerX500Principal().getName()); + return t1cn.compareToIgnoreCase(t2cn); } - } ); + }); // ソート後 List rootCANameList = new ArrayList<>(); List rootCAList = new ArrayList<>(); for (X509Certificate cert : certList) { - String cn = CertificateUtil.getCommonName( cert.getIssuerX500Principal().getName() ); + String cn = CertificateUtil.getCommonName(cert.getIssuerX500Principal().getName()); if (cn.trim().isEmpty()) continue; //String o = CertificateUtil.getOrganization( cert.getIssuerX500Principal().getName()); - rootCANameList.add( cn ); - rootCAList.add( encode( cert.getEncoded() ) ); + rootCANameList.add(cn); + rootCAList.add(encode(cert.getEncoded())); } - rootCAMap.put( "entry", rootCANameList.toArray( new String[0] ) ); - rootCAMap.put( "value", rootCAList.toArray( new String[0] ) ); + rootCAMap.put("entry", rootCANameList.toArray(new String[0])); + rootCAMap.put("value", rootCAList.toArray(new String[0])); } catch (IOException e) { - Log.e( TAG, e.getMessage(), e); + Log.e(TAG, e.getMessage(), e); } catch (KeyStoreException e) { - Log.e( TAG, e.getMessage(), e); + Log.e(TAG, e.getMessage(), e); } catch (NoSuchAlgorithmException e) { - Log.e( TAG, e.getMessage(), e); + Log.e(TAG, e.getMessage(), e); } catch (CertificateException e) { - Log.e( TAG, e.getMessage(), e); + Log.e(TAG, e.getMessage(), e); } return rootCAMap; } public static String encode(byte b[]) { - return new String( b, StandardCharsets.ISO_8859_1 ); + return new String(b, StandardCharsets.ISO_8859_1); } + public static byte[] decode(String s) { - return s.getBytes( StandardCharsets.ISO_8859_1 ); + return s.getBytes(StandardCharsets.ISO_8859_1); } public static Intent trustRootCA(X509Certificate cert) { - Log.d( TAG, "root CA is not yet trusted" ); + Log.d(TAG, "root CA is not yet trusted"); Intent intent = KeyChain.createInstallIntent(); try { if (findCAStore(cert.getIssuerDN().getName())) return null; - intent.putExtra( KeyChain.EXTRA_CERTIFICATE, cert.getEncoded() ); - intent.putExtra( KeyChain.EXTRA_NAME, getCommonName(cert.getIssuerDN().getName())); + intent.putExtra(KeyChain.EXTRA_CERTIFICATE, cert.getEncoded()); + intent.putExtra(KeyChain.EXTRA_NAME, getCommonName(cert.getIssuerDN().getName())); } catch (CertificateEncodingException e) { - Log.e( TAG, e.getMessage(), e); + Log.e(TAG, e.getMessage(), e); } return intent; } // get the CA certificate by the path - public static X509Certificate getCACertificate(byte [] buff) { + public static X509Certificate getCACertificate(byte[] buff) { X509Certificate ca = null; - try { - CertificateFactory cf = CertificateFactory.getInstance( "X.509" ); - ca = (X509Certificate) cf.generateCertificate( new ByteArrayInputStream( buff ) ); + try { + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + ca = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(buff)); } catch (CertificateException e) { e.printStackTrace(); } @@ -174,9 +176,9 @@ public static X509Certificate getCACertificate(String dir, String caName) { String CERT_FILE = dir + "/burp_export.crt"; InputStream inStream = null; try { - inStream = new FileInputStream( CERT_FILE ); - CertificateFactory cf = CertificateFactory.getInstance( "X.509" ); - return (X509Certificate) cf.generateCertificate( inStream ); + inStream = new FileInputStream(CERT_FILE); + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + return (X509Certificate) cf.generateCertificate(inStream); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (CertificateException e) { @@ -194,18 +196,18 @@ public static X509Certificate getCACertificate(String dir, String caName) { public static String getCommonName(String dn) { String cn = ""; - Matcher m = CA_COMMON_NAME.matcher( dn ); + Matcher m = CA_COMMON_NAME.matcher(dn); if (m.find()) { - cn = m.group( 1 ); + cn = m.group(1); } return cn; } public static String getOrganization(String dn) { String on = ""; - Matcher m = CA_ORGANIZATION.matcher( dn ); + Matcher m = CA_ORGANIZATION.matcher(dn); if (m.find()) { - on = m.group( 1 ); + on = m.group(1); } return on; } diff --git a/android_app/app/src/main/java/tun/utils/PackageUtil.java b/android_app/app/src/main/java/tun/utils/PackageUtil.java index c743095..18c6e3c 100644 --- a/android_app/app/src/main/java/tun/utils/PackageUtil.java +++ b/android_app/app/src/main/java/tun/utils/PackageUtil.java @@ -3,6 +3,7 @@ import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; + import tun.proxy.MyApplication; import java.util.*; @@ -14,24 +15,24 @@ public static Map getPackageMap() { final Map packageMap = new HashMap<>(); Context context = MyApplication.getInstance().getApplicationContext(); PackageManager pm = context.getPackageManager(); - List installedPackages = pm.getInstalledPackages( PackageManager.GET_META_DATA ); - Collections.sort( installedPackages, new Comparator() { + List installedPackages = pm.getInstalledPackages(PackageManager.GET_META_DATA); + Collections.sort(installedPackages, new Comparator() { @Override public int compare(PackageInfo t1, PackageInfo t2) { - String t1pkg = t1.packageName; - String t2pkg = t2.packageName; - return t1pkg.compareToIgnoreCase( t2pkg ); + String t1pkg = t1.packageName; + String t2pkg = t2.packageName; + return t1pkg.compareToIgnoreCase(t2pkg); } - } ); + }); // ソート後 List pkgLabelList = new ArrayList<>(); List pkgNameList = new ArrayList<>(); for (final PackageInfo pi : installedPackages) { - pkgLabelList.add( pi.applicationInfo.loadLabel( pm ).toString() + "(" + pi.packageName + ")" ); - pkgNameList.add( pi.packageName ); + pkgLabelList.add(pi.applicationInfo.loadLabel(pm).toString() + "(" + pi.packageName + ")"); + pkgNameList.add(pi.packageName); } - packageMap.put( "entry", pkgLabelList.toArray( new String[0] ) ); - packageMap.put( "value", pkgNameList.toArray( new String[0] ) ); + packageMap.put("entry", pkgLabelList.toArray(new String[0])); + packageMap.put("value", pkgNameList.toArray(new String[0])); return packageMap; } diff --git a/android_app/gradle.properties b/android_app/gradle.properties index 2cee810..c61e18c 100644 --- a/android_app/gradle.properties +++ b/android_app/gradle.properties @@ -13,7 +13,7 @@ org.gradle.jvmargs=-Xmx1536m # org.gradle.parallel=true APP_NAME=TunProxy -VERSION_NAME=1.1.1 +VERSION_NAME=1.1.2 productKeyStore=../androidkeystore.jks productKeyAlias=key0