psiphonClient.go 5.7 KB

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