move .Dial outside of lock to stop throtteling one conn per time; get Dialer once only
This commit is contained in:
parent
4ccdc79526
commit
59ea2902e8
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -201,9 +197,12 @@ func (tp *torProvider) SetStatusCallback(callback func(int, string)) {
|
|||
// StartTor creates/starts a Tor ACN and returns a usable ACN object
|
||||
func StartTor(appDirectory string, bundledTorPath string) (ACN, error) {
|
||||
tp, err := startTor(appDirectory, bundledTorPath)
|
||||
if err == nil {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue