psiphonClient.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. log.SetOutput(logFile)
  59. }
  60. // Handle optional profiling parameter
  61. if profileFilename != "" {
  62. profileFile, err := os.Create(profileFilename)
  63. if err != nil {
  64. log.Fatalf("error opening profile file: %s", err)
  65. }
  66. pprof.StartCPUProfile(profileFile)
  67. defer pprof.StopCPUProfile()
  68. }
  69. // Initialize data store
  70. err = psiphon.InitDataStore(config)
  71. if err != nil {
  72. log.Fatalf("error initializing datastore: %s", err)
  73. }
  74. // Handle optional embedded server list file parameter
  75. // If specified, the embedded server list is loaded and stored before
  76. // running Psiphon.
  77. if embeddedServerEntryListFilename != "" {
  78. serverEntryList, err := ioutil.ReadFile(embeddedServerEntryListFilename)
  79. if err != nil {
  80. log.Fatalf("error loading embedded server entry list file: %s", err)
  81. }
  82. // TODO: stream embedded server list data? also, the cast makaes an unnecessary copy of a large buffer?
  83. serverEntries, err := psiphon.DecodeServerEntryList(string(serverEntryList))
  84. if err != nil {
  85. log.Fatalf("error decoding embedded server entry list file: %s", err)
  86. }
  87. // Since embedded server list entries may become stale, they will not
  88. // overwrite existing stored entries for the same server.
  89. err = psiphon.StoreServerEntries(serverEntries, false)
  90. if err != nil {
  91. log.Fatalf("error storing embedded server entry list data: %s", err)
  92. }
  93. }
  94. // Run Psiphon
  95. controller, err := psiphon.NewController(config)
  96. if err != nil {
  97. log.Fatalf("error creating controller: %s", err)
  98. }
  99. controllerStopSignal := make(chan struct{}, 1)
  100. shutdownBroadcast := make(chan struct{})
  101. controllerWaitGroup := new(sync.WaitGroup)
  102. controllerWaitGroup.Add(1)
  103. go func() {
  104. defer controllerWaitGroup.Done()
  105. controller.Run(shutdownBroadcast)
  106. controllerStopSignal <- *new(struct{})
  107. }()
  108. // Wait for an OS signal or a Run stop signal, then stop Psiphon and exit
  109. systemStopSignal := make(chan os.Signal, 1)
  110. signal.Notify(systemStopSignal, os.Interrupt, os.Kill)
  111. select {
  112. case <-systemStopSignal:
  113. psiphon.Notice(psiphon.NOTICE_INFO, "shutdown by system")
  114. close(shutdownBroadcast)
  115. controllerWaitGroup.Wait()
  116. case <-controllerStopSignal:
  117. psiphon.Notice(psiphon.NOTICE_INFO, "shutdown by controller")
  118. }
  119. }