|
@@ -37,12 +37,14 @@ import android.net.ConnectivityManager;
|
|
|
import android.net.Network;
|
|
|
import android.net.NetworkCapabilities;
|
|
|
import android.net.NetworkRequest;
|
|
|
+import android.net.TrafficStats;
|
|
|
import android.net.VpnService;
|
|
|
import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.Handler;
|
|
|
import android.os.IBinder;
|
|
|
import android.os.ParcelFileDescriptor;
|
|
|
+import android.os.SystemClock;
|
|
|
import android.preference.PreferenceManager;
|
|
|
import android.security.KeyChain;
|
|
|
import android.security.KeyChainException;
|
|
@@ -60,6 +62,7 @@ import com.vpn.fastestvpnservice.beans.TvEnableApps;
|
|
|
import com.vpn.fastestvpnservice.constants.AppConstant;
|
|
|
import com.vpn.fastestvpnservice.constants.AppEnum;
|
|
|
import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper;
|
|
|
+import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.HomeScreenKt;
|
|
|
import com.vpn.fastestvpnservice.widgets.SimpleAppWidget;
|
|
|
|
|
|
import org.strongswan.android.data.VpnProfile;
|
|
@@ -96,6 +99,8 @@ import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Locale;
|
|
|
import java.util.SortedSet;
|
|
|
+import java.util.Timer;
|
|
|
+import java.util.TimerTask;
|
|
|
import java.util.TreeSet;
|
|
|
|
|
|
import de.blinkt.openvpn.core.App;
|
|
@@ -141,6 +146,12 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
|
|
|
private volatile boolean mShowNotification;
|
|
|
private Handler mHandler;
|
|
|
private VpnStateService mService;
|
|
|
+
|
|
|
+ private long lastUpdateTime;
|
|
|
+ private long lastTxBytes;
|
|
|
+ private long lastRxBytes;
|
|
|
+
|
|
|
+ public static boolean isVpnActive = false;
|
|
|
private final ServiceConnection mServiceConnection = new ServiceConnection() {
|
|
|
@Override
|
|
|
public void onServiceDisconnected(ComponentName name) { /* since the service is local this is theoretically only called when the process is terminated */
|
|
@@ -245,7 +256,7 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
|
|
|
profile.setVpnType(VpnType.IKEV2_EAP);
|
|
|
profile.setName(server_name);
|
|
|
|
|
|
-
|
|
|
+ startTrafficMonitoring();
|
|
|
|
|
|
UiModeManager uiModeManager = (UiModeManager) getApplicationContext().getSystemService(UI_MODE_SERVICE);
|
|
|
List<String> splitList = Arrays.asList("All apps use the VPN", "Only allow selected apps to use the vpn",
|
|
@@ -332,6 +343,74 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
|
|
|
return START_NOT_STICKY;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+ private void monitorVpnTraffic() {
|
|
|
+ long currentTxBytes = TrafficStats.getUidTxBytes(android.os.Process.myUid());
|
|
|
+ long currentRxBytes = TrafficStats.getUidRxBytes(android.os.Process.myUid());
|
|
|
+
|
|
|
+ Log.d("test_network_stat_IKev2", "MVT: currentTxBytes = " + currentTxBytes + " currentRxBytes = " + currentRxBytes);
|
|
|
+
|
|
|
+ long elapsedTime = SystemClock.elapsedRealtime() - lastUpdateTime;
|
|
|
+ long txBytes = currentTxBytes - lastTxBytes;
|
|
|
+ long rxBytes = currentRxBytes - lastRxBytes;
|
|
|
+
|
|
|
+ Log.d("test_network_stat_IKev2", "MVT: elapsedTime = " + elapsedTime + " txBytes = " + txBytes + " rxBytes = " + rxBytes);
|
|
|
+
|
|
|
+ // Calculate VPN traffic statistics here
|
|
|
+
|
|
|
+ String StringDown = "", StringUp = "";
|
|
|
+ if (txBytes < 1000) {
|
|
|
+ StringUp = txBytes + " byte/s";
|
|
|
+ } else if ((txBytes >= 1000) && (txBytes <= 1000_000)) {
|
|
|
+ StringUp = txBytes / 1000 + " kb/s";
|
|
|
+ } else {
|
|
|
+ StringUp = txBytes / 1000_000 + " mb/s";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (rxBytes < 1000) {
|
|
|
+ StringDown = rxBytes + " byte/s";
|
|
|
+ } else if ((rxBytes >= 1000) && (rxBytes <= 1000_000)) {
|
|
|
+ StringDown = rxBytes / 1000 + " kb/s";
|
|
|
+ } else {
|
|
|
+ StringDown = rxBytes / 1000_000 + " mb/s";
|
|
|
+ }
|
|
|
+
|
|
|
+ Log.d("test_stat_IKev2_DU", "Down: " + StringDown + " Up: " + StringUp);
|
|
|
+ HomeScreenKt.getNetworkSpeed().setNetworkSpeed(StringDown, StringUp);
|
|
|
+
|
|
|
+ // Update last update time and byte counts for next calculation
|
|
|
+ lastUpdateTime = SystemClock.elapsedRealtime();
|
|
|
+ lastTxBytes = currentTxBytes;
|
|
|
+ lastRxBytes = currentRxBytes;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void startTrafficMonitoring() {
|
|
|
+
|
|
|
+ lastUpdateTime = SystemClock.elapsedRealtime();
|
|
|
+ lastTxBytes = TrafficStats.getUidTxBytes(android.os.Process.myUid());
|
|
|
+ lastRxBytes = TrafficStats.getUidRxBytes(android.os.Process.myUid());
|
|
|
+
|
|
|
+ Log.d("test_network_stat_IKev2", "STM: lastUpdateTime = " + lastUpdateTime + " lastTxBytes = " + lastTxBytes + " lastRxBytes = " + lastRxBytes);
|
|
|
+
|
|
|
+ // Start monitoring using a timer or background thread
|
|
|
+ // For example:
|
|
|
+ BasePreferenceHelper sessionManager = new BasePreferenceHelper(this);
|
|
|
+ Timer timer = new Timer();
|
|
|
+ timer.schedule(new TimerTask() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+// Log.d("test_network_stats_DU", "isVpnActive: " + isVpnActive);
|
|
|
+ if (isVpnActive) {
|
|
|
+ monitorVpnTraffic();
|
|
|
+ } else {
|
|
|
+ if (sessionManager.getConnectState() == 0) {
|
|
|
+ cancel();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, 0, 1000); // Update every 1 second
|
|
|
+ }
|
|
|
@Override
|
|
|
public void onCreate() {
|
|
|
mLogFile = getFilesDir().getAbsolutePath() + File.separator + LOG_FILE;
|
|
@@ -646,6 +725,8 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
|
|
|
|
|
|
Intent in2 = new Intent(ACTION_VPN_DISABLED);
|
|
|
LocalBroadcastManager.getInstance(getBaseContext()).sendBroadcast(in2);
|
|
|
+
|
|
|
+ CharonVpnService.isVpnActive = false;
|
|
|
}
|
|
|
|
|
|
if (mShowNotification) {
|
|
@@ -662,6 +743,7 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
|
|
|
getApplicationContext().sendBroadcast(widgetIntent);
|
|
|
|
|
|
sessionManager.setConnectState(App.CONNECTED);
|
|
|
+ isVpnActive = true;
|
|
|
|
|
|
} else if (mService.getState() == State.DISCONNECTING) {
|
|
|
Intent in = new Intent(ACTION_VPN_DISCONNECTED);
|
|
@@ -670,6 +752,7 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
|
|
|
Intent in2 = new Intent(ACTION_VPN_NOT_CONNECTED);
|
|
|
LocalBroadcastManager.getInstance(getBaseContext()).sendBroadcast(in2);
|
|
|
|
|
|
+ CharonVpnService.isVpnActive = false;
|
|
|
} else if (mService.getState() == State.CONNECTING) {
|
|
|
Intent in = new Intent(ACTION_VPN_CONNECTING);
|
|
|
LocalBroadcastManager.getInstance(getBaseContext()).sendBroadcast(in);
|