Skip to content

Commit

Permalink
Fix select node of two processes in sync
Browse files Browse the repository at this point in the history
As proxy only mode is added in 1.4.0, I moved the toggle components to the daemon process
When user start service from toggle, the full config is generated from a cached list.
However, this cache is not in sync with the main process list.
In fact, we don't need to generate full config right before the service start. We only
need to when active node is changed.
This way, code logic and daemon process is kept simple
  • Loading branch information
yuhan6665 committed Dec 5, 2020
1 parent b3074e9 commit 7c7a623
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TaskerReceiver : BroadcastReceiver() {
return
} else if (switch) {
if (guid == AppConfig.TASKER_DEFAULT_GUID) {
Utils.startVService(context)
Utils.startVServiceFromToggle(context)
} else {
Utils.startVService(context, guid)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import com.v2ray.ang.AppConfig.TAG_DIRECT
import com.v2ray.ang.R
import com.v2ray.ang.extension.defaultDPreference
import com.v2ray.ang.extension.toSpeedString
import com.v2ray.ang.extension.toast
import com.v2ray.ang.extension.v2RayApplication
import com.v2ray.ang.ui.MainActivity
import com.v2ray.ang.ui.SettingsActivity
import com.v2ray.ang.util.MessageUtil
Expand Down Expand Up @@ -57,8 +59,13 @@ object V2RayServiceManager {
private var mSubscription: Subscription? = null
private var mNotificationManager: NotificationManager? = null

fun startV2Ray(context: Context, mode: String) {
val intent = if (mode == "VPN") {
fun startV2Ray(context: Context) {
if (context.v2RayApplication.defaultDPreference.getPrefBoolean(SettingsActivity.PREF_PROXY_SHARING, false)) {
context.toast(R.string.toast_warning_pref_proxysharing_short)
}else{
context.toast(R.string.toast_services_start)
}
val intent = if (context.v2RayApplication.defaultDPreference.getPrefString(AppConfig.PREF_MODE, "VPN") == "VPN") {
Intent(context.applicationContext, V2RayVpnService::class.java)
} else {
Intent(context.applicationContext, V2RayProxyOnlyService::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
}
showCircle()
// toast(R.string.toast_services_start)
if (!Utils.startVService(this)) {
if (!Utils.startVService(this, AngConfigManager.configs.index)) {
hideCircle()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,14 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai
} else {
mActivity.showCircle()
Utils.stopVService(mActivity)
AngConfigManager.setActiveServer(position)
Observable.timer(500, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
mActivity.showCircle()
if (!Utils.startVService(mActivity)) {
if (!Utils.startVService(mActivity, position)) {
mActivity.hideCircle()
}
}

}
notifyDataSetChanged()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.View
import com.v2ray.ang.R
import com.v2ray.ang.AppConfig
import com.v2ray.ang.extension.toast
import com.v2ray.ang.util.AngConfigManager
import com.v2ray.ang.util.Utils

class SettingsActivity : BaseActivity() {
Expand Down Expand Up @@ -74,7 +75,7 @@ class SettingsActivity : BaseActivity() {

private fun restartProxy() {
Utils.stopVService(requireContext())
Utils.startVService(requireContext())
Utils.startVService(requireContext(), AngConfigManager.configs.index)
}

private fun isRunning(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.v2ray.ang.util

import android.graphics.Bitmap
import android.text.TextUtils
import android.util.Log
import com.google.gson.Gson
import com.v2ray.ang.AngApplication
import com.v2ray.ang.AppConfig
Expand Down Expand Up @@ -154,6 +155,10 @@ object AngConfigManager {
angConfig.index = index
app.curIndex = index
storeConfigFile()
if (!genStoreV2rayConfig(index)) {
Log.d(AppConfig.ANG_PACKAGE, "set active index $index but generate full configuration failed!")
return -1
}
} catch (e: Exception) {
e.printStackTrace()
app.curIndex = -1
Expand Down Expand Up @@ -586,9 +591,9 @@ object AngConfigManager {
*/
fun shareFullContent2Clipboard(index: Int): Int {
try {
if (AngConfigManager.genStoreV2rayConfig(index)) {
val configContent = app.defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG, "")
Utils.setClipboard(app.applicationContext, configContent)
val result = V2rayConfigUtil.getV2rayConfig(app, angConfig.vmess[index])
if (result.status) {
Utils.setClipboard(app.applicationContext, result.content)
} else {
return -1
}
Expand Down
43 changes: 10 additions & 33 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import android.webkit.URLUtil
import com.v2ray.ang.AngApplication
import com.v2ray.ang.AppConfig
import com.v2ray.ang.R
import com.v2ray.ang.dto.EConfigType
import com.v2ray.ang.extension.defaultDPreference
import com.v2ray.ang.extension.responseLength
import com.v2ray.ang.extension.toast
import com.v2ray.ang.extension.v2RayApplication
Expand All @@ -32,7 +32,6 @@ import kotlinx.coroutines.isActive
import me.dozen.dpreference.DPreference
import java.io.IOException
import java.net.*
import libv2ray.Libv2ray
import kotlin.coroutines.coroutineContext

object Utils {
Expand Down Expand Up @@ -272,38 +271,13 @@ object Utils {
}

fun startVServiceFromToggle(context: Context): Boolean {
val result = startVService(context)
if (!result) {
val result = context.defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG, "")
if (result.isBlank()) {
context.toast(R.string.app_tile_first_use)
}
return result
}

/**
* startVService
*/
fun startVService(context: Context): Boolean {
if (context.v2RayApplication.defaultDPreference.getPrefBoolean(SettingsActivity.PREF_PROXY_SHARING, false)) {
context.toast(R.string.toast_warning_pref_proxysharing_short)
}else{
context.toast(R.string.toast_services_start)
}
if (AngConfigManager.genStoreV2rayConfig(-1)) {
val configContent = AngConfigManager.currGeneratedV2rayConfig()
val configType = AngConfigManager.currConfigType()
if (configType == EConfigType.CUSTOM) {
try {
Libv2ray.testConfig(configContent)
} catch (e: Exception) {
context.toast(e.toString())
return false
}
}
V2RayServiceManager.startV2Ray(context, context.v2RayApplication.defaultDPreference.getPrefString(AppConfig.PREF_MODE, "VPN"))
return true
} else {
return false
}
V2RayServiceManager.startV2Ray(context)
return true
}

/**
Expand All @@ -319,8 +293,11 @@ object Utils {
* startVService
*/
fun startVService(context: Context, index: Int): Boolean {
AngConfigManager.setActiveServer(index)
return startVService(context)
if (AngConfigManager.setActiveServer(index) < 0) {
return false
}
V2RayServiceManager.startV2Ray(context)
return true
}

/**
Expand Down

0 comments on commit 7c7a623

Please sign in to comment.