61 lines
1.1 KiB
Go
61 lines
1.1 KiB
Go
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
|
|
}
|