@@ -41,9 +41,7 @@ import android.support.v7.content.res.AppCompatResources
41
41
import android.support.v7.preference.PreferenceDataStore
42
42
import android.text.format.Formatter
43
43
import android.util.Log
44
- import android.util.Patterns
45
44
import android.view.View
46
- import android.webkit.URLUtil
47
45
import android.widget.TextView
48
46
import com.github.shadowsocks.App.Companion.app
49
47
import com.github.shadowsocks.acl.Acl
@@ -67,7 +65,6 @@ import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem
67
65
import java.io.IOException
68
66
import java.net.HttpURLConnection
69
67
import java.net.InetSocketAddress
70
- import java.net.MalformedURLException
71
68
import java.net.Proxy
72
69
import java.net.URL
73
70
import java.util.*
@@ -165,34 +162,28 @@ class MainActivity : AppCompatActivity(), ShadowsocksConnection.Interface, Drawe
165
162
* Based on: https://android.googlesource.com/platform/frameworks/base/+/97bfd27/services/core/java/com/android/server/connectivity/NetworkMonitor.java#879
166
163
*/
167
164
private fun testConnection (id : Int ) {
165
+ val url = URL (" https" , when (app.currentProfile!! .route) {
166
+ Acl .CHINALIST -> " www.qualcomm.cn"
167
+ else -> " www.google.com"
168
+ }, " /generate_204" )
169
+ val conn = (if (BaseService .usingVpnMode) url.openConnection() else
170
+ url.openConnection(Proxy (Proxy .Type .SOCKS ,
171
+ InetSocketAddress (" 127.0.0.1" , DataStore .portProxy))))
172
+ as HttpURLConnection
173
+ conn.setRequestProperty(" Connection" , " close" )
174
+ conn.instanceFollowRedirects = false
175
+ conn.useCaches = false
168
176
val (success, result) = try {
169
- val urlString = DataStore .testUrl
170
- val url = URL (urlString)
171
- if (url.protocol != " https" )
172
- throw MalformedURLException (getString(R .string.connection_test_url_non_https, urlString))
173
- if (! URLUtil .isValidUrl(urlString) || ! Patterns .WEB_URL .matcher(urlString).matches())
174
- throw MalformedURLException (getString(R .string.connection_test_url_invalid, urlString))
175
- val conn = (if (BaseService .usingVpnMode) url.openConnection() else
176
- url.openConnection(Proxy (Proxy .Type .SOCKS ,
177
- InetSocketAddress (" 127.0.0.1" , DataStore .portProxy))))
178
- as HttpURLConnection
179
- conn.setRequestProperty(" Connection" , " close" )
180
- conn.instanceFollowRedirects = false
181
- conn.useCaches = false
182
- try {
183
- val start = SystemClock .elapsedRealtime()
184
- val code = conn.responseCode
185
- val elapsed = SystemClock .elapsedRealtime() - start
186
- if (code == 204 || code == 200 && conn.responseLength == 0L )
187
- Pair (true , getString(R .string.connection_test_available, elapsed))
188
- else throw IOException (getString(R .string.connection_test_error_status_code, code))
189
- } catch (e: IOException ) {
190
- Pair (false , getString(R .string.connection_test_error, e.message))
191
- } finally {
192
- conn.disconnect()
193
- }
194
- } catch (e: MalformedURLException ) {
195
- Pair (false , getString(R .string.connection_test_url_error, e.message))
177
+ val start = SystemClock .elapsedRealtime()
178
+ val code = conn.responseCode
179
+ val elapsed = SystemClock .elapsedRealtime() - start
180
+ if (code == 204 || code == 200 && conn.responseLength == 0L )
181
+ Pair (true , getString(R .string.connection_test_available, elapsed))
182
+ else throw IOException (getString(R .string.connection_test_error_status_code, code))
183
+ } catch (e: IOException ) {
184
+ Pair (false , getString(R .string.connection_test_error, e.message))
185
+ } finally {
186
+ conn.disconnect()
196
187
}
197
188
if (testCount == id) app.handler.post {
198
189
if (success) statusText.text = result else {
0 commit comments