forked from s-rah/onionscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxycheck.go
62 lines (57 loc) · 1.61 KB
/
proxycheck.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package utils
import (
"net"
"net/http"
"net/url"
"strings"
"time"
)
type ProxyStatus int
const (
ProxyStatusOK ProxyStatus = iota
ProxyStatusWrongType
ProxyStatusCannotConnect
ProxyStatusTimeout
)
// Detect whether a proxy is connectable and is a Tor proxy
func CheckTorProxy(proxyAddress string) ProxyStatus {
// A trick to do this without making an outward connection is,
// paradoxically, to try to open it as http.
// This is documented in section 4 here: https://github.com/torproject/torspec/blob/master/socks-extensions.txt
client := &http.Client{Timeout: 2 * time.Second}
response, err := client.Get("http://" + proxyAddress + "/")
if err != nil {
switch t := err.(type) {
case *url.Error:
switch t.Err.(type) {
case *net.OpError: // Network-level error. Will in turn contain a os.SyscallError
return ProxyStatusCannotConnect
default:
// http.error unfortunately not exported, need to match on string
// net/http: request canceled
if strings.Index(t.Err.Error(), "request canceled") != -1 {
return ProxyStatusTimeout
}
}
}
// Protocol-level errors mean that http failed, so it's not Tor
return ProxyStatusWrongType
}
defer response.Body.Close()
if response.Status != "501 Tor is not an HTTP Proxy" {
return ProxyStatusWrongType
}
return ProxyStatusOK
}
func ProxyStatusMessage(status ProxyStatus) string {
switch status {
case ProxyStatusWrongType:
return "Proxy specified is not a Tor proxy"
case ProxyStatusCannotConnect:
return "Cannot connect to Tor proxy"
case ProxyStatusTimeout:
return "Proxy timeout"
default:
return "Unknown proxy error"
}
}