@@ -41,7 +41,9 @@ 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
44
45
import android.view.View
46
+ import android.webkit.URLUtil
45
47
import android.widget.TextView
46
48
import com.github.shadowsocks.App.Companion.app
47
49
import com.github.shadowsocks.acl.Acl
@@ -65,6 +67,7 @@ import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem
65
67
import java.io.IOException
66
68
import java.net.HttpURLConnection
67
69
import java.net.InetSocketAddress
70
+ import java.net.MalformedURLException
68
71
import java.net.Proxy
69
72
import java.net.URL
70
73
import java.util.*
@@ -162,28 +165,34 @@ class MainActivity : AppCompatActivity(), ShadowsocksConnection.Interface, Drawe
162
165
* Based on: https://android.googlesource.com/platform/frameworks/base/+/97bfd27/services/core/java/com/android/server/connectivity/NetworkMonitor.java#879
163
166
*/
164
167
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
176
168
val (success, result) = try {
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()
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))
187
196
}
188
197
if (testCount == id) app.handler.post {
189
198
if (success) statusText.text = result else {
0 commit comments