psiphonClient.go 3.8 KB

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