feat: Add Back Disable Proxy When Vpn Enabled

This commit is contained in:
xtaodada 2022-10-14 21:31:51 +08:00
parent cf31615891
commit cb5621bc6d
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
4 changed files with 66 additions and 13 deletions

View File

@ -35,6 +35,7 @@ import android.location.Location;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.location.LocationManager; import android.location.LocationManager;
import android.media.AudioManager; import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
@ -192,7 +193,9 @@ import tw.nekomimi.nekogram.proxy.SubInfo;
import tw.nekomimi.nekogram.proxy.SubManager; import tw.nekomimi.nekogram.proxy.SubManager;
import tw.nekomimi.nekogram.utils.AlertUtil; import tw.nekomimi.nekogram.utils.AlertUtil;
import tw.nekomimi.nekogram.utils.MonetHelper; import tw.nekomimi.nekogram.utils.MonetHelper;
import tw.nekomimi.nekogram.utils.ProxyUtil;
import tw.nekomimi.nekogram.utils.UIUtil; import tw.nekomimi.nekogram.utils.UIUtil;
import xyz.nextalone.nagram.NaConfig;
public class LaunchActivity extends BasePermissionsActivity implements ActionBarLayout.ActionBarLayoutDelegate, NotificationCenter.NotificationCenterDelegate, DialogsActivity.DialogsActivityDelegate { public class LaunchActivity extends BasePermissionsActivity implements ActionBarLayout.ActionBarLayoutDelegate, NotificationCenter.NotificationCenterDelegate, DialogsActivity.DialogsActivityDelegate {
public final static Pattern PREFIX_T_ME_PATTERN = Pattern.compile("^(?:http(?:s|)://|)([A-z0-9-]+?)\\.t\\.me"); public final static Pattern PREFIX_T_ME_PATTERN = Pattern.compile("^(?:http(?:s|)://|)([A-z0-9-]+?)\\.t\\.me");
@ -5161,6 +5164,16 @@ public class LaunchActivity extends BasePermissionsActivity implements ActionBar
passcodeView.onResume(); passcodeView.onResume();
} }
if (NaConfig.INSTANCE.getDisableProxyWhenVpnEnabled().Bool()) {
if (SharedConfig.proxyEnabled && ProxyUtil.isVPNEnabled()) {
SharedConfig.setProxyEnable(false);
} else if (!ProxyUtil.isVPNEnabled()) {
SharedConfig.setProxyEnable(true);
}
ProxyUtil.registerNetworkCallback();
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.proxySettingsChanged);
}
ConnectionsManager.getInstance(currentAccount).setAppPaused(false, false); ConnectionsManager.getInstance(currentAccount).setAppPaused(false, false);
updateCurrentConnectionState(currentAccount); updateCurrentConnectionState(currentAccount);

View File

