psiphonClient.go 2.0 KB

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