init
Publish mtproxy_checker Docker image / test (push) Successful in 19s
Publish mtproxy_checker Docker image / build-and-push (push) Successful in 48s

This commit is contained in:
Denozordec
2026-04-11 00:38:22 +07:00
commit 4905c06b9b
26 changed files with 1887 additions and 0 deletions
+83
View File
@@ -0,0 +1,83 @@
package main
import (
"context"
"errors"
"flag"
"fmt"
"os"
"time"
"mtproxy_checker/internal/checker"
"mtproxy_checker/internal/parseurl"
"mtproxy_checker/internal/secret"
)
func main() {
os.Exit(run())
}
func run() int {
timeout := flag.Duration("timeout", 15*time.Second, "overall TCP/handshake timeout")
dcID := flag.Int("dc-id", 2, "Telegram DC id (signed int16) embedded in MTProxy header")
server := flag.String("server", "", "proxy hostname (if not using tg:// positional)")
portFlag := flag.Int("port", 0, "proxy port (if not using tg:// positional)")
secFlag := flag.String("secret", "", "hex secret (if not using tg:// positional)")
flag.Parse()
args := flag.Args()
var host string
var port int
var secStr string
if len(args) >= 1 && (hasPrefix(args[0], "tg://") || hasPrefix(args[0], "TG://")) {
t, err := parseurl.ParseTGProxy(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "parse tg url: %v\n", err)
return 2
}
host, port, secStr = t.Host, t.Port, t.Secret
} else {
if *server == "" || *portFlag == 0 || *secFlag == "" {
fmt.Fprintf(os.Stderr, "usage: %s [flags] tg://proxy?server=HOST&port=PORT&secret=HEX\n", os.Args[0])
fmt.Fprintf(os.Stderr, " or: %s --server HOST --port PORT --secret HEX\n", os.Args[0])
flag.PrintDefaults()
return 2
}
host, port, secStr = *server, *portFlag, *secFlag
}
parsed, err := secret.Parse(secStr)
if err != nil {
fmt.Fprintf(os.Stderr, "secret: %v\n", err)
return 2
}
if *dcID < -32768 || *dcID > 32767 {
fmt.Fprintf(os.Stderr, "dc-id out of int16 range\n")
return 2
}
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
defer cancel()
err = checker.Check(ctx, host, port, parsed, int16(*dcID))
if err != nil {
if errors.Is(err, checker.ErrProxyClosed) {
fmt.Fprintf(os.Stderr, "FAIL: %v\n", err)
return 3
}
if errors.Is(err, context.DeadlineExceeded) {
fmt.Fprintf(os.Stderr, "FAIL: timeout\n")
return 4
}
fmt.Fprintf(os.Stderr, "FAIL: %v\n", err)
return 1
}
fmt.Println("OK")
return 0
}
func hasPrefix(s, p string) bool {
return len(s) >= len(p) && s[:len(p)] == p
}