psiphonClient.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2014, 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. var configFilename string
  32. flag.StringVar(&configFilename, "config", "", "configuration input file")
  33. var profileFilename string
  34. flag.StringVar(&profileFilename, "profile", "", "CPU profile output file")
  35. flag.Parse()
  36. if configFilename == "" {
  37. log.Fatalf("configuration file is required")
  38. }
  39. configFileContents, err := ioutil.ReadFile(configFilename)
  40. if err != nil {
  41. log.Fatalf("error loading configuration file: %s", err)
  42. }
  43. config, err := psiphon.LoadConfig(configFileContents)
  44. if err != nil {
  45. log.Fatalf("error processing configuration file: %s", err)
  46. }
  47. if profileFilename != "" {
  48. profileFile, err := os.Create(profileFilename)
  49. if err != nil {
  50. log.Fatalf("error opening profile file: %s", err)
  51. }
  52. pprof.StartCPUProfile(profileFile)
  53. defer pprof.StopCPUProfile()
  54. }
  55. err = psiphon.InitDataStore(config)
  56. if err != nil {
  57. log.Fatalf("error initializing datastore: %s", err)
  58. }
  59. if config.LogFilename != "" {
  60. logFile, err := os.OpenFile(config.LogFilename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
  61. if err != nil {
  62. log.Fatalf("error opening log file: %s", err)
  63. }
  64. defer logFile.Close()
  65. log.SetOutput(logFile)
  66. }
  67. controller := psiphon.NewController(config)
  68. shutdownBroadcast := make(chan struct{})
  69. controllerWaitGroup := new(sync.WaitGroup)
  70. controllerWaitGroup.Add(1)
  71. go func() {
  72. defer controllerWaitGroup.Done()
  73. controller.Run(shutdownBroadcast)
  74. }()
  75. systemStopSignal := make(chan os.Signal, 1)
  76. signal.Notify(systemStopSignal, os.Interrupt, os.Kill)
  77. <-systemStopSignal
  78. psiphon.Notice(psiphon.NOTICE_INFO, "shutdown by system")
  79. close(shutdownBroadcast)
  80. controllerWaitGroup.Wait()
  81. }