move .Dial outside of lock to stop throtteling one conn per time; get Dialer once only

This commit is contained in:
Dan Ballard 2019-07-31 14:31:35 -07:00
parent 4ccdc79526
commit 59ea2902e8
1 changed files with 13 additions and 13 deletions

View File

@ -42,6 +42,7 @@ type onionListenService struct {
type torProvider struct {
t *tor.Tor
dialer *tor.Dialer
appDirectory string
bundeledTorPath string
lock sync.Mutex
@ -109,6 +110,9 @@ func (tp *torProvider) Listen(identity PrivateKey, port int) (ListenService, err
var onion = ""
var privkey ed25519.PrivateKey
tp.lock.Lock()
defer tp.lock.Unlock()
if tp.t == nil {
return nil, errors.New("Tor Provider closed")
}
@ -131,14 +135,8 @@ func (tp *torProvider) Listen(identity PrivateKey, port int) (ListenService, err
localport += 1024
}
if tp.t == nil {
return nil, errors.New("Tor is offline")
}
localListener, err := net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(localport))
tp.lock.Lock()
defer tp.lock.Unlock()
conf := &tor.ListenConf{NoWait: true, Version3: true, Key: identity, RemotePorts: []int{port}, Detach: true, DiscardKey: true, LocalListener: localListener}
os, err := tp.t.Listen(nil, conf)
if err != nil && strings.Contains(err.Error(), "550 Unspecified Tor error: Onion address collision") {
@ -159,22 +157,20 @@ func (tp *torProvider) Listen(identity PrivateKey, port int) (ListenService, err
func (tp *torProvider) Open(hostname string) (net.Conn, string, error) {
tp.lock.Lock()
defer tp.lock.Unlock()
if tp.t == nil {
tp.lock.Unlock()
return nil, hostname, errors.New("Tor is offline")
}
torDailer, err := tp.t.Dialer(nil, &tor.DialConf{})
if err != nil {
return nil, "", err
}
tp.lock.Unlock()
resolvedHostname := hostname
if strings.HasPrefix(hostname, "ricochet:") {
addrParts := strings.Split(hostname, ":")
resolvedHostname = addrParts[1]
}
conn, err := torDailer.Dial("tcp", resolvedHostname+".onion:9878")
conn, err := tp.dialer.Dial("tcp", resolvedHostname+".onion:9878")
return conn, resolvedHostname, err
}
@ -202,7 +198,10 @@ func (tp *torProvider) SetStatusCallback(callback func(int, string)) {
func StartTor(appDirectory string, bundledTorPath string) (ACN, error) {
tp, err := startTor(appDirectory, bundledTorPath)
if err == nil {
go tp.monitorRestart()
tp.dialer, err = tp.t.Dialer(nil, &tor.DialConf{})
if err == nil {
go tp.monitorRestart()
}
}
return tp, err
}
@ -308,6 +307,7 @@ func (tp *torProvider) restart() {
newTp, err := startTor(tp.appDirectory, tp.bundeledTorPath)
if err == nil {
tp.t = newTp.t
tp.dialer, _ = tp.t.Dialer(nil, &tor.DialConf{})
return
}
}