This commit is contained in:
2023-12-21 22:56:24 +03:00
parent 85a42dc405
commit 0bebf8ba8a
7 changed files with 111 additions and 11 deletions

60
proxies/proxies.go Normal file
View File

@ -0,0 +1,60 @@
package proxies
import (
"bufio"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
)
//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}
clients = append(clients, client)
log.Printf("using %s as proxy", scanner.Text())
}
if len(clients) == 0 {
clients = append(clients, &http.Client{})
}
return clients, nil
}