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.

196 lines
6.8KB

  1. package app
  2. import (
  3. "cwtch.im/cwtch/app/plugins"
  4. "cwtch.im/cwtch/event"
  5. "cwtch.im/cwtch/model"
  6. "cwtch.im/cwtch/protocol/connections"
  7. "cwtch.im/cwtch/storage"
  8. "cwtch.im/tapir/primitives"
  9. "git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
  10. "git.openprivacy.ca/openprivacy/libricochet-go/log"
  11. "path"
  12. "strconv"
  13. "sync"
  14. )
  15. type applicationService struct {
  16. applicationBridge
  17. appletACN
  18. appletPlugins
  19. storage map[string]storage.ProfileStore
  20. engines map[string]connections.Engine
  21. asmutex sync.Mutex
  22. }
  23. // ApplicationService is the back end of an application that manages engines and writing storage and communicates to an ApplicationClient by an IPCBridge
  24. type ApplicationService interface {
  25. Shutdown()
  26. }
  27. // NewAppService returns an ApplicationService that runs the backend of an app and communicates with a client by the supplied IPCBridge
  28. func NewAppService(acn connectivity.ACN, appDirectory string, bridge event.IPCBridge) ApplicationService {
  29. appService := &applicationService{storage: make(map[string]storage.ProfileStore), engines: make(map[string]connections.Engine), applicationBridge: applicationBridge{applicationCore: *newAppCore(appDirectory), bridge: bridge}}
  30. appService.appletACN.init(acn, appService.getACNStatusHandler())
  31. appService.handle = appService.handleEvent
  32. go appService.listen()
  33. log.Infoln("Created new App Service")
  34. return appService
  35. }
  36. func (as *applicationService) handleEvent(ev *event.Event) {
  37. log.Infof("app Service handleEvent %v\n", ev.EventType)
  38. switch ev.EventType {
  39. case event.CreatePeer:
  40. profileName := ev.Data[event.ProfileName]
  41. password := ev.Data[event.Password]
  42. tag := ev.Data[event.Data]
  43. as.createPeer(profileName, password, tag)
  44. case event.DeletePeer:
  45. onion := ev.Data[event.Identity]
  46. as.deletePeer(onion)
  47. message := event.IPCMessage{Dest: DestApp, Message: *ev}
  48. as.bridge.Write(&message)
  49. case event.AddPeerPlugin:
  50. onion := ev.Data[event.Identity]
  51. pluginID, _ := strconv.Atoi(ev.Data[event.Data])
  52. as.AddPlugin(onion, plugins.PluginID(pluginID), as.eventBuses[onion], as.acn)
  53. case event.LoadProfiles:
  54. password := ev.Data[event.Password]
  55. as.loadProfiles(password)
  56. case event.ReloadClient:
  57. for _, storage := range as.storage {
  58. peerMsg := *storage.GetNewPeerMessage()
  59. peerMsg.Data[event.Status] = event.StorageRunning
  60. message := event.IPCMessage{Dest: DestApp, Message: peerMsg}
  61. as.bridge.Write(&message)
  62. }
  63. message := event.IPCMessage{Dest: DestApp, Message: event.NewEventList(event.ReloadDone)}
  64. as.bridge.Write(&message)
  65. case event.ReloadPeer:
  66. onion := ev.Data[event.Identity]
  67. events := as.storage[onion].GetStatusMessages()
  68. for _, ev := range events {
  69. message := event.IPCMessage{Dest: onion, Message: *ev}
  70. as.bridge.Write(&message)
  71. }
  72. case event.GetACNStatus:
  73. prog, status := as.acn.GetBootstrapStatus()
  74. as.getACNStatusHandler()(prog, status)
  75. case event.ShutdownPeer:
  76. onion := ev.Data[event.Identity]
  77. as.ShutdownPeer(onion)
  78. }
  79. }
  80. func (as *applicationService) createPeer(name, password, tag string) {
  81. log.Infof("app Service create peer %v %v\n", name, password)
  82. profile, err := as.applicationCore.CreatePeer(name)
  83. as.eventBuses[profile.Onion] = event.IPCEventManagerFrom(as.bridge, profile.Onion, as.eventBuses[profile.Onion])
  84. if err != nil {
  85. log.Errorf("Could not create Peer: %v\n", err)
  86. message := event.IPCMessage{Dest: DestApp, Message: event.NewEventList(event.PeerError, event.Error, err.Error())}
  87. as.bridge.Write(&message)
  88. return
  89. }
  90. if tag != "" {
  91. profile.SetAttribute(AttributeTag, tag)
  92. }
  93. profileStore := storage.CreateProfileWriterStore(as.eventBuses[profile.Onion], path.Join(as.directory, "profiles", profile.LocalID), password, profile)
  94. blockedPeers := profile.BlockedPeers()
  95. // TODO: Would be nice if ProtocolEngine did not need to explicitly be given the Private Key.
  96. identity := primitives.InitializeIdentity(profile.Name, &profile.Ed25519PrivateKey, &profile.Ed25519PublicKey)
  97. engine := connections.NewProtocolEngine(identity, profile.Ed25519PrivateKey, as.acn, as.eventBuses[profile.Onion], profile.GetContacts(), blockedPeers)
  98. as.storage[profile.Onion] = profileStore
  99. as.engines[profile.Onion] = engine
  100. peerMsg := *profileStore.GetNewPeerMessage()
  101. peerMsg.Data[event.Status] = event.StorageNew
  102. message := event.IPCMessage{Dest: DestApp, Message: peerMsg}
  103. as.bridge.Write(&message)
  104. }
  105. func (as *applicationService) loadProfiles(password string) {
  106. count := 0
  107. as.applicationCore.LoadProfiles(password, false, func(profile *model.Profile, profileStore storage.ProfileStore) {
  108. as.eventBuses[profile.Onion] = event.IPCEventManagerFrom(as.bridge, profile.Onion, as.eventBuses[profile.Onion])
  109. blockedPeers := profile.BlockedPeers()
  110. identity := primitives.InitializeIdentity(profile.Name, &profile.Ed25519PrivateKey, &profile.Ed25519PublicKey)
  111. engine := connections.NewProtocolEngine(identity, profile.Ed25519PrivateKey, as.acn, as.eventBuses[profile.Onion], profile.GetContacts(), blockedPeers)
  112. as.asmutex.Lock()
  113. as.storage[profile.Onion] = profileStore
  114. as.engines[profile.Onion] = engine
  115. as.asmutex.Unlock()
  116. peerMsg := *profileStore.GetNewPeerMessage()
  117. peerMsg.Data[event.Status] = event.StorageNew
  118. message := event.IPCMessage{Dest: DestApp, Message: peerMsg}
  119. as.bridge.Write(&message)
  120. count++
  121. })
  122. if count == 0 {
  123. message := event.IPCMessage{Dest: DestApp, Message: event.NewEventList(event.AppError, event.Error, event.AppErrLoaded0)}
  124. as.bridge.Write(&message)
  125. }
  126. }
  127. func (as *applicationService) getACNStatusHandler() func(int, string) {
  128. return func(progress int, status string) {
  129. progStr := strconv.Itoa(progress)
  130. as.bridge.Write(&event.IPCMessage{Dest: DestApp, Message: event.NewEventList(event.ACNStatus, event.Progreess, progStr, event.Status, status)})
  131. as.applicationCore.coremutex.Lock()
  132. defer as.applicationCore.coremutex.Unlock()
  133. for _, bus := range as.eventBuses {
  134. bus.Publish(event.NewEventList(event.ACNStatus, event.Progreess, progStr, event.Status, status))
  135. }
  136. }
  137. }
  138. func (as *applicationService) deletePeer(onion string) {
  139. as.asmutex.Lock()
  140. defer as.asmutex.Unlock()
  141. as.appletPlugins.ShutdownPeer(onion)
  142. as.plugins.Delete(onion)
  143. as.engines[onion].Shutdown()
  144. delete(as.engines, onion)
  145. as.storage[onion].Shutdown()
  146. as.storage[onion].Delete()
  147. delete(as.storage, onion)
  148. as.applicationCore.DeletePeer(onion)
  149. }
  150. func (as *applicationService) ShutdownPeer(onion string) {
  151. as.engines[onion].Shutdown()
  152. delete(as.engines, onion)
  153. as.storage[onion].Shutdown()
  154. delete(as.storage, onion)
  155. as.eventBuses[onion].Shutdown()
  156. delete(as.eventBuses, onion)
  157. }
  158. // Shutdown shuts down the application Service and all peer related backend parts
  159. func (as *applicationService) Shutdown() {
  160. log.Debugf("shutting down application service...")
  161. as.appletPlugins.Shutdown()
  162. for id := range as.engines {
  163. log.Debugf("shutting down application service peer engine %v", id)
  164. as.ShutdownPeer(id)
  165. }
  166. }