-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapt-proxy
More file actions
executable file
·288 lines (261 loc) · 9.89 KB
/
apt-proxy
File metadata and controls
executable file
·288 lines (261 loc) · 9.89 KB
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
281
282
283
284
285
286
287
288
#!/usr/bin/env bash
set -euo pipefail
PROXY="http://10.17.89.69:3142/"
WARM_SOURCES_DIR="$(mktemp -d /tmp/acng-warm-sources.XXXXXX)"
cleanup() {
rm -rf "$WARM_SOURCES_DIR"
}
trap cleanup EXIT
rewrite_url() {
local url="$1"
local suffix=""
case "$url" in
https://downloads.cursor.com/aptrepo|https://downloads.cursor.com/aptrepo/*)
suffix="${url#https://downloads.cursor.com/aptrepo}"
suffix="${suffix#/}"
if [ -n "$suffix" ]; then
printf 'http://cursor.cache/%s\n' "$suffix"
else
printf 'http://cursor.cache\n'
fi
;;
https://dl.google.com/linux/chrome/deb|https://dl.google.com/linux/chrome/deb/*)
suffix="${url#https://dl.google.com/linux/chrome/deb}"
suffix="${suffix#/}"
if [ -n "$suffix" ]; then
printf 'http://chrome.cache/%s\n' "$suffix"
else
printf 'http://chrome.cache\n'
fi
;;
https://deb.nodesource.com/node_23.x|https://deb.nodesource.com/node_23.x/*)
suffix="${url#https://deb.nodesource.com/node_23.x}"
suffix="${suffix#/}"
if [ -n "$suffix" ]; then
printf 'http://nodesrc.cache/%s\n' "$suffix"
else
printf 'http://nodesrc.cache\n'
fi
;;
https://deb.packages.mattermost.com|https://deb.packages.mattermost.com/*)
suffix="${url#https://deb.packages.mattermost.com}"
suffix="${suffix#/}"
if [ -n "$suffix" ]; then
printf 'http://matter.cache/%s\n' "$suffix"
else
printf 'http://matter.cache\n'
fi
;;
https://packages.microsoft.com/repos/code|https://packages.microsoft.com/repos/code/*)
suffix="${url#https://packages.microsoft.com/repos/code}"
suffix="${suffix#/}"
if [ -n "$suffix" ]; then
printf 'http://mscode.cache/%s\n' "$suffix"
else
printf 'http://mscode.cache\n'
fi
;;
https://ppa.launchpad.net/*)
suffix="${url#https://ppa.launchpad.net/}"
printf 'http://lp.cache/%s\n' "$suffix"
;;
https://ppa.launchpadcontent.net/*)
suffix="${url#https://ppa.launchpadcontent.net/}"
printf 'http://lp.cache/%s\n' "$suffix"
;;
*)
# For http://apt.pop-os.org/... or anything else:
printf '%s\n' "$url"
;;
esac
}
process_deb822_file() {
local file="$1"
local output_file="$2"
local types=""
local uris=""
local suites=""
local components=""
local architectures=""
local signed_by=""
local enabled=""
local in_stanza=false
local stanza_lines=()
while IFS= read -r line || [ -n "$line" ]; do
# Blank line indicates end of stanza
if [[ -z "$line" || "$line" =~ ^[[:space:]]*$ ]]; then
if [ "$in_stanza" = true ]; then
# Output the stanza with rewritten URIs
for stanza_line in "${stanza_lines[@]}"; do
if [[ "$stanza_line" =~ ^([[:space:]]*URIs:[[:space:]]*)(.*)$ ]]; then
local prefix="${BASH_REMATCH[1]}"
local uris_list="${BASH_REMATCH[2]}"
local rewritten_uris=""
local first=true
for uri in $uris_list; do
local new_uri
new_uri=$(rewrite_url "$uri")
if [ "$first" = true ]; then
rewritten_uris="$new_uri"
first=false
else
rewritten_uris="$rewritten_uris $new_uri"
fi
done
echo "${prefix}${rewritten_uris}" >> "$output_file"
else
echo "$stanza_line" >> "$output_file"
fi
done
echo "" >> "$output_file"
fi
# Reset for next stanza
types=""
uris=""
suites=""
components=""
architectures=""
signed_by=""
enabled=""
in_stanza=false
stanza_lines=()
continue
fi
# Collect all lines in the stanza
if [[ "$line" =~ ^[[:space:]]*# ]]; then
stanza_lines+=("$line")
elif [[ "$line" =~ ^[[:space:]]*(Types|URIs|Suites|Components|Architectures|Signed-By|Enabled): ]]; then
in_stanza=true
stanza_lines+=("$line")
else
stanza_lines+=("$line")
fi
done < "$file"
# Handle last stanza if file doesn't end with blank line
if [ "$in_stanza" = true ] && [ ${#stanza_lines[@]} -gt 0 ]; then
for stanza_line in "${stanza_lines[@]}"; do
if [[ "$stanza_line" =~ ^([[:space:]]*URIs:[[:space:]]*)(.*)$ ]]; then
local prefix="${BASH_REMATCH[1]}"
local uris_list="${BASH_REMATCH[2]}"
local rewritten_uris=""
local first=true
for uri in $uris_list; do
local new_uri
new_uri=$(rewrite_url "$uri")
if [ "$first" = true ]; then
rewritten_uris="$new_uri"
first=false
else
rewritten_uris="$rewritten_uris $new_uri"
fi
done
echo "${prefix}${rewritten_uris}" >> "$output_file"
else
echo "$stanza_line" >> "$output_file"
fi
done
fi
}
convert_old_format_to_deb822() {
local file="$1"
local output_file="$2"
while IFS= read -r line || [ -n "$line" ]; do
case "$line" in
deb\ *|deb-src\ *)
local type="deb"
local opts=""
local url=""
local suite=""
local components=""
local arch=""
local signed_by=""
# Use regex to parse: deb[-src] [options] URL suite components...
if [[ "$line" =~ ^(deb-src|deb)[[:space:]]+(.*)$ ]]; then
type="${BASH_REMATCH[1]}"
local rest="${BASH_REMATCH[2]}"
# Extract options bracket if present
if [[ "$rest" =~ ^\[([^\]]+)\][[:space:]]+(.*)$ ]]; then
opts="${BASH_REMATCH[1]}"
rest="${BASH_REMATCH[2]}"
fi
# Parse URL, suite, and components from remaining line
# Split by whitespace - first is URL, second is suite, rest are components
set -- $rest
url="$1"
if [ $# -ge 2 ]; then
suite="$2"
shift 2
components="$*"
fi
fi
# Parse options for arch and signed-by
if [ -n "$opts" ]; then
if [[ "$opts" =~ arch=([^[:space:],]+) ]]; then
arch="${BASH_REMATCH[1]}"
fi
if [[ "$opts" =~ signed-by=([^[:space:],]+) ]]; then
signed_by="${BASH_REMATCH[1]}"
fi
fi
# Skip if URL is empty
[ -z "$url" ] && continue
# Rewrite URL
local new_url
new_url=$(rewrite_url "$url")
# Output in deb822 format
echo "Types: $type" >> "$output_file"
echo "URIs: $new_url" >> "$output_file"
if [ -n "$suite" ]; then
echo "Suites: $suite" >> "$output_file"
fi
if [ -n "$components" ]; then
echo "Components: $components" >> "$output_file"
fi
if [ -n "$arch" ]; then
echo "Architectures: $(echo $arch | tr ',' ' ')" >> "$output_file"
fi
if [ -n "$signed_by" ]; then
echo "Signed-By: $signed_by" >> "$output_file"
fi
echo "" >> "$output_file"
;;
*)
# Skip comments and blank lines for now
;;
esac
done < "$file"
}
generate_warm_sources() {
local counter=0
# Process old-format .list files and convert to deb822
for file in /etc/apt/sources.list /etc/apt/sources.list.d/*.list; do
[ -f "$file" ] || continue
counter=$((counter + 1))
convert_old_format_to_deb822 "$file" "${WARM_SOURCES_DIR}/source${counter}.sources"
done
# Process new deb822-format .sources files
for file in /etc/apt/sources.list.d/*.sources; do
[ -f "$file" ] || continue
counter=$((counter + 1))
local filename=$(basename "$file")
process_deb822_file "$file" "${WARM_SOURCES_DIR}/${filename}"
done
}
# Ensure curl exists (for the proxy-availability check)
if ! command -v curl >/dev/null 2>&1; then
sudo apt-get install -y curl
fi
# Only bother with warm-sources if proxy is reachable
if curl -s --connect-timeout 1 "$PROXY" >/dev/null 2>&1; then
generate_warm_sources
sudo /usr/bin/apt \
-o Acquire::http::Proxy="$PROXY" \
-o Acquire::https::Proxy="$PROXY" \
-o Acquire::By-Hash=true \
-o Dir::Etc::sourcelist="/dev/null" \
-o Dir::Etc::sourceparts="$WARM_SOURCES_DIR" \
"$@"
else
# fall back to normal behavior if proxy is down
sudo /usr/bin/apt "$@"
fi