geo-mirror

Форк
0
/
worker.js 
71 строка · 2.0 Кб
1
/**
2
 * getArchMirror
3
 * @param {string} country
4
 * @returns Promise<string>
5
 */
6
export async function getArchMirror(country) {
7
    const src = await fetch('https://archlinux.org/mirrorlist/?ip_version=4', {
8
		  cf: {
9
				cacheTtl: 3600, // 1 hour
10
				cacheEverything: true
11
			}
12
		}).then(r => r.text())
13
    const re = /Server = (https?:\/\/.*)\/\$repo\/os\/\$arch/g
14
    const mirrors = {}
15
    let match
16
    while (match = re.exec(src)) {
17
        const url = new URL(match[1])
18
        const tld = url.hostname.split('.').pop()
19
        mirrors[tld] = url.toString()
20
    }
21
    if (country.toLowerCase() in mirrors) {
22
      return mirrors[country.toLowerCase()]
23
    } else {
24
      return 'https://geo.mirror.pkgbuild.com'
25
    }
26
}
27

28

29
/**
30
 * getDebianMirror
31
 * @param {string} country
32
 * @returns Promise<string>
33
 */
34
export function getDebianMirror(country) {
35
    return Promise.resolve(`https://ftp.${country.toLowerCase()}.debian.org`)
36
}
37

38
/**
39
 * getUbuntuMirror
40
 * @param {string} country
41
 * @returns Promise<string>
42
 */
43
export function getUbuntuMirror(country) {
44
  return Promise.resolve(`https://${country.toLowerCase()}.archive.ubuntu.com`)
45
}
46

47
export default {
48
  /**
49
   * fetch
50
   * @param {Request} request
51
   * @returns {Promise<Response>}
52
   */
53
  async fetch(request) {
54
    const url = new URL(request.url);
55
    const country = /** @type {string} */ (request.cf.country);
56
    const pathname =url.pathname.split('/').slice(2).join('/')
57
    if (url.pathname.startsWith('/archlinux')) {
58
      const redirect = `${await getArchMirror(country)}/${pathname}`
59
      return Response.redirect(redirect);
60
    }
61
    if (url.pathname.startsWith('/ubuntu')) {
62
      const redirect = `${await getUbuntuMirror(country)}/${pathname}`
63
      return Response.redirect(redirect)
64
    }
65
    if (url.pathname.startsWith('/debian')) {
66
      const redirect = `${await getDebianMirror(country)}/${pathname}`
67
      return Response.redirect(redirect);
68
    }
69
		return Response.redirect('https://github.com/tani/geo-mirror')
70
  }
71
};
72

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.