This repository has been archived on 2021-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
ui/go/handlers/appHandler.go

124 lines
3.3 KiB
Go

package handlers
import (
"cwtch.im/cwtch/app"
"cwtch.im/cwtch/app/plugins"
"cwtch.im/cwtch/event"
"cwtch.im/ui/go/constants"
"cwtch.im/ui/go/the"
"cwtch.im/ui/go/ui"
"git.openprivacy.ca/openprivacy/libricochet-go/log"
"os"
"strconv"
"time"
)
func App(gcd *ui.GrandCentralDispatcher, subscribed chan bool, reloadingAccounts bool) {
q := event.NewQueue()
the.AppBus.Subscribe(event.NewPeer, q)
the.AppBus.Subscribe(event.PeerError, q)
the.AppBus.Subscribe(event.AppError, q)
the.AppBus.Subscribe(event.ACNStatus, q)
the.AppBus.Subscribe(event.NetworkStatus, q)
the.AppBus.Subscribe(event.ReloadDone, q)
subscribed <- true
networkOffline := false
timeSinceLastSuccess := time.Unix(0, 0)
gcd.Loaded()
for {
e := q.Next()
switch e.EventType {
case event.NetworkStatus:
status := e.Data[event.Status]
if status == "Error" && !networkOffline {
networkOffline = true
// if it has been more that 5 minutes since we received any kind of success, then we should kill tor
// anything less that this i.e. transient networking failures, should allow us to reconnect without issue
if time.Now().Sub(timeSinceLastSuccess) > (time.Minute * 5) {
the.ACN.Restart()
}
}
if status == "Success" && networkOffline {
timeSinceLastSuccess = time.Now()
networkOffline = false
}
case event.ACNStatus:
progStr := e.Data[event.Progreess]
percent, _ := strconv.Atoi(progStr)
message := e.Data[event.Status]
var statuscode int
if percent >= 0 && percent <= 25 {
statuscode = 1
message = "Connecting to network"
} else if percent < 100 {
statuscode = 2
message = "Establishing Tor circuit"
} else if percent == 100 {
statuscode = 3
message = "tor appears to be running just fine!"
} else {
statuscode = 0
message = "can't find tor. is it running? is the controlport configured?"
}
gcd.TorStatus(statuscode, message)
case event.PeerError:
// current only case
log.Errorf("couldn't load profiles: %v", e.Data[event.Error])
os.Exit(1)
case event.AppError:
if e.Data[event.Error] == event.AppErrLoaded0 {
if reloadingAccounts {
reloadingAccounts = false
} else {
gcd.ErrorLoaded0()
}
}
case event.ReloadDone:
reloadingAccounts = false
if len(the.CwtchApp.ListPeers()) == 0 {
the.CwtchApp.LoadProfiles(the.AppPassword)
}
case event.NewPeer:
onion := e.Data[event.Identity]
peer := the.CwtchApp.GetPeer(onion)
if tag, exists := peer.GetAttribute(app.AttributeTag); !exists || tag == "" {
peer.SetAttribute(app.AttributeTag, constants.ProfileTypeV1DefaultPassword)
}
log.Infof("NewPeer for %v\n", onion)
ui.AddProfile(gcd, onion)
the.CwtchApp.AddPeerPlugin(onion, plugins.CONNECTIONRETRY)
the.CwtchApp.AddPeerPlugin(onion, plugins.NETWORKCHECK)
incSubscribed := make(chan bool)
go PeerHandler(onion, gcd.GetUiManager(peer.GetProfile().Onion), incSubscribed)
<-incSubscribed
if e.Data[event.Status] != "running" {
peer.Listen()
peer.StartPeersConnections()
peer.StartGroupConnections()
}
blockUnkownPeers, exists := peer.GetProfile().GetAttribute(constants.BlockUnknownPeersSetting)
if exists && blockUnkownPeers == "true" {
the.EventBus.Publish(event.NewEvent(event.BlockUnknownPeers, map[event.Field]string{}))
}
}
}
}