main.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2015, 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 main
  20. import (
  21. "flag"
  22. "io"
  23. "io/ioutil"
  24. "os"
  25. "os/signal"
  26. "runtime/pprof"
  27. "sync"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  29. )
  30. func main() {
  31. // Define command-line parameters
  32. var configFilename string
  33. flag.StringVar(&configFilename, "config", "", "configuration input file")
  34. var embeddedServerEntryListFilename string
  35. flag.StringVar(&embeddedServerEntryListFilename, "serverList", "", "embedded server entry list input file")
  36. var formatNotices bool
  37. flag.BoolVar(&formatNotices, "formatNotices", false, "emit notices in human-readable format")
  38. var profileFilename string
  39. flag.StringVar(&profileFilename, "profile", "", "CPU profile output file")
  40. var interfaceName string
  41. flag.StringVar(&interfaceName, "listenInterface", "", "Interface Name")
  42. flag.Parse()
  43. // Initialize default Notice output (stderr)
  44. var noticeWriter io.Writer
  45. noticeWriter = os.Stderr
  46. if formatNotices {
  47. noticeWriter = psiphon.NewNoticeConsoleRewriter(noticeWriter)
  48. }
  49. psiphon.SetNoticeOutput(noticeWriter)
  50. psiphon.EmitNoticeBuildInfo()
  51. // Handle required config file parameter
  52. if configFilename == "" {
  53. psiphon.NoticeError("configuration file is required")
  54. os.Exit(1)
  55. }
  56. configFileContents, err := ioutil.ReadFile(configFilename)
  57. if err != nil {
  58. psiphon.NoticeError("error loading configuration file: %s", err)
  59. os.Exit(1)
  60. }
  61. config, err := psiphon.LoadConfig(configFileContents)
  62. if err != nil {
  63. psiphon.NoticeError("error processing configuration file: %s", err)
  64. os.Exit(1)
  65. }
  66. // When a logfile is configured, reinitialize Notice output
  67. if config.LogFilename != "" {
  68. logFile, err := os.OpenFile(config.LogFilename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
  69. if err != nil {
  70. psiphon.NoticeError("error opening log file: %s", err)
  71. os.Exit(1)
  72. }
  73. defer logFile.Close()
  74. var noticeWriter io.Writer
  75. noticeWriter = logFile
  76. if formatNotices {
  77. noticeWriter = psiphon.NewNoticeConsoleRewriter(noticeWriter)
  78. }
  79. psiphon.SetNoticeOutput(noticeWriter)
  80. }
  81. // Handle optional profiling parameter
  82. if profileFilename != "" {
  83. profileFile, err := os.Create(profileFilename)
  84. if err != nil {
  85. psiphon.NoticeError("error opening profile file: %s", err)
  86. os.Exit(1)
  87. }
  88. pprof.StartCPUProfile(profileFile)
  89. defer pprof.StopCPUProfile()
  90. }
  91. // Initialize data store
  92. err = psiphon.InitDataStore(config)
  93. if err != nil {
  94. psiphon.NoticeError("error initializing datastore: %s", err)
  95. os.Exit(1)
  96. }
  97. // Handle optional embedded server list file parameter
  98. // If specified, the embedded server list is loaded and stored. When there
  99. // are no server candidates at all, we wait for this import to complete
  100. // before starting the Psiphon controller. Otherwise, we import while
  101. // concurrently starting the controller to minimize delay before attempting
  102. // to connect to existing candidate servers.
  103. // If the import fails, an error notice is emitted, but the controller is
  104. // still started: either existing candidate servers may suffice, or the
  105. // remote server list fetch may obtain candidate servers.
  106. if embeddedServerEntryListFilename != "" {
  107. embeddedServerListWaitGroup := new(sync.WaitGroup)
  108. embeddedServerListWaitGroup.Add(1)
  109. go func() {
  110. defer embeddedServerListWaitGroup.Done()
  111. serverEntryList, err := ioutil.ReadFile(embeddedServerEntryListFilename)
  112. if err != nil {
  113. psiphon.NoticeError("error loading embedded server entry list file: %s", err)
  114. return
  115. }
  116. // TODO: stream embedded server list data? also, the cast makes an unnecessary copy of a large buffer?
  117. serverEntries, err := psiphon.DecodeAndValidateServerEntryList(
  118. string(serverEntryList),
  119. psiphon.GetCurrentTimestamp(),
  120. psiphon.SERVER_ENTRY_SOURCE_EMBEDDED)
  121. if err != nil {
  122. psiphon.NoticeError("error decoding embedded server entry list file: %s", err)
  123. return
  124. }
  125. // Since embedded server list entries may become stale, they will not
  126. // overwrite existing stored entries for the same server.
  127. err = psiphon.StoreServerEntries(serverEntries, false)
  128. if err != nil {
  129. psiphon.NoticeError("error storing embedded server entry list data: %s", err)
  130. return
  131. }
  132. }()
  133. if psiphon.CountServerEntries(config.EgressRegion, config.TunnelProtocol) == 0 {
  134. embeddedServerListWaitGroup.Wait()
  135. } else {
  136. defer embeddedServerListWaitGroup.Wait()
  137. }
  138. }
  139. if interfaceName != "" {
  140. config.ListenInterface = interfaceName
  141. }
  142. // Run Psiphon
  143. controller, err := psiphon.NewController(config)
  144. if err != nil {
  145. psiphon.NoticeError("error creating controller: %s", err)
  146. os.Exit(1)
  147. }
  148. controllerStopSignal := make(chan struct{}, 1)
  149. shutdownBroadcast := make(chan struct{})
  150. controllerWaitGroup := new(sync.WaitGroup)
  151. controllerWaitGroup.Add(1)
  152. go func() {
  153. defer controllerWaitGroup.Done()
  154. controller.Run(shutdownBroadcast)
  155. controllerStopSignal <- *new(struct{})
  156. }()
  157. // Wait for an OS signal or a Run stop signal, then stop Psiphon and exit
  158. systemStopSignal := make(chan os.Signal, 1)
  159. signal.Notify(systemStopSignal, os.Interrupt, os.Kill)
  160. select {
  161. case <-systemStopSignal:
  162. psiphon.NoticeInfo("shutdown by system")
  163. close(shutdownBroadcast)
  164. controllerWaitGroup.Wait()
  165. case <-controllerStopSignal:
  166. psiphon.NoticeInfo("shutdown by controller")
  167. }
  168. }