2019-10-31 23:05:01 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cwtch.im/cwtch/event"
|
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
|
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/policies"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// networkCheck is a convenience plugin for testing high level availability of onion services
|
|
|
|
type networkCheck struct {
|
|
|
|
bus event.Manager
|
|
|
|
queue event.Queue
|
|
|
|
acn connectivity.ACN
|
|
|
|
onionsToCheck []string
|
|
|
|
breakChan chan bool
|
|
|
|
running bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNetworkCheck returns a Plugin that when started will attempt various network tests
|
|
|
|
func NewNetworkCheck(bus event.Manager, acn connectivity.ACN) Plugin {
|
|
|
|
nc := &networkCheck{bus: bus, acn: acn, queue: event.NewQueue(), breakChan: make(chan bool, 1)}
|
|
|
|
return nc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nc *networkCheck) Start() {
|
|
|
|
go nc.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nc *networkCheck) run() {
|
|
|
|
nc.running = true
|
|
|
|
nc.bus.Subscribe(event.ProtocolEngineStartListen, nc.queue)
|
|
|
|
nc.bus.Subscribe(event.NewMessageFromPeer, nc.queue)
|
|
|
|
nc.bus.Subscribe(event.PeerAcknowledgement, nc.queue)
|
|
|
|
nc.bus.Subscribe(event.EncryptedGroupMessage, nc.queue)
|
|
|
|
var lastMessageReceived time.Time
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-nc.breakChan:
|
|
|
|
nc.running = false
|
|
|
|
return
|
|
|
|
case e := <-nc.queue.OutChan():
|
|
|
|
switch e.EventType {
|
|
|
|
// On receipt of a Listen request for an onion service we will add the onion to our list
|
|
|
|
// and then we will wait a minute and check the connection for the first time (the onion should be up)
|
|
|
|
// under normal operating circumstances
|
|
|
|
case event.ProtocolEngineStartListen:
|
|
|
|
log.Debugf("initiating connection check for %v", e.Data[event.Onion])
|
|
|
|
nc.onionsToCheck = append(nc.onionsToCheck, e.Data[event.Onion])
|
|
|
|
default:
|
|
|
|
// if we receive either an encrypted group message or a peer acknowledgement we can assume the network
|
|
|
|
// is up and running (our onion service might still not be available, but we would aim to detect that
|
|
|
|
// through other actions
|
|
|
|
// we reset out timer
|
|
|
|
lastMessageReceived = time.Now()
|
2019-11-04 19:46:59 +00:00
|
|
|
nc.bus.Publish(event.NewEvent(event.NetworkStatus, map[event.Field]string{event.Error: "", event.Status: "Success"}))
|
2019-10-31 23:05:01 +00:00
|
|
|
}
|
|
|
|
case <-time.After(tickTime):
|
|
|
|
// if we haven't received an action in the last minute...kick off a set of testing
|
|
|
|
if time.Now().Sub(lastMessageReceived) > time.Minute {
|
|
|
|
for _, onion := range nc.onionsToCheck {
|
|
|
|
go nc.checkConnection(onion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nc *networkCheck) Shutdown() {
|
|
|
|
if nc.running {
|
|
|
|
nc.queue.Shutdown()
|
|
|
|
log.Debugf("shutting down network status plugin")
|
|
|
|
nc.breakChan <- true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
func (nc *networkCheck) checkConnection(onion string) {
|
|
|
|
// we want to definitively time these actions out faster than tor will, because these onions should definitely be
|
|
|
|
// online
|
|
|
|
ClientTimeout := policies.TimeoutPolicy(time.Second * 60)
|
|
|
|
err := ClientTimeout.ExecuteAction(func() error {
|
|
|
|
conn, _, err := nc.acn.Open(onion)
|
|
|
|
if err == nil {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
// regardless of the outcome we want to report a status to let anyone who might care know that we did do a check
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("publishing network error for %v", onion)
|
|
|
|
nc.bus.Publish(event.NewEvent(event.NetworkStatus, map[event.Field]string{event.Onion: onion, event.Error: err.Error(), event.Status: "Error"}))
|
|
|
|
} else {
|
|
|
|
log.Debugf("publishing network success for %v", onion)
|
|
|
|
nc.bus.Publish(event.NewEvent(event.NetworkStatus, map[event.Field]string{event.Onion: onion, event.Error: "", event.Status: "Success"}))
|
|
|
|
}
|
|
|
|
}
|