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.

169 lines
5.8KB

  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. ac.newPeer(localID, key, salt, reload)
  40. case event.DeletePeer:
  41. onion := ev.Data[event.Identity]
  42. ac.handleDeletedPeer(onion)
  43. case event.PeerError:
  44. ac.appBus.Publish(*ev)
  45. case event.AppError:
  46. ac.appBus.Publish(*ev)
  47. case event.ACNStatus:
  48. ac.appBus.Publish(*ev)
  49. case event.ReloadDone:
  50. ac.appBus.Publish(*ev)
  51. }
  52. }
  53. func (ac *applicationClient) newPeer(localID, key, salt string, reload bool) {
  54. var keyBytes [32]byte
  55. var saltBytes [128]byte
  56. copy(keyBytes[:], key)
  57. copy(saltBytes[:], salt)
  58. profile, err := storage.ReadProfile(path.Join(ac.directory, "profiles", localID), keyBytes, saltBytes)
  59. if err != nil {
  60. log.Errorf("Could not read profile for NewPeer event: %v\n", err)
  61. ac.appBus.Publish(event.NewEventList(event.PeerError, event.Error, fmt.Sprintf("Could not read profile for NewPeer event: %v\n", err)))
  62. return
  63. }
  64. _, exists := ac.peers[profile.Onion]
  65. if exists {
  66. log.Errorf("profile for onion %v already exists", profile.Onion)
  67. ac.appBus.Publish(event.NewEventList(event.PeerError, event.Error, fmt.Sprintf("profile for onion %v already exists", profile.Onion)))
  68. return
  69. }
  70. eventBus := event.NewIPCEventManager(ac.bridge, profile.Onion)
  71. peer := peer.FromProfile(profile)
  72. peer.Init(eventBus)
  73. ac.peerLock.Lock()
  74. defer ac.peerLock.Unlock()
  75. ac.peers[profile.Onion] = peer
  76. ac.eventBuses[profile.Onion] = eventBus
  77. npEvent := event.NewEvent(event.NewPeer, map[event.Field]string{event.Identity: profile.Onion})
  78. if reload {
  79. npEvent.Data[event.Status] = "running"
  80. }
  81. ac.appBus.Publish(npEvent)
  82. if reload {
  83. ac.bridge.Write(&event.IPCMessage{Dest: DestApp, Message: event.NewEventList(event.ReloadPeer, event.Identity, profile.Onion)})
  84. }
  85. }
  86. // CreatePeer messages the service to create a new Peer with the given name
  87. func (ac *applicationClient) CreatePeer(name string, password string) {
  88. ac.CreateTaggedPeer(name, password, "")
  89. }
  90. func (ac *applicationClient) CreateTaggedPeer(name, password, tag string) {
  91. log.Infof("appClient CreatePeer %v\n", name)
  92. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.CreatePeer, map[event.Field]string{event.ProfileName: name, event.Password: password, event.Data: tag})}
  93. ac.bridge.Write(&message)
  94. }
  95. // DeletePeer messages tehe service to delete a peer
  96. func (ac *applicationClient) DeletePeer(onion string) {
  97. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.DeletePeer, map[event.Field]string{event.Identity: onion})}
  98. ac.bridge.Write(&message)
  99. }
  100. func (ac *applicationClient) ChangePeerPassword(onion, oldpass, newpass string) {
  101. message := event.IPCMessage{Dest: onion, Message: event.NewEventList(event.ChangePassword, event.Password, oldpass, event.NewPassword, newpass)}
  102. ac.bridge.Write(&message)
  103. }
  104. func (ac *applicationClient) handleDeletedPeer(onion string) {
  105. ac.acmutex.Lock()
  106. defer ac.acmutex.Unlock()
  107. ac.peers[onion].Shutdown()
  108. delete(ac.peers, onion)
  109. ac.eventBuses[onion].Publish(event.NewEventList(event.ShutdownPeer, event.Identity, onion))
  110. ac.applicationCore.DeletePeer(onion)
  111. }
  112. func (ac *applicationClient) AddPeerPlugin(onion string, pluginID plugins.PluginID) {
  113. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.AddPeerPlugin, map[event.Field]string{event.Identity: onion, event.Data: strconv.Itoa(int(pluginID))})}
  114. ac.bridge.Write(&message)
  115. }
  116. // LoadProfiles messages the service to load any profiles for the given password
  117. func (ac *applicationClient) LoadProfiles(password string) {
  118. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.LoadProfiles, map[event.Field]string{event.Password: password})}
  119. ac.bridge.Write(&message)
  120. }
  121. func (ac *applicationClient) QueryACNStatus() {
  122. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.GetACNStatus, map[event.Field]string{})}
  123. ac.bridge.Write(&message)
  124. }
  125. // ShutdownPeer shuts down a peer and removes it from the app's management
  126. func (ac *applicationClient) ShutdownPeer(onion string) {
  127. ac.acmutex.Lock()
  128. defer ac.acmutex.Unlock()
  129. ac.eventBuses[onion].Shutdown()
  130. delete(ac.eventBuses, onion)
  131. ac.peers[onion].Shutdown()
  132. delete(ac.peers, onion)
  133. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.ShutdownPeer, map[event.Field]string{event.Identity: onion})}
  134. ac.bridge.Write(&message)
  135. }
  136. // Shutdown shuts down the application client and all front end peer components
  137. func (ac *applicationClient) Shutdown() {
  138. for id := range ac.peers {
  139. ac.ShutdownPeer(id)
  140. }
  141. ac.applicationBridge.Shutdown()
  142. ac.appBus.Shutdown()
  143. }