Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 33 additions & 27 deletions app/src/main/java/org/fptn/vpn/services/vpn/FptnService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.fptn.vpn.enums.PerAppVpnMode;
import org.fptn.vpn.services.tile.FptnTileService;
import org.fptn.vpn.utils.NetworkUtils;
import org.fptn.vpn.utils.NotificationUtils;
import org.fptn.vpn.utils.SharedPrefUtils;
import org.fptn.vpn.views.perappvpn.AppInfo;
import org.fptn.vpn.services.speedtest.SpeedTestUtils;
Expand Down Expand Up @@ -209,30 +208,36 @@ public void onCreate() {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
executorService.submit(() -> {
try {
/* check is internet connection available */
if (!NetworkUtils.isOnline(connectivityManager)) {
Log.e(TAG, "onStartCommand: no active internet connections!");
disconnect(new PVNClientException(ErrorCode.NO_ACTIVE_INTERNET_CONNECTIONS));
return;
}
// if service crashed previously
if (intent == null) {
Log.w(TAG, "onStartCommand: Intent is null, stopping service.");
stopSelf();
return START_NOT_STICKY;
}

// dismiss error notification
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.cancel(Constants.ERROR_CONNECTED_NOTIFICATION_ID);
boolean isActiveState = serviceStateMutableLiveData.getValue().getConnectionState().isActiveState();
if (ACTION_CONNECT.equals(intent.getAction()) && !isActiveState) {
startForegroundWithNotification(getString(R.string.connecting));

String sniHostname = SharedPrefUtils.getSniHostname(getApplicationContext());
BypassCensorshipMethod bypassCensorshipMethod = SharedPrefUtils.getBypassCensorshipMethod(this);
/* check is internet connection available */
if (!NetworkUtils.isOnline(connectivityManager)) {
Log.e(TAG, "onStartCommand: no active internet connections!");
disconnect(new PVNClientException(ErrorCode.NO_ACTIVE_INTERNET_CONNECTIONS));
return START_NOT_STICKY;
}

boolean isActiveState = serviceStateMutableLiveData.getValue().getConnectionState().isActiveState();
if (ACTION_DISCONNECT.equals(intent.getAction()) && isActiveState) {
// stop running threads
disconnect();
} else if (ACTION_CONNECT.equals(intent.getAction()) && !isActiveState) {
executorService.submit(() -> {
try {
setConnectionState(ConnectionState.CONNECTING, null);

// dismiss error notification
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.cancel(Constants.ERROR_CONNECTED_NOTIFICATION_ID);

String sniHostname = SharedPrefUtils.getSniHostname(getApplicationContext());
BypassCensorshipMethod bypassCensorshipMethod = SharedPrefUtils.getBypassCensorshipMethod(this);

int serverId = intent.getIntExtra(SELECTED_SERVER, SELECTED_SERVER_ID_AUTO);

// Process startService from TileService
Expand All @@ -257,9 +262,6 @@ public int onStartCommand(Intent intent, int flags, int startId) {
}
}

// Moving VPNService to foreground to give it higher priority in system
startForegroundWithNotification(getString(R.string.connecting));

if (serverId == SELECTED_SERVER_ID_AUTO) {
try {
updateNotificationWithMessage(getString(R.string.connecting_auto), "");
Expand All @@ -282,11 +284,15 @@ public int onStartCommand(Intent intent, int flags, int startId) {

connect(server, sniHostname);
}
} catch (ExecutionException | InterruptedException | RuntimeException e) {
disconnect(new PVNClientException(e.getMessage()));
}
} catch (ExecutionException | InterruptedException | RuntimeException e) {
disconnect(new PVNClientException(e.getMessage()));
}
});
});

} else if (ACTION_DISCONNECT.equals(intent.getAction()) && isActiveState) {
// stop running threads
disconnect();
}

// if it stops - it stops
return START_NOT_STICKY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void startCheckingPing() {
long startTime = System.currentTimeMillis();
try (Socket socket = new Socket()) {
// Connect with a timeout
socket.connect(new InetSocketAddress(server.getHost(), 443), 5000);
socket.connect(new InetSocketAddress(server.getHost(), server.getPort()), 5000);
server.setPingMs(System.currentTimeMillis() - startTime);

Log.d(TAG, "Ping for host: " + server.getServerInfo() + " ping: " + server.getPingMs() + "ms");
Expand Down