Refactor user link resolution in main.go to include a new function for rewriting links with a specified host, improving link handling and ensuring consistent server parameters in tg://proxy URLs.
Publish telemt-bot Docker image / build-and-push (push) Successful in 1m3s

This commit is contained in:
Denozordec
2026-03-15 21:22:09 +07:00
parent f1bcc205cc
commit 02db3a2c91
+57 -25
View File
@@ -12,6 +12,7 @@ import (
"net/url" "net/url"
"os" "os"
"os/signal" "os/signal"
"regexp"
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
@@ -385,36 +386,67 @@ func hasAnyLinks(links userLinks) bool {
return len(links.Classic) > 0 || len(links.Secure) > 0 || len(links.TLS) > 0 return len(links.Classic) > 0 || len(links.Secure) > 0 || len(links.TLS) > 0
} }
// serverParamRe matches server=... in tg://proxy URLs
var serverParamRe = regexp.MustCompile(`server=[^&]+`)
func (b *bot) rewriteLinksWithHost(links userLinks) userLinks {
host := strings.TrimSpace(b.cfg.TelemtLinkHost)
if host == "" {
return links
}
replacement := "server=" + host
rewrite := func(items []string) []string {
if len(items) == 0 {
return items
}
out := make([]string, len(items))
for i, s := range items {
out[i] = serverParamRe.ReplaceAllString(s, replacement)
}
return out
}
return userLinks{
Classic: rewrite(links.Classic),
Secure: rewrite(links.Secure),
TLS: rewrite(links.TLS),
}
}
func (b *bot) resolveUserLinks(ctx context.Context, username, secret string, initial userLinks) userLinks { func (b *bot) resolveUserLinks(ctx context.Context, username, secret string, initial userLinks) userLinks {
var result userLinks
if hasAnyLinks(initial) { if hasAnyLinks(initial) {
return initial result = initial
} } else {
var fetched userInfo
var fetched userInfo path := "/v1/users/" + url.PathEscape(username)
path := "/v1/users/" + url.PathEscape(username) if err := b.callTelemt(ctx, path, &fetched); err == nil && hasAnyLinks(fetched.Links) {
if err := b.callTelemt(ctx, path, &fetched); err == nil && hasAnyLinks(fetched.Links) { result = fetched.Links
return fetched.Links } else {
} var users []userInfo
if err := b.callTelemt(ctx, "/v1/users", &users); err == nil {
var users []userInfo for i := range users {
if err := b.callTelemt(ctx, "/v1/users", &users); err == nil { if users[i].Username == username && hasAnyLinks(users[i].Links) {
for i := range users { result = users[i].Links
if users[i].Username == username && hasAnyLinks(users[i].Links) { break
return users[i].Links }
}
}
if !hasAnyLinks(result) {
if err := b.callTelemt(ctx, "/v1/stats/users", &users); err == nil {
for i := range users {
if users[i].Username == username && hasAnyLinks(users[i].Links) {
result = users[i].Links
break
}
}
}
}
if !hasAnyLinks(result) {
result = b.generateLinksFromSecret(secret)
} }
} }
} }
return b.rewriteLinksWithHost(result)
users = nil
if err := b.callTelemt(ctx, "/v1/stats/users", &users); err == nil {
for i := range users {
if users[i].Username == username && hasAnyLinks(users[i].Links) {
return users[i].Links
}
}
}
return b.generateLinksFromSecret(secret)
} }
func (b *bot) generateLinksFromSecret(secret string) userLinks { func (b *bot) generateLinksFromSecret(secret string) userLinks {