Browse Source

more tor provider into to own package, fix race condition, minor renaming

tags/v1.1.0
Dan Ballard 2 months ago
parent
commit
e276c4fbe7
9 changed files with 48 additions and 29 deletions
  1. +2
    -1
      .gitignore
  2. +12
    -0
      acn.go
  3. +5
    -5
      localProvider.go
  4. +10
    -0
      tests.sh
  5. +1
    -1
      tor/sysProcAttr_rest.go
  6. +1
    -1
      tor/sysProcAttr_win.go
  7. +13
    -17
      tor/torProvider.go
  8. +2
    -2
      tor/torProvider_test.go
  9. +2
    -2
      tor/torUtils.go

+ 2
- 1
.gitignore View File

@@ -1,2 +1,3 @@
tor/
.idea/
coverage.*
tor/tor/

+ 12
- 0
acn.go View File

@@ -4,6 +4,18 @@ import (
"net"
)

// Error captures various common ricochet errors
type Error string

func (e Error) Error() string { return string(e) }

const (
// CannotResolveLocalTCPAddressError is thrown when a local ricochet connection has the wrong format.
CannotResolveLocalTCPAddressError = Error("CannotResolveLocalTCPAddressError")
// CannotDialLocalTCPAddressError is thrown when a connection to a local ricochet address fails.
CannotDialLocalTCPAddressError = Error("CannotDialLocalTCPAddressError")
)

// PrivateKey represents a private key using an unspecified algorithm.
type PrivateKey interface{}



+ 5
- 5
localProvider.go View File

@@ -13,6 +13,11 @@ type localListenService struct {
type localProvider struct {
}

// NewLocalACN returns a for testing use only local clearnet implementation of a ACN interface
func NewLocalACN() ACN {
return &localProvider{}
}

func (ls *localListenService) AddressFull() string {
return ls.l.Addr().String()
}
@@ -70,8 +75,3 @@ func (lp *localProvider) Restart() {
func (lp *localProvider) Close() {

}

// LocalProvider returns a for testing use only local clearnet implementation of a ACN interface
func LocalProvider() ACN {
return &localProvider{}
}

+ 10
- 0
tests.sh View File

@@ -0,0 +1,10 @@
#!/bin/bash

set -e
pwd
GORACE="haltonerror=1"
go test -race ${1} -coverprofile=tor.cover.out -v ./tor
echo "mode: set" > coverage.out && cat *.cover.out | grep -v mode: | sort -r | \
awk '{if($1 != last) {print $0;last=$1}}' >> coverage.out
rm -rf *.cover.out


sysProcAttr_rest.go → tor/sysProcAttr_rest.go View File

@@ -1,6 +1,6 @@
// +build !windows

package connectivity
package tor

import (
"syscall"

sysProcAttr_win.go → tor/sysProcAttr_win.go View File

@@ -1,6 +1,6 @@
// +build windows

package connectivity
package tor

import (
"syscall"

torProvider.go → tor/torProvider.go View File

@@ -1,8 +1,9 @@
package connectivity
package tor

import (
"context"
"errors"
"git.openprivacy.ca/openprivacy/connectivity"
"git.openprivacy.ca/openprivacy/log"
"github.com/cretz/bine/control"
"github.com/cretz/bine/process"
@@ -22,23 +23,14 @@ import (
"time"
)

// Error captures various common ricochet errors
type Error string

func (e Error) Error() string { return string(e) }

const (
// CannotResolveLocalTCPAddressError is thrown when a local ricochet connection has the wrong format.
CannotResolveLocalTCPAddressError = Error("CannotResolveLocalTCPAddressError")
// CannotDialLocalTCPAddressError is thrown when a connection to a local ricochet address fails.
CannotDialLocalTCPAddressError = Error("CannotDialLocalTCPAddressError")
// CannotDialRicochetAddressError is thrown when a connection to a ricochet address fails.
CannotDialRicochetAddressError = Error("CannotDialRicochetAddressError")
minStatusIntervalMs = 200
maxStatusIntervalMs = 2000
)

const (
minStatusIntervalMs = 200
maxStatusIntervalMs = 2000
// CannotDialRicochetAddressError is thrown when a connection to a ricochet address fails.
CannotDialRicochetAddressError = connectivity.Error("CannotDialRicochetAddressError")
)

type onionListenService struct {
@@ -112,7 +104,7 @@ func (tp *torProvider) WaitTillBootstrapped() {
}
}

func (tp *torProvider) Listen(identity PrivateKey, port int) (ListenService, error) {
func (tp *torProvider) Listen(identity connectivity.PrivateKey, port int) (connectivity.ListenService, error) {
var onion = ""
var privkey ed25519.PrivateKey

@@ -207,8 +199,8 @@ func (tp *torProvider) SetStatusCallback(callback func(int, string)) {
tp.statusCallback = callback
}

// StartTor creates/starts a Tor ACN and returns a usable ACN object
func StartTor(appDirectory string, bundledTorPath string) (ACN, error) {
// NewTorACN creates/starts a Tor ACN and returns a usable ACN object
func NewTorACN(appDirectory string, bundledTorPath string) (connectivity.ACN, error) {
tp, err := startTor(appDirectory, bundledTorPath)
if err == nil {
tp.dialer, err = tp.t.Dialer(nil, &tor.DialConf{})
@@ -293,15 +285,19 @@ func (tp *torProvider) monitorRestart() {
prog, status := tp.GetBootstrapStatus()

if prog == -1 && tp.t != nil {
tp.lock.Lock()
if tp.statusCallback != nil {
tp.statusCallback(prog, status)
}
tp.lock.Unlock()
tp.restart()
interval = minStatusIntervalMs
} else if prog != lastBootstrapProgress {
tp.lock.Lock()
if tp.statusCallback != nil {
tp.statusCallback(prog, status)
}
tp.lock.Unlock()
interval = minStatusIntervalMs
} else {
if interval < maxStatusIntervalMs {

torProvider_test.go → tor/torProvider_test.go View File

@@ -1,4 +1,4 @@
package connectivity
package tor

import (
"fmt"
@@ -14,7 +14,7 @@ func getStatusCallback(progChan chan int) func(int, string) {

func TestTorProvider(t *testing.T) {
progChan := make(chan int)
acn, err := StartTor(".", "")
acn, err := NewTorACN(".", "")
if err != nil {
t.Error(err)
return

torUtils.go → tor/torUtils.go View File

@@ -1,10 +1,10 @@
package connectivity
package tor

import (
"encoding/base32"
"strings"
"golang.org/x/crypto/ed25519"
"golang.org/x/crypto/sha3"
"strings"
)

// V3HostnameLength is the length of a Tor V3 Onion Address (without the .onion suffix)

Loading…
Cancel
Save