|
10 | 10 | <link rel="icon" href="https://searx.space/favicon.svg" type="image/svg+xml"> |
11 | 11 | <link rel="apple-touch-icon" href="https://searx.space/favicon.png"> |
12 | 12 | <title>SearXNG Random Instance Redirector</title> |
13 | | -<style>code { background-color: rgba(128, 128, 128, 0.4); padding: 0.1rem 0.25rem 0.1rem 0.25rem }</style> |
| 13 | +<style> |
| 14 | +code { background-color: rgba(128, 128, 128, 0.4); padding: 0.1rem 0.25rem 0.1rem 0.25rem } |
| 15 | +.pale { color: #808080 } |
| 16 | +</style> |
14 | 17 | </head> |
15 | 18 | <body> |
16 | 19 | <script type="module"> |
17 | 20 | import { detectAll } from '{{ site.url }}/assets/js/vendor/tinyld.min.js'; |
18 | 21 |
|
19 | | -const MIN_MONTH_UPTIME = 95; |
20 | | -const FALLBACKS = ['paulgo.io', 'search.im-in.space', 'search.ononoki.org', 'search.undertale.uk', 'searx.bndkt.io', 'searx.oloke.xyz']; |
21 | | - |
22 | | -const params = new URLSearchParams(window.location.search); |
23 | | - |
24 | | -const loadInstances = async () => { |
25 | | - const fallbacks = FALLBACKS.map(i => `https://${i}/`); |
26 | | - if (params.get('fast') === '1') return fallbacks; |
| 22 | +const INSTANCES = ['search.charliewhiskey.net', 'search.im-in.space', 'search.ipsys.bf', 'search.mycotrip.tech', 'search.ononoki.org', 'search.pollorebozado.com', 'search.rowie.at', 'search.system51.co.uk', 'search.undertale.uk', 'search.url4irl.com', 'searx.bndkt.io', 'searx.dresden.network', 'searx.foss.family', 'searx.oloke.xyz', 'searx.ox2.fr', 'searx.party', 'searx.perennialte.ch', 'searx.tiekoetter.com', 'searx.zhenyapav.com', 'searxng.shreven.org'] |
| 23 | +const LANGUAGES = ['en']; |
| 24 | +const TIMEOUT = 5000; |
| 25 | +const ATTEMPTS = 10; |
| 26 | +const CUSTOM_PARAMS = ['attempts', 'languages', 'fast', 'instances', 'timeout']; |
| 27 | + |
| 28 | +const loadAndShuffleInstances = async params => { |
| 29 | + const attempts = Number(params.get('attempts')) || ATTEMPTS; |
| 30 | + const instancesFromParams = params.get('instances')?.split(',') || []; |
| 31 | + const instances = shuffle((instancesFromParams.length > 0 ? instancesFromParams : INSTANCES)) |
| 32 | + .slice(0, attempts) |
| 33 | + .map(i => { |
| 34 | + const protocol = i.endsWith('.onion') ? 'http' : 'https'; |
| 35 | + return `${protocol}://${i}/`; |
| 36 | + }); |
| 37 | + if (params.get('fast') === '1') return instances; |
27 | 38 |
|
28 | 39 | const response = await fetch('https://searx.space/data/instances.json'); |
29 | 40 | if (!response.ok) { |
30 | | - console.log('fallback'); |
31 | | - return fallbacks; |
| 41 | + console.log('failed to load instances'); |
| 42 | + return instances; |
32 | 43 | } |
33 | 44 |
|
34 | | - const instances = (await response.json()).instances; |
35 | | - console.log('instances', instances); |
| 45 | + const loadedInstances = (await response.json()).instances; |
| 46 | + console.log('instances', loadedInstances); |
36 | 47 |
|
37 | 48 | const onion = window.location.host.endsWith('.onion'); |
38 | 49 | const best = Object |
39 | | - .entries(instances) |
| 50 | + .entries(loadedInstances) |
40 | 51 | .filter(([u, i]) => { |
41 | 52 | try { |
42 | 53 | const url = new URL(u); |
43 | 54 | if (onion) { |
44 | | - if (i.network_type !== 'tor') return false; |
| 55 | + if (i?.network_type !== 'tor') return false; |
45 | 56 | } else { |
46 | | - if (i.network_type !== 'normal' || url.protocol !== 'https:' || i.tls.grade !== 'A+') return false; |
| 57 | + if (i?.network_type !== 'normal' || url.protocol !== 'https:' || i?.tls?.grade !== 'A+' || i?.html?.grade !== 'V') return false; |
47 | 58 | } |
48 | | - return i.uptime.uptimeMonth >= MIN_MONTH_UPTIME && i.uptime.uptimeDay === 100 && !i.analytics && i.engines.duckduckgo?.error_rate < 2 && i.engines.google?.error_rate < 2; |
| 59 | + return !i?.analytics; |
49 | 60 | } catch(e) { |
| 61 | + console.error(e); |
50 | 62 | return false; |
51 | 63 | } |
52 | 64 | }) |
53 | | - .map(([u, _]) => u); |
| 65 | + .map(([u, i]) => { |
| 66 | + const key = [i?.uptime?.uptimeDay, i?.uptime?.uptimeMonth, 1 / i?.engines?.google?.error_rate, 1 / i?.engines?.duckduckgo?.error_rate]; |
| 67 | + return [u, key]; |
| 68 | + }) |
| 69 | + .sort(([_a, a], [_b, b]) => { |
| 70 | + const index = a.findIndex((_, i) => (a[i] || 0) !== (b[i] || 0)); |
| 71 | + if (index === -1) return 0; |
| 72 | + return b[index] - a[index]; |
| 73 | + }) |
| 74 | + .map(([u, _]) => u) |
| 75 | + .slice(0, attempts); |
54 | 76 |
|
55 | 77 | console.log('best', best); |
56 | | - return best; |
| 78 | + if (best.length === 0) { |
| 79 | + console.log('instances not found'); |
| 80 | + return instances; |
| 81 | + } |
| 82 | + return shuffle(best); |
57 | 83 | }; |
58 | 84 |
|
59 | | -const detectLanguage = query => { |
| 85 | +const detectLanguage = (query, params) => { |
60 | 86 | const en = 'en'; |
61 | | - const allowedLanguages = params.get('allowed_languages'); |
62 | | - const only = allowedLanguages ? allowedLanguages.split(',') : [en]; |
| 87 | + const allowedLanguages = params.get('languages'); |
| 88 | + const only = allowedLanguages ? allowedLanguages.split(',') : LANGUAGES; |
63 | 89 | console.log('allowed languages', only); |
64 | 90 | if (only.length <= 1) return en; |
65 | 91 |
|
|
74 | 100 | return detected[0].lang; |
75 | 101 | }; |
76 | 102 |
|
77 | | -const redirect = (url, queryFromForm) => { |
| 103 | +const shuffle = xs => { |
| 104 | + for (let i = xs.length - 1; i > 0; i--) { |
| 105 | + let j = Math.floor(Math.random() * (i + 1)); |
| 106 | + [xs[i], xs[j]] = [xs[j], xs[i]]; |
| 107 | + } |
| 108 | + return xs; |
| 109 | +}; |
| 110 | + |
| 111 | +const redirect = async (params, queryFromForm) => { |
| 112 | + document.body.innerHTML = '<div id="message" class="pale"></div>'; |
| 113 | + const messageElement = document.querySelector('#message'); |
| 114 | + |
| 115 | + const shuffledInstances = await loadAndShuffleInstances(params); |
| 116 | + console.log('will be trying instances', shuffledInstances); |
| 117 | + |
| 118 | + const targetUrl = new URL(shuffledInstances[0]); |
| 119 | + targetUrl.pathname = '/search'; |
| 120 | + targetUrl.searchParams.set('safesearch', params.get('safesearch') || '0'); |
| 121 | + |
78 | 122 | params |
79 | 123 | .entries() |
80 | | - .filter(([k, _]) => k !== 'fast' && k !== 'allowed_languages') |
81 | | - .forEach(([k, v]) => url.searchParams.set(k, v)); |
82 | | - url.searchParams.set('language', detectLanguage(params.get('q') || queryFromForm)); |
83 | | - if (queryFromForm) { |
84 | | - url.searchParams.set('q', queryFromForm) |
85 | | - window.location.href = url.toString(); |
| 124 | + .filter(([k, _]) => !CUSTOM_PARAMS.includes(k)) |
| 125 | + .forEach(([k, v]) => targetUrl.searchParams.set(k, v)); |
| 126 | + |
| 127 | + const query = queryFromForm || params.get('q') || ''; |
| 128 | + targetUrl.searchParams.set('language', detectLanguage(query, params)); |
| 129 | + targetUrl.searchParams.set('q', query); |
| 130 | + |
| 131 | + const timeout = Number(params.get('timeout')) || TIMEOUT; |
| 132 | + for (let i = 0; i < shuffledInstances.length; i++) setTimeout(_ => { |
| 133 | + window.stop(); |
| 134 | + const retryTargetUrl = new URL(targetUrl.toString()); |
| 135 | + retryTargetUrl.host = new URL(shuffledInstances[i]).host; |
| 136 | + if (i > 0) { |
| 137 | + messageElement.innerHTML = `Attempt ${i + 1}/${shuffledInstances.length}: ${retryTargetUrl.host}`; |
| 138 | + } |
| 139 | + const href = retryTargetUrl.toString(); |
| 140 | + console.log('trying', href); |
| 141 | + if (queryFromForm) { |
| 142 | + window.location.href = href; |
| 143 | + } else { |
| 144 | + window.location.replace(href); |
| 145 | + } |
| 146 | + }, i * timeout); |
| 147 | + |
| 148 | + setTimeout(_ => messageElement.innerHTML = 'Failed', shuffledInstances.length * timeout); |
| 149 | +}; |
| 150 | + |
| 151 | +const main = _ => { |
| 152 | + const params = new URLSearchParams(window.location.hash.split('#')[1] || ''); |
| 153 | + if (params.has('q')) { |
| 154 | + redirect(params); |
86 | 155 | } else { |
87 | | - window.location.replace(url.toString()); |
| 156 | + const pageUrl = window.location.origin + window.location.pathname; |
| 157 | + document.body.innerHTML = `<p>This page redirects to a <a rel="noopener noreferrer nofollow" href="${pageUrl}#q=" onclick="window.location.replace('${pageUrl}#q=')">random SearXNG</a> instance.</p><p>It also supports the <a rel="noopener noreferrer nofollow" target="_blank" href="https://docs.searxng.org/dev/search_api.html">GET parameters</a> (provided as an anchor for privacy) and the optional parameters:<p><p><ul><li><code>languages=en,fr,ru</code> to use automatic language filter derived from the query; default is <code>${LANGUAGES.join(',')}</code></li><li><code>fast=1</code> to force use of predefined instances (instead of fetching them using the <a rel="noopener noreferrer nofollow" target="_blank" href="https://searx.space/data/instances.json">API</a>); default is <code>0</code></li><li><code>instances=search.ononoki.org,searx.bndkt.io</code> to predefine the instances; default is <code>${INSTANCES.join(',')}</code></li><li><code>timeout</code> in milliseconds until attempting the next instance; default is <code>${TIMEOUT}</code></li><li><code>attempts</code> how many instances to try; default is <code>${ATTEMPTS}</code></li></ul></p><p>Example for <strong>browser search engine settings</strong> (<code>about:preferences#search</code> or <code>chrome://settings/search</code>): <code>${pageUrl}#q=%s&fast=1&image_proxy=True&categories=images&languages=en,fr,ru</code>.</p><p>For local use: <code>wget ${pageUrl} -O searxng.html</code>.</p><p>Additionally can be combined with the Violentmonkey <a rel="noopener noreferrer nofollow" target="_blank" href="https://userscripts.codonaft.com/searxng-redirect-on-failure.js">userscript</a> to turn search failures into redirects as well.</p>`; |
| 158 | + |
| 159 | + const input = document.createElement('input'); |
| 160 | + input.name = 'q'; |
| 161 | + input.placeholder = 'Search for...'; |
| 162 | + input.autocomplete = 'off'; |
| 163 | + input.autocapitalize = 'none'; |
| 164 | + input.spellcheck = false; |
| 165 | + input.autocorrect = 'off'; |
| 166 | + input.dir = 'auto'; |
| 167 | + input.style.width = '99%'; |
| 168 | + |
| 169 | + const form = document.createElement('form'); |
| 170 | + form.addEventListener('submit', event => { |
| 171 | + event.preventDefault(); |
| 172 | + redirect(params, input.value); |
| 173 | + }); |
| 174 | + |
| 175 | + form.appendChild(input); |
| 176 | + document.body.appendChild(form); |
88 | 177 | } |
89 | 178 | }; |
90 | 179 |
|
91 | | -const pick = xs => xs[Math.floor(Math.random() * xs.length)]; |
92 | | -const targetUrl = new URL(pick(await loadInstances())); |
93 | | -targetUrl.pathname = '/search'; |
94 | | -targetUrl.searchParams.set('safesearch', params.get('safesearch') || '0'); |
95 | | - |
96 | | -if (params.has('q')) { |
97 | | - redirect(targetUrl); |
98 | | -} else { |
99 | | - const pageUrl = window.location.origin + window.location.pathname; |
100 | | - document.body.innerHTML = `<p>Redirect to <a rel="noopener noreferrer nofollow" href="${pageUrl}?q=">random SearXNG</a> instance</p><p>This page accepts SearXNG <a target="_blank" href="https://docs.searxng.org/dev/search_api.html">GET parameters</a> and optional parameters:<p><p><ul><li><code>fast=1</code> for hardcoded instances</li><li><code>allowed_languages=en,fr,ru</code> for automatic language filter derived from the query</li></ul></p><p>Example for browser search engine settings (<code>about:preferences#search</code> or <code>chrome://settings/search</code>): <code>${pageUrl}?q=%s&fast=1&image_proxy=True&categories=images</code></p><p>Use this page locally for the best performance: <code>wget ${pageUrl} -O searxng.html</code></p><p>Additionally can be combined with the Violentmonkey <a target="_blank" href="https://userscripts.codonaft.com/searxng-redirect-on-failure.js">userscript</a> to handle search errors</p>`; |
101 | | - |
102 | | - const input = document.createElement('input'); |
103 | | - input.name = 'q'; |
104 | | - input.placeholder = 'Search for...'; |
105 | | - input.autocomplete = 'off'; |
106 | | - input.autocapitalize = 'none'; |
107 | | - input.spellcheck = false; |
108 | | - input.autocorrect = 'off'; |
109 | | - input.dir = 'auto'; |
110 | | - input.style.width = '99%'; |
111 | | - |
112 | | - const form = document.createElement('form'); |
113 | | - form.addEventListener('submit', event => { |
114 | | - event.preventDefault(); |
115 | | - redirect(targetUrl, input.value); |
116 | | - }); |
117 | | - |
118 | | - form.appendChild(input); |
119 | | - document.body.appendChild(form); |
120 | | -} |
| 180 | +window.addEventListener('hashchange', main); |
| 181 | +main(); |
| 182 | + |
121 | 183 | </script> |
122 | 184 | </body> |
123 | 185 | </html> |
0 commit comments