lightningtipbot/internal/network/http.go
LightningTipBot 1946a0e873
Satdress: Socks Proxy (2) (#348)
* 🚧 using socks

* socks proxy

* refactor http client

* remove comments

* remove comments

Co-authored-by: gohumble <lngohumble@gmail.com>
Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
2022-05-02 23:36:51 +02:00

52 lines
1.3 KiB
Go

package network
import (
"context"
"net"
"net/http"
"net/url"
"time"
"github.com/LightningTipBot/LightningTipBot/internal"
log "github.com/sirupsen/logrus"
"golang.org/x/net/proxy"
)
func GetHttpClient() (*http.Client, error) {
client := http.Client{
Timeout: 10 * time.Second,
}
if internal.Configuration.Bot.HttpProxy != "" {
proxyUrl, err := url.Parse(internal.Configuration.Bot.HttpProxy)
if err != nil {
log.Errorln(err)
return nil, err
}
client.Transport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}
}
return &client, nil
}
func GetSocksClient() (*http.Client, error) {
client := http.Client{
Timeout: 10 * time.Second,
}
if internal.Configuration.Bot.SocksProxy != "" {
proxyURL, _ := url.Parse(internal.Configuration.Bot.SocksProxy)
specialTransport := &http.Transport{}
specialTransport.Proxy = http.ProxyURL(proxyURL)
d, err := proxy.SOCKS5("tcp", internal.Configuration.Bot.SocksProxy, nil, &net.Dialer{
Timeout: 20 * time.Second,
Deadline: time.Now().Add(time.Second * 10),
KeepAlive: -1,
})
if err != nil {
log.Errorln(err)
return &client, nil
}
specialTransport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return d.Dial(network, addr)
}
client.Transport = specialTransport
}
return &client, nil
}