From d04db44190dff44983be4ccbc2627d0d12ac0c31 Mon Sep 17 00:00:00 2001 From: madrinx Date: Wed, 16 Oct 2024 20:05:22 +0330 Subject: [PATCH] feat(tgbot): Add validate for API Server URL --- web/service/tgbot.go | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/web/service/tgbot.go b/web/service/tgbot.go index 0ce07352..257fcc94 100644 --- a/web/service/tgbot.go +++ b/web/service/tgbot.go @@ -137,22 +137,33 @@ func (t *Tgbot) NewBot(token string, proxyUrl string, apiServerUrl string) (*tel return telego.NewBot(token) } - if !strings.HasPrefix(proxyUrl, "socks5://") { - logger.Warning("Invalid socks5 URL, starting with default") - return telego.NewBot(token) - } - - _, err := url.Parse(proxyUrl) - if err != nil { - logger.Warning("Can't parse proxy URL, using default instance for tgbot:", err) - return telego.NewBot(token) - } - if proxyUrl != "" { + if !strings.HasPrefix(proxyUrl, "socks5://") { + logger.Warning("Invalid socks5 URL, starting with default") + return telego.NewBot(token) + } + + _, err := url.Parse(proxyUrl) + if err != nil { + logger.Warning("Can't parse proxy URL, using default instance for tgbot:", err) + return telego.NewBot(token) + } + return telego.NewBot(token, telego.WithFastHTTPClient(&fasthttp.Client{ Dial: fasthttpproxy.FasthttpSocksDialer(proxyUrl), })) } else { + if !strings.HasPrefix(proxyUrl, "http") { + logger.Warning("Invalid http(s) URL, starting with default") + return telego.NewBot(token) + } + + _, err := url.Parse(proxyUrl) + if err != nil { + logger.Warning("Can't parse API server URL, using default instance for tgbot:", err) + return telego.NewBot(token) + } + return telego.NewBot(token, telego.WithAPIServer(apiServerUrl)) }