Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Set user agent to v2rayNG/version when update subscription #1198

Merged
merged 1 commit into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
""
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
}