diff --git a/autonat.go b/autonat.go index 7e839dd..96a5781 100644 --- a/autonat.go +++ b/autonat.go @@ -61,6 +61,9 @@ type AmbientAutoNAT struct { // If only a single autoNAT peer is known, then the confidence increases // for each failure until it reaches 3. confidence int + + callbacks []func(NATStatus, int) + cbLk sync.Mutex } // NewAutoNAT creates a new ambient NAT autodiscovery instance attached to a host @@ -214,9 +217,24 @@ func (as *AmbientAutoNAT) autodetect() { as.status = NATStatusUnknown as.addr = nil } + as.runCallbacks() as.mx.Unlock() } +func (as *AmbientAutoNAT) runCallbacks() { + as.cbLk.Lock() + defer as.cbLk.Unlock() + for _, cb := range as.callbacks { + cb(as.status, as.confidence) + } +} + +func (as *AmbientAutoNAT) AddCallback(cb func(NATStatus, int)) { + as.cbLk.Lock() + defer as.cbLk.Unlock() + as.callbacks = append(as.callbacks, cb) +} + func (as *AmbientAutoNAT) getPeers() []peer.AddrInfo { as.mx.Lock() defer as.mx.Unlock()