Explorar o código

Worked on Auto connect for OpenVPN and IKEv2 functionality

Khubaib hai 6 meses
pai
achega
fb0aa1f650

+ 1 - 1
.idea/deploymentTargetSelector.xml

@@ -4,7 +4,7 @@
     <selectionStates>
       <SelectionState runConfigName="app">
         <option name="selectionMode" value="DROPDOWN" />
-        <DropdownSelection timestamp="2024-10-17T12:26:20.989073229Z">
+        <DropdownSelection timestamp="2024-10-22T14:50:16.381154658Z">
           <Target type="DEFAULT_BOOT">
             <handle>
               <DeviceId pluginId="PhysicalDevice" identifier="serial=1C051FDF60048Z" />

+ 0 - 11
.idea/other.xml

@@ -326,17 +326,6 @@
           <option name="screenX" value="1080" />
           <option name="screenY" value="2424" />
         </PersistentDeviceSelectionData>
-        <PersistentDeviceSelectionData>
-          <option name="api" value="29" />
-          <option name="brand" value="samsung" />
-          <option name="codename" value="x1q" />
-          <option name="id" value="x1q" />
-          <option name="manufacturer" value="Samsung" />
-          <option name="name" value="Galaxy S20" />
-          <option name="screenDensity" value="480" />
-          <option name="screenX" value="1440" />
-          <option name="screenY" value="3200" />
-        </PersistentDeviceSelectionData>
       </list>
     </option>
   </component>

+ 6 - 2
app/src/main/AndroidManifest.xml

@@ -165,7 +165,8 @@
         <receiver
             android:name=".widgets.SimpleAppWidget"
             android:exported="false"
-            android:permission="android.permission.BIND_VPN_SERVICE">
+            android:permission="android.permission.BIND_VPN_SERVICE"
+            >
             <intent-filter>
                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
             </intent-filter>
@@ -206,7 +207,9 @@
         <receiver
             android:name=".utils.WifiScanReceiver"
             android:enabled="true"
-            android:exported="true">
+            android:exported="true"
+            android:permission="android.permission.BIND_VPN_SERVICE"
+            >
             <intent-filter>
                 <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
                 <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
@@ -226,6 +229,7 @@
             android:name=".utils.AutoConnectService"
             android:enabled="true"
             android:exported="true"
+            android:permission="android.permission.BIND_VPN_SERVICE"
             android:foregroundServiceType="specialUse" />
 
         <service

+ 45 - 23
app/src/main/java/com/vpn/fastestvpnservice/utils/AutoConnectService.kt

