Official cwtch.im peer and server implementations. https://cwtch.im
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

177 lines
6.1 KiB

  1. package app
  2. import (
  3. "cwtch.im/cwtch/app/plugins"
  4. "cwtch.im/cwtch/event"
  5. "cwtch.im/cwtch/peer"
  6. "cwtch.im/cwtch/storage"
  7. "fmt"
  8. "git.openprivacy.ca/openprivacy/log"
  9. "path"
  10. "strconv"
  11. "sync"
  12. )
  13. type applicationClient struct {
  14. applicationBridge
  15. appletPeers
  16. appBus event.Manager
  17. acmutex sync.Mutex
  18. }
  19. // NewAppClient returns an Application that acts as a client to a AppService, connected by the IPCBridge supplied
  20. func NewAppClient(appDirectory string, bridge event.IPCBridge) Application {
  21. appClient := &applicationClient{appletPeers: appletPeers{peers: make(map[string]peer.CwtchPeer)}, applicationBridge: applicationBridge{applicationCore: *newAppCore(appDirectory), bridge: bridge}, appBus: event.NewEventManager()}
  22. appClient.handle = appClient.handleEvent
  23. go appClient.listen()
  24. appClient.bridge.Write(&event.IPCMessage{Dest: DestApp, Message: event.NewEventList(event.ReloadClient)})
  25. log.Infoln("Created new App Client")
  26. return appClient
  27. }
  28. // GetPrimaryBus returns the bus the Application uses for events that aren't peer specific
  29. func (ac *applicationClient) GetPrimaryBus() event.Manager {
  30. return ac.appBus
  31. }
  32. func (ac *applicationClient) handleEvent(ev *event.Event) {
  33. switch ev.EventType {
  34. case event.NewPeer:
  35. localID := ev.Data[event.Identity]
  36. key := ev.Data[event.Key]
  37. salt := ev.Data[event.Salt]
  38. reload := ev.Data[event.Status] == event.StorageRunning
  39. created := ev.Data[event.Created]
  40. ac.newPeer(localID, key, salt, reload, created)
  41. case event.DeletePeer:
  42. onion := ev.Data[event.Identity]
  43. ac.handleDeletedPeer(onion)
  44. case event.PeerError:
  45. ac.appBus.Publish(*ev)
  46. case event.AppError:
  47. ac.appBus.Publish(*ev)
  48. case event.ACNStatus:
  49. ac.appBus.Publish(*ev)
  50. case event.ACNVersion:
  51. ac.appBus.Publish(*ev)
  52. case event.ReloadDone:
  53. ac.appBus.Publish(*ev)
  54. }
  55. }
  56. func (ac *applicationClient) newPeer(localID, key, salt string, reload bool, created string) {
  57. var keyBytes [32]byte
  58. var saltBytes [128]byte
  59. copy(keyBytes[:], key)
  60. copy(saltBytes[:], salt)
  61. profile, err := storage.ReadProfile(path.Join(ac.directory, "profiles", localID), keyBytes, saltBytes)
  62. if err != nil {
  63. log.Errorf("Could not read profile for NewPeer event: %v\n", err)
  64. ac.appBus.Publish(event.NewEventList(event.PeerError, event.Error, fmt.Sprintf("Could not read profile for NewPeer event: %v\n", err)))
  65. return
  66. }
  67. _, exists := ac.peers[profile.Onion]
  68. if exists {
  69. log.Errorf("profile for onion %v already exists", profile.Onion)
  70. ac.appBus.Publish(event.NewEventList(event.PeerError, event.Error, fmt.Sprintf("profile for onion %v already exists", profile.Onion)))
  71. return
  72. }
  73. eventBus := event.NewIPCEventManager(ac.bridge, profile.Onion)
  74. peer := peer.FromProfile(profile)
  75. peer.Init(eventBus)
  76. ac.peerLock.Lock()
  77. defer ac.peerLock.Unlock()
  78. ac.peers[profile.Onion] = peer
  79. ac.eventBuses[profile.Onion] = eventBus
  80. npEvent := event.NewEvent(event.NewPeer, map[event.Field]string{event.Identity: profile.Onion, event.Created: created})
  81. if reload {
  82. npEvent.Data[event.Status] = event.StorageRunning
  83. }
  84. ac.appBus.Publish(npEvent)
  85. if reload {
  86. ac.bridge.Write(&event.IPCMessage{Dest: DestApp, Message: event.NewEventList(event.ReloadPeer, event.Identity, profile.Onion)})
  87. }
  88. }
  89. // CreatePeer messages the service to create a new Peer with the given name
  90. func (ac *applicationClient) CreatePeer(name string, password string) {
  91. ac.CreateTaggedPeer(name, password, "")
  92. }
  93. func (ac *applicationClient) CreateTaggedPeer(name, password, tag string) {
  94. log.Infof("appClient CreatePeer %v\n", name)
  95. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.CreatePeer, map[event.Field]string{event.ProfileName: name, event.Password: password, event.Data: tag})}
  96. ac.bridge.Write(&message)
  97. }
  98. // DeletePeer messages tehe service to delete a peer
  99. func (ac *applicationClient) DeletePeer(onion string) {
  100. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.DeletePeer, map[event.Field]string{event.Identity: onion})}
  101. ac.bridge.Write(&message)
  102. }
  103. func (ac *applicationClient) ChangePeerPassword(onion, oldpass, newpass string) {
  104. message := event.IPCMessage{Dest: onion, Message: event.NewEventList(event.ChangePassword, event.Password, oldpass, event.NewPassword, newpass)}
  105. ac.bridge.Write(&message)
  106. }
  107. func (ac *applicationClient) handleDeletedPeer(onion string) {
  108. ac.acmutex.Lock()
  109. defer ac.acmutex.Unlock()
  110. ac.peers[onion].Shutdown()
  111. delete(ac.peers, onion)
  112. ac.eventBuses[onion].Publish(event.NewEventList(event.ShutdownPeer, event.Identity, onion))
  113. ac.applicationCore.DeletePeer(onion)
  114. }
  115. func (ac *applicationClient) AddPeerPlugin(onion string, pluginID plugins.PluginID) {
  116. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.AddPeerPlugin, map[event.Field]string{event.Identity: onion, event.Data: strconv.Itoa(int(pluginID))})}
  117. ac.bridge.Write(&message)
  118. }
  119. // LoadProfiles messages the service to load any profiles for the given password
  120. func (ac *applicationClient) LoadProfiles(password string) {
  121. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.LoadProfiles, map[event.Field]string{event.Password: password})}
  122. ac.bridge.Write(&message)
  123. }
  124. func (ac *applicationClient) QueryACNStatus() {
  125. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.GetACNStatus, map[event.Field]string{})}
  126. ac.bridge.Write(&message)
  127. }
  128. func (ac *applicationClient) QueryACNVersion() {
  129. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.GetACNVersion, map[event.Field]string{})}
  130. ac.bridge.Write(&message)
  131. }
  132. // ShutdownPeer shuts down a peer and removes it from the app's management
  133. func (ac *applicationClient) ShutdownPeer(onion string) {
  134. ac.acmutex.Lock()
  135. defer ac.acmutex.Unlock()
  136. ac.eventBuses[onion].Shutdown()
  137. delete(ac.eventBuses, onion)
  138. ac.peers[onion].Shutdown()
  139. delete(ac.peers, onion)
  140. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.ShutdownPeer, map[event.Field]string{event.Identity: onion})}
  141. ac.bridge.Write(&message)
  142. }
  143. // Shutdown shuts down the application client and all front end peer components
  144. func (ac *applicationClient) Shutdown() {
  145. for id := range ac.peers {
  146. ac.ShutdownPeer(id)
  147. }
  148. ac.applicationBridge.Shutdown()
  149. ac.appBus.Shutdown()
  150. }