-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmain.go
More file actions
643 lines (560 loc) · 17.9 KB
/
Copy pathmain.go
File metadata and controls
643 lines (560 loc) · 17.9 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
package main
import (
"context"
"embed"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io/fs"
"log"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime"
"runtime/debug"
"strings"
"sync"
"syscall"
"time"
"github.com/go-faster/errors"
"github.com/gotd/td/session"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/dcs"
)
//go:embed public
var publicFS embed.FS
var version = "dev"
const (
defaultHost = "127.0.0.1"
defaultPort = 3000
testAppID = 6
testAppHash = "eb06d4abfb49dc3eeb1aeb98ae0f581e"
// maxBatchSize entries at ~500 B of worst-case JSON each is ~5 MiB;
// maxBodySize leaves headroom above that. Exceeding either → 413.
maxBodySize = 8 * 1024 * 1024
maxBatchSize = 10_000
maxConcurrency = 50
defaultTimeout = 5
minTimeout = 3
maxTimeout = 30
tcpTimeout = 1500 * time.Millisecond
minTimeoutDuration = time.Duration(minTimeout) * time.Second
shutdownTimeout = 5 * time.Second
)
type dnsCacheEntry struct {
ips []net.IP
next time.Time
}
var (
dnsCacheMu sync.RWMutex
dnsCache = make(map[string]*dnsCacheEntry)
)
func cachedLookupHost(host string) ([]net.IP, error) {
dnsCacheMu.RLock()
entry, ok := dnsCache[host]
dnsCacheMu.RUnlock()
if ok && time.Now().Before(entry.next) {
return entry.ips, nil
}
dnsCtx, dnsCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer dnsCancel()
var resolver net.Resolver
ipAddrs, err := resolver.LookupIPAddr(dnsCtx, host)
if err != nil {
return nil, err
}
ips := make([]net.IP, len(ipAddrs))
for i, a := range ipAddrs {
ips[i] = a.IP
}
dnsCacheMu.Lock()
dnsCache[host] = &dnsCacheEntry{ips: ips, next: time.Now().Add(5 * time.Minute)}
dnsCacheMu.Unlock()
return ips, nil
}
type CheckRequest struct {
Server string `json:"server"`
Port int `json:"port"`
Secret string `json:"secret"`
Timeout int `json:"timeout,omitempty"`
}
type CheckResponse struct {
OK bool `json:"ok"`
Ping int64 `json:"ping,omitempty"`
}
func decodeSecret(s string) ([]byte, error) {
// The trim set overlaps the base64 alphabets ('+', '/', '_'), so the raw
// input must be tried before the trimmed one or a secret ending in those
// characters decodes to the wrong bytes. Hex is tried on both forms first
// so a hex secret with junk appended can't be misread as base64.
candidates := []string{s, strings.TrimRight(s, "!@#$%^&*()_+`~[]{}|;:',.<>?/ \t\n\r")}
for _, c := range candidates {
if b, err := hex.DecodeString(c); err == nil {
return b, nil
}
}
for _, c := range candidates {
for _, enc := range []*base64.Encoding{
base64.RawURLEncoding, base64.URLEncoding,
base64.RawStdEncoding, base64.StdEncoding,
} {
if b, err := enc.DecodeString(c); err == nil {
return b, nil
}
}
}
return nil, errors.Errorf("unable to decode secret %q as hex or base64", s)
}
// sharedSession is package-level and shared across all checks on purpose: the
// auth key negotiated by the first successful check is reused by every later
// one, so they skip the DH exchange that otherwise must complete inside the 2s
// ExchangeTimeout. This looks like a bug (mutable state shared across
// goroutines) and was "fixed" once — which took detection from 99/1022 to
// 0/1022. Do not make this per-check again; see the load-bearing rule in
// CLAUDE.md for the measurements.
var sharedSession = &session.StorageMemory{}
// newCheckOptions returns client options for one proxy check. All checks share
// sharedSession deliberately — a real Telegram client also reuses its auth key
// rather than running a fresh key exchange per connection.
func newCheckOptions(resolver dcs.Resolver) telegram.Options {
return telegram.Options{
Resolver: resolver,
SessionStorage: sharedSession,
DialTimeout: minTimeoutDuration,
ExchangeTimeout: 2 * time.Second,
NoUpdates: true,
Device: telegram.DeviceTDesktopWindows(),
}
}
func tcpCheck(server string, port int) error {
_, err := cachedLookupHost(server)
if err != nil {
return err
}
addr := net.JoinHostPort(server, fmt.Sprintf("%d", port))
conn, err := net.DialTimeout("tcp", addr, tcpTimeout)
if err != nil {
return err
}
conn.Close()
return nil
}
func checkProxy(ctx context.Context, server string, port int, secret string, timeoutSec int) (ping int64, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
log.Printf("PANIC in checkProxy %s:%d: %v\n%s", server, port, r, debug.Stack())
}
}()
addr := net.JoinHostPort(server, fmt.Sprintf("%d", port))
decodedSecret, err := decodeSecret(secret)
if err != nil {
return 0, errors.Wrap(err, "decode secret")
}
resolver, err := dcs.MTProxy(addr, decodedSecret, dcs.MTProxyOptions{})
if err != nil {
return 0, errors.Wrap(err, "create MTProxy resolver")
}
client := telegram.NewClient(testAppID, testAppHash, newCheckOptions(resolver))
checkCtx, cancel := context.WithTimeout(ctx, time.Duration(timeoutSec)*time.Second)
defer cancel()
var pingResult int64
err = client.Run(checkCtx, func(ctx context.Context) error {
start := time.Now()
_, apiErr := client.API().HelpGetNearestDC(ctx)
if apiErr != nil {
return errors.Wrap(apiErr, "help.getNearestDC")
}
pingResult = time.Since(start).Milliseconds()
return nil
})
if err != nil {
return 0, err
}
return pingResult, nil
}
// resolveAddr builds the listen address from the HOST and PORT env values.
// Loopback by default; exposing the server (e.g. HOST=0.0.0.0) is an explicit
// opt-in. PORT parsing is deliberately as lenient as it always was: the
// Sscanf error is ignored, so garbage keeps the default and a numeric prefix
// is used as-is.
func resolveAddr(hostEnv, portEnv string) string {
host := hostEnv
if host == "" {
host = defaultHost
}
port := defaultPort
if portEnv != "" {
fmt.Sscanf(portEnv, "%d", &port)
}
return net.JoinHostPort(host, fmt.Sprintf("%d", port))
}
// shouldOpenBrowser reports whether startup should try to launch a browser:
// only when NO_BROWSER is unset (any non-empty value suppresses) and the bound
// host is loopback. Binding a non-loopback address — HOST=0.0.0.0 on a
// headless server — suppresses the launch automatically.
func shouldOpenBrowser(addr, noBrowserEnv string) bool {
if noBrowserEnv != "" {
return false
}
host, _, err := net.SplitHostPort(addr)
if err != nil {
return false
}
if host == "localhost" {
return true
}
ip := net.ParseIP(host)
return ip != nil && ip.IsLoopback()
}
// browserCommand returns the platform launcher invocation for url.
func browserCommand(goos, url string) (string, []string) {
switch goos {
case "windows":
return "rundll32", []string{"url.dll,FileProtocolHandler", url}
case "darwin":
return "open", []string{url}
default:
return "xdg-open", []string{url}
}
}
// openBrowser fires the platform launcher without ever blocking the server: a
// missing launcher (minimal Linux without xdg-open) logs one line and moves on.
func openBrowser(url string) {
name, args := browserCommand(runtime.GOOS, url)
cmd := exec.Command(name, args...)
if err := cmd.Start(); err != nil {
log.Printf("Could not open browser: %v — open %s manually", err, url)
return
}
go func() { _ = cmd.Wait() }()
}
// readCheckRequests decodes a batch request body, enforcing maxBodySize and
// maxBatchSize. On failure it returns a non-zero HTTP status and a message the
// caller should send as {"error": msg}; on success status is 0.
func readCheckRequests(w http.ResponseWriter, r *http.Request) ([]CheckRequest, int, string) {
r.Body = http.MaxBytesReader(w, r.Body, maxBodySize)
var reqs []CheckRequest
if err := json.NewDecoder(r.Body).Decode(&reqs); err != nil {
var maxErr *http.MaxBytesError
if errors.As(err, &maxErr) {
return nil, http.StatusRequestEntityTooLarge,
fmt.Sprintf("request body exceeds %d bytes", maxBodySize)
}
return nil, http.StatusBadRequest, "invalid JSON"
}
if len(reqs) > maxBatchSize {
return nil, http.StatusRequestEntityTooLarge,
fmt.Sprintf("too many proxies: %d, max %d per request", len(reqs), maxBatchSize)
}
return reqs, 0, ""
}
func jsonResponse(w http.ResponseWriter, status int, v interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(v)
}
func main() {
mux := http.NewServeMux()
recoverMiddleware := func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rec := recover(); rec != nil {
log.Printf("PANIC HTTP %s %s: %v\n%s", r.Method, r.URL.Path, rec, debug.Stack())
jsonResponse(w, http.StatusInternalServerError, map[string]string{"error": "internal error"})
}
}()
next(w, r)
}
}
mux.HandleFunc("/check", recoverMiddleware(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
r.Body = http.MaxBytesReader(w, r.Body, maxBodySize)
var req CheckRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
var maxErr *http.MaxBytesError
if errors.As(err, &maxErr) {
jsonResponse(w, http.StatusRequestEntityTooLarge,
map[string]string{"error": fmt.Sprintf("request body exceeds %d bytes", maxBodySize)})
return
}
jsonResponse(w, http.StatusBadRequest, CheckResponse{OK: false})
return
}
timeout := req.Timeout
if timeout < minTimeout || timeout > maxTimeout {
timeout = defaultTimeout
}
start := time.Now()
ping, err := checkProxy(r.Context(), req.Server, req.Port, req.Secret, timeout)
elapsed := time.Since(start)
if err != nil {
log.Printf("CHECK FAIL %s:%d timeout=%ds (%v)", req.Server, req.Port, timeout, elapsed)
jsonResponse(w, http.StatusOK, CheckResponse{OK: false})
} else {
log.Printf("CHECK OK %s:%d %dms timeout=%ds (%v)", req.Server, req.Port, ping, timeout, elapsed)
jsonResponse(w, http.StatusOK, CheckResponse{OK: true, Ping: ping})
}
}))
mux.HandleFunc("/check-batch", recoverMiddleware(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Deprecated: removal planned for a future release. Scripts should
// use /check; streaming consumers /check-stream.
w.Header().Set("Deprecation", "true")
w.Header().Set("Link", `</check>; rel="alternate", </check-stream>; rel="successor-version"`)
log.Printf("DEPRECATED /check-batch hit from %s — use /check for scripting or /check-stream for streaming; removal planned in a future release", r.RemoteAddr)
reqs, status, msg := readCheckRequests(w, r)
if status != 0 {
jsonResponse(w, status, map[string]string{"error": msg})
return
}
limit := 10
if l := r.Header.Get("X-Concurrency"); l != "" {
fmt.Sscanf(l, "%d", &limit)
}
if limit < 1 {
limit = 1
}
if limit > maxConcurrency {
limit = maxConcurrency
}
timeout := defaultTimeout
if len(reqs) > 0 && reqs[0].Timeout >= minTimeout && reqs[0].Timeout <= maxTimeout {
timeout = reqs[0].Timeout
}
log.Printf("BATCH START %d proxies, concurrency=%d, timeout=%ds", len(reqs), limit, timeout)
start := time.Now()
results := make([]CheckResponse, len(reqs))
type indexedReq struct {
idx int
req CheckRequest
}
// Phase 1: TCP pre-check — filter dead proxies fast (~3s max)
tcpStart := time.Now()
var reachable []indexedReq
var reachableMu sync.Mutex
var tcpWg sync.WaitGroup
tcpSem := make(chan struct{}, limit)
for i, p := range reqs {
tcpWg.Add(1)
go func(idx int, proxy CheckRequest) {
defer tcpWg.Done()
tcpSem <- struct{}{}
defer func() { <-tcpSem }()
if err := tcpCheck(proxy.Server, proxy.Port); err != nil {
results[idx] = CheckResponse{OK: false}
} else {
reachableMu.Lock()
reachable = append(reachable, indexedReq{idx: idx, req: proxy})
reachableMu.Unlock()
}
}(i, p)
}
tcpWg.Wait()
log.Printf("TCP phase done: %d/%d reachable (%v)", len(reachable), len(reqs), time.Since(tcpStart))
// Phase 2: Full Telegram check — only for reachable proxies
telegramStart := time.Now()
telegramSem := make(chan struct{}, limit)
var telegramWg sync.WaitGroup
for _, ir := range reachable {
telegramWg.Add(1)
go func(item indexedReq) {
defer telegramWg.Done()
telegramSem <- struct{}{}
defer func() { <-telegramSem }()
t := item.req.Timeout
if t < minTimeout || t > maxTimeout {
t = defaultTimeout
}
ping, err := checkProxy(r.Context(), item.req.Server, item.req.Port, item.req.Secret, t)
if err != nil {
results[item.idx] = CheckResponse{OK: false}
} else {
results[item.idx] = CheckResponse{OK: true, Ping: ping}
}
}(ir)
}
telegramWg.Wait()
working := 0
for _, res := range results {
if res.OK {
working++
}
}
log.Printf("BATCH DONE %d/%d working | tcp=%v telegram=%v total=%v",
working, len(reqs), time.Since(tcpStart), time.Since(telegramStart), time.Since(start))
jsonResponse(w, http.StatusOK, results)
}))
mux.HandleFunc("/check-stream", recoverMiddleware(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
flusher, ok := w.(http.Flusher)
if !ok {
jsonResponse(w, http.StatusInternalServerError, map[string]string{"error": "streaming not supported"})
return
}
// Decode (and reject) before committing to SSE: a limit violation
// answers with plain 4xx JSON, not an empty event stream.
reqs, status, msg := readCheckRequests(w, r)
if status != 0 {
jsonResponse(w, status, map[string]string{"error": msg})
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
limit := 10
if l := r.Header.Get("X-Concurrency"); l != "" {
fmt.Sscanf(l, "%d", &limit)
}
if limit < 1 {
limit = 1
}
if limit > maxConcurrency {
limit = maxConcurrency
}
timeout := defaultTimeout
if len(reqs) > 0 && reqs[0].Timeout >= minTimeout && reqs[0].Timeout <= maxTimeout {
timeout = reqs[0].Timeout
}
total := len(reqs)
log.Printf("STREAM START %d proxies, concurrency=%d, timeout=%ds", total, limit, timeout)
type strProgress struct {
Completed int `json:"completed"`
Total int `json:"total"`
Working int `json:"working"`
Server string `json:"server"`
Port int `json:"port"`
Secret string `json:"secret"`
OK bool `json:"ok"`
Ping int64 `json:"ping,omitempty"`
}
sendEvent := func(event string, v interface{}) {
data, _ := json.Marshal(v)
fmt.Fprintf(w, "event: %s\ndata: %s\n\n", event, data)
flusher.Flush()
}
// Send initial progress
sendEvent("progress", &strProgress{Completed: 0, Total: total, Working: 0})
sem := make(chan struct{}, limit)
var mu sync.Mutex
var wg sync.WaitGroup
completed := 0
working := 0
for _, p := range reqs {
wg.Add(1)
go func(proxy CheckRequest) {
defer wg.Done()
sem <- struct{}{}
defer func() { <-sem }()
t := proxy.Timeout
if t < minTimeout || t > maxTimeout {
t = timeout
}
err := tcpCheck(proxy.Server, proxy.Port)
if err != nil {
mu.Lock()
completed++
sendEvent("progress", &strProgress{
Completed: completed, Total: total, Working: working,
Server: proxy.Server, Port: proxy.Port, Secret: proxy.Secret,
OK: false,
})
mu.Unlock()
return
}
// Hard timeout: never let a proxy hang longer than t+10s total
hardCtx, hardCancel := context.WithTimeout(r.Context(), time.Duration(t+10)*time.Second)
defer hardCancel()
type tgResult struct {
ping int64
err error
}
tgCh := make(chan tgResult, 1)
go func() {
ping, tgErr := checkProxy(hardCtx, proxy.Server, proxy.Port, proxy.Secret, t)
tgCh <- tgResult{ping, tgErr}
}()
var ping int64
var tgErr error
select {
case res := <-tgCh:
ping = res.ping
tgErr = res.err
case <-hardCtx.Done():
tgErr = hardCtx.Err()
}
mu.Lock()
completed++
if tgErr != nil {
sendEvent("progress", &strProgress{
Completed: completed, Total: total, Working: working,
Server: proxy.Server, Port: proxy.Port, Secret: proxy.Secret,
OK: false,
})
} else {
working++
sendEvent("progress", &strProgress{
Completed: completed, Total: total, Working: working,
Server: proxy.Server, Port: proxy.Port, Secret: proxy.Secret,
OK: true, Ping: ping,
})
}
mu.Unlock()
}(p)
}
wg.Wait()
log.Printf("STREAM DONE %d/%d working", working, total)
sendEvent("done", map[string]int{"working": working, "total": total})
}))
embeddedFS, err := fs.Sub(publicFS, "public")
if err != nil {
log.Fatalf("Failed to embed public directory: %v", err)
}
mux.Handle("/", http.FileServer(http.FS(embeddedFS)))
addr := resolveAddr(os.Getenv("HOST"), os.Getenv("PORT"))
log.Printf("MTProto Checker %s", version)
log.Printf("Server running at http://%s", addr)
srv := &http.Server{
Addr: addr,
Handler: mux,
ReadTimeout: 30 * time.Second,
WriteTimeout: 300 * time.Second,
IdleTimeout: 120 * time.Second,
}
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)
// Listen explicitly so the browser only opens once the address is
// actually bound; a bind failure dies here, before any launch attempt.
ln, err := net.Listen("tcp", addr)
if err != nil {
log.Fatalf("Listen error: %v", err)
}
go func() {
if err := srv.Serve(ln); err != nil && err != http.ErrServerClosed {
log.Fatalf("Server error: %v", err)
}
}()
if shouldOpenBrowser(addr, os.Getenv("NO_BROWSER")) {
openBrowser("http://" + addr)
}
<-done
log.Println("Shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("Shutdown error: %v", err)
}
log.Println("Server stopped")
}