Clean up ACN Closing Logic
continuous-integration/drone/push Build is pending Details
continuous-integration/drone/pr Build is pending Details

This commit is contained in:
Sarah Jamie Lewis 2022-01-12 11:44:45 -08:00
parent 384d59e9ef
commit 35247bd044
2 changed files with 24 additions and 25 deletions

3
acn.go
View File

@ -21,9 +21,6 @@ type PrivateKey interface{}
// ListenService is an address that was opened with Listen() and can Accept() new connections
type ListenService interface {
// AddressIdentity is the core "identity" part of an address, ex: rsjeuxzlexy4fvo75vrdtj37nrvlmvbw57n5mhypcjpzv3xkka3l4yyd
AddressIdentity() string
// AddressFull is the full network address, ex: rsjeuxzlexy4fvo75vrdtj37nrvlmvbw57n5mhypcjpzv3xkka3l4yyd.onion:9878
AddressFull() string

View File

@ -81,21 +81,13 @@ func (ols *onionListenService) AddressFull() string {
return ols.os.Addr().String()
}
func (ols *onionListenService) AddressIdentity() string {
ols.lock.Lock()
defer ols.lock.Unlock()
return ols.os.Addr().String()[:56]
}
func (ols *onionListenService) Accept() (net.Conn, error) {
return ols.os.Accept()
}
func (ols *onionListenService) Close() {
address := ols.AddressIdentity()
ols.lock.Lock()
defer ols.lock.Unlock()
ols.tp.unregisterListener(address)
ols.os.Close()
}
@ -157,9 +149,15 @@ func (tp *torProvider) GetVersion() string {
return "No Tor"
}
func (tp *torProvider) closed() bool {
tp.lock.Lock()
defer tp.lock.Unlock()
return tp.isClosed
}
// WaitTillBootstrapped Blocks until underlying network is bootstrapped
func (tp *torProvider) WaitTillBootstrapped() error {
for !tp.isClosed {
for !tp.closed() {
progress, _ := tp.GetBootstrapStatus()
if progress == 100 {
return nil
@ -170,9 +168,6 @@ func (tp *torProvider) WaitTillBootstrapped() error {
}
func (tp *torProvider) Listen(identity connectivity.PrivateKey, port int) (connectivity.ListenService, error) {
var onion = ""
var privkey ed25519.PrivateKey
tp.lock.Lock()
defer tp.lock.Unlock()
@ -180,6 +175,9 @@ func (tp *torProvider) Listen(identity connectivity.PrivateKey, port int) (conne
return nil, errors.New("tor provider closed")
}
var onion string
var privkey ed25519.PrivateKey
switch pk := identity.(type) {
case ed25519.PrivateKey:
privkey = pk
@ -187,7 +185,11 @@ func (tp *torProvider) Listen(identity connectivity.PrivateKey, port int) (conne
switch pubk := gpubk.(type) {
case ed25519.PublicKey:
onion = GetTorV3Hostname(pubk)
default:
return nil, fmt.Errorf("unknown public key type %v", pubk)
}
default:
return nil, fmt.Errorf("unknown private key type %v", pk)
}
// Hack around tor detached onions not having a more obvious resume mechanism
@ -223,7 +225,7 @@ func (tp *torProvider) Listen(identity connectivity.PrivateKey, port int) (conne
os.CloseLocalListenerOnClose = true
ols := &onionListenService{os: os, tp: tp}
tp.childListeners[ols.AddressIdentity()] = ols
tp.childListeners[ols.AddressFull()] = ols
return ols, nil
}
@ -286,11 +288,17 @@ func (tp *torProvider) Open(hostname string) (net.Conn, string, error) {
}
func (tp *torProvider) Close() {
for _, child := range tp.childListeners {
child.Close()
}
tp.lock.Lock()
defer tp.lock.Unlock()
// Unregister Child Listeners
for addr, child := range tp.childListeners {
child.Close()
delete(tp.childListeners, addr)
}
// Break out of any background checks and close
// the underlying tor connection
tp.isClosed = true
tp.breakChan <- true
if tp.t != nil {
@ -438,12 +446,6 @@ func (tp *torProvider) GetPID() (int, error) {
return 0, err
}
func (tp *torProvider) unregisterListener(id string) {
tp.lock.Lock()
defer tp.lock.Unlock()
delete(tp.childListeners, id)
}
func (tp *torProvider) monitorRestart() {
lastBootstrapProgress := networkUnknown
interval := minStatusIntervalMs