From dfdcebc28215f177169d545166991a1104d01a75 Mon Sep 17 00:00:00 2001 From: Surbiks Date: Thu, 14 Dec 2023 12:28:39 +0330 Subject: [PATCH] use socks in tg bot --- web/service/tgbot.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/web/service/tgbot.go b/web/service/tgbot.go index edbf3c03..f199a094 100644 --- a/web/service/tgbot.go +++ b/web/service/tgbot.go @@ -4,6 +4,7 @@ import ( "embed" "fmt" "net" + "net/url" "os" "strconv" "strings" @@ -20,6 +21,8 @@ import ( "github.com/mymmrac/telego" th "github.com/mymmrac/telego/telegohandler" tu "github.com/mymmrac/telego/telegoutil" + "github.com/valyala/fasthttp" + "github.com/valyala/fasthttp/fasthttpproxy" ) var bot *telego.Bot @@ -93,10 +96,10 @@ func (t *Tgbot) Start(i18nFS embed.FS) error { if err != nil || tgBotProxy == "" { logger.Warning("Telegram proxy not valid use direct connection") } else { - + } - bot, err = telego.NewBot(tgBottoken) + bot, err = t.NewBot(tgBottoken, tgBotProxy) if err != nil { fmt.Println("Get tgbot's api error:", err) return err @@ -112,6 +115,23 @@ func (t *Tgbot) Start(i18nFS embed.FS) error { return nil } +func (t *Tgbot) NewBot(token string, proxyUrl string) (*telego.Bot, error) { + if proxyUrl == "" || !strings.HasPrefix(proxyUrl, "socks5://") { + logger.Warning("invalid socks5 url, start with default") + return telego.NewBot(token) + } + + _, err := url.Parse(proxyUrl) + if err != nil { + logger.Warning("cant parse proxy url, use default instance for tgbot:", err) + return telego.NewBot(token) + } + + return telego.NewBot(token, telego.WithFastHTTPClient(&fasthttp.Client{ + Dial: fasthttpproxy.FasthttpSocksDialer(proxyUrl), + })) +} + func (t *Tgbot) IsRunning() bool { return isRunning }