Set user agent to v2rayNG/version when update subscription

This commit is contained in:
yuhan6665
2021-08-20 18:28:20 -04:00
parent 26cc29944b
commit 901a82eb54
2 changed files with 14 additions and 2 deletions

View File

@@ -371,7 +371,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
}
GlobalScope.launch(Dispatchers.IO) {
val configText = try {
URL(url).readText()
Utils.getUrlContentWithCustomUserAgent(url)
} catch (e: Exception) {
e.printStackTrace()
""
@@ -408,7 +408,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
Log.d(ANG_PACKAGE, url)
GlobalScope.launch(Dispatchers.IO) {
val configText = try {
URL(url).readText()
Utils.getUrlContentWithCustomUserAgent(url)
} catch (e: Exception) {
e.printStackTrace()
launch(Dispatchers.Main) {

View File

@@ -20,6 +20,7 @@ import android.util.Patterns
import android.webkit.URLUtil
import com.tencent.mmkv.MMKV
import com.v2ray.ang.AppConfig
import com.v2ray.ang.BuildConfig
import com.v2ray.ang.R
import com.v2ray.ang.extension.responseLength
import com.v2ray.ang.extension.toast
@@ -456,5 +457,16 @@ object Utils {
tcpTestingSockets.clear()
}
}
@Throws(IOException::class)
fun getUrlContentWithCustomUserAgent(url: String?): String {
val conn = URL(url).openConnection()
conn.setRequestProperty("Connection", "close")
conn.setRequestProperty("User-agent", "v2rayNG/${BuildConfig.VERSION_NAME}")
conn.useCaches = false
return conn.inputStream.use {
it.bufferedReader().readText()
}
}
}