Example #1
0
// IsConnectionOverTor will make a connection to the check.torproject page to see if we're using Tor or not
func (*defaultTorManager) IsConnectionOverTor(d proxy.Dialer) bool {
	if d == nil {
		d = proxy.Direct
	}

	c := &http.Client{Transport: &http.Transport{Dial: func(network, addr string) (net.Conn, error) {
		return d.Dial(network, addr)
	}}}

	resp, err := c.Get("https://check.torproject.org/api/ip")
	if err != nil {
		log.Printf("Got error when trying to check tor: %v", err)
		return false
	}

	content, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Printf("Got error when trying to check tor: %v", err)
		return false
	}

	v := CheckTorResult{}
	err = json.Unmarshal(content, &v)
	if err != nil {
		log.Printf("Got error when trying to check tor: %v", err)
		return false
	}

	return v.IsTor
}
Example #2
0
func dialTimeout(network, addr string, dialer proxy.Dialer, t time.Duration) (c net.Conn, err error) {
	result := make(chan bool, 1)

	go func() {
		c, err = dialer.Dial(network, addr)
		result <- true
	}()

	select {
	case <-time.After(t):
		log.Println("tcp: dial timed out")
		return nil, ourNet.ErrTimeout
	case <-result:
		return
	}
}
Example #3
0
// LookupSRVWith looks up the provided service and protocol on the given name using the proxy dialer given and the dns server provided
func LookupSRVWith(dialer proxy.Dialer, dnsServer, service, proto, name string) (cname string, addrs []*net.SRV, err error) {
	return timingOutLookup(func() (cname string, addrs []*net.SRV, err error) {
		cname = createCName(service, proto, name)

		conn, err := dialer.Dial("tcp", dnsServer)
		if err != nil {
			return
		}

		dnsConn := &dns.Conn{Conn: conn}
		defer dnsConn.Close()

		r, err := exchange(dnsConn, msgSRV(cname))
		if err != nil {
			return
		}

		addrs = convertAnswersToSRV(r.Answer)
		return
	}, defaultLookupTimeout)
}