@ -120,6 +120,7 @@ public class NekoGeneralSettingsActivity extends BaseFragment {
private final AbstractConfigCell hideProxyByDefaultRow = cellGroup.appendCell(new ConfigCellTextCheck(NekoConfig.hideProxyByDefault)); private final AbstractConfigCell hideProxyByDefaultRow = cellGroup.appendCell(new ConfigCellTextCheck(NekoConfig.hideProxyByDefault));
private final AbstractConfigCell autoUpdateSubInfoRow = cellGroup.appendCell(new ConfigCellTextCheck(NekoConfig.autoUpdateSubInfo)); private final AbstractConfigCell autoUpdateSubInfoRow = cellGroup.appendCell(new ConfigCellTextCheck(NekoConfig.autoUpdateSubInfo));
private final AbstractConfigCell useSystemDNSRow = cellGroup.appendCell(new ConfigCellTextCheck(NekoConfig.useSystemDNS)); private final AbstractConfigCell useSystemDNSRow = cellGroup.appendCell(new ConfigCellTextCheck(NekoConfig.useSystemDNS));
private final AbstractConfigCell disableProxyWhenVpnEnabledRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getDisableProxyWhenVpnEnabled()));
private final AbstractConfigCell customDoHRow = cellGroup.appendCell(new ConfigCellTextInput(null, NekoConfig.customDoH, "https://1.0.0.1/dns-query", null)); private final AbstractConfigCell customDoHRow = cellGroup.appendCell(new ConfigCellTextInput(null, NekoConfig.customDoH, "https://1.0.0.1/dns-query", null));
private final AbstractConfigCell customPublicProxyIPRow = cellGroup.appendCell(new ConfigCellTextDetail(NekoConfig.customPublicProxyIP, (view, position) -> { private final AbstractConfigCell customPublicProxyIPRow = cellGroup.appendCell(new ConfigCellTextDetail(NekoConfig.customPublicProxyIP, (view, position) -> {
customDialog_BottomInputString(position, NekoConfig.customPublicProxyIP, LocaleController.getString("customPublicProxyIPNotice"), "IP"); customDialog_BottomInputString(position, NekoConfig.customPublicProxyIP, LocaleController.getString("customPublicProxyIPNotice"), "IP");

View File

@ -13,6 +13,10 @@ import android.content.pm.PackageManager
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Color import android.graphics.Color
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.Build import android.os.Build
import android.os.Environment import android.os.Environment
import android.util.Base64 import android.util.Base64
@ -43,35 +47,64 @@ import org.json.JSONException
import org.telegram.messenger.* import org.telegram.messenger.*
import org.telegram.messenger.browser.Browser import org.telegram.messenger.browser.Browser
import org.yaml.snakeyaml.Yaml import org.yaml.snakeyaml.Yaml
import tw.nekomimi.nekogram.ui.BottomBuilder
import tw.nekomimi.nekogram.proxy.ShadowsocksLoader import tw.nekomimi.nekogram.proxy.ShadowsocksLoader
import tw.nekomimi.nekogram.proxy.ShadowsocksRLoader import tw.nekomimi.nekogram.proxy.ShadowsocksRLoader
import tw.nekomimi.nekogram.ui.BottomBuilder
import tw.nekomimi.nekogram.utils.AlertUtil.showToast import tw.nekomimi.nekogram.utils.AlertUtil.showToast
import java.io.File import java.io.File
import java.net.NetworkInterface
import java.util.* import java.util.*
import kotlin.collections.HashMap
object ProxyUtil { object ProxyUtil {
@JvmStatic @JvmStatic
fun isVPNEnabled(): Boolean { fun isVPNEnabled(): Boolean {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
val networkList = mutableListOf<String>() return false
}
runCatching { runCatching {
val connectivityManager = ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
Collections.list(NetworkInterface.getNetworkInterfaces()).forEach { val network = connectivityManager.activeNetwork
val networkCapabilities = connectivityManager.getNetworkCapabilities(network)
if (it.isUp) networkList.add(it.name) return networkCapabilities!!.hasTransport(NetworkCapabilities.TRANSPORT_VPN)
}
return false
} }
@JvmStatic
fun registerNetworkCallback() {
val connectivityManager = ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkCallback: ConnectivityManager.NetworkCallback =
object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
val networkCapabilities = connectivityManager.getNetworkCapabilities(network)
val vpn = networkCapabilities!!.hasTransport(NetworkCapabilities.TRANSPORT_VPN)
if (!vpn) {
if (SharedConfig.currentProxy == null) {
if (!SharedConfig.proxyList.isEmpty()) {
SharedConfig.setCurrentProxy(SharedConfig.proxyList[0])
} else {
return
}
}
}
if ((SharedConfig.proxyEnabled && vpn) || (!SharedConfig.proxyEnabled && !vpn)) {
SharedConfig.setProxyEnable(!vpn)
UIUtil.runOnUIThread(Runnable {
NotificationCenter.getGlobalInstance()
.postNotificationName(NotificationCenter.proxySettingsChanged)
})
}
}
} }
return networkList.contains("tun0") if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
connectivityManager.registerDefaultNetworkCallback(networkCallback)
} else {
val request: NetworkRequest = NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build()
connectivityManager.registerNetworkCallback(request, networkCallback)
}
} }
@JvmStatic @JvmStatic

View File

@ -328,6 +328,12 @@ object NaConfig {
ConfigItem.configTypeBool, ConfigItem.configTypeBool,
false false
) )
val disableProxyWhenVpnEnabled =
addConfig(
"DisableProxyWhenVpnEnabled",
ConfigItem.configTypeBool,
false
)
private fun addConfig( private fun addConfig(
k: String, k: String,