socks5 option added to telegram bot settings

This commit is contained in:
Surbiks 2023-12-20 09:37:14 +03:30
parent c0d8f931da
commit 1635dbf8be
7 changed files with 43 additions and 2 deletions

View file

@ -14,6 +14,7 @@ class AllSetting {
this.remarkModel = "-ieo";
this.tgBotEnable = false;
this.tgBotToken = "";
this.tgBotProxy = "";
this.tgBotChatId = "";
this.tgRunTime = "@daily";
this.tgBotBackup = false;

View file

@ -28,6 +28,7 @@ type AllSetting struct {
RemarkModel string `json:"remarkModel" form:"remarkModel"`
TgBotEnable bool `json:"tgBotEnable" form:"tgBotEnable"`
TgBotToken string `json:"tgBotToken" form:"tgBotToken"`
TgBotProxy string `json:"tgBotProxy" form:"tgBotProxy"`
TgBotChatId string `json:"tgBotChatId" form:"tgBotChatId"`
TgRunTime string `json:"tgRunTime" form:"tgRunTime"`
TgBotBackup bool `json:"tgBotBackup" form:"tgBotBackup"`

View file

@ -243,6 +243,7 @@
<setting-list-item type="switch" title='{{ i18n "pages.settings.tgNotifyBackup" }}' desc='{{ i18n "pages.settings.tgNotifyBackupDesc" }}' v-model="allSetting.tgBotBackup"></setting-list-item>
<setting-list-item type="switch" title='{{ i18n "pages.settings.tgNotifyLogin" }}' desc='{{ i18n "pages.settings.tgNotifyLoginDesc" }}' v-model="allSetting.tgBotLoginNotify"></setting-list-item>
<setting-list-item type="number" title='{{ i18n "pages.settings.tgNotifyCpu" }}' desc='{{ i18n "pages.settings.tgNotifyCpuDesc" }}' v-model="allSetting.tgCpu" :min="0" :max="100"></setting-list-item>
<setting-list-item type="text" title='{{ i18n "pages.settings.telegramProxy"}}' desc='{{ i18n "pages.settings.telegramProxyDesc"}}' v-model="allSetting.tgBotProxy"></setting-list-item>
<a-list-item>
<a-row style="padding: 20px">
<a-col :lg="24" :xl="12">

View file

@ -38,6 +38,7 @@ var defaultValueMap = map[string]string{
"timeLocation": "Asia/Tehran",
"tgBotEnable": "false",
"tgBotToken": "",
"tgBotProxy": "",
"tgBotChatId": "",
"tgRunTime": "@daily",
"tgBotBackup": "false",
@ -244,6 +245,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")
}

View file

@ -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
@ -89,7 +92,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
@ -105,6 +113,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
}

View file

@ -255,6 +255,8 @@
"telegramBotEnableDesc" = "Connect to the features of this panel through the Telegram bot."
"telegramToken" = "Telegram Token"
"telegramTokenDesc" = "The token you have got from @BotFather."
"telegramProxy" = "Telegram Socks5 Proxy"
"telegramProxyDesc" = "If you need Telegram to connect through a socks5 proxy. (socks5://user:pass@host:port)"
"telegramChatId" = "Telegram Admin Chat IDs"
"telegramChatIdDesc" = "Multiple chat IDs separated by comma. use @userinfobot or use '/id' command in bot to get your Chat IDs."
"telegramNotifyTime" = "Telegram bot notification time"

View file

@ -50,7 +50,9 @@ func (x *XrayAPI) Init(apiPort int) (err error) {
}
func (x *XrayAPI) Close() {
x.grpcClient.Close()
if x.grpcClient != nil {
x.grpcClient.Close()
}
x.HandlerServiceClient = nil
x.StatsServiceClient = nil
x.isConnected = false