psiphonClient.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. psiphon "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  23. "log"
  24. "os"
  25. "os/signal"
  26. "sync"
  27. )
  28. func main() {
  29. var configFilename string
  30. flag.StringVar(&configFilename, "config", "", "configuration file")
  31. flag.Parse()
  32. if configFilename == "" {
  33. log.Fatalf("configuration file is required")
  34. }
  35. config, err := psiphon.LoadConfig(configFilename)
  36. if err != nil {
  37. log.Fatalf("error loading configuration file: %s", err)
  38. }
  39. if config.LogFilename != "" {
  40. logFile, err := os.OpenFile(config.LogFilename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
  41. if err != nil {
  42. log.Fatalf("error opening log file: %s", err)
  43. }
  44. defer logFile.Close()
  45. log.SetOutput(logFile)
  46. }
  47. controller := psiphon.NewController(config)
  48. shutdownBroadcast := make(chan struct{})
  49. controllerWaitGroup := new(sync.WaitGroup)
  50. controllerWaitGroup.Add(1)
  51. go func() {
  52. defer controllerWaitGroup.Done()
  53. controller.Run(shutdownBroadcast)
  54. }()
  55. systemStopSignal := make(chan os.Signal, 1)
  56. signal.Notify(systemStopSignal, os.Interrupt, os.Kill)
  57. <-systemStopSignal
  58. psiphon.Notice(psiphon.NOTICE_INFO, "shutdown by system")
  59. close(shutdownBroadcast)
  60. controllerWaitGroup.Wait()
  61. }