You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

58 lines
1.2 KiB

package settlers
import (
"net/http"
"net/url"
"strings"
"golang.org/x/net/proxy"
)
// This also disables redirect handling.
func checkRedirect(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
}
func isOnion(someURL string) (bool, error) {
parsedURL, err := url.Parse(someURL)
if err != nil {
return true, err
}
urlParts := strings.Split(parsedURL.Host, ".")
tld := urlParts[len(urlParts)-1]
if tld == "onion" {
return true, err
} else {
return false, err
}
}
func onionHTTP() (client *http.Client, err error) {
torProxyURL, err := url.Parse("socks5://127.0.0.1:9050")
if err != nil {
return
}
torDialer, err := proxy.FromURL(torProxyURL, proxy.Direct)
if err != nil {
return
}
torTransport := &http.Transport{Dial: torDialer.Dial}
client = &http.Client{Transport: torTransport, CheckRedirect: checkRedirect}
return
}
func clearnetOrOnionHTTP(url string) (client *http.Client, err error) {
// Returns a http.Client using a local Tor SOCKS proxy if .onion,
// plain old http.Client if not.
reallyIsOnion, err := isOnion(url)
if err != nil {
return
}
if reallyIsOnion {
client, err = onionHTTP()
} else {
client = &http.Client{CheckRedirect: checkRedirect}
}
return
}