Skip to content

Commit

Permalink
Added Auto Pick Instance #949
Browse files Browse the repository at this point in the history
  • Loading branch information
ManeraKai committed Aug 26, 2024
1 parent 8828f1c commit dd4b741
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/pages/icons/AutoPickIcon.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<svg
{...$$restProps}
xmlns="http://www.w3.org/2000/svg"
height="20px"
viewBox="0 -960 960 960"
width="20px"
fill="currentColor"
><path
d="M144-144v-178l342-342-54-53 51-51 72 72 110.22-110.22q4.45-4.45 11.11-7.11Q683-816 691-816t15 2.5q7 2.5 12 7.5l87 88q4.55 5.83 7.27 12.64 2.73 6.8 2.73 14.58t-2.66 14.44q-2.67 6.66-7.11 11.1L696-556l72 73-51 51-54-54-341 342H144Zm72-72h76l320-320-75-76-321 320v76Zm424-385 90-91-38-39-91 90 39 40Zm0 0-39-40 39 40Z"
/></svg
>
37 changes: 33 additions & 4 deletions src/pages/options_src/Services/Instances.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import AddIcon from "../../icons/AddIcon.svelte"
import { options, config } from "../stores"
import PingIcon from "../../icons/PingIcon.svelte"
import AutoPickIcon from "../../icons/AutoPickIcon.svelte"
import Row from "../../components/Row.svelte"
import Input from "../../components/Input.svelte"
import Label from "../../components/Label.svelte"
Expand Down Expand Up @@ -36,10 +37,9 @@
allInstances = []
if (_options[selectedFrontend]) allInstances.push(..._options[selectedFrontend])
if (redirects && redirects[selectedFrontend]) {
for (const network in redirects[selectedFrontend]) {
allInstances.push(...redirects[selectedFrontend][network])
}
allInstances.push(...redirects[selectedFrontend]["clearnet"])
}
allInstances = [...new Set(allInstances)]
}
let pingCache
Expand All @@ -64,6 +64,31 @@
pingCache[instance] = colorTime(time)
}
}
function randomInstances(n) {
let instances = []
for (let i = 0; i < n; i++) {
instances.push(redirects[selectedFrontend]["clearnet"][Math.floor(Math.random() * allInstances.length)])
}
return instances
}
async function autoPickInstance() {
const instances = randomInstances(5)
const myInstancesCache = []
for (const instance of instances) {
pingCache[instance] = { color: "lightblue", value: "pinging..." }
const time = await utils.ping(instance)
pingCache[instance] = colorTime(time)
myInstancesCache.push([instance, time])
}
myInstancesCache.sort(function (a, b) {
return a[1] - b[1]
})
_options[selectedFrontend].push(myInstancesCache[0][0])
options.set(_options)
}
function colorTime(time) {
let value
let color
Expand Down Expand Up @@ -102,11 +127,15 @@
{#if serviceConf.frontends[selectedFrontend].instanceList && redirects && blacklist}
<hr />

<div class="ping">
<div>
<Button on:click={pingInstances}>
<PingIcon class="margin margin_{document.body.dir}" />
{browser.i18n.getMessage("pingInstances") || "Ping Instances"}
</Button>
<Button on:click={autoPickInstance}>
<AutoPickIcon class="margin margin_{document.body.dir}" />
{browser.i18n.getMessage("autoPickInstance") || "Auto Pick Instance"}
</Button>
</div>

<Row>
Expand Down

0 comments on commit dd4b741

Please sign in to comment.