diff --git a/.gitignore b/.gitignore index 9e85f5a..a0787cc 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ tor/tor/ vendor/ *.cover.out tmp/ +testing/tor/torrc +testing/tor/* \ No newline at end of file diff --git a/testing/tor/torrc b/testing/tor/torrc deleted file mode 100644 index 459089c..0000000 --- a/testing/tor/torrc +++ /dev/null @@ -1,4 +0,0 @@ -SOCKSPort 9050 -ControlPort 9051 -# "examplehashedpassword" - used for testing -HashedControlPassword 16:C15305F97789414B601259E3EC5E76B8E55FC56A9F562B713F3D2BA257 diff --git a/tor/torProvider.go b/tor/torProvider.go index d45effc..8510d8d 100644 --- a/tor/torProvider.go +++ b/tor/torProvider.go @@ -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 { diff --git a/tor/torProvider_test.go b/tor/torProvider_test.go index 7400517..e7104a1 100644 --- a/tor/torProvider_test.go +++ b/tor/torProvider_test.go @@ -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 { diff --git a/tor/torUtils_test.go b/tor/torUtils_test.go index 0416c2a..2e7c60d 100644 --- a/tor/torUtils_test.go +++ b/tor/torUtils_test.go @@ -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) + } +} diff --git a/tor/torrcBuilder.go b/tor/torrcBuilder.go index 1b4ea2f..6937d7c 100644 --- a/tor/torrcBuilder.go +++ b/tor/torrcBuilder.go @@ -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.