82 lines
1.8 KiB
Go
82 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"time"
|
|
|
|
"golang.org/x/net/proxy"
|
|
)
|
|
|
|
const URL = "http://connectivitycheck.gstatic.com/generate_204"
|
|
|
|
func proxyTestStatusCode(proxyURL string, URL string, StatusCode int) bool {
|
|
// create a socks5 dialer
|
|
u, err := url.Parse(proxyURL)
|
|
if err != nil {
|
|
fmt.Println("error parsing")
|
|
return false
|
|
}
|
|
dialer, err := proxy.FromURL(u, proxy.Direct)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "can't connect to the proxy:", err)
|
|
return false
|
|
}
|
|
// setup a http client
|
|
httpTransport := &http.Transport{}
|
|
httpClient := &http.Client{
|
|
Transport: httpTransport,
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
fmt.Printf("redirect %v", *req)
|
|
return http.ErrUseLastResponse
|
|
},
|
|
}
|
|
// set our socks5 as the dialer
|
|
httpTransport.Dial = dialer.Dial
|
|
// create a request
|
|
req, err := http.NewRequest("GET", URL, nil)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "can't create request:", err)
|
|
return false
|
|
}
|
|
// use the http client to fetch the page
|
|
resp, err := httpClient.Do(req)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "can't GET page:", err)
|
|
return false
|
|
}
|
|
// defer resp.Body.Close()
|
|
// b, err := ioutil.ReadAll(resp.Body)
|
|
// if err != nil {
|
|
// fmt.Fprintln(os.Stderr, "error reading body:", err)
|
|
// return false
|
|
// }
|
|
return resp.StatusCode == StatusCode
|
|
}
|
|
|
|
// testFn := func() bool {
|
|
// status := proxyTestStatusCode("socks5://127.0.0.1:1080", URL, 204)
|
|
// fmt.Println(status)
|
|
// return status
|
|
// }
|
|
// stop := make(chan bool)
|
|
// go testTask(testFn, 5, stop)
|
|
// time.Sleep(10 * time.Second)
|
|
// stop <- true
|
|
// time.Sleep(10 * time.Second)
|
|
|
|
func testTask(testfn func() bool, interval int, stop chan bool) {
|
|
for {
|
|
go testfn()
|
|
select {
|
|
case <-stop:
|
|
fmt.Println("stopping")
|
|
return
|
|
default:
|
|
}
|
|
time.Sleep(time.Duration(interval) * time.Second)
|
|
}
|
|
}
|