-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
48 lines (39 loc) · 1.11 KB
/
example_test.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
// Copyright (c) 2022 Vasiliy Vasilyuk. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tornado_test
import (
"context"
"log"
"net/http"
"net/http/httputil"
"time"
"github.com/xorcare/tornado"
)
func ExampleNewProxy() {
const proxyServerStartupTimeout = 15 * time.Second
ctx, done := context.WithTimeout(context.Background(), proxyServerStartupTimeout)
defer done()
prx, err := tornado.NewProxy(ctx)
if err != nil {
log.Panicln("failed to create new instance of proxy:", err)
}
// After usage Proxy must be closed to prevent memory leak and tor
// demon process leak.
defer prx.Close()
httpcli := &http.Client{
Transport: &http.Transport{
DialContext: prx.DialContext,
},
Timeout: time.Second * 15,
}
resp, err := httpcli.Get("https://check.torproject.org/api/ip")
if err != nil {
log.Panicln("failed to execute http request to tor project api:", err)
}
text, err := httputil.DumpResponse(resp, true)
if err != nil {
log.Panicln("failed to dump full response info:", err)
}
log.Println(string(text))
}