From a7056bd8fca3ea153f982afb1a88739214cfe346 Mon Sep 17 00:00:00 2001 From: HMBSbige Date: Fri, 11 Oct 2019 16:29:36 +0800 Subject: [PATCH] Drop tcping --- .../shadowsocks/ProfileManagerActivity.java | 185 +++--------- .../github/shadowsocks/database/DBHelper.kt | 169 +---------- .../github/shadowsocks/database/Profile.kt | 4 +- .../github/shadowsocks/job/SSRSubUpdateJob.kt | 2 - .../shadowsocks/network/ping/PingHelper.kt | 268 ++++++------------ .../main/res/layout/layout_profiles_item.xml | 46 ++- .../main/res/menu/profile_manager_menu.xml | 19 +- app/src/main/res/values-ja/strings.xml | 2 - app/src/main/res/values-ru/strings.xml | 2 - app/src/main/res/values-zh-rCN/strings.xml | 2 - app/src/main/res/values-zh-rTW/strings.xml | 2 - app/src/main/res/values/strings.xml | 4 +- 12 files changed, 158 insertions(+), 547 deletions(-) diff --git a/app/src/main/java/com/github/shadowsocks/ProfileManagerActivity.java b/app/src/main/java/com/github/shadowsocks/ProfileManagerActivity.java index 6e5fbbbf..54c105f0 100644 --- a/app/src/main/java/com/github/shadowsocks/ProfileManagerActivity.java +++ b/app/src/main/java/com/github/shadowsocks/ProfileManagerActivity.java @@ -27,7 +27,6 @@ import android.text.Spanned; import android.text.TextUtils; import android.text.style.TextAppearanceSpan; -import android.util.Log; import android.util.SparseArray; import android.view.KeyEvent; import android.view.LayoutInflater; @@ -182,7 +181,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { } if (action != null && action.equals(Constants.Action.SORT)) { - Log.i("ShadowsocksR Sort:", "TRUE"); is_sort = true; } @@ -610,7 +608,6 @@ public void onClick(DialogInterface dialogInterface, int i) { EditText editText1 = myView.findViewById(R.id.editTextInput); // add ssr sub by url String subUrl = editText1.getText().toString(); - Log.i("SubURL: ", subUrl); addSSRSubByUrl(subUrl); } }) @@ -839,10 +836,7 @@ public boolean onMenuItemClick(MenuItem item) { startActivity(new Intent(Constants.Action.SORT)); return true; case R.id.action_full_test: - pingAll(1); - return true; - case R.id.action_tcp_ping_full_test: - pingAll(2); + pingAll(); return true; default: break; @@ -850,7 +844,7 @@ public boolean onMenuItemClick(MenuItem item) { return false; } - private void pingAll(int pingtype) { + private void pingAll() { // reject repeat operation if (isTesting) { return; @@ -877,88 +871,46 @@ public void onCancel(DialogInterface dialog) { } }); - // get current profile list + // get profile list List profiles = profilesAdapter.profiles; // start test - switch (pingtype) { - case 1: - PingHelper.Companion.instance().pingAll(this, profiles, new PingCallback() { - - @Override - public void onSuccess(Profile profile, long elapsed) { - profile.setElapsed(elapsed); - ShadowsocksApplication.app.profileManager.updateProfile(profile); - // set progress message - setProgressMessage(profile.getName() + " " + getResultMsg()); - } - - @Override - public void onFailed(Profile profile) { - profile.setElapsed(-1); - ShadowsocksApplication.app.profileManager.updateProfile(profile); + PingHelper.Companion.instance().pingAll(this, profiles, new PingCallback() { - // set progress message - setProgressMessage(getResultMsg()); - } - - /** - * set progress message - * - * @param message tips message - */ - private void setProgressMessage(String message) { - if (testProgressDialog != null) { - testProgressDialog.setMessage(message); - } - } - - @Override - public void onFinished(Profile profile) { - mProgressHandler.sendEmptyMessageDelayed(MSG_FULL_TEST_FINISH, 2000); - PingHelper.Companion.instance().releaseTempActivity(); - } - }, 1); - break; - case 2: - PingHelper.Companion.instance().pingAll(this, profiles, new PingCallback() { - - @Override - public void onSuccess(Profile profile, long tcpdelay) { - profile.setTcpdelay(tcpdelay); - ShadowsocksApplication.app.profileManager.updateProfile(profile); - // set progress message - setProgressMessage(profile.getName() + " " + getResultMsg()); - } + @Override + public void onSuccess(Profile profile, long elapsed) { + profile.setElapsed(elapsed); + ShadowsocksApplication.app.profileManager.updateProfile(profile); - @Override - public void onFailed(Profile profile) { - profile.setElapsed(-1); - ShadowsocksApplication.app.profileManager.updateProfile(profile); + // set progress message + setProgressMessage(profile.getName() + "\n" + getResultMsg()); + } - // set progress message - setProgressMessage(getResultMsg()); - } + @Override + public void onFailed(Profile profile) { + profile.setElapsed(-1); + ShadowsocksApplication.app.profileManager.updateProfile(profile); - /** - * set progress message - * - * @param message tips message - */ - private void setProgressMessage(String message) { - if (testProgressDialog != null) { - testProgressDialog.setMessage(message); - } - } + // set progress message + setProgressMessage(getResultMsg()); + } - @Override - public void onFinished(Profile profile) { - mProgressHandler.sendEmptyMessageDelayed(MSG_FULL_TEST_FINISH, 2000); - PingHelper.Companion.instance().releaseTempActivity(); - } - }, 2); - break; - } + /** + * set progress message + * + * @param message tips message + */ + private void setProgressMessage(String message) { + if (testProgressDialog != null) { + testProgressDialog.setMessage(message); + } + } + @Override + public void onFinished(Profile profile) { + mProgressHandler.sendEmptyMessageDelayed(MSG_FULL_TEST_FINISH, 2000); + PingHelper.Companion.instance().releaseTempActivity(); + } + }); } private class ProfileViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnKeyListener { @@ -973,7 +925,6 @@ public ProfileViewHolder(View view) { itemView.setOnKeyListener(this); initShareBtn(); - initTcpPingBtn(); initPingBtn(); } @@ -1021,6 +972,7 @@ public void onClick(DialogInterface dialog, int which) { dialog.show(); } }); + shareBtn.setOnLongClickListener(v -> { Utils.INSTANCE.positionToast(Toast.makeText(ProfileManagerActivity.this, R.string.share, Toast.LENGTH_SHORT), shareBtn, getWindow(), 0, Utils.INSTANCE.dpToPx(ProfileManagerActivity.this, 8)).show(); @@ -1046,7 +998,7 @@ public void onSuccess(@NotNull Profile profile, long elapsed) { } ShadowsocksApplication.app.profileManager.updateProfile(profile); - updateText(profile.getTx(), profile.getRx(), elapsed, profile.getTcpdelay()); + updateText(profile.getTx(), profile.getRx(), elapsed); } @Override @@ -1073,77 +1025,27 @@ public void onFinished(Profile profile) { }); } - /** - * init tcp ping btn - */ - private void initTcpPingBtn() { - final ImageView pingBtn = itemView.findViewById(R.id.tcp_ping_single); - pingBtn.setOnClickListener(v -> { - item.setElapsed(0); - final ProgressDialog singleTestProgressDialog = ProgressDialog.show(ProfileManagerActivity.this, getString(R.string.tips_testing), getString(R.string.tips_testing), false, true); - PingHelper.Companion.instance().tcpPing(ProfileManagerActivity.this, item, new PingCallback() { - @Override - public void onSuccess(@NotNull Profile profile, long tcpdelay) { - if (profile.getTcpdelay() == 0) { - profile.setTcpdelay(tcpdelay); - } else if (profile.getTcpdelay() > tcpdelay) { - profile.setTcpdelay(tcpdelay); - } - - ShadowsocksApplication.app.profileManager.updateProfile(profile); - updateText(profile.getTx(), profile.getRx(), profile.getElapsed(), tcpdelay); - } - - @Override - public void onFailed(Profile profile) { - } - - @Override - public void onFinished(Profile profile) { - Snackbar.make(findViewById(android.R.id.content), getResultMsg(), Snackbar.LENGTH_LONG).show(); - singleTestProgressDialog.dismiss(); - PingHelper.Companion.instance().releaseTempActivity(); - } - }); - }); - - pingBtn.setOnLongClickListener(v -> { - Utils.INSTANCE.positionToast(Toast.makeText(ProfileManagerActivity.this, R.string.tcp_ping, Toast.LENGTH_SHORT), - pingBtn, - getWindow(), - 0, - Utils.INSTANCE.dpToPx(ProfileManagerActivity.this, 8)) - .show(); - return true; - }); - } - - public void updateText() { updateText(0, 0); } public void updateText(long txTotal, long rxTotal) { - updateText(txTotal, rxTotal, -1, -1); + updateText(txTotal, rxTotal, -1); } - public void updateText(long txTotal, long rxTotal, long elapsedInput, long tcpdelayInput) { + public void updateText(long txTotal, long rxTotal, long elapsedInput) { final SpannableStringBuilder builder = new SpannableStringBuilder(); long tx = item.getTx() + txTotal; long rx = item.getRx() + rxTotal; long elapsed = item.getElapsed(); - long tcpdelay = item.getTcpdelay(); if (elapsedInput != -1) { elapsed = elapsedInput; } - if (tcpdelayInput != -1) { - tcpdelay = tcpdelayInput; - } builder.append(item.getName()); if (tx != 0 || rx != 0 || elapsed != 0 || item.getUrl_group() != "") { int start = builder.length(); builder.append(getString(R.string.stat_profiles, - TrafficMonitor.formatTraffic(tx), TrafficMonitor.formatTraffic(rx), String.valueOf(tcpdelay), String.valueOf(elapsed))); + TrafficMonitor.formatTraffic(tx), TrafficMonitor.formatTraffic(rx), String.valueOf(elapsed), item.getUrl_group())); builder.setSpan(new TextAppearanceSpan(ProfileManagerActivity.this, android.R.style.TextAppearance_Small), start + 1, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } @@ -1340,12 +1242,7 @@ public void updateText(boolean isShowUrl) { builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } - handler.post(new Runnable() { - @Override - public void run() { - text.setText(builder); - } - }); + handler.post(() -> text.setText(builder)); } public void copyText() { @@ -1371,7 +1268,7 @@ public void onClick(View v) { @Override public boolean onLongClick(View v) { copyText(); - return false; + return true; } @Override diff --git a/app/src/main/java/com/github/shadowsocks/database/DBHelper.kt b/app/src/main/java/com/github/shadowsocks/database/DBHelper.kt index d94833cf..f3343a48 100644 --- a/app/src/main/java/com/github/shadowsocks/database/DBHelper.kt +++ b/app/src/main/java/com/github/shadowsocks/database/DBHelper.kt @@ -1,9 +1,7 @@ package com.github.shadowsocks.database import android.content.* -import android.content.pm.* import android.database.sqlite.* -import android.text.* import com.github.shadowsocks.* import com.github.shadowsocks.utils.* import com.j256.ormlite.android.apptools.* @@ -11,13 +9,11 @@ import com.j256.ormlite.dao.* import com.j256.ormlite.support.* import com.j256.ormlite.table.* import java.sql.* -import java.util.* -class DBHelper(private val context: Context) : OrmLiteSqliteOpenHelper(context, PROFILE, null, VERSION) +class DBHelper(context: Context) : OrmLiteSqliteOpenHelper(context, PROFILE, null, VERSION) { internal lateinit var profileDao: Dao internal lateinit var ssrsubDao: Dao - private var apps: List? = null init { @@ -41,66 +37,6 @@ class DBHelper(private val context: Context) : OrmLiteSqliteOpenHelper(context, } - /** - * is all digits - */ - private fun isAllDigits(x: String): Boolean - { - if (!TextUtils.isEmpty(x)) - { - for (ch in x.toCharArray()) - { - val digit = Character.isDigit(ch) - if (!digit) - { - return false - } - } - return true - } - return false - } - - /** - * update proxied apps - */ - @Synchronized - private fun updateProxiedApps(context: Context, old: String): String - { - if (apps == null) - { - apps = context.packageManager.getInstalledApplications(0) - } - - val uidSet = ArrayList() - val split = old.split("|") - .dropLastWhile { it.isEmpty() } - .toTypedArray() - for (item in split) - { - if (isAllDigits(item)) - { - // add to uid list - uidSet.add(Integer.parseInt(item)) - } - } - val sb = StringBuilder() - for (i in apps!!.indices) - { - val ai = apps!![i] - if (uidSet.contains(ai.uid)) - { - if (i > 0) - { - // adding separator - sb.append("\n") - } - sb.append(ai.packageName) - } - } - return sb.toString() - } - override fun onCreate(database: SQLiteDatabase, connectionSource: ConnectionSource?) { try @@ -121,107 +57,6 @@ class DBHelper(private val context: Context) : OrmLiteSqliteOpenHelper(context, { try { - if (oldVersion < 7) - { - profileDao.executeRawNoArgs("DROP TABLE IF EXISTS 'profile';") - onCreate(database, connectionSource) - return - } - if (oldVersion < 8) - { - profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN udpdns SMALLINT;") - } - if (oldVersion < 9) - { - profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN route VARCHAR DEFAULT 'all';") - } - else if (oldVersion < 19) - { - profileDao.executeRawNoArgs("UPDATE `profile` SET route = 'all' WHERE route IS NULL;") - } - if (oldVersion < 10) - { - profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN auth SMALLINT;") - } - if (oldVersion < 11) - { - profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN ipv6 SMALLINT;") - } - if (oldVersion < 12) - { - profileDao.executeRawNoArgs("BEGIN TRANSACTION;") - profileDao.executeRawNoArgs("ALTER TABLE `profile` RENAME TO `tmp`;") - TableUtils.createTable(connectionSource, Profile::class.java) - profileDao.executeRawNoArgs("INSERT INTO `profile`(id, name, host, localPort, remotePort, password, method, route, proxyApps, bypass," + " udpdns, auth, ipv6, individual) " + "SELECT id, name, host, localPort, remotePort, password, method, route, 1 - global, bypass, udpdns, auth," + " ipv6, individual FROM `tmp`;") - profileDao.executeRawNoArgs("DROP TABLE `tmp`;") - profileDao.executeRawNoArgs("COMMIT;") - } - else if (oldVersion < 13) - { - profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN tx LONG;") - profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN rx LONG;") - profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN date VARCHAR;") - } - - if (oldVersion < 15) - { - if (oldVersion >= 12) - { - profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN userOrder LONG;") - } - var i = 0 - for (profile in profileDao.queryForAll()) - { - if (oldVersion < 14) - { - profile.individual = updateProxiedApps(context, profile.individual) - } - profile.userOrder = i.toLong() - profileDao.update(profile) - i += 1 - } - } - - - if (oldVersion < 16) - { - profileDao.executeRawNoArgs("UPDATE `profile` SET route = 'bypass-lan-china' WHERE route = 'bypass-china'") - } - - if (oldVersion < 19) - { - profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN dns VARCHAR DEFAULT '8.8.8.8:53';") - } - - if (oldVersion < 20) - { - profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN china_dns VARCHAR DEFAULT '114.114.114.114:53,223.5.5.5:53';") - } - - if (oldVersion < 21) - { - profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN protocol_param VARCHAR DEFAULT '';") - } - - if (oldVersion < 22) - { - profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN elapsed LONG DEFAULT 0;") - } - - if (oldVersion < 23) - { - profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN tcpdelay LONG DEFAULT 0;") - } - - if (oldVersion < 24) - { - profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN url_group VARCHAR DEFAULT '';") - } - - if (oldVersion < 25) - { - TableUtils.createTable(connectionSource, SSRSub::class.java) - } } catch (e: Exception) @@ -248,6 +83,6 @@ class DBHelper(private val context: Context) : OrmLiteSqliteOpenHelper(context, { const val PROFILE = "profile.db" private const val TAG = "DBHelper" - private const val VERSION = 25 + private const val VERSION = 1 } } diff --git a/app/src/main/java/com/github/shadowsocks/database/Profile.kt b/app/src/main/java/com/github/shadowsocks/database/Profile.kt index 25ad7ff1..1c6cc29a 100644 --- a/app/src/main/java/com/github/shadowsocks/database/Profile.kt +++ b/app/src/main/java/com/github/shadowsocks/database/Profile.kt @@ -31,7 +31,7 @@ class Profile @DatabaseField var method = "aes-256-cfb" @DatabaseField - var route = "all" + var route = "bypass-lan-china" @DatabaseField var proxyApps = false @DatabaseField @@ -55,8 +55,6 @@ class Profile @DatabaseField var elapsed: Long = 0 @DatabaseField - var tcpdelay: Long = 0 - @DatabaseField var userOrder: Long = 0 /** diff --git a/app/src/main/java/com/github/shadowsocks/job/SSRSubUpdateJob.kt b/app/src/main/java/com/github/shadowsocks/job/SSRSubUpdateJob.kt index 42894db5..4f562bcb 100644 --- a/app/src/main/java/com/github/shadowsocks/job/SSRSubUpdateJob.kt +++ b/app/src/main/java/com/github/shadowsocks/job/SSRSubUpdateJob.kt @@ -1,6 +1,5 @@ package com.github.shadowsocks.job -import android.util.* import com.evernote.android.job.* import com.github.shadowsocks.* import com.github.shadowsocks.R @@ -22,7 +21,6 @@ class SSRSubUpdateJob : Job() { VayLog.d(TAG, "onRunJob() update sub success!") ToastUtils.showShort(context.getString(R.string.sub_autoupdate_success, subName)) - Log.i("sub", subName) } override fun onFailed() diff --git a/app/src/main/java/com/github/shadowsocks/network/ping/PingHelper.kt b/app/src/main/java/com/github/shadowsocks/network/ping/PingHelper.kt index 6f927d0d..4e7f30a9 100644 --- a/app/src/main/java/com/github/shadowsocks/network/ping/PingHelper.kt +++ b/app/src/main/java/com/github/shadowsocks/network/ping/PingHelper.kt @@ -3,8 +3,6 @@ package com.github.shadowsocks.network.ping import android.app.* import android.content.pm.* -import android.os.* -import android.util.* import com.github.shadowsocks.GuardedProcess import com.github.shadowsocks.R import com.github.shadowsocks.database.* @@ -17,14 +15,7 @@ import java.net.* import java.util.* import java.util.concurrent.* - -/** - * Created by vay on 2018/07/18 - */ class PingHelper -/** - * private construction - */ private constructor() { private val mThreadPool: ScheduledThreadPoolExecutor @@ -58,20 +49,7 @@ private constructor() */ fun ping(aty: Activity, profile: Profile, callback: PingCallback) { - mThreadPool.execute { pingByProfile(aty, profile, callback, 1) } - } - - - /** - * ping profile - * - * @param aty activity object - * @param profile profile object - * @param callback ping callback object - */ - fun tcpPing(aty: Activity, profile: Profile, callback: PingCallback) - { - mThreadPool.execute { pingByProfile(aty, profile, callback, 2) } + mThreadPool.execute { pingByProfile(aty, profile, callback) } } /** @@ -79,9 +57,9 @@ private constructor() * * @see .pingAll */ - fun pingAll(aty: Activity, profiles: List, callback: PingCallback, pingtype: Int) + fun pingAll(aty: Activity, profiles: List, callback: PingCallback) { - pingAll(aty, profiles, 0, callback, pingtype) + pingAll(aty, profiles, 0, callback) } /** @@ -91,9 +69,9 @@ private constructor() * @param position list start index * @param callback ping callback */ - fun pingAll(aty: Activity, profiles: List?, position: Int, callback: PingCallback, pingtype: Int) + fun pingAll(aty: Activity, profiles: List?, position: Int, callback: PingCallback) { - mThreadPool.execute { pingAllByProfiles(aty, profiles, position, callback, pingtype) } + mThreadPool.execute { pingAllByProfiles(aty, profiles, position, callback) } } /** @@ -118,7 +96,7 @@ private constructor() * @param position list start index * @param callback ping callback */ - private fun pingAllByProfiles(aty: Activity, profiles: List?, position: Int, callback: PingCallback, pingtype: Int) + private fun pingAllByProfiles(aty: Activity, profiles: List?, position: Int, callback: PingCallback) { if (profiles == null || profiles.isEmpty()) { @@ -136,7 +114,6 @@ private constructor() override fun onSuccess(profile: Profile, elapsed: Long) { callback.resultMsg = resultMsg - Log.i("d", resultMsg) callback.onSuccess(profile, elapsed) } @@ -149,9 +126,9 @@ private constructor() override fun onFinished(profile: Profile?) { // test next profile - pingAll(aty, profiles, position + 1, callback, pingtype) + pingAll(aty, profiles, position + 1, callback) } - }, pingtype) + }) } else { @@ -160,18 +137,14 @@ private constructor() } } - private var mUIHandler = Handler(Looper.getMainLooper()) - - /** - * pint by profile + * ping by profile * * @param profile profile * @param callback ping callback */ - private fun pingByProfile(aty: Activity, profile: Profile, callback: PingCallback, pingtype: Int) + private fun pingByProfile(aty: Activity, profile: Profile, callback: PingCallback) { - Log.i("profile testing: ", profile.name) mTempActivity = aty // Resolve the server address var host: String = profile.host @@ -192,141 +165,114 @@ private constructor() return } } - if (pingtype == 1) - { - val conf = String.format(Locale.ENGLISH, Constants.ConfigUtils.SHADOWSOCKS, host, profile.remotePort, profile.localPort + 2, Constants.ConfigUtils.EscapedJson(profile.password), profile.method, 600, profile.protocol, profile.obfs, Constants.ConfigUtils.EscapedJson(profile.obfs_param), Constants.ConfigUtils.EscapedJson(profile.protocol_param)) - Utils.printToFile(File(applicationInfo!!.dataDir + "/libssr-local.so-test.conf"), conf, true) + val conf = String.format(Locale.ENGLISH, Constants.ConfigUtils.SHADOWSOCKS, host, profile.remotePort, profile.localPort + 2, Constants.ConfigUtils.EscapedJson(profile.password), profile.method, 600, profile.protocol, profile.obfs, Constants.ConfigUtils.EscapedJson(profile.obfs_param), Constants.ConfigUtils.EscapedJson(profile.protocol_param)) - val cmd = arrayOf(applicationInfo!!.nativeLibraryDir + "/libssr-local.so", "-t", "600", "-L", "www.google.com:80", "-c", applicationInfo!!.dataDir + "/libssr-local.so-test.conf") + Utils.printToFile(File(applicationInfo!!.dataDir + "/libssr-local.so-test.conf"), conf, true) - val cmds = ArrayList(listOf(*cmd)) + val cmd = arrayOf(applicationInfo!!.nativeLibraryDir + "/libssr-local.so", "-t", "600", "-L", "www.google.com:80", "-c", applicationInfo!!.dataDir + "/libssr-local.so-test.conf") - if (TcpFastOpen.sendEnabled()) - { - cmds.add("--fast-open") - } + val cmds = ArrayList(Arrays.asList(*cmd)) - if (ssTestProcess != null) - { - ssTestProcess!!.destroy() - ssTestProcess = null - } + if (TcpFastOpen.sendEnabled()) + { + cmds.add("--fast-open") + } + + if (ssTestProcess != null) + { + ssTestProcess!!.destroy() + ssTestProcess = null + } + + try + { + ssTestProcess = GuardedProcess(cmds).start() + } + catch (e: InterruptedException) + { + val result = getString(R.string.connection_test_error, "GuardedProcess start exception") + callback.resultMsg = result + callback.onFailed(profile) + callback.onFinished(profile) + return + } + val start = System.currentTimeMillis() + while (System.currentTimeMillis() - start < 5 * 1000 && isPortAvailable(profile.localPort + 2)) + { try { - ssTestProcess = GuardedProcess(cmds).start() + Thread.sleep(50) } catch (e: InterruptedException) { - val result = getString(R.string.connection_test_error, "GuardedProcess start exception") - callback.resultMsg = result - callback.onFailed(profile) - callback.onFinished(profile) - return + VayLog.e(TAG, "pingByProfile", e) } + } + //val proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", profile.localPort + 2)) - val start = System.currentTimeMillis() - while (System.currentTimeMillis() - start < 5 * 1000 && isPortAvailable(profile.localPort + 2)) - { - try - { - Thread.sleep(50) - } - catch (e: InterruptedException) - { - // Ignored - } - - } - //val proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", profile.localPort + 2)) - - // Based on: https://android.googlesource.com/platform/frameworks/base/+/master/services/core/java/com/android/server/connectivity/NetworkMonitor.java#640 - val request = Request.Builder() - .url("http://127.0.0.1:" + (profile.localPort + 2) + "/generate_204") - .removeHeader("Host") - .addHeader("Host", "www.google.com") - .build() + // Based on: https://android.googlesource.com/platform/frameworks/base/+/master/services/core/java/com/android/server/connectivity/NetworkMonitor.java#640 + val request = Request.Builder() + .url("http://127.0.0.1:" + (profile.localPort + 2) + "/generate_204") + .removeHeader("Host") + .addHeader("Host", "www.google.com") + .build() - val rinstance = RequestHelper.instance() - val rcallback = object : RequestCallback() + // request + val rInstance = RequestHelper.instance() + val rCallback = object : RequestCallback() + { + override fun isRequestOk(code: Int): Boolean { - override fun isRequestOk(code: Int): Boolean - { - return code == 204 || code == 200 - } - - override fun onSuccess(code: Int, response: String) - { - // update profile - val elapsed = System.currentTimeMillis() - this.start + return code == 204 || code == 200 + } - val result = getString(R.string.connection_test_available, elapsed) - callback.resultMsg = result - callback.onSuccess(profile, elapsed) - } + override fun onSuccess(code: Int, response: String) + { + // update profile + val elapsed = System.currentTimeMillis() - this.start + val result = getString(R.string.connection_test_available, elapsed) + callback.resultMsg = result + callback.onSuccess(profile, elapsed) + } - override fun onFailed(code: Int, msg: String) + override fun onFailed(code: Int, msg: String) + { + val result: String + if (code != 404) { - val result: String = if (code != 404) - { - getString(R.string.connection_test_error_status_code, code) - } - else - { - getString(R.string.connection_test_error, msg) - } - callback.resultMsg = result - callback.onFailed(profile) + result = getString(R.string.connection_test_error_status_code, code) } - - override fun onFinished() + else { - callback.onFinished(profile) - if (ssTestProcess != null) - { - ssTestProcess!!.destroy() - ssTestProcess = null - } + result = getString(R.string.connection_test_error, msg) } + callback.resultMsg = result + callback.onFailed(profile) } - // flag start request time - rcallback.start = System.currentTimeMillis() - // request - rinstance!!.request(request, rcallback) - } - else - { - mThreadPool.execute { - val elapsed = tcping(host, profile.remotePort) - // run to main thread - mUIHandler.post { - try - { - if (elapsed == 0.toLong()) - { - callback.resultMsg = getString(R.string.connection_test_error, "port can't reach") - callback.onFailed(profile) - - } - else - { - callback.resultMsg = getString(R.string.connection_test_available, elapsed) - callback.onSuccess(profile, elapsed) - } - callback.onFinished(profile) - } - catch (e: Exception) - { - callback.onFailed(profile) - } + override fun onFinished() + { + callback.onFinished(profile) + //Snackbar.make(findViewById(android.R.id.content), result, Snackbar.LENGTH_LONG).show(); + if (ssTestProcess != null) + { + ssTestProcess!!.destroy() + ssTestProcess = null } } } + // flag start request time + rCallback.start = System.currentTimeMillis() + + // request + rInstance!!.request(request, rCallback) + + // Based on: https://android.googlesource.com/platform/frameworks/base/+/master/services/core/java/com/android/server/connectivity/NetworkMonitor.java#640 } private fun isPortAvailable(port: Int): Boolean @@ -358,6 +304,7 @@ private constructor() companion object { + private const val TAG = "PingHelper" private var sInstance: PingHelper? = null @@ -379,39 +326,4 @@ private constructor() return sInstance!! } } - - private fun tcping(target: String, port: Int): Long - { - try - { - var minDelay: Long = 5000 - for (i in 1..2) - { - val start = System.currentTimeMillis() - val sock = Socket() - val socketAddress = InetSocketAddress(target, port) - sock.connect(socketAddress, 1000) - val delay = System.currentTimeMillis() - start - sock.close() - if (delay < minDelay) - { - minDelay = delay - } - } - return minDelay - } - catch (e: UnknownHostException) - { - return 0 - } - catch (e: InterruptedException) - { - return 0 - } - catch (e: IOException) - { - return 0 - } - - } } diff --git a/app/src/main/res/layout/layout_profiles_item.xml b/app/src/main/res/layout/layout_profiles_item.xml index 5c849a30..ac60f9aa 100644 --- a/app/src/main/res/layout/layout_profiles_item.xml +++ b/app/src/main/res/layout/layout_profiles_item.xml @@ -1,66 +1,54 @@ + android:paddingEnd="?attr/dialogPreferredPadding" + android:paddingRight="?attr/dialogPreferredPadding"> - - - + android:minHeight="?android:attr/listPreferredItemHeightSmall" + android:scrollHorizontally="false" + android:textAppearance="?android:attr/textAppearanceMedium" + android:textColor="?android:attr/textColorAlertDialogListItem" /> + android:nextFocusLeft="@+id/container" + android:padding="5dp" + app:srcCompat="@drawable/ic_click" /> + android:nextFocusLeft="@+id/ping_single" + android:padding="2dp" + app:srcCompat="@drawable/ic_social_share" /> diff --git a/app/src/main/res/menu/profile_manager_menu.xml b/app/src/main/res/menu/profile_manager_menu.xml index 9f5c40e9..e0a81c15 100644 --- a/app/src/main/res/menu/profile_manager_menu.xml +++ b/app/src/main/res/menu/profile_manager_menu.xml @@ -1,32 +1,25 @@ - diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 26739aeb..b6c2230c 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -207,8 +207,6 @@ KCP ポート %d(リモート KCPTUN サーバーのポート番号) KCP パラメータ - TCP 延迟 - 完整TCP延迟测试 All Groups %s: autoupdate success diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 0111738b..fe1f3d46 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -203,8 +203,6 @@ Отправлено: Получено: ShadowsocksR - TCP 延迟 - 完整TCP延迟测试 All Groups %s: autoupdate success diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 93307a44..ebdd952a 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -203,8 +203,6 @@ 发送: 接收: - TCP 延迟 - 完整TCP延迟测试 全部群组 %s:订阅更新成功 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 66d05c16..b42f13df 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -205,8 +205,6 @@ 傳送: 接收: - TCP 延迟 - 完整TCP延迟测试 All Groups %s: autoupdate success diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3aa72d8a..4eda86ea 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -22,7 +22,7 @@ 用于远程分析的DNS(默认名称服务器8.8.8.8) Network Traffic Sent: \t\t\t\t\t%3$s\t↑\t%1$s/s\nReceived: \t%4$s\t↓\t%2$s/s - \n%1$s↑\t%2$s↓\n%3$sms - %4$sms + \n%1$s↑\t%2$s↓\t%3$sms\n%4$s %1$s/s↑\t%2$s/s↓ Check Connectivity Testing… @@ -159,10 +159,8 @@ Sorting... Full HTTP latency test - Full TCP ping test Auto sort by latency Ping - TCP Ping Enable Address