Skip to content

Commit

Permalink
feat: add proxy config in telegram dialer
Browse files Browse the repository at this point in the history
  • Loading branch information
krau committed Jan 7, 2025
1 parent 3b42ba8 commit 4b5cabc
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 69 deletions.
34 changes: 31 additions & 3 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,62 @@ package bot

import (
"context"
"net/url"
"os"
"time"

"github.com/celestix/gotgproto"
"github.com/celestix/gotgproto/sessionMaker"
"github.com/glebarez/sqlite"
"github.com/gotd/td/telegram/dcs"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/logger"
"golang.org/x/net/proxy"
)

var Client *gotgproto.Client

func newProxyDialer(proxyUrl string) (proxy.Dialer, error) {
url, err := url.Parse(proxyUrl)
if err != nil {
return nil, err
}
return proxy.FromURL(url, proxy.Direct)
}

func Init() {
logger.L.Info("Initializing client...")
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

resultChan := make(chan struct {
client *gotgproto.Client
err error
})

go func() {
client, err := gotgproto.NewClient(int(config.Cfg.Telegram.AppID), config.Cfg.Telegram.AppHash, gotgproto.ClientTypeBot(config.Cfg.Telegram.Token),
var resolver dcs.Resolver
if config.Cfg.Telegram.Proxy.Enable && config.Cfg.Telegram.Proxy.URL != "" {
dialer, err := newProxyDialer(config.Cfg.Telegram.Proxy.URL)
if err != nil {
resultChan <- struct {
client *gotgproto.Client
err error
}{nil, err}
return
}
resolver = dcs.Plain(dcs.PlainOptions{
Dial: dialer.(proxy.ContextDialer).DialContext,
})
} else {
resolver = dcs.DefaultResolver()
}
client, err := gotgproto.NewClient(config.Cfg.Telegram.AppID,
config.Cfg.Telegram.AppHash,
gotgproto.ClientTypeBot(config.Cfg.Telegram.Token),
&gotgproto.ClientOpts{
Session: sessionMaker.SqlSession(sqlite.Open("data/session.db")),
DisableCopyright: true,
Middlewares: FloodWaitMiddleware(),
Resolver: resolver,
},
)
resultChan <- struct {
Expand Down
6 changes: 5 additions & 1 deletion config.example.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
workers = 4 # 同时下载文件数
workers = 4 # 同时下载文件数

[telegram]
token = "" # Bot Token
admins = [777000] # 你的 user_id
app_id = 123456 # Telegram API ID
app_hash = "0123456789abcdef0123456789abcdef" # Telegram API Hash

[telegram.proxy]
enable = false
url = "socks5://127.0.0.1:7890" # 代理地址

[log]
level = "DEBUG" # 日志等级

Expand Down
14 changes: 10 additions & 4 deletions config/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ type dbConfig struct {
}

type telegramConfig struct {
Token string `toml:"token" mapstructure:"token"`
AppID int32 `toml:"app_id" mapstructure:"app_id"`
AppHash string `toml:"app_hash" mapstructure:"app_hash"`
Admins []int64 `toml:"admins" mapstructure:"admins"`
Token string `toml:"token" mapstructure:"token"`
AppID int `toml:"app_id" mapstructure:"app_id"`
AppHash string `toml:"app_hash" mapstructure:"app_hash"`
Admins []int64 `toml:"admins" mapstructure:"admins"`
Proxy proxyConfig `toml:"proxy" mapstructure:"proxy"`
}

type proxyConfig struct {
Enable bool `toml:"enable" mapstructure:"enable"`
URL string `toml:"url" mapstructure:"url"`
}

type storageConfig struct {
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/studio-b12/gowebdav v0.10.0
golang.org/x/net v0.33.0
golang.org/x/time v0.8.0
)

Expand Down Expand Up @@ -67,16 +68,13 @@ require (
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.24.0 // indirect
golang.org/x/tools v0.28.0 // indirect
google.golang.org/protobuf v1.36.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
modernc.org/libc v1.61.6 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.8.1 // indirect
modernc.org/sqlite v1.34.4 // indirect
nhooyr.io/websocket v1.8.17 // indirect
rsc.io/qr v0.2.0 // indirect
)

Expand Down
Loading

0 comments on commit 4b5cabc

Please sign in to comment.