-
Notifications
You must be signed in to change notification settings - Fork 6
/
tor.go
75 lines (65 loc) · 2.15 KB
/
tor.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
// Copyright (c) 2017-2021 Ivan Jelincic <parazyd@dyne.org>
//
// This file is part of tordam
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package tordam
import (
"fmt"
"net"
"os"
"os/exec"
"strings"
)
// newtorrc returns a torrc string that is fed as standard input to the Tor
// binary for its configuration.
func newtorrc(listener, torlistener *net.TCPAddr, portmap []string) string {
var pm []string
for _, i := range pm {
p := strings.Split(i, ":")
pm = append(pm, fmt.Sprintf("HiddenServicePort %s %s",
p[0], strings.Join([]string{"127.0.0.1", p[1]}, ":")))
}
return fmt.Sprintf(`
Log warn syslog
RunAsDaemon 0
DataDirectory tor
SocksPort %s
HiddenServiceDir hs
HiddenServicePort %d %s
%s
`, torlistener.String(),
listener.Port, listener.String(), strings.Join(pm, "\n"))
}
// SpawnTor runs the system's Tor binary with the torrc created by newtorrc.
// It takes listener (which is the local JSON-RPC server net.TCPAddr),
// portmap (to map HiddenServicePort entries) and datadir (to store Tor files)
// as parameters. Returns exec.Cmd pointer and/or error.
func SpawnTor(listener *net.TCPAddr, portmap []string, datadir string) (*exec.Cmd, error) {
var err error
if err = ValidatePortmap(portmap); err != nil {
return nil, err
}
Cfg.TorAddr, err = GetAvailableListener()
if err != nil {
return nil, err
}
if err := os.MkdirAll(datadir, 0700); err != nil {
return nil, err
}
cmd := exec.Command("tor", "-f", "-")
cmd.Stdin = strings.NewReader(newtorrc(listener, Cfg.TorAddr, portmap))
cmd.Dir = datadir
return cmd, cmd.Start()
}