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.

appClient.go 5.7KB

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