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.

appService.go 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. message := event.IPCMessage{Dest: DestApp, Message: *storage.GetNewPeerMessage()}
  59. as.bridge.Write(&message)
  60. }
  61. message := event.IPCMessage{Dest: DestApp, Message: event.NewEventList(event.ReloadDone)}
  62. as.bridge.Write(&message)
  63. case event.ReloadPeer:
  64. onion := ev.Data[event.Identity]
  65. events := as.storage[onion].GetStatusMessages()
  66. for _, ev := range events {
  67. message := event.IPCMessage{Dest: onion, Message: *ev}
  68. as.bridge.Write(&message)
  69. }
  70. case event.GetACNStatus:
  71. prog, status := as.acn.GetBootstrapStatus()
  72. as.getACNStatusHandler()(prog, status)
  73. case event.ShutdownPeer:
  74. onion := ev.Data[event.Identity]
  75. as.ShutdownPeer(onion)
  76. }
  77. }
  78. func (as *applicationService) createPeer(name, password, tag string) {
  79. log.Infof("app Service create peer %v %v\n", name, password)
  80. profile, err := as.applicationCore.CreatePeer(name)
  81. as.eventBuses[profile.Onion] = event.IPCEventManagerFrom(as.bridge, profile.Onion, as.eventBuses[profile.Onion])
  82. if err != nil {
  83. log.Errorf("Could not create Peer: %v\n", err)
  84. message := event.IPCMessage{Dest: DestApp, Message: event.NewEventList(event.PeerError, event.Error, err.Error())}
  85. as.bridge.Write(&message)
  86. return
  87. }
  88. if tag != "" {
  89. profile.SetAttribute(AttributeTag, tag)
  90. }
  91. profileStore := storage.NewProfileWriterStore(as.eventBuses[profile.Onion], path.Join(as.directory, "profiles", profile.LocalID), password, profile)
  92. blockedPeers := profile.BlockedPeers()
  93. // TODO: Would be nice if ProtocolEngine did not need to explicitly be given the Private Key.
  94. identity := primitives.InitializeIdentity(profile.Name, &profile.Ed25519PrivateKey, &profile.Ed25519PublicKey)
  95. engine := connections.NewProtocolEngine(identity, profile.Ed25519PrivateKey, as.acn, as.eventBuses[profile.Onion], profile.GetContacts(), blockedPeers)
  96. as.storage[profile.Onion] = profileStore
  97. as.engines[profile.Onion] = engine
  98. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.NewPeer, map[event.Field]string{event.Identity: profile.LocalID, event.Password: password})}
  99. as.bridge.Write(&message)
  100. }
  101. func (as *applicationService) loadProfiles(password string) {
  102. count := 0
  103. as.applicationCore.LoadProfiles(password, false, func(profile *model.Profile, profileStore storage.ProfileStore) {
  104. as.eventBuses[profile.Onion] = event.IPCEventManagerFrom(as.bridge, profile.Onion, as.eventBuses[profile.Onion])
  105. blockedPeers := profile.BlockedPeers()
  106. identity := primitives.InitializeIdentity(profile.Name, &profile.Ed25519PrivateKey, &profile.Ed25519PublicKey)
  107. engine := connections.NewProtocolEngine(identity, profile.Ed25519PrivateKey, as.acn, as.eventBuses[profile.Onion], profile.GetContacts(), blockedPeers)
  108. as.asmutex.Lock()
  109. as.storage[profile.Onion] = profileStore
  110. as.engines[profile.Onion] = engine
  111. as.asmutex.Unlock()
  112. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.NewPeer, map[event.Field]string{event.Identity: profile.LocalID, event.Password: password})}
  113. as.bridge.Write(&message)
  114. count++
  115. })
  116. if count == 0 {
  117. message := event.IPCMessage{Dest: DestApp, Message: event.NewEventList(event.AppError, event.Error, event.AppErrLoaded0)}
  118. as.bridge.Write(&message)
  119. }
  120. }
  121. func (as *applicationService) getACNStatusHandler() func(int, string) {
  122. return func(progress int, status string) {
  123. progStr := strconv.Itoa(progress)
  124. as.bridge.Write(&event.IPCMessage{Dest: DestApp, Message: event.NewEventList(event.ACNStatus, event.Progreess, progStr, event.Status, status)})
  125. for _, bus := range as.eventBuses {
  126. bus.Publish(event.NewEventList(event.ACNStatus, event.Progreess, progStr, event.Status, status))
  127. }
  128. }
  129. }
  130. func (as *applicationService) deletePeer(onion string) {
  131. as.asmutex.Lock()
  132. defer as.asmutex.Unlock()
  133. as.appletPlugins.ShutdownPeer(onion)
  134. as.plugins.Delete(onion)
  135. as.engines[onion].Shutdown()
  136. delete(as.engines, onion)
  137. as.storage[onion].Shutdown()
  138. as.storage[onion].Delete()
  139. delete(as.storage, onion)
  140. as.applicationCore.DeletePeer(onion)
  141. }
  142. func (as *applicationService) ShutdownPeer(onion string) {
  143. as.engines[onion].Shutdown()
  144. delete(as.engines, onion)
  145. as.storage[onion].Shutdown()
  146. delete(as.storage, onion)
  147. as.eventBuses[onion].Shutdown()
  148. delete(as.eventBuses, onion)
  149. }
  150. // Shutdown shuts down the application Service and all peer related backend parts
  151. func (as *applicationService) Shutdown() {
  152. log.Debugf("shutting down application service...")
  153. as.appletPlugins.Shutdown()
  154. for id := range as.engines {
  155. log.Debugf("shutting down application service peer engine %v", id)
  156. as.ShutdownPeer(id)
  157. }
  158. }