-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
479 lines (415 loc) · 13.3 KB
/
install.sh
File metadata and controls
479 lines (415 loc) · 13.3 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/usr/bin/env bash
set -euo pipefail
# =============================================================================
# Configuration
# =============================================================================
REPO_URL="https://raw.githubusercontent.com/ichinya/mtproxy-installer/main"
INSTALL_DIR="${INSTALL_DIR:-/opt/mtproxy-installer}"
PROVIDER="${1:-${PROVIDER:-telemt}}"
# PORT can be passed as second positional arg or via environment
PORT="${2:-${PORT:-443}}"
PROVIDER_DIR="${INSTALL_DIR}/providers/${PROVIDER}"
DATA_DIR="${PROVIDER_DIR}/data"
DEFAULT_TELEMT_IMAGE_SOURCE="whn0thacked/telemt-docker:latest"
DEFAULT_MTG_IMAGE_SOURCE="nineseconds/mtg:2"
# Defaults (can be overridden via environment)
API_PORT="${API_PORT:-9091}"
TLS_DOMAIN="${TLS_DOMAIN:-www.wikipedia.org}"
# =============================================================================
# Logging
# =============================================================================
log() { printf '%s\n' "$1"; }
die() { printf 'Error: %s\n' "$1" >&2; exit 1; }
log_fix() { log "[FIX] $1"; }
# =============================================================================
# Prerequisites
# =============================================================================
require_root() {
if [ "${EUID}" -ne 0 ]; then
die "Run as root. Example: curl -fsSL .../install.sh | sudo bash"
fi
}
ensure_apt_package() {
local package="$1"
dpkg -s "${package}" >/dev/null 2>&1 || { apt-get update && apt-get install -y "${package}"; }
}
ensure_base_tools() {
command -v curl >/dev/null 2>&1 || ensure_apt_package curl
command -v openssl >/dev/null 2>&1 || ensure_apt_package openssl
command -v docker >/dev/null 2>&1 || { log "Installing Docker..."; curl -fsSL https://get.docker.com | sh; }
docker compose version >/dev/null 2>&1 || ensure_apt_package docker-compose-plugin
}
# =============================================================================
# Utils
# =============================================================================
backup_if_exists() {
if [ -f "$1" ]; then
cp "$1" "$1.bak.$(date +%s)"
fi
}
set_env_value() {
local file="$1"
local key="$2"
local value="$3"
local tmp
tmp="$(mktemp)"
if [ -f "${file}" ]; then
awk -v key="${key}" -v value="${value}" '
BEGIN { updated = 0 }
index($0, key "=") == 1 {
print key "=" value
updated = 1
next
}
{ print }
END {
if (!updated) {
print key "=" value
}
}
' "${file}" > "${tmp}"
else
printf '%s=%s\n' "${key}" "${value}" > "${tmp}"
fi
mv "${tmp}" "${file}"
}
resolve_image_ref() {
local source_ref="$1"
local pinned_ref
docker pull "${source_ref}" >/dev/null
pinned_ref="$(docker image inspect --format '{{range .RepoDigests}}{{println .}}{{end}}' "${source_ref}" 2>/dev/null | sed '/^$/d' | head -n1 || true)"
if [ -n "${pinned_ref}" ]; then
printf '%s\n' "${pinned_ref}"
else
printf '%s\n' "${source_ref}"
fi
}
generate_secret() {
case "${PROVIDER}" in
telemt)
openssl rand -hex 16
;;
mtg)
local image="${MTG_IMAGE_SOURCE:-${MTG_IMAGE:-${DEFAULT_MTG_IMAGE_SOURCE}}}"
docker run --rm "${image}" generate-secret "${TLS_DOMAIN}"
;;
*)
die "Unknown provider: ${PROVIDER}"
;;
esac
}
# =============================================================================
# Provider: telemt
# =============================================================================
write_telemt_env() {
# Root .env for docker compose
cat > "${INSTALL_DIR}/.env" <<EOF
PROVIDER=telemt
PORT=${PORT}
API_PORT=${API_PORT}
PUBLIC_IP=${PUBLIC_IP}
TELEMT_IMAGE_SOURCE=${TELEMT_IMAGE_SOURCE:-${DEFAULT_TELEMT_IMAGE_SOURCE}}
TELEMT_IMAGE=${TELEMT_IMAGE:-${TELEMT_IMAGE_SOURCE:-${DEFAULT_TELEMT_IMAGE_SOURCE}}}
RUST_LOG=${RUST_LOG:-info}
TLS_DOMAIN=${TLS_DOMAIN}
PROXY_USER=${PROXY_USER:-main}
SECRET=${SECRET}
EOF
}
write_telemt_config() {
cat > "${PROVIDER_DIR}/telemt.toml" <<EOF
[general]
use_middle_proxy = true
proxy_secret_path = "/var/lib/telemt/proxy-secret"
middle_proxy_nat_ip = "${PUBLIC_IP}"
middle_proxy_nat_probe = true
log_level = "normal"
[general.modes]
classic = false
secure = false
tls = true
[general.links]
show = "*"
public_host = "${PUBLIC_IP}"
public_port = ${PORT}
[server]
port = 443
listen_addr_ipv4 = "0.0.0.0"
listen_addr_ipv6 = "::"
proxy_protocol = false
metrics_whitelist = ["127.0.0.1/32", "::1/128"]
[server.api]
enabled = true
listen = "0.0.0.0:${API_PORT}"
whitelist = []
read_only = true
[[server.listeners]]
ip = "0.0.0.0"
announce = "${PUBLIC_IP}"
[censorship]
tls_domain = "${TLS_DOMAIN}"
mask = true
mask_port = 443
fake_cert_len = 2048
tls_emulation = false
tls_front_dir = "/var/lib/telemt/tlsfront"
[access.users]
"${PROXY_USER:-main}" = "${SECRET}"
EOF
}
write_telemt_compose() {
cat > "${INSTALL_DIR}/docker-compose.yml" <<'EOF'
services:
telemt:
image: ${TELEMT_IMAGE}
container_name: telemt
restart: unless-stopped
environment:
RUST_LOG: ${RUST_LOG}
volumes:
- ./providers/telemt/telemt.toml:/etc/telemt.toml:ro
- ./providers/telemt/data:/var/lib/telemt
ports:
- "${PORT}:443/tcp"
- "127.0.0.1:${API_PORT}:9091/tcp"
security_opt:
- no-new-privileges:true
cap_drop:
- ALL
cap_add:
- NET_BIND_SERVICE
read_only: true
tmpfs:
- /tmp:rw,nosuid,nodev,noexec,size=16m
ulimits:
nofile:
soft: 65536
hard: 65536
EOF
}
get_telemt_link() {
local url="http://127.0.0.1:${API_PORT}/v1/health"
local attempt
for attempt in $(seq 1 30); do
if curl -fsS "${url}" >/dev/null 2>&1; then
curl -fsS "http://127.0.0.1:${API_PORT}/v1/users" 2>/dev/null | \
tr -d '\n' | sed -n 's/.*"tls":\["\([^"]*\)"\].*/\1/p' | head -n1
return 0
fi
sleep 2
done
return 1
}
# =============================================================================
# Provider: mtg
# =============================================================================
write_mtg_env() {
# Root .env for docker compose
cat > "${INSTALL_DIR}/.env" <<EOF
PROVIDER=mtg
PORT=${PORT}
PUBLIC_IP=${PUBLIC_IP}
MTG_IMAGE_SOURCE=${MTG_IMAGE_SOURCE:-${DEFAULT_MTG_IMAGE_SOURCE}}
MTG_IMAGE=${MTG_IMAGE:-${MTG_IMAGE_SOURCE:-${DEFAULT_MTG_IMAGE_SOURCE}}}
MTG_DEBUG=${MTG_DEBUG:-info}
TLS_DOMAIN=${TLS_DOMAIN}
SECRET=${SECRET}
EOF
}
write_mtg_config() {
local debug_flag=false
if [ "${MTG_DEBUG:-info}" = "debug" ] || [ "${MTG_DEBUG:-info}" = "true" ] || [ "${MTG_DEBUG:-info}" = "1" ]; then
debug_flag=true
fi
cat > "${PROVIDER_DIR}/mtg.conf" <<EOF
secret = "${SECRET}"
bind-to = "0.0.0.0:3128"
debug = ${debug_flag}
EOF
}
write_mtg_compose() {
cat > "${INSTALL_DIR}/docker-compose.yml" <<'EOF'
services:
mtg:
image: ${MTG_IMAGE:-nineseconds/mtg:2}
container_name: mtg
restart: unless-stopped
volumes:
- ./providers/mtg/mtg.conf:/config.toml:ro
- ./providers/mtg/data:/var/lib/mtg
ports:
- "${PORT}:3128/tcp"
security_opt:
- no-new-privileges:true
cap_drop:
- ALL
cap_add:
- NET_BIND_SERVICE
read_only: true
tmpfs:
- /tmp:rw,nosuid,nodev,noexec,size=16m
ulimits:
nofile:
soft: 65536
hard: 65536
EOF
}
validate_mtg_config() {
log_fix "Validating generated mtg config with mtg access."
docker run --rm \
-v "${PROVIDER_DIR}/mtg.conf:/config.toml:ro" \
"${MTG_IMAGE}" \
access /config.toml >/dev/null
}
prepare_provider_image() {
local env_file="${INSTALL_DIR}/.env"
local source_ref
local pinned_ref
case "${PROVIDER}" in
telemt)
source_ref="${TELEMT_IMAGE_SOURCE:-${TELEMT_IMAGE:-${DEFAULT_TELEMT_IMAGE_SOURCE}}}"
log "Pulling Telemt image source: ${source_ref}"
pinned_ref="$(resolve_image_ref "${source_ref}")"
export TELEMT_IMAGE_SOURCE="${source_ref}"
export TELEMT_IMAGE="${pinned_ref}"
set_env_value "${env_file}" "TELEMT_IMAGE_SOURCE" "${TELEMT_IMAGE_SOURCE}"
set_env_value "${env_file}" "TELEMT_IMAGE" "${TELEMT_IMAGE}"
;;
mtg)
source_ref="${MTG_IMAGE_SOURCE:-${MTG_IMAGE:-${DEFAULT_MTG_IMAGE_SOURCE}}}"
log "Pulling mtg image source: ${source_ref}"
pinned_ref="$(resolve_image_ref "${source_ref}")"
export MTG_IMAGE_SOURCE="${source_ref}"
export MTG_IMAGE="${pinned_ref}"
set_env_value "${env_file}" "MTG_IMAGE_SOURCE" "${MTG_IMAGE_SOURCE}"
set_env_value "${env_file}" "MTG_IMAGE" "${MTG_IMAGE}"
;;
esac
}
urlencode() {
local str="$1"
# Portable fallback if python3 not present
if command -v python3 >/dev/null 2>&1; then
python3 -c "import urllib.parse,sys; print(urllib.parse.quote(sys.argv[1], safe=''))" "$str"
elif command -v python >/dev/null 2>&1; then
python -c "import urllib.parse,sys; print(urllib.parse.quote(sys.argv[1], safe=''))" "$str"
else
# Not ideal, but dot and alnum are safe; fallback means secret may break on some clients
printf '%s' "$str"
fi
}
get_mtg_link() {
local encoded_secret="$(urlencode "${SECRET}")"
printf 'tg://proxy?server=%s&port=%s&secret=%s\n' "${PUBLIC_IP}" "${PORT}" "${encoded_secret}"
}
# =============================================================================
# Provider Dispatcher
# =============================================================================
validate_provider() {
case "${PROVIDER}" in
telemt|mtg) return 0 ;;
*) die "Unsupported provider: ${PROVIDER}. Supported: telemt, mtg" ;;
esac
}
setup_provider() {
mkdir -p "${PROVIDER_DIR}" "${DATA_DIR}"
[ "${PROVIDER}" = "telemt" ] && mkdir -p "${DATA_DIR}/cache" "${DATA_DIR}/tlsfront"
chown -R 65532:65532 "${DATA_DIR}"
backup_if_exists "${INSTALL_DIR}/docker-compose.yml"
backup_if_exists "${PROVIDER_DIR}/.env"
backup_if_exists "${PROVIDER_DIR}/telemt.toml"
backup_if_exists "${PROVIDER_DIR}/mtg.conf"
}
write_provider_files() {
case "${PROVIDER}" in
telemt)
write_telemt_compose
write_telemt_env
write_telemt_config
;;
mtg)
write_mtg_compose
write_mtg_env
write_mtg_config
;;
esac
}
validate_provider_files() {
case "${PROVIDER}" in
telemt) return 0 ;;
mtg) validate_mtg_config ;;
esac
}
get_proxy_link() {
case "${PROVIDER}" in
telemt) get_telemt_link ;;
mtg) get_mtg_link ;;
esac
}
print_info() {
log ""
log "=============================="
log "${PROVIDER} installed"
log "=============================="
log ""
log "Install dir: ${INSTALL_DIR}"
log "Provider: ${PROVIDER}"
log "Public endpoint: ${PUBLIC_IP}:${PORT}"
log "Secret: ${SECRET}"
log ""
[ -n "${PROXY_LINK:-}" ] && { log "Proxy link:"; log "${PROXY_LINK}"; log ""; }
case "${PROVIDER}" in
telemt)
log "API: http://127.0.0.1:${API_PORT}/v1/health"
log "Config: ${PROVIDER_DIR}/telemt.toml"
log ""
log_fix "Telegram voice calls are not guaranteed over MTProto proxy."
;;
mtg)
log "Config: ${PROVIDER_DIR}/mtg.conf"
log ""
log_fix "mtg v2 does not support ad_tag."
log_fix "mtg has no HTTP API for automatic link extraction."
log_fix "Telegram voice calls are not guaranteed over MTProto proxy."
;;
esac
log ""
log "Logs: docker compose -f ${INSTALL_DIR}/docker-compose.yml logs -f ${PROVIDER}"
}
# =============================================================================
# Main
# =============================================================================
main() {
require_root
validate_provider
ensure_base_tools
log "================================"
log "MTProxy Installer"
log "================================"
log "Provider: ${PROVIDER}"
# Set provider-specific defaults
case "${PROVIDER}" in
telemt)
export TELEMT_IMAGE_SOURCE="${TELEMT_IMAGE_SOURCE:-${TELEMT_IMAGE:-${DEFAULT_TELEMT_IMAGE_SOURCE}}}"
export RUST_LOG="${RUST_LOG:-info}"
;;
mtg)
export MTG_IMAGE_SOURCE="${MTG_IMAGE_SOURCE:-${MTG_IMAGE:-${DEFAULT_MTG_IMAGE_SOURCE}}}"
export MTG_DEBUG="${MTG_DEBUG:-info}"
;;
esac
PUBLIC_IP="${PUBLIC_IP:-$(curl -fsSL https://api.ipify.org)}"
SECRET="${SECRET:-$(generate_secret)}"
export PORT PUBLIC_IP SECRET TLS_DOMAIN
setup_provider
write_provider_files
prepare_provider_image
validate_provider_files
log "Starting ${PROVIDER}..."
docker compose -f "${INSTALL_DIR}/docker-compose.yml" --project-directory "${INSTALL_DIR}" --env-file "${INSTALL_DIR}/.env" down || true
docker compose -f "${INSTALL_DIR}/docker-compose.yml" --project-directory "${INSTALL_DIR}" --env-file "${INSTALL_DIR}/.env" up -d --force-recreate
sleep 3
PROXY_LINK="$(get_proxy_link)" || true
print_info
}
main "$@"