-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathinstall_mtproxy_english.sh
More file actions
581 lines (499 loc) · 23.4 KB
/
Copy pathinstall_mtproxy_english.sh
File metadata and controls
581 lines (499 loc) · 23.4 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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#!/bin/bash
# Script for Debian 10-12 and Ubuntu 20.04-24.04
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No color
# Function to print a colored message
print_status() {
echo -e "${GREEN}* ${NC}$1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_header() {
echo -e "${BLUE}================================================${NC}"
echo -e "${BLUE}$1${NC}"
echo -e "${BLUE}================================================${NC}"
}
# --- Self-installation of the script to PATH ---
# If the script is not run from /usr/local/bin, it copies itself there and restarts.
# This allows using delete/reinstall/update-secret commands from any directory.
INSTALL_PATH="/usr/local/bin"
SCRIPT_NAME="$(basename "$0")"
INSTALLED_SCRIPT_PATH="$INSTALL_PATH/$SCRIPT_NAME"
# Check if the current script is already installed in /usr/local/bin
# If not, copy it and restart
if [ "$(readlink -f "$0")" != "$INSTALLED_SCRIPT_PATH" ]; then
print_header "Script Preparation"
print_status "Installing the script to $INSTALL_PATH for convenient management commands..."
# Check if the directory exists and is writable
if [ ! -d "$INSTALL_PATH" ] || [ ! -w "$INSTALL_PATH" ]; then
print_error "Directory $INSTALL_PATH does not exist or is not writable. Script installation is not possible."
print_warning "Management commands may only work by running the script with './$SCRIPT_NAME' from the current directory."
# Continue execution without copying
else
if sudo cp "$0" "$INSTALLED_SCRIPT_PATH"; then
sudo chmod +x "$INSTALLED_SCRIPT_PATH"
print_status "Script copied. Restarting from $INSTALL_PATH..."
# Restart the script with the same arguments, replacing the current process
# Use exec so the new process replaces the old one
exec sudo "$INSTALLED_SCRIPT_PATH" "$@"
else
print_error "Failed to copy script to $INSTALL_PATH."
print_warning "Management commands will only work by running the script from its current location with the full path or via ./"
# Continue execution from the current location
fi
fi
fi
# --- End of self-installation ---
# --- Management Functions ---
# Function to update the main MTProxy secret
update_mtproxy_secret() {
print_header "Updating MTProxy secret..."
local service_file="/etc/systemd/system/mtproxy.service"
if [ ! -f "$service_file" ]; then
print_error "Systemd service file '$service_file' not found."
print_error "Make sure MTProxy is installed."
exit 1
fi
print_status "Generating new secret..."
NEW_SECRET=$(head -c 16 /dev/urandom | xxd -ps)
print_status "Updating service unit file '$service_file'..."
# Use sed to replace the old secret with the new one in the ExecStart line
# Look for the pattern -S <32 hex characters> and replace it
if sudo sed -i "s|-S [a-f0-9]\{32\}|-S ${NEW_SECRET}|" "$service_file"; then
print_status "Secret successfully updated in the unit file."
# Update the secret also in /etc/mtproxy/config file for reference
if [ -f "/etc/mtproxy/config" ]; then
sudo sed -i "s/^SECRET=.*/SECRET=$NEW_SECRET/" "/etc/mtproxy/config" 2>/dev/null || true
print_status "Secret updated in /etc/mtproxy/config."
fi
print_status "Reloading systemd unit files..."
sudo systemctl daemon-reload
print_status "Restarting MTProxy service..."
if sudo systemctl restart mtproxy; then
print_status "MTProxy service successfully restarted with the new secret!"
echo
print_header "MTProxy secret update complete!"
echo -e "${GREEN}* ${NC}New MTProxy secret:"
echo -e "${GREEN}* ${NC}${NEW_SECRET}"
print_warning "Remove the old MTProxy in Telegram!"
# Attempt to generate a new link if IP is available
local CURRENT_EXTERNAL_PORT=$(grep -oP '(?<=-H )[0-9]+' "$service_file" | head -1)
local SERVER_IP=$(curl -s ifconfig.me 2>/dev/null || curl -s ipinfo.io/ip 2>/dev/null || echo "YOUR_PUBLIC_IP_ADDRESS")
if [ -n "$CURRENT_EXTERNAL_PORT" ] && [ "$SERVER_IP" != "YOUR_PUBLIC_IP_ADDRESS" ]; then
echo -e "${GREEN}* ${NC}New link:"
echo -e "${GREEN}* ${NC}https://t.me/proxy?server=${SERVER_IP}&port=${CURRENT_EXTERNAL_PORT}&secret=${NEW_SECRET}"
echo -e "${GREEN}* ${NC}New link ONLY for the app:"
echo -e "${GREEN}* ${NC}tg://proxy?server=${SERVER_IP}&port=${CURRENT_EXTERNAL_PORT}&secret=${NEW_SECRET}"
else
print_warning "Failed to generate a new link automatically (port not found in unit or IP)."
print_warning "New secret: ${NEW_SECRET}"
fi
else
print_error "Failed to restart MTProxy service after updating the secret."
print_error "Check logs: sudo journalctl -u mtproxy"
exit 1
fi
else
print_error "Failed to update secret in service unit file '$service_file'."
print_error "Check file content and permissions."
exit 1
fi
}
uninstall_mtproxy() {
local silent_prompt=false
if [ "$1" == "--silent-prompt" ]; then
silent_prompt=true
else
print_header "Uninstalling MTProxy"
print_warning "This will completely remove MTProxy and all its files."
echo "Enter 'y' or '+' to confirm and press Enter:"
read -r confirm
if [[ "$confirm" != "y" && "$confirm" != "Y" && "$confirm" != "+" ]]; then
print_status "Uninstallation cancelled."
exit 0
fi
fi
print_status "Stopping and disabling service..."
sudo systemctl stop mtproxy 2>/dev/null || true
sudo systemctl disable mtproxy 2>/dev/null || true
sudo rm -f /etc/systemd/system/mtproxy.service 2>/dev/null || true
sudo systemctl daemon-reload || true # allow failure if daemon-reload fails for some reason
print_status "Removing binary..."
sudo rm -f /usr/local/bin/mtproto-proxy 2>/dev/null || true
print_status "Clearing binary privileges..."
sudo setcap -r /usr/local/bin/mtproto-proxy 2>/dev/null || true # Clearing capabilities
print_status "Removing configs and directories..."
sudo rm -rf /etc/mtproxy 2>/dev/null || true
sudo rm -rf /var/lib/mtproxy 2>/dev/null || true
sudo rm -rf /var/log/mtproxy 2>/dev/null || true
print_status "Removing logrotate config..."
sudo rm -f /etc/logrotate.d/mtproxy 2>/dev/null || true
print_status "Removing update script..."
sudo rm -f /usr/local/bin/mtproxy-update 2>/dev/null || true
print_status "Removing cron job..."
sudo rm -f /etc/cron.d/mtproxy-update 2>/dev/null || true
print_status "Removing user 'mtproxy'..."
if id "mtproxy" &>/dev/null; then
sudo userdel mtproxy 2>/dev/null || print_warning "Failed to remove user 'mtproxy'. It might still own files."
fi
print_status "Cleaning up sysctl..."
SYSCTL_FILE="/etc/sysctl.conf"
# Remove the exact line added by the script, if it exists
if grep -q "^net.core.somaxconn[[:space:]]*=.*1024" "$SYSCTL_FILE"; then
sudo sed -i '/^net.core.somaxconn[[:space:]]*=.*1024/d' "$SYSCTL_FILE"
sudo sysctl -p || print_warning "Failed to apply sysctl changes."
fi
print_warning "Firewall settings (UFW/iptables/cloud) are NOT removed."
if ! $silent_prompt; then
print_header "MTProxy uninstallation complete."
fi
}
reinstall_mtproxy() {
print_header "Reinstalling MTProxy"
print_warning "This will completely remove the current MTProxy installation and start a new one."
echo "Enter 'y' or '+' to confirm and press Enter:"
read -r confirm
if [[ "$confirm" != "y" && "$confirm" != "Y" && "$confirm" != "+" ]]; then
print_status "Reinstallation cancelled."
exit 0
fi
uninstall_mtproxy --silent-prompt
print_header "Starting new MTProxy installation..."
}
# --- Command Line Argument Handling ---
case "$1" in
delete)
uninstall_mtproxy
exit 0
;;
reinstall)
reinstall_mtproxy
;;
update-secret)
update_mtproxy_secret
exit 0
;;
*)
print_header "Starting MTProxy installation"
;;
esac
# --- Main installation logic starts here ---
# OS Check
if ! grep -q -E "Debian|Ubuntu" /etc/os-release; then
print_error "This script is intended for Debian or Ubuntu only."
exit 1
fi
# Determine Ubuntu version
UBUNTU_VERSION=$(grep "VERSION_ID" /etc/os-release | cut -d '"' -f2)
# If Ubuntu 23.x, fix sources.list
if [[ "$UBUNTU_VERSION" =~ ^23 ]]; then
print_header "Ubuntu $UBUNTU_VERSION detected, applying fixes"
# Create a backup of sources.list
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
print_status "sources.list backup created."
# Update server addresses in sources.list
sudo sed -i 's|http://.*archive.ubuntu.com|http://old-releases.ubuntu.com|g' /etc/apt/sources.list
sudo sed -i 's|http://security.ubuntu.com|http://old-releases.ubuntu.com|g' /etc/apt/sources.list
sudo sed -i 's|http://archive.ubuntu.com|http://old-releases.ubuntu.com|g' /etc/apt/sources.list
print_status "sources.list file updated to support old repositories."
# Update package list
print_status "Updating package list..."
sudo apt update
fi
# Package Update
print_status "Updating packages..."
sudo apt update -y && sudo apt upgrade -y
# Dependency Installation
print_status "Installing dependencies..."
sudo apt install -y git build-essential libssl-dev zlib1g-dev curl wget \
libc6-dev gcc-multilib make cmake pkg-config netcat-openbsd xxd iproute2 dos2unix
# Creating mtproxy user for secure service execution
print_status "mtproxy user (for security)..."
if ! id "mtproxy" &>/dev/null; then
sudo useradd -r -s /bin/false -d /var/lib/mtproxy -M mtproxy
sudo mkdir -p /var/lib/mtproxy
sudo chown mtproxy:mtproxy /var/lib/mtproxy
print_status "'mtproxy' user created."
else
print_status "'mtproxy' user already exists."
fi
# --- Port Selection and Check ---
EXTERNAL_PORT=""
while true; do
print_header "External Port Selection"
echo "Enter the desired external port (default is 443)."
echo -n "If the port is suitable, just press Enter: "
read -r EXTERNAL_PORT_INPUT
if [ -z "$EXTERNAL_PORT_INPUT" ]; then EXTERNAL_PORT_INPUT=443; fi
if ! [[ "$EXTERNAL_PORT_INPUT" =~ ^[0-9]+$ ]] || (( EXTERNAL_PORT_INPUT <= 0 || EXTERNAL_PORT_INPUT > 65535 )); then
print_error "Invalid port: $EXTERNAL_PORT_INPUT."
continue
fi
print_status "Checking external port $EXTERNAL_PORT_INPUT availability..."
if sudo ss -tulnp | grep -q ":$EXTERNAL_PORT_INPUT\b"; then
print_error "Port $EXTERNAL_PORT_INPUT is busy. Please choose another."
else
print_status "Port $EXTERNAL_PORT_INPUT is free."
EXTERNAL_PORT="$EXTERNAL_PORT_INPUT"
break
fi
done
INTERNAL_PORT=""
DEFAULT_INTERNAL_PORT=8008
while true; do
print_header "Internal Port Selection"
echo "Enter the desired internal port (default is ${DEFAULT_INTERNAL_PORT})."
echo -n "If the port is suitable, just press Enter: "
read -r INTERNAL_PORT_INPUT
if [ -z "$INTERNAL_PORT_INPUT" ]; then INTERNAL_PORT_INPUT=$DEFAULT_INTERNAL_PORT; fi
if ! [[ "$INTERNAL_PORT_INPUT" =~ ^[0-9]+$ ]] || (( INTERNAL_PORT_INPUT <= 0 || INTERNAL_PORT_INPUT > 65535 )); then
print_error "Invalid port: $INTERNAL_PORT_INPUT."
continue
fi
if [ "$INTERNAL_PORT_INPUT" -eq "$EXTERNAL_PORT" ]; then
print_error "Internal port ($INTERNAL_PORT_INPUT) cannot be the same as the external port ($EXTERNAL_PORT)."
continue
fi
print_status "Checking internal port $INTERNAL_PORT_INPUT availability..."
if sudo ss -tulnp | grep -q ":$INTERNAL_PORT_INPUT\b"; then
print_error "Port $INTERNAL_PORT_INPUT is busy. Please choose another."
else
print_status "Port $INTERNAL_PORT_INPUT is free."
INTERNAL_PORT="$INTERNAL_PORT_INPUT"
break
fi
done
# Building MTProxy from source
print_header "Building MTProxy"
cd /tmp
rm -rf MTProxy MTProxy-community 2>/dev/null || true
BUILD_SUCCESS=false
print_status "Building from GetPageSpeed/MTProxy..."
if git clone https://github.com/GetPageSpeed/MTProxy.git MTProxy-community; then
cd MTProxy-community
if [ -f "Makefile" ]; then sed -i 's/-Werror//g' Makefile 2>/dev/null || true; fi
if make -j$(nproc) 2>/dev/null; then BUILD_SUCCESS=true; print_status "Success (GetPageSpeed)."; else print_warning "Failed (GetPageSpeed). make output:"; make -j$(nproc); cd /tmp; fi
fi
if [ "$BUILD_SUCCESS" = false ]; then
print_status "Building from TelegramMessenger/MTProxy..."
if git clone https://github.com/TelegramMessenger/MTProxy.git; then
cd MTProxy
if [ -f "Makefile" ]; then sed -i 's/-Werror//g' Makefile 2>/dev/null || true; fi
if [ -f "Makefile" ]; then grep -q -- "-fcommon" Makefile || sed -i 's/CFLAGS =/CFLAGS = -fcommon/g' Makefile 2>/dev/null || true; fi
if [ -f "Makefile" ]; then sed -i 's/-march=native/-march=native -fcommon/g' Makefile 2>/dev/null || true; fi
find . -name "*.c" -exec sed -i '1i#include <string.h>' {} \; 2>/dev/null || true
find . -name "*.c" -exec sed -i '1i#include <unistd.h>' {} \; 2>/dev/null || true
if make -j$(nproc) CFLAGS="-fcommon -Wno-error" 2>/dev/null; then BUILD_SUCCESS=true; print_status "Success (TelegramMessenger)."; else print_warning "Failed (TelegramMessenger). make output:"; make -j$(nproc) CFLAGS="-fcommon -Wno-error"; print_warning "Attempting with min flags..."; if make CC=gcc CFLAGS="-O2 -fcommon -w"; then BUILD_SUCCESS=true; print_status "Success (min flags)."; fi; fi
fi
fi
if [ "$BUILD_SUCCESS" = false ]; then
print_error "Failed to build MTProxy."
print_error "Check the make output above. Consider alternatives."
exit 1
fi
# Installing binary and configuring
print_header "Installation and Configuration"
sudo mkdir -p /etc/mtproxy /var/log/mtproxy 2>/dev/null || true
print_status "Copying binary..."
MTPROXY_BINARY_PATH=""
if [ -f "objs/bin/mtproto-proxy" ]; then MTPROXY_BINARY_PATH="objs/bin/mtproto-proxy"; fi
if [ -z "$MTPROXY_BINARY_PATH" ] && [ -f "mtproto-proxy" ]; then MTPROXY_BINARY_PATH="mtproto-proxy"; fi
if [ -z "$MTPROXY_BINARY_PATH" ] && [ -f "bin/mtproto-proxy" ]; then MTPROXY_BINARY_PATH="bin/mtproto-proxy"; fi
if [ -n "$MTPROXY_BINARY_PATH" ] && [ -f "$MTPROXY_BINARY_PATH" ]; then
sudo cp "$MTPROXY_BINARY_PATH" /usr/local/bin/mtproto-proxy
sudo chmod +x /usr/local/bin/mtproto-proxy
print_status "Binary installed."
else
print_error "Binary not found!"
exit 1
fi
print_status "Generating secret..."
SECRET=$(head -c 16 /dev/urandom | xxd -ps)
echo "SECRET=$SECRET" | sudo tee /etc/mtproxy/config > /dev/null
print_status "Secret generated."
print_status "Downloading Telegram configs..."
sudo curl -s https://core.telegram.org/getProxySecret -o /etc/mtproxy/proxy-secret || print_warning "Failed to download proxy-secret."
sudo curl -s https://core.telegram.org/getProxyConfig -o /etc/mtproxy/proxy-multi.conf || print_warning "Failed to download proxy-multi.conf."
print_status "Setting permissions..."
sudo chown -R mtproxy:mtproxy /etc/mtproxy /var/log/mtproxy /var/lib/mtproxy 2>/dev/null || true
sudo chmod 600 /etc/mtproxy/* 2>/dev/null || true
print_status "Setting port privileges..."
if (( EXTERNAL_PORT <= 1024 )); then
sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/mtproto-proxy
print_status "CAP_NET_BIND_SERVICE capabilities set."
else
sudo setcap 'cap_net_bind_service=-ep' /usr/local/bin/mtproto-proxy 2>/dev/null || true
print_status "CAP_NET_BIND_SERVICE not required."
fi
print_status "Creating systemd service..."
sudo tee /etc/systemd/system/mtproxy.service > /dev/null <<EOF
[Unit]
Description=MTProxy
After=network.target
[Service]
Type=simple
User=mtproxy
Group=mtproxy
WorkingDirectory=/var/lib/mtproxy
ExecStart=/usr/local/bin/mtproto-proxy -u mtproxy -p ${INTERNAL_PORT} -H ${EXTERNAL_PORT} -S ${SECRET} --aes-pwd /etc/mtproxy/proxy-secret /etc/mtproxy/proxy-multi.conf -M 1
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=mtproxy
AmbientCapabilities=CAP_NET_BIND_SERVICE
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
print_status "systemd service created."
print_status "Reloading systemd..."
sudo systemctl daemon-reload
print_status "Enabling service on boot..."
sudo systemctl enable mtproxy
print_status "Creating update script..."
sudo tee /usr/local/bin/mtproxy-update > /dev/null <<'EOF'
#!/bin/bash
RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m'
print_status() { echo -e "${GREEN}* ${NC}$1"; }
print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
print_status "Updating MTProxy configuration..."
CONFIG_DIR="/etc/mtproxy"
if ! command -v curl &>/dev/null; then print_error "Error: curl not found."; exit 1; fi
if [ ! -d "$CONFIG_DIR" ]; then print_error "Error: Directory $CONFIG_DIR does not exist."; exit 1; fi
# Download to temporary files
if curl -s https://core.telegram.org/getProxySecret -o "$CONFIG_DIR/proxy-secret.new"; then
mv "$CONFIG_DIR/proxy-secret.new" "$CONFIG_DIR/proxy-secret"
print_status "proxy-secret updated."
else
print_warning "Failed to download proxy-secret."
fi
if curl -s https://core.telegram.org/getProxyConfig -o "$CONFIG_DIR/proxy-multi.conf.new"; then
mv "$CONFIG_DIR/proxy-multi.conf.new" "$CONFIG_DIR/proxy-multi.conf"
print_status "proxy-multi.conf updated."
else
print_warning "Failed to download proxy-multi.conf."
fi
# Set permissions and ownership
if [ -f "$CONFIG_DIR/proxy-secret" ]; then sudo chown mtproxy:mtproxy "$CONFIG_DIR/proxy-secret" 2>/dev/null || true; sudo chmod 600 "$CONFIG_DIR/proxy-secret" 2>/dev/null || true; fi
if [ -f "$CONFIG_DIR/proxy-multi.conf" ]; then sudo chown mtproxy:mtproxy "$CONFIG_DIR/proxy-multi.conf" 2>/dev/null || true; sudo chmod 600 "$CONFIG_DIR/proxy-multi.conf" 2>/dev/null || true; fi
print_status "Permissions updated."
print_status "Restarting service..."
if systemctl restart mtproxy; then print_status "Service restarted."; else print_error "Failed to restart service. Check logs."; exit 1; fi
print_status "Configuration update complete."
EOF
sudo chmod +x /usr/local/bin/mtproxy-update
print_status "Update script created."
# --- Configuring daily updates via cron ---
print_status "Configuring daily configuration update (cron)..."
if [ ! -f "/etc/cron.d/mtproxy-update" ] || ! grep -q "/usr/local/bin/mtproxy-update" /etc/cron.d/mtproxy-update; then
sudo tee /etc/cron.d/mtproxy-update > /dev/null <<EOF
0 3 * * * root /usr/local/bin/mtproxy-update > /dev/null 2>&1
EOF
print_status "Daily update configured for 03:00 UTC."
else
print_status "Cron job for daily update already exists."
fi
# Log Rotation Configuration
print_status "Configuring log rotation..."
sudo tee /etc/logrotate.d/mtproxy > /dev/null <<EOF
/var/log/mtproxy/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 mtproxy mtproxy
postrotate
systemctl try-reload-or-restart mtproxy > /dev/null 2>&1 || true
endscript
}
EOF
print_status "Logrotate configuration added."
# Firewall Configuration (if ufw is installed)
if command -v ufw &> /dev/null; then
print_status "Configuring UFW firewall..."
sudo ufw allow $EXTERNAL_PORT/tcp comment "MTProxy External Port" || print_warning "Failed to add UFW rule for port $EXTERNAL_PORT."
print_status "UFW rule added for external port $EXTERNAL_PORT/tcp."
print_warning "If UFW is disabled, enable it: sudo ufw enable"
elif command -v iptables &> /dev/null; then
print_status "iptables detected. Add rules for ${EXTERNAL_PORT}/tcp manually."
else
print_warning "Firewall not detected. Open the external port ${EXTERNAL_PORT}/tcp manually in your system and with your provider."
fi
print_status "Starting MTProxy service..."
if sudo systemctl start mtproxy; then
print_status "MTProxy service started."
else
print_error "Failed to start MTProxy service. Check logs: sudo journalctl -u mtproxy -f"
exit 1
fi
# Optimizations after successful startup
print_header "Optimizations"
print_status "Descriptor limits are set in the systemd unit."
print_status "Configuring network parameters (net.core.somaxconn)..."
SYSCTL_FILE="/etc/sysctl.conf"
SYSCTL_SETTING="net.core.somaxconn = 1024"
if grep -q "^net.core.somaxconn[[:space:]]*=" "$SYSCTL_FILE"; then
sudo sed -i 's/^net.core.somaxconn[[:space:]]*=.*$/net.core.somaxconn = 1024/' "$SYSCTL_FILE"
print_status "net.core.somaxconn updated."
else
echo "$SYSCTL_SETTING" | sudo tee -a "$SYSCTL_FILE" > /dev/null
print_status "net.core.somaxconn = 1024 added."
fi
print_status "Applying network parameters..."
sudo sysctl -p || print_warning "Failed to apply sysctl -p."
# Final Output
print_header "MTProxy Installation Complete!"
echo -e "${GREEN}* ${NC}MTProxy has been successfully installed and is running in the background."
print_header "Management Commands"
echo "• Start:"
echo "sudo systemctl start mtproxy"
echo "• Stop:"
echo "sudo systemctl stop mtproxy"
echo "• Restart:"
echo "sudo systemctl restart mtproxy"
echo "• Status:"
echo "sudo systemctl status mtproxy"
echo "• Logs:"
echo "sudo journalctl -u mtproxy -f"
echo "• Update config:"
echo "sudo mtproxy-update"
echo "• Check external port status:"
echo "sudo ss -tulnp | grep mtproto-proxy"
echo "• Change port:"
echo "sudo $SCRIPT_NAME reinstall"
echo "• Change secret:"
echo "sudo $SCRIPT_NAME update-secret"
echo "• Uninstall completely:"
echo "sudo $SCRIPT_NAME delete"
print_header "MTProxy Details"
echo -e "${BLUE}IMPORTANT REMINDER!${NC}"
echo -e "${BLUE}Open the selected EXTERNAL port (${EXTERNAL_PORT}/tcp) in your Firewall and/or with your provider, if necessary!${NC}"
echo -e "${GREEN}*${NC} External Port (Internet <-> MTProxy): ${EXTERNAL_PORT}"
echo -e "${GREEN}*${NC} Internal Port (MTProxy <-> Telegram): ${INTERNAL_PORT}"
# Get server IP address for links
SERVER_IP=$(curl -s ifconfig.me 2>/dev/null || curl -s ipinfo.io/ip 2>/dev/null || echo "YOUR_PUBLIC_IP_ADDRESS")
if [ "$SERVER_IP" = "YOUR_PUBLIC_IP_ADDRESS" ]; then
print_warning "Could not determine public IP."
print_warning "Use your actual public IP instead of 'YOUR_PUBLIC_IP_ADDRESS'."
fi
echo -e "${GREEN}*${NC} Server Public IP: ${SERVER_IP}"
echo -e "${GREEN}*${NC} MTProxy Secret: ${SECRET}"
echo -e "${GREEN}*${NC} Link:"
echo -e "${GREEN}*${NC} https://t.me/proxy?server=${SERVER_IP}&port=${EXTERNAL_PORT}&secret=${SECRET}"
echo -e "${GREEN}*${NC} Link ONLY for the app:"
echo -e "${GREEN}*${NC} tg://proxy?server=${SERVER_IP}&port=${EXTERNAL_PORT}&secret=${SECRET}"
print_header "Enjoy!"