Files
mtproxy_checker/internal/parseurl/parseurl.go
T
Denozordec 4905c06b9b
Publish mtproxy_checker Docker image / test (push) Successful in 19s
Publish mtproxy_checker Docker image / build-and-push (push) Successful in 48s
init
2026-04-11 00:38:22 +07:00

58 lines
1.3 KiB
Go

package parseurl
import (
"fmt"
"net/url"
"strconv"
"strings"
)
// Target is host, port, secret string from tg://proxy or explicit flags.
type Target struct {
Host string
Port int
Secret string
}
// ParseTGProxy parses tg://proxy?server=&port=&secret= or t.me/proxy style query.
func ParseTGProxy(raw string) (*Target, error) {
raw = strings.TrimSpace(raw)
if raw == "" {
return nil, fmt.Errorf("empty proxy url")
}
u, err := url.Parse(raw)
if err != nil {
return nil, err
}
if u.Scheme != "tg" {
return nil, fmt.Errorf("expected tg:// scheme, got %q", u.Scheme)
}
host := u.Hostname()
if host != "" && host != "proxy" {
return nil, fmt.Errorf("unexpected tg host %q", host)
}
q := u.Query()
server := strings.TrimSpace(q.Get("server"))
if server == "" {
return nil, fmt.Errorf("missing server parameter")
}
portStr := strings.TrimSpace(q.Get("port"))
if portStr == "" {
return nil, fmt.Errorf("missing port parameter")
}
port, err := strconv.Atoi(portStr)
if err != nil || port < 1 || port > 65535 {
return nil, fmt.Errorf("invalid port %q", portStr)
}
sec := strings.TrimSpace(q.Get("secret"))
if sec == "" {
return nil, fmt.Errorf("missing secret parameter")
}
return &Target{Host: server, Port: port, Secret: sec}, nil
}