mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-09-05 01:36:19 +00:00
use socks in tg bot
This commit is contained in:
parent
b6c787f5d7
commit
dfdcebc282
1 changed files with 22 additions and 2 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -20,6 +21,8 @@ import (
|
||||||
"github.com/mymmrac/telego"
|
"github.com/mymmrac/telego"
|
||||||
th "github.com/mymmrac/telego/telegohandler"
|
th "github.com/mymmrac/telego/telegohandler"
|
||||||
tu "github.com/mymmrac/telego/telegoutil"
|
tu "github.com/mymmrac/telego/telegoutil"
|
||||||
|
"github.com/valyala/fasthttp"
|
||||||
|
"github.com/valyala/fasthttp/fasthttpproxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
var bot *telego.Bot
|
var bot *telego.Bot
|
||||||
|
@ -93,10 +96,10 @@ func (t *Tgbot) Start(i18nFS embed.FS) error {
|
||||||
if err != nil || tgBotProxy == "" {
|
if err != nil || tgBotProxy == "" {
|
||||||
logger.Warning("Telegram proxy not valid use direct connection")
|
logger.Warning("Telegram proxy not valid use direct connection")
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bot, err = telego.NewBot(tgBottoken)
|
bot, err = t.NewBot(tgBottoken, tgBotProxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Get tgbot's api error:", err)
|
fmt.Println("Get tgbot's api error:", err)
|
||||||
return err
|
return err
|
||||||
|
@ -112,6 +115,23 @@ func (t *Tgbot) Start(i18nFS embed.FS) error {
|
||||||
return nil
|
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 {
|
func (t *Tgbot) IsRunning() bool {
|
||||||
return isRunning
|
return isRunning
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue