Skip to content

Commit

Permalink
Fallback to the currently selected server when it's not possible to d…
Browse files Browse the repository at this point in the history
…etect the fastest server.

#273
  • Loading branch information
stenya committed Apr 20, 2023
1 parent 96794b4 commit 975debb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
11 changes: 6 additions & 5 deletions ui/src/daemon-client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1304,12 +1304,13 @@ async function Connect() {
console.error(e);
}

// NOTE: in case if not possible to ping - we will have exception here (next line will not be executed)
// Surround 'PingServers()' in try/catch if it is necessary to continue anyway

// fastestServer returns: the server with the lowest latency
// (looking for the active servers that have latency info)
// If there is no latency info for any server:
// - return the nearest server (if geolocation info is known)
// - else: return the currently selected server (if applicable)
// - else: return the first server in the list (as a fallback)
fastest = store.getters["vpnState/fastestServer"];
// if fastest ping == null - it means no any ping info available (e.g. communication blocked by firewall or no internet connectivity)
// Anyway, we have to use the server calculated by 'vpnState/fastestServer' as fastest
}
if (fastest != null) store.dispatch("settings/serverEntry", fastest);
} else if (store.getters["settings/isRandomServer"]) {
Expand Down
15 changes: 14 additions & 1 deletion ui/src/store/module-vpn-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ export default {
isAntitrackerHardcoreEnabled: (state) => {
return isAntitrackerHardcoreActive(state);
},

// fastestServer returns: the server with the lowest latency
// (looking for the active servers that have latency info)
// If there is no latency info for any server:
// - return the nearest server (if geolocation info is known)
// - else: return the currently selected server (if applicable)
// - else: return the first server in the list (as a fallback)
fastestServer(state, getters, rootState) {
let servers = getActiveServers(state, rootState);
if (servers == null || servers.length <= 0) return null;
Expand All @@ -302,6 +309,10 @@ export default {
return gatewayName.split(".")[0];
};

let selectedGwId = rootState.settings.serverEntry
? getGatewayId(rootState.settings.serverEntry.gateway)
: null;

const funcGetPing = getters["funcGetPing"];
for (let i = 0; i < servers.length; i++) {
let curSvr = servers[i];
Expand All @@ -311,7 +322,8 @@ export default {
const curGwID = getGatewayId(curSvr.gateway);
if (skipSvrs.find((ss) => curGwID == getGatewayId(ss))) continue;

if (!fallbackSvr) fallbackSvr = curSvr;
if (!fallbackSvr && selectedGwId === curGwID) fallbackSvr = curSvr;

const svrPing = funcGetPing(curSvr);
if (
svrPing &&
Expand All @@ -322,6 +334,7 @@ export default {
retSvrPing = svrPing;
}
}
if (!fallbackSvr) fallbackSvr = servers[0];

if (!retSvr) {
// No fastest server detected (due to no ping info available)
Expand Down

0 comments on commit 975debb

Please sign in to comment.