Files
platiparser/proxies/proxies.go
dilap54 f983091e2b
All checks were successful
Build and push image / deploy (push) Successful in 2m6s
add timeout
2023-12-28 19:03:38 +03:00

65 lines
1.1 KiB
Go

package proxies
import (
"bufio"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"time"
)
//https://www.reddit.com/r/golang/comments/ezg1ka/how_can_i_specify_a_proxy_server_when_using/
func Default() []*http.Client {
clis, err := ParseProxiesFromFile("./proxies.list")
if err != nil {
log.Fatal(err)
}
return clis
}
func ParseProxiesFromFile(fileName string) ([]*http.Client, error) {
f, err := os.OpenFile(fileName, os.O_RDONLY, 0400)
if err != nil {
return nil, err
}
defer f.Close()
return ParseProxies(f)
}
func ParseProxies(r io.Reader) ([]*http.Client, error) {
clients := make([]*http.Client, 0)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
proxyURL, err := url.Parse(scanner.Text())
if err != nil {
return nil, fmt.Errorf("url parse %s: %w", scanner.Text(), err)
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
client := &http.Client{
Transport: transport,
Timeout: 1 * time.Minute,
}
clients = append(clients, client)
log.Printf("using %s as proxy", scanner.Text())
}
if len(clients) == 0 {
clients = append(clients, &http.Client{})
}
return clients, nil
}