浏览代码

on protocol switching - server is also switch to its selected protocol

Khubaib 1 年之前
父节点
当前提交
3900def50d

二进制
app/release/fvpn_release_beta_1.0.3.apk


+ 5 - 0
app/src/main/java/com/vpn/fastestvpnservice/beans/Server.kt

@@ -33,4 +33,9 @@ class Server(
     var enableServers: Int = 1,
     var distance: Float = 0.0f,
     var ping: Int = 0
+)
+
+class ServerProtocol(
+    val server: Server,
+    val isFound: Boolean
 )

+ 0 - 2
app/src/main/java/com/vpn/fastestvpnservice/helpers/BasePreferenceHelper.kt

@@ -306,8 +306,6 @@ class BasePreferenceHelper(private val context: Context) : PreferencesHelper() {
         putStringPreference(context, KEY_FILENAME, KEY_SERVERS, Gson().toJson(serverObj))
     }
 
-
-
     fun getConnectedServer(): Server? {
         var serverObj: Server? = null
         val serverObjStr: String = getStringPreference(context, KEY_FILENAME, KEY_CONNECTED_SERVER)

+ 2 - 1
app/src/main/java/com/vpn/fastestvpnservice/interfaces/ServerCallbacks.kt

@@ -5,6 +5,7 @@ import androidx.compose.runtime.Composable
 import com.vpn.fastestvpnservice.beans.Protocol
 import com.vpn.fastestvpnservice.beans.Server
 import com.vpn.fastestvpnservice.viewmodels.HomeViewModel
+import com.vpn.fastestvpnservice.viewmodels.ServerListViewModel
 
 interface ServerCallbacks {
     @Composable
@@ -16,5 +17,5 @@ interface ServerCallbacks {
         server: Server
     )
 
-    fun onChangeProtocol(protocol: Protocol, context: Context)
+    fun onChangeProtocol(protocol: Protocol, context: Context, serverListViewModel: ServerListViewModel)
 }

+ 35 - 21
app/src/main/java/com/vpn/fastestvpnservice/screens/bottomNavBarScreens/HomeScreen.kt

@@ -145,14 +145,12 @@ lateinit var vpnConnectionsUtil: VPNConnectionsUtil
         val basePreferenceHelper = BasePreferenceHelper(context)
 
         Log.d("ServerCallbacks", "onServerSelected called!")
-        Log.d("ServerCallbacks", "onServerSelected server = ${server.server_name}")
 
         val lastServer = basePreferenceHelper.getConnectedServer()
         if (isServerDialogShown) {
             Log.d("ServerCallbacks", "onServerSelected true!")
             if (wg.isVPNConnected()) {
                 Log.d("ServerCallbacks", "onServerSelected isVPNConnected!")
-                Log.d("ServerCallbacks", "onServerSelected ${lastServer?.id} ${server?.id}")
                 if (lastServer?.id != server.id) {
                     isServerDialog.value = true
                     serverObj.value = server
@@ -160,7 +158,6 @@ lateinit var vpnConnectionsUtil: VPNConnectionsUtil
                 else {
                     basePreferenceHelper.setConnectedServer(server)
                     basePreferenceHelper.setServerObject(server)
-                    Log.d("isConnect_State", "onServerSelected ${homeViewModel.isConnect.value}")
                 }
             } else {
                 basePreferenceHelper.setConnectedServer(server)
@@ -170,7 +167,6 @@ lateinit var vpnConnectionsUtil: VPNConnectionsUtil
         }
         else {
             wg.startVpn()
-            Log.d("isConnect_State", "onServerSelected ${homeViewModel.isConnect.value}")
         }
          MainActivity.isSelectedServersShown = true
 
@@ -178,16 +174,11 @@ lateinit var vpnConnectionsUtil: VPNConnectionsUtil
 
     }
 
-     override fun onChangeProtocol(protocol: Protocol, context: Context) {
+     override fun onChangeProtocol(protocol: Protocol, context: Context, serverListViewModel: ServerListViewModel) {
          val prefHelper = BasePreferenceHelper(context)
          val wg = VPNConnectionsUtil(context, act , homeViewModel1)
          protocolObj.value = protocol
 
-//         Toast.makeText(
-//             context, "Test", Toast.LENGTH_SHORT
-//         ).show()
-
-         Log.d("OnChangeProtocol", "OnChangeProtocol ${protocol.full_name} ${protocol.title}!")
          if (getEnableProtocols(protocol.title, prefHelper)) {
              Log.d("OnChangeProtocol", "getEnableProtocols yes!")
 
@@ -199,12 +190,23 @@ lateinit var vpnConnectionsUtil: VPNConnectionsUtil
                  }
                  else {
                      prefHelper.saveProtocol(protocol = protocol)
+
+                     val lastServer = prefHelper.getConnectedServer()
+                     val serverList = prefHelper.getServerData()
+
+                     if (lastServer != null) {
+                         val serverProtocol = serverListViewModel.getFilteredServerByProtocolChanged(serverList, lastServer, protocol)
+
+                         if (serverProtocol.isFound) {
+                             prefHelper.setServerObject(serverProtocol.server)
+                             val lastServer1 = prefHelper.getServerObject()
+                         }
+                     }
                  }
              }
 
          }
          else {
-             Log.d("OnChangeProtocol", "getEnableProtocols no! ${navHostController1.currentDestination}")
 //             Screen.Subscription.isTrue = true
              navHostController1.let {
                     it.navigate(Screen.Subscription.route)
@@ -223,7 +225,6 @@ lateinit var vpnConnectionsUtil: VPNConnectionsUtil
          val basePreferenceHelper = BasePreferenceHelper(context)
          if (basePreferenceHelper.getProtocol().index == 0) {
              Log.d("ServerNotResponding", "Auto -> ${wg.isBound}")
-
          } else {
              Log.d("ServerNotResponding", "IKEv2 -> ${wg.isBound}")
              Toast.makeText(
@@ -290,13 +291,11 @@ fun Home(navHostController: NavHostController, activity: ComponentActivity) {
                 var filterServerByConnectionCount = Server()
                 if (prefHelper.getServerObject() != null) {
                     prefHelper.getServerObject()?.let {
-                        Log.d("test_smart_server_n", "server nn = ${it.server_name}")
                         filterServerByConnectionCount = it
                     }
                 } else {
                     val smartServer = basePreferenceHelper.getSmartServerObject()
                     smartServer?.let {
-                        Log.d("test_smart_server_n", "server = ${it.server_name}")
                         filterServerByConnectionCount = it
                     }
                 }
@@ -341,10 +340,6 @@ fun Home(navHostController: NavHostController, activity: ComponentActivity) {
     }
 
     val ip = homeViewModel.mutableLiveDataIpInfo.observeAsState().value
-    ip?.let {
-        Log.d("test_api_response", "ip home = ${it.country}")
-    }
-
     val validateResponse = homeViewModel._mutableLiveDataValidate.observeAsState().value
     validateResponse?.let {
         Log.d("test_api_validate", it.message.toString())
@@ -401,7 +396,8 @@ fun Home(navHostController: NavHostController, activity: ComponentActivity) {
         if (isProtocolDialog.value) {
             ShowProtocolDialog(
                 basePreferenceHelper,
-                vpnConnectionsUtil
+                vpnConnectionsUtil,
+                serverListViewModel
             )
         }
 
@@ -912,7 +908,8 @@ fun BoxScope.ShowServerDialog(
 @Composable
 fun BoxScope.ShowProtocolDialog(
     prefHelper: BasePreferenceHelper,
-    wg: VPNConnectionsUtil
+    wg: VPNConnectionsUtil,
+    serverListViewModel: ServerListViewModel
 ) {
     AlertDialog(
         onDismissRequest = { isProtocolDialog.value = false },
@@ -927,6 +924,23 @@ fun BoxScope.ShowProtocolDialog(
         val oldProtocolTitle = if (selectedProtocol.index == 0) AppEnum.AUTO_PROTOCOL.key else selectedProtocol.title
         val newProtocolTitle = if (protocolObj.value.index == 0) AppEnum.AUTO_PROTOCOL.key else protocolObj.value.title
 
+        val serverList = prefHelper.getServerData()
+        var desiredServer = Server()
+        var isServerFound: Boolean = false
+
+        if (lastServer != null) {
+            val serverProtocol = serverListViewModel.getFilteredServerByProtocolChanged(serverList, lastServer,
+                protocolObj.value)
+
+            if (serverProtocol.isFound) {
+                desiredServer = serverProtocol.server
+                isServerFound = true
+            }
+        }
+
+        Log.d("server_protocol", "Home: $isServerFound ${desiredServer.id} ${desiredServer.server_name} ${desiredServer.ip} ${desiredServer.protocol} ${desiredServer.remoteId}")
+
+
         Surface(
             color = colorResource(id = R.color.white),
             modifier = Modifier
@@ -988,7 +1002,7 @@ fun BoxScope.ShowProtocolDialog(
                     Button(
                         onClick = {
                             Log.d("ServerCallbacks", "Yes")
-                            val server = prefHelper.getConnectedServer()
+                            val server = if (isServerFound) desiredServer else prefHelper.getConnectedServer()
                             prefHelper.setServerObject(server)
                             prefHelper.setConnectedServer(server)
                             prefHelper.saveProtocol(protocol = protocolObj.value)

+ 14 - 8
app/src/main/java/com/vpn/fastestvpnservice/screens/bottomNavBarScreens/SettingsScreen.kt

@@ -138,6 +138,8 @@ fun Settings(navHostController: NavHostController) {
 //        val window = (view.context as Activity).window
 //        window.statusBarColor = colorResource(id = R.color.background_color_gray2).toArgb()
 
+        val context = LocalContext.current
+        val basePreferenceHelper = BasePreferenceHelper(context)
         Column(
             verticalArrangement = Arrangement.Top,
             horizontalAlignment = Alignment.Start,
@@ -401,6 +403,9 @@ fun ColumnScope.AddRowSettingsColumn(
         "Auto", "WireGuard", "IKEv2", "OpenVPN TCP", "OpenVPN UDP"
     )
     var selectedProtocol by remember { mutableStateOf(basePreferenceHelper.getProtocol().full_name) }
+    val serverListViewModel: ServerListViewModel = viewModel{
+        ServerListViewModel(context)
+    }
 
     Log.d("availableProtocols", availableProtocols.toString())
 
@@ -485,6 +490,7 @@ fun ColumnScope.AddRowSettingsColumn(
         }
 
         if (isSheetOpen) {
+
             ModalBottomSheet(
                 onDismissRequest = { isSheetOpen = false },
                 sheetState = sheetState,
@@ -519,7 +525,7 @@ fun ColumnScope.AddRowSettingsColumn(
                                         onClick = {
                                             selectedProtocol = protocol
                                             isSheetOpen = false
-                                            SelectProtocolCallback(protocol = protocol, context)
+                                            SelectProtocolCallback(protocol = protocol, context, serverListViewModel)
                                         },
                                     )
 //                                    .indication(
@@ -535,7 +541,7 @@ fun ColumnScope.AddRowSettingsColumn(
                                     onClick = {
                                         selectedProtocol = protocol
                                         isSheetOpen = false
-                                        SelectProtocolCallback(protocol = protocol, context)
+                                        SelectProtocolCallback(protocol = protocol, context, serverListViewModel)
                                     },
                                     colors = RadioButtonDefaults.colors(
                                         selectedColor = colorResource(id = R.color.radio_button_blue),
@@ -555,27 +561,27 @@ fun ColumnScope.AddRowSettingsColumn(
     }
 }
 
-fun SelectProtocolCallback(protocol: String, context: Context) {
+fun SelectProtocolCallback(protocol: String, context: Context, serverListViewModel: ServerListViewModel) {
     when (protocol) {
         AppEnum.AUTO_PROTOCOL.title -> {
             onServer.onChangeProtocol(
-                Protocol(AppEnum.AUTO_PROTOCOL.title,AppEnum.IKEV2_PROTOCOL.key, 0), context = context)
+                Protocol(AppEnum.AUTO_PROTOCOL.title,AppEnum.IKEV2_PROTOCOL.key, 0), context = context, serverListViewModel)
         }
         AppEnum.WG_PROTOCOL.title -> {
             onServer.onChangeProtocol(
-                Protocol(AppEnum.WG_PROTOCOL.title,AppEnum.WG_PROTOCOL.key, 1), context = context)
+                Protocol(AppEnum.WG_PROTOCOL.title,AppEnum.WG_PROTOCOL.key, 1), context = context, serverListViewModel)
         }
         AppEnum.IKEV2_PROTOCOL.title -> {
             onServer.onChangeProtocol(
-                Protocol(AppEnum.IKEV2_PROTOCOL.title,AppEnum.IKEV2_PROTOCOL.key, 2), context = context)
+                Protocol(AppEnum.IKEV2_PROTOCOL.title,AppEnum.IKEV2_PROTOCOL.key, 2), context = context, serverListViewModel)
         }
         AppEnum.TCP_PROTOCOL.title -> {
             onServer.onChangeProtocol(
-                Protocol(AppEnum.TCP_PROTOCOL.title,AppEnum.TCP_PROTOCOL.key, 3), context = context)
+                Protocol(AppEnum.TCP_PROTOCOL.title,AppEnum.TCP_PROTOCOL.key, 3), context = context, serverListViewModel)
         }
         AppEnum.UDP_PROTOCOL.title -> {
             onServer.onChangeProtocol(
-                Protocol(AppEnum.UDP_PROTOCOL.title,AppEnum.UDP_PROTOCOL.key, 4), context = context)
+                Protocol(AppEnum.UDP_PROTOCOL.title,AppEnum.UDP_PROTOCOL.key, 4), context = context, serverListViewModel)
         }
 
 

+ 1 - 6
app/src/main/java/com/vpn/fastestvpnservice/utils/VPNConnectionsUtil.kt

@@ -25,6 +25,7 @@ import com.google.gson.Gson
 import com.google.gson.reflect.TypeToken
 import com.vpn.fastestvpnservice.MainActivity
 import com.vpn.fastestvpnservice.R
+import com.vpn.fastestvpnservice.beans.Server
 import com.vpn.fastestvpnservice.constants.AppConstant
 import de.blinkt.openvpn.core.App
 import com.vpn.fastestvpnservice.constants.AppEnum
@@ -123,12 +124,7 @@ class VPNConnectionsUtil: VpnStatus.StateListener {
 
                 if (basePreferenceHelper.getProtocol().title.contentEquals(AppEnum.IKEV2_PROTOCOL.key))
                 {
-                    basePreferenceHelper.getConnectedServer().let {
-                        Log.d("getConnectedServer", "s = ${it?.server_name}")
-                    }
-
                     basePreferenceHelper.getConnectedServer().let { server ->
-
                         /*Connect IKEV2*/
 
                         val intent = Intent(context, VpnProfileControlActivity::class.java)
@@ -158,7 +154,6 @@ class VPNConnectionsUtil: VpnStatus.StateListener {
                     MainActivity.isWGDown = true
                     vpnWireGuardPermission(true)
 
-
                 } else {
 
                     /*Connect UDP / TCP*/

+ 31 - 59
app/src/main/java/com/vpn/fastestvpnservice/viewmodels/ServerListViewModel.kt

@@ -11,8 +11,10 @@ import com.google.gson.reflect.TypeToken
 import com.stealthcopter.networktools.Ping
 import com.stealthcopter.networktools.ping.PingResult
 import com.stealthcopter.networktools.ping.PingStats
+import com.vpn.fastestvpnservice.beans.Protocol
 import com.vpn.fastestvpnservice.beans.Server
 import com.vpn.fastestvpnservice.beans.ServerData
+import com.vpn.fastestvpnservice.beans.ServerProtocol
 import com.vpn.fastestvpnservice.beans.ServerResponse
 import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
 import com.vpn.fastestvpnservice.retrofit.RetrofitNetworkHandling
@@ -60,8 +62,6 @@ class ServerListViewModel(context: Context): ViewModel() {
                     android.os.Handler(Looper.getMainLooper()).post {
                         val ping = pingResult?.timeTaken?.toInt()!!
                         onPingResult(ping)
-                        Log.d("test_ping", "ping[0] = $ping")
-                        Log.d("test_ping_stream", "calculatePing: ${ping}")
                     }
                 }
                 override fun onError(e: java.lang.Exception?) {}
@@ -71,37 +71,20 @@ class ServerListViewModel(context: Context): ViewModel() {
     }
 
     fun filterServersByISO(serverlist: ArrayList<Server>, isAlphabetList: Boolean = false): ArrayList<Server> {
-//        Log.d("test_compose_servers", "size = ${serverlist.size}")
-
-        serverlist.forEachIndexed { index, server ->
-            Log.d("serverlist_test", server.server_name + " , " + server.country)
-        }
 
         serverlist.sortWith(Comparator { s1, s2 ->
             return@Comparator s1.country_sort.compareTo(s2.country_sort)
         })
 
-        serverlist.forEachIndexed { index, server ->
-            Log.d("serverlist_test 2", server.server_name + " , " + server.country)
-        }
-
         var serverprotocol = serverlist.filter {
             it.protocol.equals(preferencesHelper.getProtocol().title, ignoreCase = true)
         }
 
-        serverprotocol.forEachIndexed { index, server ->
-            Log.d("serverlist_test p", server.server_name + " , " + server.country)
-        }
-
         var distinctBy: List<Server>
         distinctBy = serverprotocol.distinctBy {
             it.iso
         }
 
-        distinctBy.forEachIndexed { index, server ->
-            Log.d("serverlist_test d", server.server_name + " , " + server.country)
-        }
-
         serverprotocol.groupBy(Server::country).mapValues { entry ->
 
             for ((index, value) in distinctBy.withIndex()) {
@@ -121,13 +104,6 @@ class ServerListViewModel(context: Context): ViewModel() {
 
 //                    distinctBy.get(index).countryServers = entry.value
 
-//                    value.countryServers?.forEachIndexed { indexs1, serverInfo ->
-//                        calculatePing(serverInfo) {
-//                            serverInfo.ping = it
-//                            Log.d("test_return_ping","ISO=> " + serverInfo.server_name + serverInfo.ping)
-//                        }
-//                    }
-
                     if (total_enables >= 1) {
                         distinctBy.get(index).enableServers = 1
                     } else {
@@ -146,47 +122,24 @@ class ServerListViewModel(context: Context): ViewModel() {
             }
         }
 
-//        distinctBy.forEachIndexed { index, server ->
-//            calculatePing(server) {
-//                server.ping = it
-//            }
-//            Log.d("serverlist_test_d2", server.server_name + " , " + server.country + " , " + server.totalServers)
-//        }
-
         return (filterListCountries)
     }
 
     fun filterServersByISOTV(serverlist: ArrayList<Server>): ArrayList<Server> {
 
-        serverlist.forEachIndexed { index, server ->
-            Log.d("serverlist test", server.server_name + " , " + server.country)
-        }
-
         serverlist.sortWith(Comparator { s1, s2 ->
             return@Comparator s1.country_sort.compareTo(s2.country_sort)
         })
 
-        serverlist.forEachIndexed { index, server ->
-            Log.d("serverlist 2 test 2", server.server_name + " , " + server.country)
-        }
-
         var serverprotocol = serverlist.filter {
             it.protocol.equals(preferencesHelper.getProtocol().title, ignoreCase = true)
         }
 
-        serverprotocol.forEachIndexed { index, server ->
-            Log.d("serverprotocol test", server.server_name + " , " + server.country)
-        }
-
         var distinctBy: List<Server>
         distinctBy = serverprotocol.distinctBy {
             it.iso
         }
 
-        distinctBy.forEachIndexed { index, server ->
-            Log.d("distinctBy test", server.server_name + " , " + server.country)
-        }
-
         serverprotocol.groupBy(Server::country).mapValues { entry ->
 
             for ((index, value) in distinctBy.withIndex()) {
@@ -208,10 +161,6 @@ class ServerListViewModel(context: Context): ViewModel() {
             }
         }
 
-        distinctBy.forEachIndexed { index, server ->
-            Log.d("distinctBy_test_enable", server.server_name + " , " + server.enable)
-        }
-
         val enabledServer = distinctBy.filter {
             it.enable == 1
         }
@@ -263,9 +212,7 @@ class ServerListViewModel(context: Context): ViewModel() {
             serverDataList.servers?.let {serverList ->
                 serverList.forEachIndexed { index, server ->
                     calculatePing(server) {
-                        Log.d("test_ping_servers", "${server.server_name} $it")
                         server.ping = it
-                        Log.d("test_ping_data", "${server.server_name} ${server.ping}")
                         serverList.set(index, server)
                     }
                 }
@@ -402,10 +349,6 @@ class ServerListViewModel(context: Context): ViewModel() {
             it.iso
         }
 
-        distinctBy.forEachIndexed { index, server ->
-            Log.d("test server cc", server.server_name.toString() + server.country)
-        }
-
         return distinctBy[0]
     }
 
@@ -450,6 +393,35 @@ class ServerListViewModel(context: Context): ViewModel() {
         return tempList2[0]
     }
 
+    fun getFilteredServerByProtocolChanged(
+        serverList: ArrayList<ServerData>,
+        serverObj: Server,
+        protocol: Protocol
+    ): ServerProtocol {
+//        val protocol = preferencesHelper.getProtocol().title
+        val tempList = ArrayList<Server>()
+        var server = Server()
+        var isServerFound: Boolean = false
+
+        serverList.let {
+            it.forEach {
+                it.servers?.forEach { server ->
+                    tempList.add(server)
+                }
+            }
+        }
+
+        for (i in tempList.indices) {
+            val obj = tempList[i]
+            if (obj.server_name == serverObj.server_name && protocol.title.equals(obj.protocol, ignoreCase = true)) {
+                server = obj
+                isServerFound = true
+            }
+        }
+
+        return ServerProtocol(server, isServerFound)
+    }
+
     fun favAndUnFav(server: Server) {
         updateFavServer(server)
         WebServiceFactory.getInstance().setFavUnfav("${server.id}")

+ 3 - 0
app/src/main/java/org/strongswan/android/logic/CharonVpnService.java

@@ -225,6 +225,8 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
                     String remote_id = bundle.getString("remote_id");
                     String server_name = bundle.getString("server_name");
 
+                    Log.d("test_ikev2_file","ikev2 => " + email + " " + password + " " + profile_id + " " + ip + " " + remote_id + " " + server_name);
+
                     profile = new VpnProfile();
 //					profile.setGateway("45.9.249.110");
 //					profile.setUsername("glistenhanif@gmail.com");
@@ -239,6 +241,7 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
                     profile.setPassword(password);
                     profile.setIkeProposal(VpnType.IKEV2_EAP.toString());
                     profile.setRemoteId(remote_id);
+//                    profile.setRemoteId("jumptoserver.com");
                     profile.setVpnType(VpnType.IKEV2_EAP);
                     profile.setName(server_name);