Allow Custom Tor Config

This commit is contained in:
Sarah Jamie Lewis 2022-01-10 12:51:21 -08:00
parent b36f6dc33f
commit d3398bd074
6 changed files with 29 additions and 8 deletions

2
.gitignore vendored
View File

@ -4,3 +4,5 @@ tor/tor/
vendor/
*.cover.out
tmp/
testing/tor/torrc
testing/tor/*

View File

@ -1,4 +0,0 @@
SOCKSPort 9050
ControlPort 9051
# "examplehashedpassword" - used for testing
HashedControlPassword 16:C15305F97789414B601259E3EC5E76B8E55FC56A9F562B713F3D2BA257

View File

@ -55,9 +55,9 @@ func (l *logWriter) Write(p []byte) (int, error) {
}
type onionListenService struct {
lock sync.Mutex
os *tor.OnionService
tp *torProvider
lock sync.Mutex
os *tor.OnionService
tp *torProvider
}
type torProvider struct {

View File

@ -3,7 +3,7 @@ package tor
import (
"fmt"
"git.openprivacy.ca/openprivacy/log"
"path"
path "path/filepath"
"testing"
)
@ -18,6 +18,9 @@ func TestTorProvider(t *testing.T) {
progChan := make(chan int)
log.SetLevel(log.LevelDebug)
torpath := path.Join("..", "tmp/tor")
NewTorrc().WithControlPort(9051).WithHashedPassword("examplehashedpassword").Build(path.Join("..", "testing", "tor", "torrc"))
log.Debugf("setting tor path %v", torpath)
acn, err := NewTorACNWithAuth(path.Join("../testing/"), torpath, 9051, HashedPasswordAuthenticator{"examplehashedpassword"})
if err != nil {

View File

@ -98,3 +98,11 @@ func TestGenerateTorrc(t *testing.T) {
}
os.Remove(path)
}
func TestPreviewTorrc(t *testing.T) {
expected := "SocksPort 9050 OnionTrafficOnly\nControlPort 9061"
torrc := NewTorrc().WithCustom([]string{"SocksPort 9050"}).WithControlPort(9061).WithOnionTrafficOnly().Preview()
if torrc != expected {
t.Fatalf("unexpected torrc generated: [%v] [%v]", expected, torrc)
}
}

View File

@ -34,6 +34,13 @@ func (tb *TorrcBuilder) WithControlPort(port int) *TorrcBuilder {
return tb
}
// WithCustom clobbers the torrc builder and allows the client to set any option they want, while benefiting
// from other configuration options.
func (tb *TorrcBuilder) WithCustom(lines []string) *TorrcBuilder {
tb.lines = lines
return tb
}
// WithOnionTrafficOnly ensures that the tor process only routes tor onion traffic.
func (tb *TorrcBuilder) WithOnionTrafficOnly() *TorrcBuilder {
for i, line := range tb.lines {
@ -61,6 +68,11 @@ func (tb *TorrcBuilder) Build(path string) error {
return ioutil.WriteFile(path, []byte(strings.Join(tb.lines, "\n")), 0600)
}
// Preview provides a string representation of the torrc file without writing it to a file location.
func (tb *TorrcBuilder) Preview() string {
return strings.Join(tb.lines, "\n")
}
// GenerateHashedPassword calculates a hash in the same way tha tor --hash-password does
// this function takes a salt as input which is not great from an api-misuse perspective, but
// we make it private.