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

This commit is contained in:
Dan Ballard 2020-02-07 17:15:37 -05:00
parent 504139e4c6
commit e276c4fbe7
9 changed files with 49 additions and 30 deletions

3
.gitignore vendored
View File

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

12
acn.go
View File

@ -4,6 +4,18 @@ import (
"net" "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. // PrivateKey represents a private key using an unspecified algorithm.
type PrivateKey interface{} type PrivateKey interface{}

View File

@ -13,6 +13,11 @@ type localListenService struct {
type localProvider 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 { func (ls *localListenService) AddressFull() string {
return ls.l.Addr().String() return ls.l.Addr().String()
} }
@ -70,8 +75,3 @@ func (lp *localProvider) Restart() {
func (lp *localProvider) Close() { func (lp *localProvider) Close() {
} }
// LocalProvider returns a for testing use only local clearnet implementation of a ACN interface
func LocalProvider() ACN {
return &localProvider{}
}

10
tests.sh Normal file
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

View File

@ -1,6 +1,6 @@
// +build !windows // +build !windows
package connectivity package tor
import ( import (
"syscall" "syscall"

View File

@ -1,6 +1,6 @@
// +build windows // +build windows
package connectivity package tor
import ( import (
"syscall" "syscall"

View File

@ -1,8 +1,9 @@
package connectivity package tor
import ( import (
"context" "context"
"errors" "errors"
"git.openprivacy.ca/openprivacy/connectivity"
"git.openprivacy.ca/openprivacy/log" "git.openprivacy.ca/openprivacy/log"
"github.com/cretz/bine/control" "github.com/cretz/bine/control"
"github.com/cretz/bine/process" "github.com/cretz/bine/process"
@ -22,25 +23,16 @@ import (
"time" "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")
)
const ( const (
minStatusIntervalMs = 200 minStatusIntervalMs = 200
maxStatusIntervalMs = 2000 maxStatusIntervalMs = 2000
) )
const (
// CannotDialRicochetAddressError is thrown when a connection to a ricochet address fails.
CannotDialRicochetAddressError = connectivity.Error("CannotDialRicochetAddressError")
)
type onionListenService struct { type onionListenService struct {
os *tor.OnionService os *tor.OnionService
tp *torProvider tp *torProvider
@ -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 onion = ""
var privkey ed25519.PrivateKey var privkey ed25519.PrivateKey
@ -207,8 +199,8 @@ func (tp *torProvider) SetStatusCallback(callback func(int, string)) {
tp.statusCallback = callback tp.statusCallback = callback
} }
// StartTor creates/starts a Tor ACN and returns a usable ACN object // NewTorACN creates/starts a Tor ACN and returns a usable ACN object
func StartTor(appDirectory string, bundledTorPath string) (ACN, error) { func NewTorACN(appDirectory string, bundledTorPath string) (connectivity.ACN, error) {
tp, err := startTor(appDirectory, bundledTorPath) tp, err := startTor(appDirectory, bundledTorPath)
if err == nil { if err == nil {
tp.dialer, err = tp.t.Dialer(nil, &tor.DialConf{}) tp.dialer, err = tp.t.Dialer(nil, &tor.DialConf{})
@ -293,15 +285,19 @@ func (tp *torProvider) monitorRestart() {
prog, status := tp.GetBootstrapStatus() prog, status := tp.GetBootstrapStatus()
if prog == -1 && tp.t != nil { if prog == -1 && tp.t != nil {
tp.lock.Lock()
if tp.statusCallback != nil { if tp.statusCallback != nil {
tp.statusCallback(prog, status) tp.statusCallback(prog, status)
} }
tp.lock.Unlock()
tp.restart() tp.restart()
interval = minStatusIntervalMs interval = minStatusIntervalMs
} else if prog != lastBootstrapProgress { } else if prog != lastBootstrapProgress {
tp.lock.Lock()
if tp.statusCallback != nil { if tp.statusCallback != nil {
tp.statusCallback(prog, status) tp.statusCallback(prog, status)
} }
tp.lock.Unlock()
interval = minStatusIntervalMs interval = minStatusIntervalMs
} else { } else {
if interval < maxStatusIntervalMs { if interval < maxStatusIntervalMs {

View File

@ -1,4 +1,4 @@
package connectivity package tor
import ( import (
"fmt" "fmt"
@ -14,7 +14,7 @@ func getStatusCallback(progChan chan int) func(int, string) {
func TestTorProvider(t *testing.T) { func TestTorProvider(t *testing.T) {
progChan := make(chan int) progChan := make(chan int)
acn, err := StartTor(".", "") acn, err := NewTorACN(".", "")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return

View File

@ -1,10 +1,10 @@
package connectivity package tor
import ( import (
"encoding/base32" "encoding/base32"
"strings"
"golang.org/x/crypto/ed25519" "golang.org/x/crypto/ed25519"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
"strings"
) )
// V3HostnameLength is the length of a Tor V3 Onion Address (without the .onion suffix) // V3HostnameLength is the length of a Tor V3 Onion Address (without the .onion suffix)