Skip to content

Commit

Permalink
Merge branch 'onionscan-0.2' of github.com:s-rah/onionscan into onion…
Browse files Browse the repository at this point in the history
…scan-0.2
  • Loading branch information
s-rah committed Oct 10, 2016
2 parents f56acb2 + 1c249e0 commit 3ed2d7d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/s-rah/onionscan/deanonymization"
"github.com/s-rah/onionscan/onionscan"
"github.com/s-rah/onionscan/report"
"github.com/s-rah/onionscan/utils"
"golang.org/x/crypto/ssh/terminal"
"io/ioutil"
"log"
Expand Down Expand Up @@ -49,6 +50,11 @@ func main() {
log.Fatalf("You must set one of --simpleReport or --jsonReport or --jsonSimpleReport")
}

proxyStatus := utils.CheckTorProxy(*torProxyAddress)
if proxyStatus != utils.ProxyStatusOK {
log.Fatalf("%s, is the --torProxyAddress setting correct?", utils.ProxyStatusMessage(proxyStatus))
}

onionsToScan := []string{}
if *list == "" {
onionsToScan = append(onionsToScan, flag.Args()[0])
Expand Down
62 changes: 62 additions & 0 deletions utils/proxycheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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"
}
}

0 comments on commit 3ed2d7d

Please sign in to comment.