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
+45 -13
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
} }
func (b *bot) resolveUserLinks(ctx context.Context, username, secret string, initial userLinks) userLinks { // serverParamRe matches server=... in tg://proxy URLs
if hasAnyLinks(initial) { var serverParamRe = regexp.MustCompile(`server=[^&]+`)
return initial
}
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 {
var result userLinks
if hasAnyLinks(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) {
return fetched.Links result = fetched.Links
} } else {
var users []userInfo var users []userInfo
if err := b.callTelemt(ctx, "/v1/users", &users); err == nil { if err := b.callTelemt(ctx, "/v1/users", &users); err == nil {
for i := range users { for i := range users {
if users[i].Username == username && hasAnyLinks(users[i].Links) { if users[i].Username == username && hasAnyLinks(users[i].Links) {
return users[i].Links result = users[i].Links
break
} }
} }
} }
if !hasAnyLinks(result) {
users = nil
if err := b.callTelemt(ctx, "/v1/stats/users", &users); err == nil { if err := b.callTelemt(ctx, "/v1/stats/users", &users); err == nil {
for i := range users { for i := range users {
if users[i].Username == username && hasAnyLinks(users[i].Links) { if users[i].Username == username && hasAnyLinks(users[i].Links) {
return users[i].Links result = users[i].Links
break
} }
} }
} }
}
return b.generateLinksFromSecret(secret) if !hasAnyLinks(result) {
result = b.generateLinksFromSecret(secret)
}
}
}
return b.rewriteLinksWithHost(result)
} }
func (b *bot) generateLinksFromSecret(secret string) userLinks { func (b *bot) generateLinksFromSecret(secret string) userLinks {