2019-06-05 20:40:55 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2019-08-02 01:09:01 +00:00
|
|
|
"cwtch.im/cwtch/app/plugins"
|
2019-06-05 20:40:55 +00:00
|
|
|
"cwtch.im/cwtch/event"
|
|
|
|
"cwtch.im/cwtch/peer"
|
|
|
|
"cwtch.im/cwtch/storage"
|
|
|
|
"fmt"
|
2020-02-10 22:09:24 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/log"
|
2019-06-05 20:40:55 +00:00
|
|
|
"path"
|
2019-08-02 01:09:01 +00:00
|
|
|
"strconv"
|
2019-12-13 19:34:59 +00:00
|
|
|
"sync"
|
2019-06-05 20:40:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type applicationClient struct {
|
|
|
|
applicationBridge
|
2019-07-10 20:30:24 +00:00
|
|
|
appletPeers
|
2019-06-05 20:40:55 +00:00
|
|
|
|
2020-01-13 22:11:00 +00:00
|
|
|
appBus event.Manager
|
2019-12-13 19:34:59 +00:00
|
|
|
acmutex sync.Mutex
|
2019-06-05 20:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAppClient returns an Application that acts as a client to a AppService, connected by the IPCBridge supplied
|
2019-06-21 21:50:43 +00:00
|
|
|
func NewAppClient(appDirectory string, bridge event.IPCBridge) Application {
|
2019-07-10 20:30:24 +00:00
|
|
|
appClient := &applicationClient{appletPeers: appletPeers{peers: make(map[string]peer.CwtchPeer)}, applicationBridge: applicationBridge{applicationCore: *newAppCore(appDirectory), bridge: bridge}, appBus: event.NewEventManager()}
|
2019-06-05 20:40:55 +00:00
|
|
|
appClient.handle = appClient.handleEvent
|
|
|
|
|
|
|
|
go appClient.listen()
|
|
|
|
|
2019-07-19 17:27:50 +00:00
|
|
|
appClient.bridge.Write(&event.IPCMessage{Dest: DestApp, Message: event.NewEventList(event.ReloadClient)})
|
|
|
|
|
2019-06-21 21:50:43 +00:00
|
|
|
log.Infoln("Created new App Client")
|
2019-06-05 20:40:55 +00:00
|
|
|
return appClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPrimaryBus returns the bus the Application uses for events that aren't peer specific
|
|
|
|
func (ac *applicationClient) GetPrimaryBus() event.Manager {
|
|
|
|
return ac.appBus
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *applicationClient) handleEvent(ev *event.Event) {
|
|
|
|
switch ev.EventType {
|
|
|
|
case event.NewPeer:
|
|
|
|
localID := ev.Data[event.Identity]
|
2020-01-13 22:11:00 +00:00
|
|
|
key := ev.Data[event.Key]
|
|
|
|
salt := ev.Data[event.Salt]
|
|
|
|
reload := ev.Data[event.Status] == event.StorageRunning
|
2020-11-02 22:35:11 +00:00
|
|
|
created := ev.Data[event.Created]
|
|
|
|
ac.newPeer(localID, key, salt, reload, created)
|
2021-06-14 23:50:35 +00:00
|
|
|
case event.PeerDeleted:
|
2019-12-10 23:45:43 +00:00
|
|
|
onion := ev.Data[event.Identity]
|
|
|
|
ac.handleDeletedPeer(onion)
|
2019-06-05 20:40:55 +00:00
|
|
|
case event.PeerError:
|
|
|
|
ac.appBus.Publish(*ev)
|
|
|
|
case event.AppError:
|
|
|
|
ac.appBus.Publish(*ev)
|
2019-07-10 20:30:24 +00:00
|
|
|
case event.ACNStatus:
|
|
|
|
ac.appBus.Publish(*ev)
|
2020-12-01 03:25:17 +00:00
|
|
|
case event.ACNVersion:
|
|
|
|
ac.appBus.Publish(*ev)
|
2019-07-19 17:27:50 +00:00
|
|
|
case event.ReloadDone:
|
|
|
|
ac.appBus.Publish(*ev)
|
2019-06-05 20:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 22:35:11 +00:00
|
|
|
func (ac *applicationClient) newPeer(localID, key, salt string, reload bool, created string) {
|
2020-01-13 22:11:00 +00:00
|
|
|
var keyBytes [32]byte
|
|
|
|
var saltBytes [128]byte
|
|
|
|
copy(keyBytes[:], key)
|
|
|
|
copy(saltBytes[:], salt)
|
|
|
|
profile, err := storage.ReadProfile(path.Join(ac.directory, "profiles", localID), keyBytes, saltBytes)
|
2019-06-05 20:40:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not read profile for NewPeer event: %v\n", err)
|
|
|
|
ac.appBus.Publish(event.NewEventList(event.PeerError, event.Error, fmt.Sprintf("Could not read profile for NewPeer event: %v\n", err)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, exists := ac.peers[profile.Onion]
|
|
|
|
if exists {
|
|
|
|
log.Errorf("profile for onion %v already exists", profile.Onion)
|
|
|
|
ac.appBus.Publish(event.NewEventList(event.PeerError, event.Error, fmt.Sprintf("profile for onion %v already exists", profile.Onion)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
eventBus := event.NewIPCEventManager(ac.bridge, profile.Onion)
|
|
|
|
peer := peer.FromProfile(profile)
|
2019-06-21 21:50:43 +00:00
|
|
|
peer.Init(eventBus)
|
2019-06-05 20:40:55 +00:00
|
|
|
|
2020-02-03 18:46:15 +00:00
|
|
|
ac.peerLock.Lock()
|
|
|
|
defer ac.peerLock.Unlock()
|
2019-06-05 20:40:55 +00:00
|
|
|
ac.peers[profile.Onion] = peer
|
|
|
|
ac.eventBuses[profile.Onion] = eventBus
|
2020-11-02 22:35:11 +00:00
|
|
|
npEvent := event.NewEvent(event.NewPeer, map[event.Field]string{event.Identity: profile.Onion, event.Created: created})
|
2019-07-19 17:27:50 +00:00
|
|
|
if reload {
|
2020-11-02 22:35:11 +00:00
|
|
|
npEvent.Data[event.Status] = event.StorageRunning
|
2019-07-19 17:27:50 +00:00
|
|
|
}
|
|
|
|
ac.appBus.Publish(npEvent)
|
|
|
|
|
|
|
|
if reload {
|
|
|
|
ac.bridge.Write(&event.IPCMessage{Dest: DestApp, Message: event.NewEventList(event.ReloadPeer, event.Identity, profile.Onion)})
|
|
|
|
}
|
2019-06-05 20:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePeer messages the service to create a new Peer with the given name
|
|
|
|
func (ac *applicationClient) CreatePeer(name string, password string) {
|
2019-12-10 23:45:43 +00:00
|
|
|
ac.CreateTaggedPeer(name, password, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *applicationClient) CreateTaggedPeer(name, password, tag string) {
|
2019-06-05 20:40:55 +00:00
|
|
|
log.Infof("appClient CreatePeer %v\n", name)
|
2019-12-10 23:45:43 +00:00
|
|
|
message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.CreatePeer, map[event.Field]string{event.ProfileName: name, event.Password: password, event.Data: tag})}
|
|
|
|
ac.bridge.Write(&message)
|
|
|
|
}
|
|
|
|
|
2021-06-14 22:42:44 +00:00
|
|
|
// DeletePeer messages the service to delete a peer
|
2021-06-14 23:50:35 +00:00
|
|
|
func (ac *applicationClient) DeletePeer(onion string, password string) {
|
2021-06-14 22:42:44 +00:00
|
|
|
message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.DeletePeer, map[event.Field]string{event.Identity: onion, event.Password: password})}
|
2019-06-05 20:40:55 +00:00
|
|
|
ac.bridge.Write(&message)
|
2019-08-02 01:09:01 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 20:21:14 +00:00
|
|
|
func (ac *applicationClient) ChangePeerPassword(onion, oldpass, newpass string) {
|
|
|
|
message := event.IPCMessage{Dest: onion, Message: event.NewEventList(event.ChangePassword, event.Password, oldpass, event.NewPassword, newpass)}
|
|
|
|
ac.bridge.Write(&message)
|
|
|
|
}
|
|
|
|
|
2019-12-10 23:45:43 +00:00
|
|
|
func (ac *applicationClient) handleDeletedPeer(onion string) {
|
2019-12-13 19:34:59 +00:00
|
|
|
ac.acmutex.Lock()
|
|
|
|
defer ac.acmutex.Unlock()
|
2019-12-10 23:45:43 +00:00
|
|
|
ac.peers[onion].Shutdown()
|
|
|
|
delete(ac.peers, onion)
|
|
|
|
ac.eventBuses[onion].Publish(event.NewEventList(event.ShutdownPeer, event.Identity, onion))
|
|
|
|
|
|
|
|
ac.applicationCore.DeletePeer(onion)
|
2021-06-14 23:50:35 +00:00
|
|
|
ac.appBus.Publish(event.NewEventList(event.PeerDeleted, event.Identity, onion))
|
2019-12-10 23:45:43 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 01:09:01 +00:00
|
|
|
func (ac *applicationClient) AddPeerPlugin(onion string, pluginID plugins.PluginID) {
|
|
|
|
message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.AddPeerPlugin, map[event.Field]string{event.Identity: onion, event.Data: strconv.Itoa(int(pluginID))})}
|
|
|
|
ac.bridge.Write(&message)
|
2019-06-05 20:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadProfiles messages the service to load any profiles for the given password
|
|
|
|
func (ac *applicationClient) LoadProfiles(password string) {
|
|
|
|
message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.LoadProfiles, map[event.Field]string{event.Password: password})}
|
|
|
|
ac.bridge.Write(&message)
|
|
|
|
}
|
|
|
|
|
2019-09-26 23:43:34 +00:00
|
|
|
func (ac *applicationClient) QueryACNStatus() {
|
|
|
|
message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.GetACNStatus, map[event.Field]string{})}
|
|
|
|
ac.bridge.Write(&message)
|
|
|
|
}
|
|
|
|
|
2020-12-01 03:25:17 +00:00
|
|
|
func (ac *applicationClient) QueryACNVersion() {
|
|
|
|
message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.GetACNVersion, map[event.Field]string{})}
|
|
|
|
ac.bridge.Write(&message)
|
|
|
|
}
|
|
|
|
|
2019-06-05 20:40:55 +00:00
|
|
|
// ShutdownPeer shuts down a peer and removes it from the app's management
|
|
|
|
func (ac *applicationClient) ShutdownPeer(onion string) {
|
2019-12-13 19:34:59 +00:00
|
|
|
ac.acmutex.Lock()
|
|
|
|
defer ac.acmutex.Unlock()
|
2019-06-05 20:40:55 +00:00
|
|
|
ac.eventBuses[onion].Shutdown()
|
|
|
|
delete(ac.eventBuses, onion)
|
|
|
|
ac.peers[onion].Shutdown()
|
|
|
|
delete(ac.peers, onion)
|
|
|
|
message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.ShutdownPeer, map[event.Field]string{event.Identity: onion})}
|
|
|
|
ac.bridge.Write(&message)
|
|
|
|
}
|
|
|
|
|
2019-10-31 23:05:01 +00:00
|
|
|
// Shutdown shuts down the application client and all front end peer components
|
2019-06-05 20:40:55 +00:00
|
|
|
func (ac *applicationClient) Shutdown() {
|
|
|
|
for id := range ac.peers {
|
|
|
|
ac.ShutdownPeer(id)
|
|
|
|
}
|
|
|
|
ac.applicationBridge.Shutdown()
|
|
|
|
ac.appBus.Shutdown()
|
|
|
|
}
|