-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-e2e.sh
More file actions
executable file
·699 lines (637 loc) · 22.2 KB
/
test-e2e.sh
File metadata and controls
executable file
·699 lines (637 loc) · 22.2 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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#!/usr/bin/env bash
# E2E test script for call-vpn relay-to-relay and direct modes
#
# Usage: ./test-e2e.sh [--n=4] [--links=1] [--monitor=5] [--download-url=URL] [--vk-token[=TOKEN]] [--direct[=HOST:PORT]]
#
# Options:
# --n=N Number of parallel connections per call (default: 4)
# --links=N Number of call links to use (default: 1, reads VK_CALL_LINK_1..N from .env)
# --monitor=MIN Enable periodic connectivity checks every 60s for MIN minutes.
# Without this flag, the script runs speed tests and exits.
# --download-url=U URL for large file download test (default: https://linkmeter.net)
# --vk-token Use VK tokens from .env (VK_TOKEN_1, VK_TOKEN_2)
# --vk-token=TOKEN Use specified VK token for both server and client
# --direct Direct mode: server listens on UDP, client connects via TURN.
# Uses DIRECT_SERVER from .env (e.g. 85.198.85.223:9000)
# --direct=H:P Direct mode with explicit server address
# --listen=ADDR Server listen address for direct mode (default: 0.0.0.0:9000)
# --proxy=URL Upstream proxy for server traffic (socks5://host:port or http://host:port)
#
# Examples:
# ./test-e2e.sh --n=1 # relay: 1 call × 1 conn
# ./test-e2e.sh --n=4 # relay: 1 call × 4 conns
# ./test-e2e.sh --n=1 --vk-token # relay + token from .env
# ./test-e2e.sh --n=4 --vk-token=vk1.a.X # relay + explicit token
# ./test-e2e.sh --n=4 --monitor=5 # relay + 5 min monitoring
# ./test-e2e.sh --n=2 --links=2 # relay: 2 calls × 2 conns
# ./test-e2e.sh --direct # direct: server on :9000, addr from .env
# ./test-e2e.sh --direct=85.198.85.223:9000 --n=2 # direct: explicit addr
# ./test-e2e.sh --download-url=http://example.com/100MB.bin
set -euo pipefail
cd "$(dirname "$0")"
# --- Config ---
CONNS=4
LINKS=1
MONITOR_MIN=0
SOCKS_PORT=2080
HTTP_PORT=3080
DOWNLOAD_URL="https://cdn.kernel.org/pub/linux/kernel/v6.x/patch-6.12.xz"
USE_VK_TOKEN="" # empty=anonymous, "env"=from .env, "vk1.a.X"=explicit
VERBOSE=""
STRIPING=""
CAPTCHA_PORT=18090
DIRECT_MODE="" # empty=relay, "env"=from .env, "host:port"=explicit
LISTEN_ADDR="0.0.0.0:9000"
USE_TELEMOST="" # "1" = use TELEMOST_LINK from .env instead of VK_CALL_LINK
UPSTREAM_PROXY="" # upstream proxy URL for server (socks5:// or http://)
for arg in "$@"; do
case $arg in
--n=*) CONNS="${arg#*=}" ;;
--links=*) LINKS="${arg#*=}" ;;
--monitor=*) MONITOR_MIN="${arg#*=}" ;;
--download-url=*) DOWNLOAD_URL="${arg#*=}" ;;
--vk-token=*) USE_VK_TOKEN="${arg#*=}" ;;
--vk-token) USE_VK_TOKEN="env" ;;
--direct=*) DIRECT_MODE="${arg#*=}" ;;
--direct) DIRECT_MODE="env" ;;
--listen=*) LISTEN_ADDR="${arg#*=}" ;;
--verbose) VERBOSE="--verbose" ;;
--striping) STRIPING=1 ;;
--telemost) USE_TELEMOST=1 ;;
--proxy=*) UPSTREAM_PROXY="${arg#*=}" ;;
esac
done
# --- Load .env ---
if [[ ! -f .env ]]; then
echo "FATAL: .env not found. See GET_TOKEN.md"
exit 1
fi
# Parse .env safely (handles values with $ and special chars)
while IFS='=' read -r key val; do
[[ -z "$key" || "$key" =~ ^# ]] && continue
val="${val%\"}" ; val="${val#\"}"
val="${val%\'}" ; val="${val#\'}"
export "$key=$val"
done < <(grep -v '^\s*#' .env | grep '=')
# Resolve direct mode server address
DIRECT_SERVER=""
if [[ -n "$DIRECT_MODE" ]]; then
if [[ "$DIRECT_MODE" == "env" ]]; then
if [[ -z "${DIRECT_SERVER_ADDR:-}" ]]; then
echo "FATAL: --direct requires DIRECT_SERVER_ADDR in .env (e.g. 85.198.85.223:9000)"
exit 1
fi
DIRECT_SERVER="$DIRECT_SERVER_ADDR"
else
DIRECT_SERVER="$DIRECT_MODE"
fi
fi
# Build link args based on --links=N (needed for both relay and direct modes — client uses TURN)
LINK_ARGS=""
if [[ -n "$USE_TELEMOST" ]]; then
# Telemost режим: используем TELEMOST_LINK (провайдер сам определит
# через IsTelemostLink и переключится в WebRTC-SFU транспорт).
if [[ -z "${TELEMOST_LINK:-}" ]]; then
echo "FATAL: TELEMOST_LINK not set in .env (required for --telemost)"
exit 1
fi
LINK_ARGS="--link=$TELEMOST_LINK"
elif [[ "$LINKS" -gt 1 ]]; then
for i in $(seq 1 "$LINKS"); do
varname="VK_CALL_LINK_$i"
if [[ -z "${!varname:-}" ]]; then
echo "FATAL: $varname not set in .env (needed for --links=$LINKS)"
exit 1
fi
LINK_ARGS="$LINK_ARGS --link=${!varname}"
done
else
if [[ -z "${VK_CALL_LINK:-}" ]]; then
echo "FATAL: VK_CALL_LINK not set in .env"
exit 1
fi
LINK_ARGS="--link=$VK_CALL_LINK"
fi
# Determine auth mode.
if [[ -z "${VPN_TOKEN:-}" ]]; then
echo "FATAL: VPN_TOKEN not set in .env"
exit 1
fi
AUTH_MODE="anonymous"
VK_TOKEN_ARGS_SERVER=""
VK_TOKEN_ARGS_CLIENT=""
if [[ -n "$USE_VK_TOKEN" ]]; then
if [[ "$USE_VK_TOKEN" == "env" ]]; then
# --vk-token (no value) → load from .env
if [[ -z "${VK_TOKEN_1:-}" || -z "${VK_TOKEN_2:-}" ]]; then
echo "FATAL: --vk-token requires VK_TOKEN_1 and VK_TOKEN_2 in .env"
exit 1
fi
AUTH_MODE="token (env)"
VK_TOKEN_ARGS_SERVER="--vk-token=$VK_TOKEN_2"
VK_TOKEN_ARGS_CLIENT="--vk-token=$VK_TOKEN_1"
else
# --vk-token=VALUE → use explicit token for both
AUTH_MODE="token (explicit)"
VK_TOKEN_ARGS_SERVER="--vk-token=$USE_VK_TOKEN"
VK_TOKEN_ARGS_CLIENT="--vk-token=$USE_VK_TOKEN"
fi
fi
TUNNEL_MODE="relay-to-relay"
if [[ -n "$DIRECT_SERVER" ]]; then
TUNNEL_MODE="direct → $DIRECT_SERVER (listen $LISTEN_ADDR)"
elif [[ -n "$USE_TELEMOST" ]]; then
TUNNEL_MODE="telemost → $TELEMOST_LINK"
fi
echo "=== E2E Test ==="
echo " Tunnel: $TUNNEL_MODE"
echo " Auth: $AUTH_MODE"
echo " Connections: $CONNS"
echo " Links: $LINKS"
echo " Monitor: ${MONITOR_MIN}m"
echo " Verbose: $([ -n "$VERBOSE" ] && echo yes || echo no)"
echo " Proxy: $([ -n "$UPSTREAM_PROXY" ] && echo "$UPSTREAM_PROXY" || echo none)"
echo " Striping: $([ -n "$STRIPING" ] && echo forced || echo adaptive)"
echo ""
# --- Helpers ---
ts() { date +"%H:%M:%S"; }
speed_fmt() {
local bps=$1
if (( $(echo "$bps > 1000000" | bc -l 2>/dev/null || echo 0) )); then
echo "$(echo "scale=2; $bps * 8 / 1000000" | bc) Mbps"
elif (( $(echo "$bps > 1000" | bc -l 2>/dev/null || echo 0) )); then
echo "$(echo "scale=1; $bps / 1024" | bc) KB/s"
else
echo "${bps} B/s"
fi
}
check_connectivity() {
local result
result=$(curl -x socks5://127.0.0.1:$SOCKS_PORT -s -o /dev/null \
-w "%{http_code} %{time_total}" \
--connect-timeout 10 --max-time 15 \
http://httpbin.org/get 2>/dev/null) || true
local code=$(echo "$result" | awk '{print $1}')
local time=$(echo "$result" | awk '{print $2}')
if [[ "$code" == "200" ]]; then
echo "[$(ts)] CONNECTIVITY: OK (${time}s)"
return 0
else
echo "[$(ts)] CONNECTIVITY: FAIL (code=$code, ${time}s)"
return 1
fi
}
# run_curl_speed: runs a single curl speed measurement
# Args: $1=label, $2=url, $3=max_time
run_curl_speed() {
local label=$1 url=$2 max_time=${3:-30}
echo -n "[$(ts)] ${label}: "
local r
r=$(curl -x socks5://127.0.0.1:$SOCKS_PORT -s -o /dev/null \
-w "%{size_download} %{speed_download} %{time_total}" \
--connect-timeout 15 --max-time "$max_time" \
"$url" 2>/dev/null) || true
local size=$(echo "$r" | awk '{print $1}')
local speed=$(echo "$r" | awk '{print $2}')
local ttime=$(echo "$r" | awk '{print $3}')
if [[ -n "$speed" && "$speed" != "0" && "$speed" != "0.000" ]]; then
echo "${size}b in ${ttime}s — $(speed_fmt "$speed")"
else
echo "FAILED (${size:-0}b, ${ttime:-timeout}s)"
fi
}
run_speed_test() {
local label=$1
echo ""
echo "====== SPEED TEST ($label) ======"
echo "[$(ts)] conns=$CONNS"
run_curl_speed "linkmeter.net" "https://linkmeter.net" 30
run_curl_speed "httpbin 100KB" "http://httpbin.org/stream-bytes/102400" 30
echo "================================="
}
run_download_test() {
echo ""
echo "====== DOWNLOAD TEST ======"
echo "[$(ts)] conns=$CONNS url=$DOWNLOAD_URL"
# Test via HTTP proxy (more reliable for large downloads)
echo -n "[$(ts)] HTTP proxy: "
local rh
rh=$(curl -x http://127.0.0.1:$HTTP_PORT -s -o /dev/null \
-w "%{size_download} %{speed_download} %{time_total}" \
--connect-timeout 15 --max-time 60 \
"$DOWNLOAD_URL" 2>/dev/null) || true
local sizeh=$(echo "$rh" | awk '{print $1}')
local speedh=$(echo "$rh" | awk '{print $2}')
local ttimeh=$(echo "$rh" | awk '{print $3}')
if [[ -n "$speedh" && "$speedh" != "0" && "$speedh" != "0.000" ]]; then
local human_sizeh
if (( $(echo "$sizeh > 1048576" | bc -l 2>/dev/null || echo 0) )); then
human_sizeh="$(echo "scale=1; $sizeh / 1048576" | bc) MB"
elif (( $(echo "$sizeh > 1024" | bc -l 2>/dev/null || echo 0) )); then
human_sizeh="$(echo "scale=0; $sizeh / 1024" | bc) KB"
else
human_sizeh="${sizeh}b"
fi
echo "${human_sizeh} in ${ttimeh}s — $(speed_fmt "$speedh")"
else
echo "FAILED (${sizeh:-0}b, ${ttimeh:-timeout}s)"
fi
# Test via SOCKS5 proxy
echo -n "[$(ts)] SOCKS5: "
local r
r=$(curl -x socks5://127.0.0.1:$SOCKS_PORT -s -o /dev/null \
-w "%{size_download} %{speed_download} %{time_total}" \
--connect-timeout 15 --max-time 60 \
"$DOWNLOAD_URL" 2>/dev/null) || true
local size=$(echo "$r" | awk '{print $1}')
local speed=$(echo "$r" | awk '{print $2}')
local ttime=$(echo "$r" | awk '{print $3}')
if [[ -n "$speed" && "$speed" != "0" && "$speed" != "0.000" ]]; then
local human_size
if (( $(echo "$size > 1048576" | bc -l 2>/dev/null || echo 0) )); then
human_size="$(echo "scale=1; $size / 1048576" | bc) MB"
elif (( $(echo "$size > 1024" | bc -l 2>/dev/null || echo 0) )); then
human_size="$(echo "scale=0; $size / 1024" | bc) KB"
else
human_size="${size}b"
fi
echo "${human_size} in ${ttime}s — $(speed_fmt "$speed")"
else
echo "FAILED (${size:-0}b, ${ttime:-timeout}s)"
fi
echo "==========================="
}
run_max_throughput_test() {
local total=$TOTAL_CONNS
local max_time=60
local tmpdir
tmpdir=$(mktemp -d)
echo ""
echo "====== MAX THROUGHPUT TEST ======"
echo "[$(ts)] $total parallel downloads via SOCKS5, url=$DOWNLOAD_URL"
# Launch N parallel curl processes
local pids=()
for i in $(seq 1 "$total"); do
curl -x socks5://127.0.0.1:$SOCKS_PORT -s -o /dev/null \
-w "%{size_download} %{speed_download} %{time_total}" \
--connect-timeout 15 --max-time "$max_time" \
"$DOWNLOAD_URL" > "$tmpdir/result_$i" 2>/dev/null &
pids+=($!)
done
# Wait for all to finish
for pid in "${pids[@]}"; do
wait "$pid" 2>/dev/null || true
done
# Aggregate results
local total_bytes=0
local total_speed=0
local max_time_taken=0
local ok=0
local failed=0
for i in $(seq 1 "$total"); do
local res
res=$(cat "$tmpdir/result_$i" 2>/dev/null) || true
local sz=$(echo "$res" | awk '{print $1}')
local sp=$(echo "$res" | awk '{print $2}')
local tt=$(echo "$res" | awk '{print $3}')
if [[ -n "$sp" && "$sp" != "0" && "$sp" != "0.000" ]]; then
total_bytes=$(awk "BEGIN{printf \"%.0f\", $total_bytes + ${sz:-0}}")
total_speed=$(awk "BEGIN{printf \"%.3f\", $total_speed + $sp}")
if awk "BEGIN{exit !(${tt:-0} > $max_time_taken)}" 2>/dev/null; then
max_time_taken=$tt
fi
ok=$((ok + 1))
local mbps_i=$(awk "BEGIN{printf \"%.2f\", $sp * 8 / 1000000}")
echo " [stream $i] ${mbps_i} Mbps (${tt}s)"
else
failed=$((failed + 1))
echo " [stream $i] FAILED"
fi
done
echo ""
if [[ $ok -gt 0 ]]; then
local human_total
if awk "BEGIN{exit !($total_bytes > 1048576)}" 2>/dev/null; then
human_total="$(awk "BEGIN{printf \"%.1f\", $total_bytes / 1048576}") MB"
elif awk "BEGIN{exit !($total_bytes > 1024)}" 2>/dev/null; then
human_total="$(awk "BEGIN{printf \"%.0f\", $total_bytes / 1024}") KB"
else
human_total="${total_bytes}b"
fi
local mbps_total=$(awk "BEGIN{printf \"%.2f\", $total_speed * 8 / 1000000}")
echo " AGGREGATE: ${human_total} in ${max_time_taken}s — ${mbps_total} Mbps"
echo " Streams: $ok ok, $failed failed"
else
echo " ALL STREAMS FAILED"
fi
rm -rf "$tmpdir"
echo "================================="
}
run_builtin_speedtest() {
echo ""
echo "====== BUILT-IN SPEED TEST ======"
echo "[$(ts)] Running speed test through tunnel..."
local tmpfile
tmpfile=$(mktemp)
# Stream JSON lines from the running client's HTTP proxy
curl -s -N http://127.0.0.1:$HTTP_PORT/__speedtest__ > "$tmpfile" 2>/dev/null || true
# Display progress lines (all but the last line)
local line_count
line_count=$(wc -l < "$tmpfile")
if [[ "$line_count" -gt 1 ]]; then
head -n $((line_count - 1)) "$tmpfile" | while IFS= read -r line; do
local phase=$(echo "$line" | jq -r '.phase // empty' 2>/dev/null)
local current_mbps=$(echo "$line" | jq -r '.current_mbps // empty' 2>/dev/null)
local elapsed=$(echo "$line" | jq -r '.elapsed_s // empty' 2>/dev/null)
local error=$(echo "$line" | jq -r '.error // empty' 2>/dev/null)
if [[ -n "$error" ]]; then
echo "[$(ts)] ERROR: $error"
elif [[ -n "$current_mbps" && -n "$elapsed" ]]; then
echo "[$(ts)] $phase: ${current_mbps} Mbps (${elapsed}s)"
elif [[ -n "$phase" ]]; then
echo "[$(ts)] Phase: $phase"
fi
done
fi
# Parse final result (last line)
local result
result=$(tail -n 1 "$tmpfile")
rm -f "$tmpfile"
if echo "$result" | jq -e '.ping' >/dev/null 2>&1; then
echo ""
local ping_avg=$(echo "$result" | jq -r '.ping.avg_ms')
local ping_jitter=$(echo "$result" | jq -r '.ping.jitter_ms')
local down_mbps=$(echo "$result" | jq -r '.download.mbps')
local up_mbps=$(echo "$result" | jq -r '.upload.mbps')
local num_conns=$(echo "$result" | jq -r '.connections | length')
echo " Ping: ${ping_avg}ms (jitter ${ping_jitter}ms)"
echo " Download: ${down_mbps} Mbps"
echo " Upload: ${up_mbps} Mbps"
echo " Connections: ${num_conns}"
echo "$result" | jq -r '.connections[] | " #\(.index): \(.down_mbps)/\(.up_mbps) Mbps, \(.latency_ms)ms"' 2>/dev/null
else
echo "[$(ts)] Speed test failed or returned no result"
fi
echo "================================="
}
SERVER_PID=""
CLIENT_PID=""
CAPTCHA_PID=""
cleanup() {
echo ""
echo "[$(ts)] Shutting down gracefully..."
if $IS_WINDOWS; then
# Kill the actual binaries (pipe makes $PID point to `sed`, not .exe).
taskkill //F //IM "callvpn-client${EXE}" >/dev/null 2>&1 || true
taskkill //F //IM "callvpn-server${EXE}" >/dev/null 2>&1 || true
taskkill //F //IM "callvpn-captcha${EXE}" >/dev/null 2>&1 || true
# Kill the sed pipe processes so `wait` doesn't hang.
kill -9 $CLIENT_PID 2>/dev/null || true
kill -9 $SERVER_PID 2>/dev/null || true
kill -9 $CAPTCHA_PID 2>/dev/null || true
else
kill -INT $CLIENT_PID 2>/dev/null || true
kill -INT $SERVER_PID 2>/dev/null || true
kill -INT $CAPTCHA_PID 2>/dev/null || true
sleep 3
kill -9 $CLIENT_PID 2>/dev/null || true
kill -9 $SERVER_PID 2>/dev/null || true
kill -9 $CAPTCHA_PID 2>/dev/null || true
fi
wait $CLIENT_PID 2>/dev/null || true
wait $SERVER_PID 2>/dev/null || true
wait $CAPTCHA_PID 2>/dev/null || true
echo "[$(ts)] Done."
}
# --- Platform detection ---
if [[ "$(uname -s)" == MINGW* || "$(uname -s)" == MSYS* || "$(uname -s)" == CYGWIN* ]]; then
IS_WINDOWS=true
EXE=".exe"
else
IS_WINDOWS=false
EXE=""
fi
trap cleanup EXIT
# --- Resign bundled scripts (auto-add new files, recompute hashes) ---
echo "[$(ts)] Updating bundled scripts manifest..."
bash scripts/resign-bundled.sh
# Clear local scripts cache so the fresh bundled version takes priority.
rm -rf var/scripts/current
# --- Build ---
echo "[$(ts)] Building binaries..."
go build -o "callvpn-server${EXE}" ./cmd/server
go build -o "callvpn-client${EXE}" ./cmd/client
go build -o "callvpn-captcha${EXE}" ./cmd/captcha-service
echo "[$(ts)] Build OK"
# --- Kill stale processes ---
if $IS_WINDOWS; then
taskkill //F //IM "callvpn-server${EXE}" >/dev/null 2>&1 || true
taskkill //F //IM "callvpn-client${EXE}" >/dev/null 2>&1 || true
taskkill //F //IM "callvpn-captcha${EXE}" >/dev/null 2>&1 || true
else
pkill -f "callvpn-server" 2>/dev/null || true
pkill -f "callvpn-client" 2>/dev/null || true
pkill -f "callvpn-captcha" 2>/dev/null || true
fi
sleep 1
# --- Start captcha-service (not needed in direct/telemost modes) ---
CAPTCHA_ENDPOINT=""
if [[ -z "$DIRECT_SERVER" && -z "$USE_TELEMOST" ]]; then
echo "[$(ts)] Starting captcha-service on port $CAPTCHA_PORT..."
./callvpn-captcha${EXE} \
--port="$CAPTCHA_PORT" \
--max-concurrent=1 \
--solve-timeout=90s \
--request-timeout=2m \
$VERBOSE \
2>&1 | sed "s/^/ [captcha] /" &
CAPTCHA_PID=$!
sleep 2
if ! kill -0 $CAPTCHA_PID 2>/dev/null; then
echo "FATAL: captcha-service died"
exit 1
fi
echo "[$(ts)] Captcha-service running (PID $CAPTCHA_PID)"
CAPTCHA_ENDPOINT="http://127.0.0.1:$CAPTCHA_PORT"
fi
# --- Start server ---
echo ""
TOTAL_CONNS=$((CONNS * LINKS))
PROXY_ARG=""
if [[ -n "$UPSTREAM_PROXY" ]]; then
PROXY_ARG="--proxy=$UPSTREAM_PROXY"
fi
if [[ -n "$DIRECT_SERVER" ]]; then
echo "[$(ts)] Starting server in DIRECT mode (listen=$LISTEN_ADDR)..."
VK_CALL_LINK="" ./callvpn-server${EXE} \
--listen="$LISTEN_ADDR" \
--token="$VPN_TOKEN" \
$PROXY_ARG \
$VERBOSE \
2>&1 | sed "s/^/ [server] /" &
SERVER_PID=$!
sleep 3
elif [[ -n "$USE_TELEMOST" ]]; then
echo "[$(ts)] Starting server in TELEMOST mode (n=$CONNS)..."
ENABLE_STRIPING=${STRIPING:-} ./callvpn-server${EXE} \
$LINK_ARGS \
--n="$CONNS" \
--token="$VPN_TOKEN" \
$PROXY_ARG \
$VERBOSE \
2>&1 | sed "s/^/ [server] /" &
SERVER_PID=$!
sleep 15
else
echo "[$(ts)] Starting server (links=$LINKS, n=$CONNS, total=$TOTAL_CONNS)..."
ENABLE_STRIPING=${STRIPING:-} ./callvpn-server${EXE} \
$LINK_ARGS \
--tcp=true \
--n="$CONNS" \
--token="$VPN_TOKEN" \
--captcha-endpoint="$CAPTCHA_ENDPOINT" \
$VK_TOKEN_ARGS_SERVER \
$PROXY_ARG \
$VERBOSE \
2>&1 | sed "s/^/ [server] /" &
SERVER_PID=$!
sleep 12
fi
if $IS_WINDOWS; then
# On Windows/MSYS, kill -0 is unreliable with piped processes.
# Check by process name instead.
if ! tasklist //FI "IMAGENAME eq callvpn-server${EXE}" 2>/dev/null | grep -qi "callvpn-server"; then
echo "FATAL: server died"
exit 1
fi
else
if ! kill -0 $SERVER_PID 2>/dev/null; then
echo "FATAL: server died"
exit 1
fi
fi
echo "[$(ts)] Server running (PID $SERVER_PID)"
# --- Start client ---
echo ""
if [[ -n "$DIRECT_SERVER" ]]; then
echo "[$(ts)] Starting client in DIRECT mode (server=$DIRECT_SERVER, n=$CONNS)..."
echo "[$(ts)] Captcha will open in browser for manual solving"
ENABLE_STRIPING=${STRIPING:-} ./callvpn-client${EXE} \
$LINK_ARGS \
--server="$DIRECT_SERVER" \
--n="$CONNS" \
--tcp=true \
--token="$VPN_TOKEN" \
--socks5-port=$SOCKS_PORT \
--http-port=$HTTP_PORT \
$VK_TOKEN_ARGS_CLIENT \
$VERBOSE \
2>&1 | sed "s/^/ [client] /" &
CLIENT_PID=$!
elif [[ -n "$USE_TELEMOST" ]]; then
echo "[$(ts)] Starting client in TELEMOST mode (n=$CONNS)..."
ENABLE_STRIPING=${STRIPING:-} ./callvpn-client${EXE} \
$LINK_ARGS \
--n="$CONNS" \
--token="$VPN_TOKEN" \
--socks5-port=$SOCKS_PORT \
--http-port=$HTTP_PORT \
$VERBOSE \
2>&1 | sed "s/^/ [client] /" &
CLIENT_PID=$!
else
echo "[$(ts)] Starting client (links=$LINKS, n=$CONNS, total=$TOTAL_CONNS)..."
ENABLE_STRIPING=${STRIPING:-} ./callvpn-client${EXE} \
$LINK_ARGS \
--n="$CONNS" \
--tcp=true \
--token="$VPN_TOKEN" \
--socks5-port=$SOCKS_PORT \
--http-port=$HTTP_PORT \
--captcha-endpoint="$CAPTCHA_ENDPOINT" \
$VK_TOKEN_ARGS_CLIENT \
$VERBOSE \
2>&1 | sed "s/^/ [client] /" &
CLIENT_PID=$!
fi
# --- Wait for proxy ---
echo ""
PROXY_ATTEMPTS=90
if [[ -n "$DIRECT_SERVER" ]]; then
PROXY_ATTEMPTS=180 # 6 min — allow time for manual captcha solving
fi
echo "[$(ts)] Waiting for proxy (socks5://127.0.0.1:$SOCKS_PORT)..."
READY=0
for i in $(seq 1 $PROXY_ATTEMPTS); do
if ! kill -0 $CLIENT_PID 2>/dev/null; then
echo "FATAL: client died"
exit 1
fi
if curl -x socks5://127.0.0.1:$SOCKS_PORT -s -o /dev/null \
-w "%{http_code}" --connect-timeout 3 --max-time 5 \
http://httpbin.org/get 2>/dev/null | grep -q 200; then
READY=1
echo "[$(ts)] Proxy ready after ${i} attempts (~$((i*2))s)"
break
fi
sleep 2
done
if [[ $READY -ne 1 ]]; then
echo "FATAL: proxy not ready after $((PROXY_ATTEMPTS*2))s"
exit 1
fi
# --- IP check ---
echo ""
echo "====== IP CHECK ======"
DIRECT_IP=$(curl -s --max-time 10 httpbin.org/ip 2>/dev/null | grep -oP '"origin":\s*"\K[^"]+' || echo "unknown")
TUNNEL_IP=$(curl -x socks5://127.0.0.1:$SOCKS_PORT -s --max-time 10 httpbin.org/ip 2>/dev/null | grep -oP '"origin":\s*"\K[^"]+' || echo "unknown")
echo " Direct IP: $DIRECT_IP"
echo " Tunnel IP: $TUNNEL_IP"
if [[ "$DIRECT_IP" == "$TUNNEL_IP" && "$DIRECT_IP" != "unknown" ]]; then
echo " WARNING: IPs match — traffic may not be proxied!"
fi
echo "======================"
# --- Speed test ---
run_speed_test "n=$CONNS"
# --- Download test ---
run_download_test
# --- Max throughput test ---
run_max_throughput_test
# --- Built-in speed test (skip in direct mode — protocol mismatch) ---
if [[ -z "$DIRECT_SERVER" ]]; then
run_builtin_speedtest
fi
# --- Monitoring (optional) ---
if [[ $MONITOR_MIN -gt 0 ]]; then
echo ""
echo "====== MONITORING (${MONITOR_MIN} min, check every 60s) ======"
START_TIME=$(date +%s)
END_TIME=$((START_TIME + MONITOR_MIN * 60))
MINUTE=0
while true; do
sleep 60
MINUTE=$((MINUTE + 1))
NOW=$(date +%s)
if ! kill -0 $SERVER_PID 2>/dev/null; then
echo "[$(ts)] WARNING: server died at T+${MINUTE}m"
break
fi
if ! kill -0 $CLIENT_PID 2>/dev/null; then
echo "[$(ts)] WARNING: client died at T+${MINUTE}m"
break
fi
echo -n "T+${MINUTE}m "
check_connectivity
if [[ $NOW -ge $END_TIME ]]; then
break
fi
done
# Final speed test after monitoring
if kill -0 $CLIENT_PID 2>/dev/null; then
run_speed_test "final (T+${MONITOR_MIN}m)"
else
echo "[$(ts)] Skipping final speed test — client not running"
fi
fi
echo ""
echo "====== TEST COMPLETE ======"