psiphonClient.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. "log"
  25. "os"
  26. "os/signal"
  27. "runtime/pprof"
  28. "sync"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  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. flag.Parse()
  42. // Handle required config file parameter
  43. if configFilename == "" {
  44. log.Fatalf("configuration file is required")
  45. }
  46. configFileContents, err := ioutil.ReadFile(configFilename)
  47. if err != nil {
  48. log.Fatalf("error loading configuration file: %s", err)
  49. }
  50. config, err := psiphon.LoadConfig(configFileContents)
  51. if err != nil {
  52. log.Fatalf("error processing configuration file: %s", err)
  53. }
  54. // Initialize notice output; use logfile, if configured
  55. var noticeWriter io.Writer
  56. noticeWriter = os.Stderr
  57. if config.LogFilename != "" {
  58. logFile, err := os.OpenFile(config.LogFilename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
  59. if err != nil {
  60. log.Fatalf("error opening log file: %s", err)
  61. }
  62. defer logFile.Close()
  63. }
  64. if formatNotices {
  65. noticeWriter = psiphon.NewNoticeConsoleRewriter(noticeWriter)
  66. }
  67. psiphon.SetNoticeOutput(noticeWriter)
  68. // Handle optional profiling parameter
  69. if profileFilename != "" {
  70. profileFile, err := os.Create(profileFilename)
  71. if err != nil {
  72. log.Fatalf("error opening profile file: %s", err)
  73. }
  74. pprof.StartCPUProfile(profileFile)
  75. defer pprof.StopCPUProfile()
  76. }
  77. // Initialize data store
  78. err = psiphon.InitDataStore(config)
  79. if err != nil {
  80. log.Fatalf("error initializing datastore: %s", err)
  81. }
  82. // Handle optional embedded server list file parameter
  83. // If specified, the embedded server list is loaded and stored before
  84. // running Psiphon.
  85. if embeddedServerEntryListFilename != "" {
  86. serverEntryList, err := ioutil.ReadFile(embeddedServerEntryListFilename)
  87. if err != nil {
  88. log.Fatalf("error loading embedded server entry list file: %s", err)
  89. }
  90. // TODO: stream embedded server list data? also, the cast makaes an unnecessary copy of a large buffer?
  91. serverEntries, err := psiphon.DecodeAndValidateServerEntryList(string(serverEntryList))
  92. if err != nil {
  93. log.Fatalf("error decoding embedded server entry list file: %s", err)
  94. }
  95. // Since embedded server list entries may become stale, they will not
  96. // overwrite existing stored entries for the same server.
  97. err = psiphon.StoreServerEntries(serverEntries, false)
  98. if err != nil {
  99. log.Fatalf("error storing embedded server entry list data: %s", err)
  100. }
  101. }
  102. // Run Psiphon
  103. controller, err := psiphon.NewController(config)
  104. if err != nil {
  105. log.Fatalf("error creating controller: %s", err)
  106. }
  107. controllerStopSignal := make(chan struct{}, 1)
  108. shutdownBroadcast := make(chan struct{})
  109. controllerWaitGroup := new(sync.WaitGroup)
  110. controllerWaitGroup.Add(1)
  111. go func() {
  112. defer controllerWaitGroup.Done()
  113. controller.Run(shutdownBroadcast)
  114. controllerStopSignal <- *new(struct{})
  115. }()
  116. // Wait for an OS signal or a Run stop signal, then stop Psiphon and exit
  117. systemStopSignal := make(chan os.Signal, 1)
  118. signal.Notify(systemStopSignal, os.Interrupt, os.Kill)
  119. select {
  120. case <-systemStopSignal:
  121. psiphon.NoticeInfo("shutdown by system")
  122. close(shutdownBroadcast)
  123. controllerWaitGroup.Wait()
  124. case <-controllerStopSignal:
  125. psiphon.NoticeInfo("shutdown by controller")
  126. }
  127. }