about summary refs log tree commit diff stats
path: root/src/pages/options_src/Services/Instances.svelte
blob: b37c39048b677398cf741c138c0c02f05224cdc6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<script>
  const browser = window.browser || window.chrome

  import Button from "../../components/Button.svelte"
  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"
  import CloseIcon from "../../icons/CloseIcon.svelte"
  import { onDestroy, onMount } from "svelte"
  import utils from "../../../assets/javascripts/utils"

  export let selectedService
  export let selectedFrontend

  let _options
  let _config

  const unsubscribeOptions = options.subscribe(val => (_options = val))
  const unsubscribeConfig = config.subscribe(val => (_config = val))
  onDestroy(() => {
    unsubscribeOptions()
    unsubscribeConfig()
  })

  let blacklist
  let redirects

  $: serviceConf = _config.services[selectedService]

  let allInstances = []

  $: {
    allInstances = []
    if (_options[selectedFrontend]) allInstances.push(..._options[selectedFrontend])
    if (redirects && redirects[selectedFrontend]) {
      allInstances.push(...redirects[selectedFrontend]["clearnet"])
    }
    allInstances = [...new Set(allInstances)]
  }

  let pingCache
  $: {
    if (pingCache) browser.storage.local.set({ pingCache })
  }

  function isCustomInstance(instance) {
    if (redirects[selectedFrontend]) {
      for (const network in redirects[selectedFrontend]) {
        if (redirects[selectedFrontend][network].includes(instance)) return false
      }
    }
    return true
  }

  function colorTime(time) {
    let value
    let color
    if (time < 5000) {
      value = `${time}ms`
      if (time <= 1000) color = "green"
      else if (time <= 2000) color = "orange"
    } else if (time >= 5000) {
      color = "red"
      if (time == 5000) value = "5000ms+"
      if (time > 5000) value = `Error: ${time - 5000}`
    } else {
      color = "red"
      value = "Server not found"
    }
    return { color, value }
  }

  onMount(async () => {
    blacklist = await utils.getBlacklist(_options)
    redirects = await utils.getList(_options)
    pingCache = await utils.getPingCache()
  })

  let addInstanceValue
  function addInstance() {
    const instance = utils.protocolHost(new URL(addInstanceValue))
    if (!_options[selectedFrontend].includes(instance)) {
      _options[selectedFrontend].push(instance)
      addInstanceValue = ""
      options.set(_options)
    }
  }

  let autoPicking = false
  let pinging = false
</script>

{#if serviceConf.frontends[selectedFrontend].instanceList && redirects && blacklist}
  <hr />

  <div>
    <Button
      on:click={async () => {
        pinging = true
        pingCache = {}
        for (const instance of allInstances) {
          pingCache[instance] = { color: "lightblue", value: "pinging..." }
          const time = await utils.ping(instance)
          pingCache[instance] = colorTime(time)
        }
        pinging = false
      }}
      disabled={pinging}
    >
      <PingIcon class="margin margin_{document.body.dir}" />
      {browser.i18n.getMessage("pingInstances") || "Ping Instances"}
    </Button>
    <Button
      on:click={async () => {
        autoPicking = true
        const clearnet = redirects[selectedFrontend]["clearnet"]
        for (const instance of _options[selectedFrontend]) {
          const i = clearnet.indexOf(instance)
          if (i >= 0) clearnet.splice(i, 1)
        }
        const instance = await utils.autoPickInstance(clearnet)
        _options[selectedFrontend].push(instance)
        options.set(_options)
        autoPicking = false
      }}
      disabled={autoPicking}
    >
      <AutoPickIcon class="margin margin_{document.body.dir}" />
      {browser.i18n.getMessage("autoPickInstance") || "Auto Pick Instance"}
    </Button>
  </div>

  <Row>
    <Label>{browser.i18n.getMessage("addYourFavoriteInstances") || "Add your favorite instances"}</Label>
  </Row>
  <div dir="ltr">
    <Row>
      <Input
        bind:value={addInstanceValue}
        type="url"
        placeholder="https://instance.com"
        aria-label="Add instance input"
        on:keydown={e => e.key === "Enter" && addInstance()}
      />
      <button on:click={addInstance} class="add" aria-label="Add the instance">
        <AddIcon />
      </button>
    </Row>

    {#each _options[selectedFrontend] as instance}
      <Row>
        <span>
          <a href={instance} target="_blank" rel="noopener noreferrer">{instance}</a>
          {#if isCustomInstance(instance)}
            <span style="color:grey">custom</span>
          {/if}
          {#if pingCache && pingCache[instance]}
            <span style="color:{pingCache[instance].color}">{pingCache[instance].value}</span>
          {/if}
        </span>
        <button
          class="add"
          aria-label="Remove Instance"
          on:click={() => {
            const index = _options[selectedFrontend].indexOf(instance)
            if (index > -1) {
              _options[selectedFrontend].splice(index, 1)
              options.set(_options)
            }
          }}
        >
          <CloseIcon />
        </button>
      </Row>
      <hr />
    {/each}

    {#if redirects !== "disabled" && blacklist !== "disabled"}
      {#if redirects[selectedFrontend] && redirects[selectedFrontend]["clearnet"]}
        {#each Object.entries(_config.networks) as [networkName, network]}
          {#if redirects[selectedFrontend] && redirects[selectedFrontend][networkName] && redirects[selectedFrontend][networkName].length > 0}
            <Row></Row>
            <Row><Label>{network.name}</Label></Row>
            <hr />
            {#each redirects[selectedFrontend][networkName] as instance}
              <Row>
                <span>
                  <a href={instance} target="_blank" rel="noopener noreferrer">{instance}</a>
                  {#if blacklist.cloudflare.includes(instance)}
                    <a
                      href="https://libredirect.github.io/docs.html#instances"
                      target="_blank"
                      rel="noopener noreferrer"
                      style="color:red;"
                    >
                      cloudflare
                    </a>
                  {/if}
                  {#if _options[selectedFrontend].includes(instance)}
                    <span style="color:grey">chosen</span>
                  {/if}
                  {#if pingCache && pingCache[instance]}
                    <span style="color:{pingCache[instance].color}">{pingCache[instance].value}</span>
                  {/if}
                </span>
                {#if !_options[selectedFrontend].includes(instance)}
                  <button
                    class="add"
                    aria-label="Add instance"
                    on:click={() => {
                      if (_options[selectedFrontend]) {
                        _options[selectedFrontend].push(instance)
                        options.set(_options)
                      }
                    }}
                  >
                    <AddIcon />
                  </button>
                {:else}
                  <button
                    class="add"
                    aria-label="Remove Instance"
                    on:click={() => {
                      const index = _options[selectedFrontend].indexOf(instance)
                      if (index > -1) {
                        _options[selectedFrontend].splice(index, 1)
                        options.set(_options)
                      }
                    }}
                  >
                    <CloseIcon />
                  </button>
                {/if}
              </Row>
              <hr />
            {/each}
          {/if}
        {/each}
      {:else}
        <Row><Label>No instances found.</Label></Row>
      {/if}
    {/if}
  </div>
{/if}

<style>
  .add {
    background-color: transparent;
    border: none;
    color: var(--text);
    padding: 0;
    margin: 0;
    text-decoration: none;
    display: inline-block;
    cursor: pointer;
  }

  a {
    color: var(--text);
    text-decoration: none;
    word-wrap: anywhere;
  }

  a:hover {
    text-decoration: underline;
  }

  :global(.margin) {
    margin-right: 10px;
    margin-left: 0;
  }
  :global(.margin_rtl) {
    margin-right: 0;
    margin-left: 10px;
  }
</style>