@@ -124,6 +124,8 @@ class AutoConnectService : Service(), VpnStatus.StateListener {
 
     override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
 
+        Log.d("AutoConnectService", "onStartCommand")
+
         startForegroundNotification()
         startListenerIKEV2()
 
@@ -134,7 +136,7 @@ class AutoConnectService : Service(), VpnStatus.StateListener {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
             val channelId = "FastestVPN_Foreground_Wifi"
             val channel =
-                NotificationChannel(channelId, "FastestVPN", NotificationManager.IMPORTANCE_LOW)
+                NotificationChannel(channelId, "FastestVPN", NotificationManager.IMPORTANCE_HIGH)
             val notificationManager = getSystemService(
                 NotificationManager::class.java
             )
@@ -147,7 +149,7 @@ class AutoConnectService : Service(), VpnStatus.StateListener {
                 .setContentText("Auto-connect feature will automatically detect network changes as set in the app settings.")
                 .setColor(applicationContext.resources.getColor(R.color.blue_text))
                 .setSmallIcon(R.drawable.ic_logo_notify)
-            startForeground(1, notification.build())
+            startForeground(10, notification.build())
         }
     }
 
@@ -266,6 +268,17 @@ class AutoConnectService : Service(), VpnStatus.StateListener {
                     else if (!isTrustedWifiConnected && isConnect == App.DISCONNECTED) {
                         Log.d("AutoConnectService", "Connect VPN")
                         startVpn()
+
+
+//                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+//                            Log.d("wg test s fore", "startForegroundService")
+//
+//                            applicationContext.startForegroundService(Intent(applicationContext, WireGuardService::class.java))
+//                        }
+//                        else{
+//                            Log.d("wg test s serv", "startService")
+//                            applicationContext.startService(Intent(applicationContext, WireGuardService::class.java))
+//                        }
                     }
                     else if (isTrustedWifiConnected && isConnect == App.CONNECTING) {
                         Log.d("AutoConnectService", "Connecting VPN")
@@ -302,7 +315,7 @@ class AutoConnectService : Service(), VpnStatus.StateListener {
         countDownTimer()
         try {
             basePreferenceHelper = BasePreferenceHelper(applicationContext)
-            Log.d("auto_conn startVpn", App.connection_status.toString())
+            Log.d("auto_conn startVpn", "startVpn() " + App.connection_status.toString())
             if (CheckInternetConnection.getInternetConnection(applicationContext).isConnectedToInternet) {
                 if (basePreferenceHelper.getProtocol().title.toLowerCase()
                         .contentEquals(AppEnum.WG_PROTOCOL.key.toLowerCase())
@@ -314,10 +327,14 @@ class AutoConnectService : Service(), VpnStatus.StateListener {
                 else if (basePreferenceHelper.getProtocol().title.contentEquals(AppEnum.IKEV2_PROTOCOL.key))
                 {
 
+                    Log.d("auto_conn startVpn", "IKEv2 Connection Outside")
+
                     basePreferenceHelper.getConnectedServer().let { server ->
 
+
+                        Log.d("auto_conn startVpn", "IKEv2 Connection Inside")
+
                         /*Connect IKEV2*/
-                        Log.d("auto_conn onstart", "IKEv2 Connection")
 
 //                            startListenerIKEV2()
 //                        App.createIKEV2Listener()
@@ -357,7 +374,7 @@ class AutoConnectService : Service(), VpnStatus.StateListener {
     fun startTcpUDP() {
         Log.d("auto_conn startVpn", "startTcpUDP()")
 
-        countDownTimer?.start()
+//        countDownTimer?.start()
 
         /*Connect UDP,TCP*/
         App.connection_status = App.CONNECTION_STATE_CONNECTING
@@ -498,10 +515,13 @@ class AutoConnectService : Service(), VpnStatus.StateListener {
                     .contentEquals(AppEnum.WG_PROTOCOL.key.toLowerCase())
             )
             {
+                Log.d("auto_conn stopVpn", "vpnWireGuardPermission stop")
                 vpnWireGuardPermission(false)
             }
             else if (basePreferenceHelper.getProtocol().title.contentEquals(AppEnum.IKEV2_PROTOCOL.key))
             {
+                Log.d("auto_conn stopVpn", "IKEv2 stop")
+
                 /*Disconnect IKEV2*/
                 val intent = Intent(applicationContext, VpnProfileControlActivity::class.java)
                 intent.action = VpnProfileControlActivity.DISCONNECT
@@ -520,6 +540,8 @@ class AutoConnectService : Service(), VpnStatus.StateListener {
 
 //                applicationContext.unbindService(mConnection)
 
+                Log.d("auto_conn stopVpn", "OpenVPN stop")
+
                 Log.d("auto_conn msr mcon", App.mService.toString() + " " + mConnection.toString())
 
                 /*Disconnect TCP,UDP*/
@@ -554,19 +576,19 @@ class AutoConnectService : Service(), VpnStatus.StateListener {
                 }
             }
 
-            val widgetIntent = Intent(applicationContext, SimpleAppWidget::class.java)
-            widgetIntent.action = SimpleAppWidget.ACTION_STOP_SERVICE
-            applicationContext.sendBroadcast(widgetIntent)
-
-            Handler().postDelayed({
-                try {
-                    LocalBroadcastManager.getInstance(applicationContext)
-                        .unregisterReceiver(ikevConnectionStatesReceiverAutoConnect)
-
-                } catch (e: Exception) {
-                    e.printStackTrace()
-                }
-            }, 500)
+//            val widgetIntent = Intent(applicationContext, SimpleAppWidget::class.java)
+//            widgetIntent.action = SimpleAppWidget.ACTION_STOP_SERVICE
+//            applicationContext.sendBroadcast(widgetIntent)
+//
+//            Handler().postDelayed({
+//                try {
+//                    LocalBroadcastManager.getInstance(applicationContext)
+//                        .unregisterReceiver(ikevConnectionStatesReceiverAutoConnect)
+//
+//                } catch (e: Exception) {
+//                    e.printStackTrace()
+//                }
+//            }, 500)
 
         } catch (e: Exception) {
             e.printStackTrace()
@@ -784,7 +806,7 @@ class AutoConnectService : Service(), VpnStatus.StateListener {
 
 //                        basePreferenceHelper.setConnectState(App.CONNECTED)
                         val connectState1 = basePreferenceHelper.getConnectState()
-                        Log.d("auto_conn ip w", "ACTION_VPN_CONNECTED widget $connectState $connectState1")
+//                        Log.d("auto_conn ip w", "ACTION_VPN_CONNECTED widget $connectState $connectState1")
 //                        if (connectState1 == 2) {
 //                            val widgetIntent = Intent(applicationContext, SimpleAppWidget::class.java)
 //                            widgetIntent.action = SimpleAppWidget.ACTION_CONNECT_VPN
@@ -792,7 +814,7 @@ class AutoConnectService : Service(), VpnStatus.StateListener {
 //                        }
                     }
                     CharonVpnService.ACTION_VPN_DISABLED -> {
-                        Log.d("auto_conn ip w", "ACTION_VPN_DISABLED widget $connectState")
+//                        Log.d("auto_conn ip w", "ACTION_VPN_DISABLED widget $connectState")
 
                         if (connectState == App.CONNECTING)
                         {
@@ -822,11 +844,11 @@ class AutoConnectService : Service(), VpnStatus.StateListener {
                         }
                     }
                     CharonVpnService.ACTION_VPN_NOT_CONNECTED -> {
-                        Log.d("auto_conn ip w", "ACTION_VPN_NOT_CONNECTED widget $connectState")
+//                        Log.d("auto_conn ip w", "ACTION_VPN_NOT_CONNECTED widget $connectState")
                     }
                     CharonVpnService.ACTION_VPN_CONNECTING -> {
                         val connectState1 = basePreferenceHelper.getConnectState()
-                        Log.d("auto_conn ip w", "ACTION_VPN_CONNECTING widget $connectState1")
+//                        Log.d("auto_conn ip w", "ACTION_VPN_CONNECTING widget $connectState1")
                         App.connection_status = App.CONNECTION_STATE_CONNECTING
                         MainActivity.vpnConnectionCallBacks?.onVpnConnecting()
 
@@ -837,7 +859,7 @@ class AutoConnectService : Service(), VpnStatus.StateListener {
                         applicationContext?.sendBroadcast(widgetIntent)
                     }
                     CharonVpnService.ACTION_VPN_SERVER_NOT_RESPONDING -> {
-                        Log.d("auto_conn ip w", "ACTION_VPN_SERVER_NOT_RESPONDING widget")
+//                        Log.d("auto_conn ip w", "ACTION_VPN_SERVER_NOT_RESPONDING widget")
                         App.connection_status = App.CONNECTION_STATE_SERVER_NOT_RESPONDING
                         if (basePreferenceHelper.getProtocol().index == 0)
                         {

+ 4 - 0
app/src/main/java/com/vpn/fastestvpnservice/utils/WifiScanReceiver.java

@@ -41,10 +41,14 @@ public class WifiScanReceiver extends BroadcastReceiver {
 
             AutoConnectService.LocalBinder binder = (AutoConnectService.LocalBinder) service;
             autoConnectService = binder.getService();
+
+            // currently commenting...
             isBound = true;
 
             autoConnectService.getConnectedWifiService();
 
+//            autoConnectService.startVpn();
+
             if (isBound) {
                 context.unbindService(this);
                 isBound = false;

+ 5 - 1
app/src/main/java/com/vpn/fastestvpnservice/utils/WireGuardService.kt

@@ -124,17 +124,20 @@ class WireGuardService : Service(), VpnStatus.StateListener {
         countDownTimer()
         try {
             basePreferenceHelper = BasePreferenceHelper(applicationContext)
-            Log.d("wg test s startVpn", App.connection_status.toString())
+            Log.d("wg_test_s startVpn","startVpn: " + App.connection_status.toString())
             if (CheckInternetConnection.getInternetConnection(applicationContext).isConnectedToInternet) {
                 if (basePreferenceHelper.getProtocol().title.toLowerCase()
                         .contentEquals(AppEnum.WG_PROTOCOL.key.toLowerCase())
                 ) {
+                    Log.d("wg_test_s startVpn","WG: " + App.connection_status.toString())
+
                     /*Connect Wire-Guard*/
                     MainActivity.isWGDown = true
                     vpnWireGuardPermission(true)
                 }
                 else if (basePreferenceHelper.getProtocol().title.contentEquals(AppEnum.IKEV2_PROTOCOL.key))
                 {
+                    Log.d("wg_test_s startVpn","IKEv2: " + App.connection_status.toString() + basePreferenceHelper.getConnectedServer())
 
                     basePreferenceHelper.getConnectedServer().let { server ->
 
@@ -168,6 +171,7 @@ class WireGuardService : Service(), VpnStatus.StateListener {
                     /* setting receiver to listen ikev2 protocol connection states */
                 }
                 else {
+                    Log.d("wg_test_s startVpn","OpenVPN: " + App.connection_status.toString() + basePreferenceHelper.getConnectedServer())
 
                     startTcpUDP()
                 }

+ 1 - 1
app/src/main/java/org/strongswan/android/ui/VpnProfileControlActivity.java

@@ -91,7 +91,7 @@ public class VpnProfileControlActivity extends AppCompatActivity {
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
-        Log.d("test sync ikev2", "onCreate VpnProfileControlActivity");
+        Log.d("auto_conn ikev2", "onCreate VpnProfileControlActivity");
 
         if (savedInstanceState != null) {
             mWaitingForResult = savedInstanceState.getBoolean(WAITING_FOR_RESULT, false);