main.go 5.9 KB

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