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.0KB

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