add 切换源时同步订阅地址

This commit is contained in:
cyh 2025-04-22 16:25:05 +08:00
parent 10d934f6e5
commit 47da4f2c3d
3 changed files with 68 additions and 16 deletions

View File

@ -37,9 +37,6 @@ import com.yingmao.clash.remote.Resource
import com.cncat.vpn.service.model.Profile import com.cncat.vpn.service.model.Profile
import com.cncat.vpn.service.remote.IProfileManager import com.cncat.vpn.service.remote.IProfileManager
import com.cncat.vpn.service.remote.IRemoteService import com.cncat.vpn.service.remote.IRemoteService
import com.yingmao.clash.act.UIMainActivity.Companion.clashBinder
import com.yingmao.clash.act.UIMainActivity.Companion.profileBinder
import com.yingmao.clash.act.UIMainActivity.Companion.subUrl
import com.yingmao.clash.util.Utils import com.yingmao.clash.util.Utils
import com.yingmao.clash.view.RxToast import com.yingmao.clash.view.RxToast
import com.yingmao.clash.view.adapter.NodeListAdapter import com.yingmao.clash.view.adapter.NodeListAdapter
@ -189,7 +186,7 @@ class ChangeNodeActivity : BaseActivity(), View.OnClickListener {
return return
} }
//缓存订阅地址 //缓存订阅地址
subUrl=nowSubUrl UIMainActivity.subUrl=nowSubUrl
MMKV.defaultMMKV().encode(BZYConstants.PROFILE_SUB_URL, nowSubUrl) MMKV.defaultMMKV().encode(BZYConstants.PROFILE_SUB_URL, nowSubUrl)
//缓存节点 //缓存节点
MMKV.defaultMMKV().encode(BZYConstants.NODE_All_ITEM_NAME_KEY, json) MMKV.defaultMMKV().encode(BZYConstants.NODE_All_ITEM_NAME_KEY, json)
@ -234,7 +231,7 @@ class ChangeNodeActivity : BaseActivity(), View.OnClickListener {
launch(Dispatchers.IO) { launch(Dispatchers.IO) {
profileBinder?.let { profileManager -> UIMainActivity.profileBinder?.let { profileManager ->
var mProfile = profileManager.queryAll().find { var mProfile = profileManager.queryAll().find {
it.name == Conf.getClashConfName(this@ChangeNodeActivity.applicationContext) it.name == Conf.getClashConfName(this@ChangeNodeActivity.applicationContext)
} }
@ -311,7 +308,7 @@ class ChangeNodeActivity : BaseActivity(), View.OnClickListener {
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
subUrl?.let { UIMainActivity.subUrl?.let {
nowSubUrl = it nowSubUrl = it
} }
doProfile() doProfile()
@ -329,7 +326,6 @@ class ChangeNodeActivity : BaseActivity(), View.OnClickListener {
private fun changeSubUrl(originalUrl: String, newUrl: String) { private fun changeSubUrl(originalUrl: String, newUrl: String) {
nowSubUrl = originalUrl.replace(Regex("https://[^/]+/"), "$newUrl/") nowSubUrl = originalUrl.replace(Regex("https://[^/]+/"), "$newUrl/")
} }
val reloadLock = Semaphore(10) val reloadLock = Semaphore(10)
@ -345,7 +341,7 @@ class ChangeNodeActivity : BaseActivity(), View.OnClickListener {
testRuning = true testRuning = true
updateMoreBar("测速中") updateMoreBar("测速中")
launch(Dispatchers.IO) { launch(Dispatchers.IO) {
var group = reloadLock.withPermit {clashBinder?.let { cb -> var group = reloadLock.withPermit { UIMainActivity.clashBinder?.let { cb ->
var names = cb.queryProxyGroupNames(false) var names = cb.queryProxyGroupNames(false)
if (names.isNotEmpty()) { if (names.isNotEmpty()) {
cb.healthCheck(names[0]) cb.healthCheck(names[0])
@ -435,18 +431,18 @@ class ChangeNodeActivity : BaseActivity(), View.OnClickListener {
private fun createProfile() { private fun createProfile() {
launch(Dispatchers.IO) { launch(Dispatchers.IO) {
profileBinder?.let { pro -> UIMainActivity.profileBinder?.let { pro ->
uuid?.let { uu -> uuid?.let { uu ->
pro.delete(uu) pro.delete(uu)
delay(500) delay(500)
setSub2(pro, subUrl) setSub2(pro, UIMainActivity.subUrl)
} }
} }
} }
} }
public suspend fun setSub2(pm: IProfileManager, url: String?) { public suspend fun setSub2(pm: IProfileManager, url: String?) {
com.cncat.vpn.common.log.Log.d("setSub2.... url==>$url") // com.cncat.vpn.common.log.Log.d("setSub2.... url==>$url")
if (TextUtils.isEmpty(url)) { if (TextUtils.isEmpty(url)) {
com.cncat.vpn.common.log.Log.d("setSub2 url isEmpty") com.cncat.vpn.common.log.Log.d("setSub2 url isEmpty")
return return
@ -495,9 +491,9 @@ class ChangeNodeActivity : BaseActivity(), View.OnClickListener {
var isCreate: Boolean = false var isCreate: Boolean = false
private var timerJob: Job? = null private var timerJob: Job? = null
private fun startTimer() { private fun startTimer() {
GetNodeTimeOutNum = 10 GetNodeTimeOutNum = 15
timerJob = launch { timerJob = launch {
repeat(11) { repeat(16) {
Log.d("GetNodeTimeOutNum==>$GetNodeTimeOutNum") Log.d("GetNodeTimeOutNum==>$GetNodeTimeOutNum")
if (GetNodeTimeOutNum == 0) { if (GetNodeTimeOutNum == 0) {
hideLoading() hideLoading()
@ -532,6 +528,7 @@ class ChangeNodeActivity : BaseActivity(), View.OnClickListener {
// com.cncat.vpn.common.log.Log.d("tryButton currentHost==>${currentHost}") // com.cncat.vpn.common.log.Log.d("tryButton currentHost==>${currentHost}")
var oldUrl = nowSubUrl var oldUrl = nowSubUrl
currentHost?.let { changeSubUrl(oldUrl, it) } currentHost?.let { changeSubUrl(oldUrl, it) }
doProfile() doProfile()
} }

View File

@ -196,13 +196,33 @@ class UIMainActivity : BaseActivity() {
} }
data.data.let { url -> data.data.let { url ->
android.util.Log.e("Data", "机场URL=${url}") android.util.Log.e("Data", "机场URL=${url}")
// if (url.isNotEmpty()) {
// subUrl = url
// lifecycleScope.launch(Dispatchers.IO) {
// setSub2(url)
// }
// }
if (url.isNotEmpty()) { if (url.isNotEmpty()) {
//todo 判断订阅地址是否修改过,未修改就用接口返回的地址反之使用本地修改的地址
var pro_sub_url =
MMKV.defaultMMKV().decodeString(BZYConstants.PROFILE_SUB_URL, "")
// Log.d("pro_sub_url==>${pro_sub_url}")
if (!TextUtils.isEmpty(pro_sub_url)){
if (pro_sub_url == url) {
subUrl = url subUrl = url
} else {
subUrl = pro_sub_url
}
}else{
subUrl = url
}
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
setSub2(url) // setSub(url)
subUrl?.let { setSub2(it) }
} }
} }
} }
} }
HttpReqType.GetActivity -> { HttpReqType.GetActivity -> {
@ -391,6 +411,23 @@ class UIMainActivity : BaseActivity() {
pro?.let { pro?.let {
pm.setActive(it) pm.setActive(it)
delay(500) delay(500)
//判断配置文件source
if (!TextUtils.isEmpty(pro.source)) {
Log.d("setSub2 pro.source==>${pro.source}, url==>${url}")
//订阅链接不同就更换
if (pro.source != url) {
pm.patch(
pro.uuid, pro.name,
url, 1800000L
)
pm.commit(pro.uuid) { status ->
android.util.Log.d(
Conf.TAG,
"new updateStatus: " + Gson().toJson(status.args) + ", pro==>" + status.progress
)
}
}
}
pm.update(it.uuid) pm.update(it.uuid)
newUuid = pro.uuid.toString() newUuid = pro.uuid.toString()
isSubbed = true isSubbed = true

View File

@ -5,6 +5,7 @@ import android.app.Activity
import android.content.Context import android.content.Context
import android.content.DialogInterface import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.text.TextUtils
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -17,6 +18,7 @@ import com.cncat.vpn.common.log.Log
import com.tencent.mmkv.MMKV import com.tencent.mmkv.MMKV
import com.yingmao.clash.R import com.yingmao.clash.R
import com.yingmao.clash.act.UIMainActivity
import com.yingmao.clash.conf.BZYConstants import com.yingmao.clash.conf.BZYConstants
import com.yingmao.clash.entity.DomainItemBean import com.yingmao.clash.entity.DomainItemBean
@ -116,16 +118,32 @@ open class DialogDoMainChoose : RxDialog {
domainListAdapter.selectedPosition domainListAdapter.selectedPosition
domainListAdapter.setOnItemClickListener { baseQuickAdapter: BaseQuickAdapter<*, *>, view: View, position: Int -> domainListAdapter.setOnItemClickListener { baseQuickAdapter: BaseQuickAdapter<*, *>, view: View, position: Int ->
(baseQuickAdapter.getItem(position) as DomainItemBean).let { bean -> (baseQuickAdapter.getItem(position) as DomainItemBean).let { bean ->
if (!TextUtils.isEmpty(bean.address)) {
MMKV.defaultMMKV().encode(BZYConstants.MMKV_KEY_CURRENT_HOST, bean.address) MMKV.defaultMMKV().encode(BZYConstants.MMKV_KEY_CURRENT_HOST, bean.address)
if (!TextUtils.isEmpty(UIMainActivity.subUrl)) {
var beanUrl=changeSubUrl(UIMainActivity.subUrl!!, bean.address)
UIMainActivity.subUrl=beanUrl
MMKV.defaultMMKV().encode(
BZYConstants.PROFILE_SUB_URL,
beanUrl
)
}
}
domainListAdapter.notifyItemChanged(domainListAdapter.selectedPosition) domainListAdapter.notifyItemChanged(domainListAdapter.selectedPosition)
domainListAdapter.selectedPosition = domainListAdapter.selectedPosition =
if (domainListAdapter.selectedPosition == position) -1 else position if (domainListAdapter.selectedPosition == position) -1 else position
domainListAdapter.notifyItemChanged(position) domainListAdapter.notifyItemChanged(position)
this.onTryListener.tryButton() this.onTryListener.tryButton()
} }
} }
} }
private fun changeSubUrl(originalUrl: String, newUrl: String): String {
return originalUrl.replace(Regex("https://[^/]+/"), "$newUrl/")
}
interface OnTryButtonListener { interface OnTryButtonListener {
fun tryButton() fun tryButton()
fun cancel() fun cancel()