diff options
author | terminaldweller <devi@terminaldweller.com> | 2023-12-06 03:41:44 +0000 |
---|---|---|
committer | terminaldweller <devi@terminaldweller.com> | 2023-12-06 03:41:44 +0000 |
commit | c1279e7db2e492d3b098e58369c70b06a5234ecd (patch) | |
tree | 136036361d4cfafcd77953a6a5774e0d39d69176 | |
parent | temp fix for the telebot crash (diff) | |
download | hived-c1279e7db2e492d3b098e58369c70b06a5234ecd.tar.gz hived-c1279e7db2e492d3b098e58369c70b06a5234ecd.zip |
fixes #5
-rw-r--r-- | telebot/telebot.go | 44 |
1 files changed, 35 insertions, 9 deletions
diff --git a/telebot/telebot.go b/telebot/telebot.go index 5cf2045..d4a8255 100644 --- a/telebot/telebot.go +++ b/telebot/telebot.go @@ -33,23 +33,50 @@ type server struct { } func GetProxiedClient() (*http.Client, error) { + var isProxied bool proxyURL := os.Getenv("ALL_PROXY") if proxyURL == "" { proxyURL = os.Getenv("HTTPS_PROXY") } - dialer, err := proxy.SOCKS5("tcp", proxyURL, nil, proxy.Direct) - if err != nil { - return nil, fmt.Errorf("[GetProxiedClient] : %w", err) + if proxyURL == "" { + isProxied = false } - dialContext := func(ctx context.Context, network, address string) (net.Conn, error) { - netConn, err := dialer.Dial(network, address) - if err == nil { - return netConn, nil + var dialer_proxy proxy.Dialer + var dialer net.Dialer + var err error + + if isProxied { + dialer_proxy, err = proxy.SOCKS5("tcp", proxyURL, nil, proxy.Direct) + if err != nil { + return nil, fmt.Errorf("[GetProxiedClient] : %w", err) } + } else { + dialer = net.Dialer{ + Timeout: 5 * time.Second, + } + if err != nil { + return nil, fmt.Errorf("[GetProxiedClient] : %w", err) + } + } - return netConn, fmt.Errorf("[dialContext] : %w", err) + dialContext := func(ctx context.Context, network, address string) (net.Conn, error) { + if isProxied { + netConn, err := dialer_proxy.Dial(network, address) + if err == nil { + return netConn, nil + } + + return netConn, fmt.Errorf("[dialContext] : %w", err) + } else { + netConn, err := dialer.Dial(network, address) + if err == nil { + return netConn, nil + } + + return netConn, fmt.Errorf("[dialContext] : %w", err) + } } transport := &http.Transport{ @@ -85,7 +112,6 @@ func getTGBot() *tgbotapi.BotAPI { func sendMessage(bot *tgbotapi.BotAPI, msgText string, channelID int64) error { msg := tgbotapi.NewMessage(channelID, msgText) - fmt.Println("XXXXXXXXXXXXXXXXXX", msg) _, err := bot.Send(msg) return err |