Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[feat] Update the remoteURL of profiles after receiving a 301 response #26

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions app/src/main/java/io/nekohasekai/sfa/bg/UpdateProfileWork.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ class UpdateProfileWork {
continue
}
try {
val content = HTTPClient().use { it.getString(profile.typed.remoteURL) }
val (content, newURL) = HTTPClient().use { it.getConfigWithUpdatedURL(profile.typed.remoteURL) }
Libbox.checkConfig(content)
File(profile.typed.path).writeText(content)
profile.typed.remoteURL = newURL
profile.typed.lastUpdated = Date()
ProfileManager.update(profile)
} catch (e: Exception) {
Expand All @@ -94,4 +95,4 @@ class UpdateProfileWork {
}


}
}
5 changes: 4 additions & 1 deletion app/src/main/java/io/nekohasekai/sfa/bg/VPNService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.os.IBinder
import io.nekohasekai.libbox.TunOptions
import io.nekohasekai.sfa.database.Settings
import io.nekohasekai.sfa.ktx.toIpPrefix
import io.nekohasekai.sfa.ktx.toList
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -169,7 +170,9 @@ class VPNService : VpnService(), PlatformInterfaceWrapper {
systemProxyEnabled = Settings.systemProxyEnabled
if (systemProxyEnabled) builder.setHttpProxy(
ProxyInfo.buildDirectProxy(
options.httpProxyServer, options.httpProxyServerPort
options.httpProxyServer,
options.httpProxyServerPort,
options.httpProxyBypassDomain.toList()
)
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ class EditProfileActivity : AbstractActivity<ActivityEditProfileBinding>() {
binding.progressView.isVisible = true
lifecycleScope.launch(Dispatchers.IO) {
try {
val content = HTTPClient().use { it.getString(profile.typed.remoteURL) }
val (content, newURL) = HTTPClient().use { it.getConfigWithUpdatedURL(profile.typed.remoteURL) }
Libbox.checkConfig(content)
File(profile.typed.path).writeText(content)
profile.typed.remoteURL = newURL
profile.typed.lastUpdated = Date()
ProfileManager.update(profile)
} catch (e: Exception) {
Expand All @@ -185,4 +186,4 @@ class EditProfileActivity : AbstractActivity<ActivityEditProfileBinding>() {
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ class NewProfileActivity : AbstractActivity<ActivityAddProfileBinding>() {
TypedProfile.Type.Remote.name -> {
typedProfile.type = TypedProfile.Type.Remote
val remoteURL = binding.remoteURL.text
val content = HTTPClient().use { it.getString(remoteURL) }
val (content, newURL) = HTTPClient().use { it.getConfigWithUpdatedURL(remoteURL) }
Libbox.checkConfig(content)
configFile.writeText(content)
typedProfile.remoteURL = remoteURL
typedProfile.remoteURL = newURL
typedProfile.lastUpdated = Date()
typedProfile.autoUpdate = EnabledType.valueOf(binding.autoUpdate.text).boolValue
binding.autoUpdateInterval.text.toIntOrNull()?.also {
Expand Down Expand Up @@ -206,4 +206,4 @@ class NewProfileActivity : AbstractActivity<ActivityAddProfileBinding>() {
}


}
}
18 changes: 13 additions & 5 deletions app/src/main/java/io/nekohasekai/sfa/utils/HTTPClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class HTTPClient : Closeable {
val userAgent by lazy {
var userAgent = "SFA/"
userAgent += BuildConfig.VERSION_NAME
userAgent += " ("
userAgent += " (Build "
userAgent += BuildConfig.VERSION_CODE
userAgent += "; sing-box "
userAgent += Libbox.version()
Expand All @@ -25,17 +25,25 @@ class HTTPClient : Closeable {
client.modernTLS()
}

fun getString(url: String): String {
private fun _getResponse(url: String): HTTPResponse {
val request = client.newRequest()
request.setUserAgent(userAgent)
request.setURL(url)
val response = request.execute()
return response.contentString
return request.execute()
}

fun getString(url: String): String {
return _getResponse(url).contentString
}

fun getConfigWithUpdatedURL(url: String): Pair<String, String> {
val response = _getResponse(url);
return Pair(response.contentString, response.finalURL)
}

override fun close() {
client.close()
}


}
}