main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2016, 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. "fmt"
  23. "io/ioutil"
  24. "os"
  25. "strings"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/server"
  28. )
  29. func main() {
  30. var generateServerIPaddress, generateServerNetworkInterface string
  31. var generateConfigFilename, generateServerEntryFilename string
  32. var runConfigFilenames stringListFlag
  33. flag.StringVar(
  34. &generateConfigFilename,
  35. "newConfig",
  36. server.SERVER_CONFIG_FILENAME,
  37. "generate new config with this `filename`")
  38. flag.StringVar(
  39. &generateServerEntryFilename,
  40. "newServerEntry",
  41. server.SERVER_ENTRY_FILENAME,
  42. "generate new server entry with this `filename`")
  43. flag.StringVar(
  44. &generateServerNetworkInterface,
  45. "interface",
  46. "",
  47. "generate with server IP address from this `network-interface`")
  48. flag.StringVar(
  49. &generateServerIPaddress,
  50. "ipaddress",
  51. server.DEFAULT_SERVER_IP_ADDRESS,
  52. "generate with this server `IP address`")
  53. flag.Var(
  54. &runConfigFilenames,
  55. "config",
  56. "run with this config `filename`; flag may be repeated to load multiple config files")
  57. flag.Usage = func() {
  58. fmt.Fprintf(os.Stderr,
  59. "Usage:\n\n"+
  60. "%s <flags> generate generates a configuration and server entry\n"+
  61. "%s <flags> run runs configured services\n\n",
  62. os.Args[0], os.Args[0])
  63. flag.PrintDefaults()
  64. }
  65. flag.Parse()
  66. args := flag.Args()
  67. if len(args) < 1 {
  68. flag.Usage()
  69. os.Exit(1)
  70. } else if args[0] == "generate" {
  71. serverIPaddress := generateServerIPaddress
  72. if generateServerNetworkInterface != "" {
  73. var err error
  74. serverIPaddress, err = psiphon.GetInterfaceIPAddress(generateServerNetworkInterface)
  75. fmt.Errorf("generate failed: %s", err)
  76. os.Exit(1)
  77. }
  78. configFileContents, serverEntryFileContents, err :=
  79. server.GenerateConfig(serverIPaddress)
  80. if err != nil {
  81. fmt.Errorf("generate failed: %s", err)
  82. os.Exit(1)
  83. }
  84. err = ioutil.WriteFile(generateConfigFilename, configFileContents, 0600)
  85. if err != nil {
  86. fmt.Errorf("error writing configuration file: %s", err)
  87. os.Exit(1)
  88. }
  89. err = ioutil.WriteFile(generateServerEntryFilename, serverEntryFileContents, 0600)
  90. if err != nil {
  91. fmt.Errorf("error writing server entry file: %s", err)
  92. os.Exit(1)
  93. }
  94. } else if args[0] == "run" {
  95. if len(runConfigFilenames) == 0 {
  96. runConfigFilenames = []string{server.SERVER_CONFIG_FILENAME}
  97. }
  98. var configFileContents [][]byte
  99. for _, configFilename := range runConfigFilenames {
  100. contents, err := ioutil.ReadFile(configFilename)
  101. if err != nil {
  102. fmt.Errorf("error loading configuration file: %s", err)
  103. os.Exit(1)
  104. }
  105. configFileContents = append(configFileContents, contents)
  106. }
  107. err := server.RunServices(configFileContents)
  108. if err != nil {
  109. fmt.Errorf("run failed: %s", err)
  110. os.Exit(1)
  111. }
  112. }
  113. }
  114. type stringListFlag []string
  115. func (list *stringListFlag) String() string {
  116. return strings.Join(*list, ", ")
  117. }
  118. func (list *stringListFlag) Set(flagValue string) error {
  119. *list = append(*list, flagValue)
  120. return nil
  121. }