-
Notifications
You must be signed in to change notification settings - Fork 5
/
pacman-accel
executable file
·53 lines (47 loc) · 1.69 KB
/
pacman-accel
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
#!/usr/bin/ruby
#
# A simple local redirector for pacman, to get you the latest packages and
# utilize available mirrors.
#
# Usage:
# - Set multiple mirrors in /etc/pacman.d/mirrorlist-accel with ordering:
# https://fastest-mirror-but-updates-once-a-day/archlinux/
# https://relatively-slower-mirror-that-updates-more-frequently/archlinux/
# ...
# https://pkgbuild-dot-com-or-another-mirror-that-gives-you-the-latest-packages/
#
# - Set /etc/pacman.d/mirrorlist to this redirector:
# Server = http://127.0.0.1:4567/$repo/os/$arch
require 'http'
require 'sinatra'
config = ENV.fetch("PACMAN_ACCEL_CONF", "/etc/pacman.d/mirrorlist-accel")
mirrors = File.readlines(config).filter_map { |line| line.strip if line && line[0] != "#" }
get '/*' do |path|
# Set TIER 0/1 mirrors as the last one, for:
# - DB syncing
# - Download fallback
# These two use cases always the same server for consistency.
mirror = mirrors[-1]
unless path.end_with? '.db'
# Find a faster mirror with the requested file present
mirrors[..-2].each { |m|
begin
response = HTTP.head(m + path)
rescue => e
logger.error "skipping mirror #{m}, failed to HEAD: #{e}"
next
end
if response.status == 200
mirror = m
break
else
logger.info "skipping #{m} for #{path}, code: #{response.status}"
end
}
end
logger.info "redirecting to #{mirror + path}"
redirect mirror + path, 302
end
set :bind, ENV.fetch("PACMAN_ACCEL_BIND", "127.0.0.1")
set :port, ENV.fetch("PACMAN_ACCEL_PORT", "4567")
set :host_authorization, { permitted_hosts: [] }