psiphonClient.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. "sync"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  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. err = psiphon.InitDataStore(config.DataStoreFilename)
  45. if err != nil {
  46. return fmt.Errorf("error initializing datastore: %s", err)
  47. }
  48. if config.LogFilename != "" {
  49. logFile, err := os.OpenFile(config.LogFilename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
  50. if err != nil {
  51. log.Fatalf("error opening log file: %s", err)
  52. }
  53. defer logFile.Close()
  54. log.SetOutput(logFile)
  55. }
  56. controller := psiphon.NewController(config)
  57. shutdownBroadcast := make(chan struct{})
  58. controllerWaitGroup := new(sync.WaitGroup)
  59. controllerWaitGroup.Add(1)
  60. go func() {
  61. defer controllerWaitGroup.Done()
  62. controller.Run(shutdownBroadcast)
  63. }()
  64. systemStopSignal := make(chan os.Signal, 1)
  65. signal.Notify(systemStopSignal, os.Interrupt, os.Kill)
  66. <-systemStopSignal
  67. psiphon.Notice(psiphon.NOTICE_INFO, "shutdown by system")
  68. close(shutdownBroadcast)
  69. controllerWaitGroup.Wait()
  70. }