psiphonClient.go 5.6 KB

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