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.

server.go 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package server
  2. import (
  3. "cwtch.im/cwtch/server/fetch"
  4. "cwtch.im/cwtch/server/listen"
  5. "cwtch.im/cwtch/server/metrics"
  6. "cwtch.im/cwtch/server/send"
  7. "cwtch.im/cwtch/server/storage"
  8. "git.openprivacy.ca/openprivacy/libricochet-go/application"
  9. "git.openprivacy.ca/openprivacy/libricochet-go/channels"
  10. "git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
  11. "git.openprivacy.ca/openprivacy/libricochet-go/utils"
  12. "git.openprivacy.ca/openprivacy/libricochet-go/log"
  13. "os"
  14. "strconv"
  15. "time"
  16. )
  17. // Server encapsulates a complete, compliant Cwtch server.
  18. type Server struct {
  19. app *application.RicochetApplication
  20. config Config
  21. metricsPack metrics.Monitors
  22. }
  23. // Run starts a server with the given privateKey
  24. // TODO: surface errors
  25. // TODO: handle HUP/KILL signals to exit and close Tor gracefully
  26. // TODO: handle user input to exit
  27. func (s *Server) Run(acn connectivity.ACN, serverConfig Config) {
  28. s.config = serverConfig
  29. cwtchserver := new(application.RicochetApplication)
  30. s.metricsPack.Start(cwtchserver, serverConfig.ConfigDir, s.config.ServerReporting.LogMetricsToFile)
  31. af := application.InstanceFactory{}
  32. af.Init()
  33. ms := new(storage.MessageStore)
  34. err := ms.Init(serverConfig.ConfigDir, s.config.MaxBufferLines, s.metricsPack.MessageCounter)
  35. if err != nil {
  36. log.Errorln(err)
  37. acn.Close()
  38. os.Exit(1)
  39. }
  40. af.AddHandler("im.cwtch.server.listen", func(rai *application.Instance) func() channels.Handler {
  41. return func() channels.Handler {
  42. cslc := new(listen.CwtchServerListenChannel)
  43. return cslc
  44. }
  45. })
  46. af.AddHandler("im.cwtch.server.fetch", func(rai *application.Instance) func() channels.Handler {
  47. si := new(Instance)
  48. si.Init(rai, cwtchserver, ms)
  49. return func() channels.Handler {
  50. cssc := new(fetch.CwtchServerFetchChannel)
  51. cssc.Handler = si
  52. return cssc
  53. }
  54. })
  55. af.AddHandler("im.cwtch.server.send", func(rai *application.Instance) func() channels.Handler {
  56. si := new(Instance)
  57. si.Init(rai, cwtchserver, ms)
  58. return func() channels.Handler {
  59. cssc := new(send.CwtchServerSendChannel)
  60. cssc.Handler = si
  61. return cssc
  62. }
  63. })
  64. addressIdentity := utils.GetTorV3Hostname(s.config.PublicKey)
  65. cwtchserver.Init(acn, "cwtch server for "+addressIdentity, s.config.Identity(), af, new(application.AcceptAllContactManager))
  66. port := strconv.Itoa(application.RicochetPort)
  67. log.Infof("cwtch server running on cwtch:%s\n", addressIdentity+".onion:"+port)
  68. s.app = cwtchserver
  69. for true {
  70. listenService, err := acn.Listen(s.config.PrivateKey, application.RicochetPort)
  71. if err != nil {
  72. log.Errorf("Listen() error setting up onion service: %v\n", err)
  73. } else {
  74. s.app.Run(listenService)
  75. }
  76. time.Sleep(5 * time.Second)
  77. }
  78. }
  79. // Shutdown kills the app closing all connections and freeing all goroutines
  80. func (s *Server) Shutdown() {
  81. s.app.Shutdown()
  82. s.metricsPack.Stop()
  83. }