Repository navigation
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·279 lines (238 loc) · 7.11 KB
/
Copy pathstart.sh
File metadata and controls
executable file
·279 lines (238 loc) · 7.11 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
#!/bin/bash
# MTProto Proxy Startup Script
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
# Logging functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Load environment variables
load_env() {
if [[ -f .env ]]; then
log_info "Loading environment variables from .env"
export $(grep -v '^#' .env | xargs)
else
log_error ".env file not found. Please copy .env.example to .env and configure it."
exit 1
fi
}
# Validate configuration
validate_config() {
log_info "Validating configuration..."
if [[ -z "$MTG_SECRET" ]]; then
log_error "MTG_SECRET is not set. Please configure it in .env file."
exit 1
fi
# MTG v2 secrets can be either:
# - 32 chars (16 bytes hex) for simple mode
# - Longer for domain fronting (ee + 32 chars + domain hex)
if [[ ${#MTG_SECRET} -lt 32 ]]; then
log_error "MTG_SECRET must be at least 32 characters."
exit 1
fi
# Check if it's a domain fronting secret (starts with 'ee')
if [[ $MTG_SECRET == ee* ]]; then
log_info "Using domain fronting secret (${#MTG_SECRET} characters)"
else
log_info "Using simple secret (${#MTG_SECRET} characters)"
if [[ ${#MTG_SECRET} -ne 32 ]]; then
log_error "Simple secrets must be exactly 32 characters (16 bytes in hex)."
exit 1
fi
fi
if [[ -z "$MTG_PORT" ]]; then
MTG_PORT=8443
log_warning "MTG_PORT not set, using default: $MTG_PORT"
fi
if [[ -z "$SERVER_HOST" ]]; then
SERVER_HOST="localhost"
log_warning "SERVER_HOST not set, using default: $SERVER_HOST"
fi
log_success "Configuration validated"
}
# Check if mtg is installed
check_mtg() {
if ! command -v mtg &> /dev/null; then
log_error "mtg is not installed. Please run ./install.sh first."
exit 1
fi
log_info "mtg found: $(which mtg)"
}
# Create necessary directories
create_dirs() {
mkdir -p logs config
}
# Generate proxy link
generate_proxy_link() {
local secret_hex="$MTG_SECRET"
local secret_base64=$(echo -n "$secret_hex" | xxd -r -p | base64 | tr -d '=')
PROXY_LINK="https://t.me/proxy?server=${SERVER_HOST}&port=${MTG_PORT}&secret=${secret_hex}"
PROXY_LINK_TG="tg://proxy?server=${SERVER_HOST}&port=${MTG_PORT}&secret=${secret_hex}"
log_success "Proxy links generated:"
echo "Web link: $PROXY_LINK"
echo "Telegram link: $PROXY_LINK_TG"
echo ""
echo "QR Code for easy sharing:"
if command -v qrencode &> /dev/null; then
qrencode -t ANSIUTF8 "$PROXY_LINK"
else
echo "Install qrencode to display QR code: sudo apt-get install qrencode"
fi
}
# Start the proxy
start_proxy() {
log_info "Starting MTProto proxy..."
# Build mtg command
MTG_CMD="mtg"
# Add basic parameters
MTG_CMD="$MTG_CMD --bind ${MTG_BIND_IP:-0.0.0.0}:${MTG_PORT}"
MTG_CMD="$MTG_CMD --secret $MTG_SECRET"
# Add optional parameters
if [[ -n "$MTG_WORKERS" ]]; then
MTG_CMD="$MTG_CMD --workers $MTG_WORKERS"
fi
if [[ -n "$MTG_BUFFER_SIZE" ]]; then
MTG_CMD="$MTG_CMD --buffer-size $MTG_BUFFER_SIZE"
fi
if [[ "$MTG_SECURE_ONLY" == "true" ]]; then
MTG_CMD="$MTG_CMD --secure-only"
fi
if [[ "$MTG_DISABLE_IPV6" == "true" ]]; then
MTG_CMD="$MTG_CMD --disable-ipv6"
fi
if [[ -n "$MTG_DOMAIN" ]]; then
MTG_CMD="$MTG_CMD --domain $MTG_DOMAIN"
fi
if [[ "$MTG_STATS_ENABLED" == "true" && -n "$MTG_STATS_IP" && -n "$MTG_STATS_PORT" ]]; then
MTG_CMD="$MTG_CMD --stats ${MTG_STATS_IP}:${MTG_STATS_PORT}"
fi
if [[ -n "$MTG_ANTI_REPLAY_MAX_SIZE" ]]; then
MTG_CMD="$MTG_CMD --anti-replay-max-size $MTG_ANTI_REPLAY_MAX_SIZE"
fi
if [[ -n "$MTG_TIMEOUT" ]]; then
MTG_CMD="$MTG_CMD --timeout ${MTG_TIMEOUT}s"
fi
# Logging
case "${MTG_LOG_LEVEL:-info}" in
"debug") MTG_CMD="$MTG_CMD -vv" ;;
"info") MTG_CMD="$MTG_CMD -v" ;;
"warn"|"error") ;;
esac
log_info "Command: $MTG_CMD"
# Start the proxy
if [[ "$1" == "--daemon" ]]; then
log_info "Starting in daemon mode..."
nohup $MTG_CMD > logs/mtg.log 2>&1 &
echo $! > logs/mtg.pid
log_success "Proxy started in background (PID: $(cat logs/mtg.pid))"
else
log_info "Starting in foreground mode..."
$MTG_CMD 2>&1 | tee logs/mtg.log
fi
}
# Stop the proxy
stop_proxy() {
if [[ -f logs/mtg.pid ]]; then
PID=$(cat logs/mtg.pid)
if kill -0 $PID 2>/dev/null; then
log_info "Stopping proxy (PID: $PID)..."
kill $PID
rm logs/mtg.pid
log_success "Proxy stopped"
else
log_warning "Proxy process not found"
rm logs/mtg.pid
fi
else
log_warning "PID file not found"
fi
}
# Show status
show_status() {
if [[ -f logs/mtg.pid ]]; then
PID=$(cat logs/mtg.pid)
if kill -0 $PID 2>/dev/null; then
log_success "Proxy is running (PID: $PID)"
# Show statistics if available
if [[ "$MTG_STATS_ENABLED" == "true" ]]; then
log_info "Statistics available at: http://${MTG_STATS_IP:-127.0.0.1}:${MTG_STATS_PORT:-3001}/stats"
fi
generate_proxy_link
else
log_error "Proxy is not running (stale PID file)"
rm logs/mtg.pid
fi
else
log_error "Proxy is not running"
fi
}
# Main function
main() {
case "${1:-start}" in
"start")
load_env
validate_config
check_mtg
create_dirs
generate_proxy_link
start_proxy
;;
"daemon")
load_env
validate_config
check_mtg
create_dirs
generate_proxy_link
start_proxy --daemon
;;
"stop")
stop_proxy
;;
"restart")
stop_proxy
sleep 2
load_env
validate_config
check_mtg
create_dirs
start_proxy --daemon
;;
"status")
load_env
show_status
;;
"link")
load_env
validate_config
generate_proxy_link
;;
*)
echo "Usage: $0 {start|daemon|stop|restart|status|link}"
echo ""
echo "Commands:"
echo " start - Start proxy in foreground"
echo " daemon - Start proxy in background"
echo " stop - Stop proxy"
echo " restart - Restart proxy"
echo " status - Show proxy status"
echo " link - Show proxy links"
exit 1
;;
esac
}
# Run main function
main "$@"