-
Notifications
You must be signed in to change notification settings - Fork 9
/
check-tor-connection.go
78 lines (69 loc) · 1.68 KB
/
check-tor-connection.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"bytes"
"context"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/cretz/bine/process"
"github.com/cretz/bine/tor"
libtor "github.com/ipsn/go-libtor"
"golang.org/x/net/html"
)
var creator = libtor.Creator
type LibTorWrapper struct{}
func (LibTorWrapper) New(ctx context.Context, args ...string) (process.Process, error) {
return creator.New(ctx, args...)
}
func main() {
// Start tor with some defaults + elevated verbosity
fmt.Println("Starting tor instance, please wait a bit...")
t, err := tor.Start(nil, &tor.StartConf{ProcessCreator: LibTorWrapper{}, DebugWriter: os.Stderr})
if err != nil {
log.Panicf("Failed to start tor: %v", err)
}
defer t.Close()
// Wait at most a few minutes to publish the service
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
// Make connection
dialer, err := t.Dialer(ctx, nil)
if err != nil {
log.Fatal(err)
return
}
httpClient := &http.Client{Transport: &http.Transport{DialContext: dialer.DialContext}}
// Get /
resp, err := httpClient.Get("https://check.torproject.org")
if err != nil {
log.Fatal(err)
return
}
defer resp.Body.Close()
// Grab the <title>
parsed, err := html.Parse(resp.Body)
if err != nil {
log.Fatal(err)
return
}
fmt.Printf("Title: %v\n", getTitle(parsed))
return
}
func getTitle(n *html.Node) string {
if n.Type == html.ElementNode && n.Data == "title" {
var title bytes.Buffer
if err := html.Render(&title, n.FirstChild); err != nil {
panic(err)
}
return strings.TrimSpace(title.String())
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
if title := getTitle(c); title != "" {
return title
}
}
return ""
}