From 901a82eb54afe821ec526d01c0ddb5b0a4eec338 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Fri, 20 Aug 2021 18:28:20 -0400 Subject: [PATCH] Set user agent to v2rayNG/version when update subscription --- .../src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt | 4 ++-- .../app/src/main/kotlin/com/v2ray/ang/util/Utils.kt | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt index 77a702e63..bf70292cd 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt @@ -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) { diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt index 0f9c84db2..c8db3d86f 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt @@ -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() + } + } }