Skip to content

Commit

Permalink
socks5 proxy option added to telegram bot settings (#1500)
Browse files Browse the repository at this point in the history
* socks5 option added to telegram bot settings

* update socks5 proxy settings translations
  • Loading branch information
surbiks authored and MHSanaei committed Jan 3, 2024
1 parent c761995 commit 5e3478f
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/pelletier/go-toml/v2 v2.1.1
github.com/robfig/cron/v3 v3.0.1
github.com/shirou/gopsutil/v3 v3.23.12
github.com/valyala/fasthttp v1.51.0
github.com/xtls/xray-core v1.8.6
go.uber.org/atomic v1.11.0
golang.org/x/text v0.14.0
Expand Down Expand Up @@ -75,7 +76,6 @@ require (
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/v2fly/ss-bloomring v0.0.0-20210312155135-28617310f63e // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.0 // indirect
github.com/vishvananda/netlink v1.2.1-beta.2.0.20230316163032-ced5aaba43e3 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
github.com/xtls/reality v0.0.0-20231112171332-de1173cf2b19 // indirect
Expand Down
1 change: 1 addition & 0 deletions web/assets/js/model/setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class AllSetting {
this.datepicker = "gregorian";
this.tgBotEnable = false;
this.tgBotToken = "";
this.tgBotProxy = "";
this.tgBotChatId = "";
this.tgRunTime = "@daily";
this.tgBotBackup = false;
Expand Down
1 change: 1 addition & 0 deletions web/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
1 change: 1 addition & 0 deletions web/html/xui/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,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" placeholder="socks5://[user:pass@]host:port"></setting-list-item>
<a-list-item>
<a-row style="padding: 20px">
<a-col :lg="24" :xl="12">
Expand Down
9 changes: 9 additions & 0 deletions web/service/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var defaultValueMap = map[string]string{
"timeLocation": "Asia/Tehran",
"tgBotEnable": "false",
"tgBotToken": "",
"tgBotProxy": "",
"tgBotChatId": "",
"tgRunTime": "@daily",
"tgBotBackup": "false",
Expand Down Expand Up @@ -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")
}
Expand Down
27 changes: 26 additions & 1 deletion web/service/tgbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"embed"
"fmt"
"net"
"net/url"
"os"
"strconv"
"strings"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions web/translation/translate.en_US.toml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,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" = "Socks5 Proxy"
"telegramProxyDesc" = "If you need the Socks5 proxy to connect to Telegram. Adjust its settings as per the guide."
"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"
Expand Down
2 changes: 2 additions & 0 deletions web/translation/translate.es_ES.toml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@
"telegramBotEnableDesc" = "Conéctese a las funciones de este panel a través del bot de Telegram."
"telegramToken" = "Token de Telegram"
"telegramTokenDesc" = "Debe obtener el token del administrador de bots de Telegram @botfather."
"telegramProxy" = "Socks5 Proxy"
"telegramProxyDesc" = "Si necesita el proxy Socks5 para conectarse a Telegram. Ajuste su configuración según la guía."
"telegramChatId" = "IDs de Chat de Telegram para Administradores"
"telegramChatIdDesc" = "IDs de Chat múltiples separados por comas. Use @userinfobot o use el comando '/id' en el bot para obtener sus IDs de Chat."
"telegramNotifyTime" = "Hora de Notificación del Bot de Telegram"
Expand Down
2 changes: 2 additions & 0 deletions web/translation/translate.fa_IR.toml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@
"telegramBotEnableDesc" = "از طریق بات تلگرام به امکانات این پنل متصل شوید"
"telegramToken" = "توکن تلگرام"
"telegramTokenDesc" = "توکن را باید از مدیر بات های تلگرام دریافت کنید @botfather"
"telegramProxy" = "پروکسی Socks5"
"telegramProxyDesc" = "اگر برای اتصال به تلگرام به پروکسی Socks5 نیاز دارید. تنظیمات خود را طبق راهنما تنظیم کنید."
"telegramChatId" = "آی دی تلگرام مدیریت"
"telegramChatIdDesc" = "از @userinfobot یا دستور '/id' در ربات برای دریافت شناسه های چت خود استفاده کنید. با استفاده از کاما میتونید چند آی دی را از هم جدا کنید. "
"telegramNotifyTime" = "مدت زمان نوتیفیکیشن ربات تلگرام"
Expand Down
2 changes: 2 additions & 0 deletions web/translation/translate.ru_RU.toml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@
"telegramBotEnableDesc" = "Подключайтесь к функциям этой панели через Telegram бота"
"telegramToken" = "Токен Telegram бота"
"telegramTokenDesc" = "Необходимо получить токен у менеджера ботов Telegram @botfather"
"telegramProxy" = "Прокси Socks5"
"telegramProxyDesc" = "Если для подключения к Telegram вам нужен прокси Socks5. Настройте его параметры согласно руководству."
"telegramChatId" = "Telegram ID админа бота"
"telegramChatIdDesc" = "Множественные идентификаторы чата, разделенные запятыми. Чтобы получить свои идентификаторы чатов, используйте @userinfobot или команду '/id' в боте."
"telegramNotifyTime" = "Частота уведомлений бота Telegram"
Expand Down
2 changes: 2 additions & 0 deletions web/translation/translate.vi_VN.toml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@
"telegramBotEnableDesc" = "Kết nối với các tính năng của bảng điều khiển này thông qua bot Telegram"
"telegramToken" = "Token Telegram"
"telegramTokenDesc" = "Bạn phải nhận token từ quản lý bot Telegram @botfather"
"telegramProxy" = "Socks5 Proxy"
"telegramProxyDesc" = "Nếu bạn cần socks5 proxy để kết nối với Telegram. Điều chỉnh cài đặt của nó theo hướng dẫn."
"telegramChatId" = "Chat ID Telegram của quản trị viên"
"telegramChatIdDesc" = "Nhiều Chat ID phân tách bằng dấu phẩy. Sử dụng @userinfobot hoặc sử dụng lệnh '/id' trong bot để lấy Chat ID của bạn."
"telegramNotifyTime" = "Thời gian thông báo của bot Telegram"
Expand Down
2 changes: 2 additions & 0 deletions web/translation/translate.zh_Hans.toml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@
"telegramBotEnableDesc" = "重启面板生效"
"telegramToken" = "电报机器人TOKEN"
"telegramTokenDesc" = "重启面板生效"
"telegramProxy" = "Socks5 代理"
"telegramProxyDesc" = "如果您需要 Socks5 代理来连接 Telegram。 根据指南调整其设置。"
"telegramChatId" = "以逗号分隔的多个 chatID 重启面板生效"
"telegramChatIdDesc" = "多个聊天 ID 用逗号分隔。使用 @userinfobot 或在机器人中使用'/id'命令获取您的聊天 ID。"
"telegramNotifyTime" = "电报机器人通知时间"
Expand Down
4 changes: 3 additions & 1 deletion xray/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5e3478f

Please sign in to comment.