services.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2016, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. // Package psiphon/server implements the core tunnel functionality of a Psiphon server.
  20. // The main function is RunServices, which runs one or all of a Psiphon API web server,
  21. // a tunneling SSH server, and an Obfuscated SSH protocol server. The server configuration
  22. // is created by the GenerateConfig function.
  23. package server
  24. import (
  25. "os"
  26. "os/signal"
  27. "sync"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  29. )
  30. // RunServices initializes support functions including logging, GeoIP service, and
  31. // redis connection pooling; and then starts the server components and runs them
  32. // until os.Interrupt or os.Kill signals are received. The config determines
  33. // which components are run.
  34. func RunServices(encodedConfigs [][]byte) error {
  35. config, err := LoadConfig(encodedConfigs)
  36. if err != nil {
  37. log.WithContextFields(LogFields{"error": err}).Error("load config failed")
  38. return psiphon.ContextError(err)
  39. }
  40. err = InitLogging(config)
  41. if err != nil {
  42. log.WithContextFields(LogFields{"error": err}).Error("init logging failed")
  43. return psiphon.ContextError(err)
  44. }
  45. err = InitGeoIP(config)
  46. if err != nil {
  47. log.WithContextFields(LogFields{"error": err}).Error("init GeoIP failed")
  48. return psiphon.ContextError(err)
  49. }
  50. if config.UseRedis() {
  51. err = InitRedis(config)
  52. if err != nil {
  53. log.WithContextFields(LogFields{"error": err}).Error("init redis failed")
  54. return psiphon.ContextError(err)
  55. }
  56. }
  57. waitGroup := new(sync.WaitGroup)
  58. shutdownBroadcast := make(chan struct{})
  59. errors := make(chan error)
  60. if config.RunWebServer() {
  61. waitGroup.Add(1)
  62. go func() {
  63. defer waitGroup.Done()
  64. err := RunWebServer(config, shutdownBroadcast)
  65. select {
  66. case errors <- err:
  67. default:
  68. }
  69. }()
  70. }
  71. if config.RunSSHServer() || config.RunObfuscatedSSHServer() {
  72. waitGroup.Add(1)
  73. go func() {
  74. defer waitGroup.Done()
  75. err := RunSSHServer(config, shutdownBroadcast)
  76. select {
  77. case errors <- err:
  78. default:
  79. }
  80. }()
  81. }
  82. // An OS signal triggers an orderly shutdown
  83. systemStopSignal := make(chan os.Signal, 1)
  84. signal.Notify(systemStopSignal, os.Interrupt, os.Kill)
  85. err = nil
  86. select {
  87. case <-systemStopSignal:
  88. log.WithContext().Info("shutdown by system")
  89. case err = <-errors:
  90. log.WithContextFields(LogFields{"error": err}).Error("service failed")
  91. }
  92. close(shutdownBroadcast)
  93. waitGroup.Wait()
  94. return err
  95. }