diff options
| author | Saeid <43953720+surbiks@users.noreply.github.com> | 2024-01-02 12:42:07 +0300 |
|---|---|---|
| committer | MHSanaei <ho3ein.sanaei@gmail.com> | 2024-01-03 15:59:29 +0300 |
| commit | 5e3478f1c16e0c42a299bcfce3e0b567bf4c2f7f (patch) | |
| tree | 3592a4c8c146ea669e777d5e8909de79410a2cca /web/service | |
| parent | c76199514a62c921901b2b2a1c4dd3f93ff8df2a (diff) | |
socks5 proxy option added to telegram bot settings (#1500)
* socks5 option added to telegram bot settings
* update socks5 proxy settings translations
Diffstat (limited to 'web/service')
| -rw-r--r-- | web/service/setting.go | 9 | ||||
| -rw-r--r-- | web/service/tgbot.go | 27 |
2 files changed, 35 insertions, 1 deletions
diff --git a/web/service/setting.go b/web/service/setting.go index 89d3f400..25770373 100644 --- a/web/service/setting.go +++ b/web/service/setting.go @@ -38,6 +38,7 @@ var defaultValueMap = map[string]string{ "timeLocation": "Asia/Tehran", "tgBotEnable": "false", "tgBotToken": "", + "tgBotProxy": "", "tgBotChatId": "", "tgRunTime": "@daily", "tgBotBackup": "false", @@ -245,6 +246,14 @@ func (s *SettingService) SetTgBotToken(token string) error { return s.setString("tgBotToken", token) } +func (s *SettingService) GetTgBotProxy() (string, error) { + return s.getString("tgBotProxy") +} + +func (s *SettingService) SetTgBotProxy(token string) error { + return s.setString("tgBotProxy", token) +} + func (s *SettingService) GetTgBotChatId() (string, error) { return s.getString("tgBotChatId") } diff --git a/web/service/tgbot.go b/web/service/tgbot.go index 9f11c19d..fdb5d312 100644 --- a/web/service/tgbot.go +++ b/web/service/tgbot.go @@ -4,6 +4,7 @@ import ( "embed" "fmt" "net" + "net/url" "os" "strconv" "strings" @@ -21,6 +22,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 @@ -90,7 +93,12 @@ func (t *Tgbot) Start(i18nFS embed.FS) error { } } - bot, err = telego.NewBot(tgBottoken) + tgBotProxy, err := t.settingService.GetTgBotProxy() + if err != nil { + logger.Warning("Failed to get ProxyUrl:", err) + } + + bot, err = t.NewBot(tgBottoken, tgBotProxy) if err != nil { fmt.Println("Get tgbot's api error:", err) return err @@ -106,6 +114,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 } |
