Всех с наступающими новогодними праздниками!)
В общем, вместо тысячи слов, сразу код
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"time"
"golang.org/x/net/proxy"
)
var plist = []string{"1.1.1.1:9010", "123.21.22.1:80", "127.0.0.1:9050"}
var timeout = 10
func main() {
fmt.Println("Good is: ", SelectProxy())
}
func SelectProxy() string {
var s string
var err error
s = Choise(plist)
fmt.Println("Try to ", s)
err = proxyGet(s)
if err != nil {
log.Println(err.Error())
SelectProxy()
}
return s
}
func proxyGet(p string) error {
check_url := "http://ip-api.com/json/"
dialer, err := proxy.SOCKS5("tcp", p, nil, proxy.Direct)
if err != nil {
return err
}
httpTransport := &http.Transport{}
httpClient := &http.Client{Transport: httpTransport, Timeout: time.Second * time.Duration(timeout)}
httpTransport.Dial = dialer.Dial
req, err := http.NewRequest("GET", check_url, nil)
if err != nil {
return err
}
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
pp := &Proxy{}
err = json.Unmarshal(body, pp)
if err != nil {
return err
}
fmt.Printf("%#v\n", pp)
return nil
}
func Choise(arr []string) string {
rand.Seed(time.Now().Unix())
return arr[rand.Intn(len(arr))]
}
type Proxy struct {
Country string `json:"country"`
Query string `json:"query"`
}
Получаю выхлоп
./tp
Try to 123.21.22.1:80
2018/12/29 14:30:05 Get http://ip-api.com/json/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Try to 1.1.1.1:9010
2018/12/29 14:30:15 Get http://ip-api.com/json/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Try to 123.21.22.1:80
2018/12/29 14:30:25 Get http://ip-api.com/json/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Try to 123.21.22.1:80
2018/12/29 14:30:35 Get http://ip-api.com/json/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Try to 1.1.1.1:9010
2018/12/29 14:30:45 Get http://ip-api.com/json/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Try to 1.1.1.1:9010
2018/12/29 14:30:55 Get http://ip-api.com/json/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Try to 127.0.0.1:9050
&main.Proxy{Country:"Germany", Query:"185.47.140.7"}
Good is: 123.21.22.1:80
Проблема заключается в том, что возвращается всегда то значение, которое помещается в переменную `s` первый раз. Что я делаю не правильно?