main.go 6.1 KB

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