aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorterminaldweller <devi@terminaldweller.com>2023-12-06 03:41:44 +0000
committerterminaldweller <devi@terminaldweller.com>2023-12-06 03:41:44 +0000
commitc1279e7db2e492d3b098e58369c70b06a5234ecd (patch)
tree136036361d4cfafcd77953a6a5774e0d39d69176
parenttemp fix for the telebot crash (diff)
downloadhived-c1279e7db2e492d3b098e58369c70b06a5234ecd.tar.gz
hived-c1279e7db2e492d3b098e58369c70b06a5234ecd.zip
fixes #5
-rw-r--r--telebot/telebot.go44
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