/
client.go
68 lines (57 loc) · 1.39 KB
/
client.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
package torproxy
import (
"context"
"fmt"
"io"
"log"
"os"
"strconv"
"github.com/cretz/bine/tor"
"github.com/caddyserver/caddy"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
)
func (t *Tor) Start(c *caddy.Controller) {
var debugger io.Writer
if t.DebugMode {
if t.LogFile != "" {
debugger = &lumberjack.Logger{
Filename: t.LogFile,
MaxSize: 100,
MaxAge: 14,
MaxBackups: 10,
}
}
debugger = os.Stdout
}
torInstance, err := tor.Start(nil, t.starterConfig(debugger))
if err != nil {
log.Panicf("Unable to start Tor: %v", err)
}
listenCtx := context.Background()
onion, err := torInstance.Listen(listenCtx, &tor.ListenConf{LocalPort: 8868, RemotePorts: []int{80}})
if err != nil {
log.Panicf("Unable to start onion service: %v", err)
}
t.onion = onion
t.instance = torInstance
}
// Stop stops the tor instance, context listener and the onion service
func (t *Tor) Stop() error {
if err := t.instance.Close(); err != nil {
return fmt.Errorf("[txtdirect]: Couldn't close the tor instance. %s", err.Error())
}
t.onion.Close()
return nil
}
func (t *Tor) starterConfig(debugger io.Writer) *tor.StartConf {
config := &tor.StartConf{
NoAutoSocksPort: true,
ExtraArgs: []string{"--SocksPort", strconv.Itoa(t.Port)},
TempDataDirBase: t.DataDir,
TorrcFile: t.Torrc,
}
if debugger != nil {
config.DebugWriter = debugger
}
return config
}