psiphonClient.go 4.1 KB

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