main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/server"
  27. )
  28. func main() {
  29. var generateServerIPaddress string
  30. var generateWebServerPort, generateSSHServerPort, generateObfuscatedSSHServerPort int
  31. var runConfigFilenames stringListFlag
  32. flag.StringVar(
  33. &generateServerIPaddress,
  34. "ipaddress",
  35. server.DEFAULT_SERVER_IP_ADDRESS,
  36. "generate with this server `IP address`")
  37. flag.IntVar(
  38. &generateWebServerPort,
  39. "webport",
  40. server.DEFAULT_WEB_SERVER_PORT,
  41. "generate with this web server `port`; 0 for no web server")
  42. flag.IntVar(
  43. &generateSSHServerPort,
  44. "sshport",
  45. server.DEFAULT_SSH_SERVER_PORT,
  46. "generate with this SSH server `port`; 0 for no SSH server")
  47. flag.IntVar(
  48. &generateObfuscatedSSHServerPort,
  49. "osshport",
  50. server.DEFAULT_OBFUSCATED_SSH_SERVER_PORT,
  51. "generate with this Obfuscated SSH server `port`; 0 for no Obfuscated SSH server")
  52. flag.Var(
  53. &runConfigFilenames,
  54. "config",
  55. "run with this config `filename`; flag may be repeated to load multiple config files")
  56. flag.Usage = func() {
  57. fmt.Fprintf(os.Stderr,
  58. "Usage:\n\n"+
  59. "%s <flags> generate generates a configuration and server entry\n"+
  60. "%s <flags> run runs configured services\n\n",
  61. os.Args[0], os.Args[0])
  62. flag.PrintDefaults()
  63. }
  64. flag.Parse()
  65. args := flag.Args()
  66. configFilename := server.SERVER_CONFIG_FILENAME
  67. serverEntryFilename := server.SERVER_ENTRY_FILENAME
  68. if len(args) < 1 {
  69. flag.Usage()
  70. os.Exit(1)
  71. } else if args[0] == "generate" {
  72. configFileContents, serverEntryFileContents, err := server.GenerateConfig(
  73. &server.GenerateConfigParams{
  74. ServerIPAddress: generateServerIPaddress,
  75. WebServerPort: generateWebServerPort,
  76. SSHServerPort: generateSSHServerPort,
  77. ObfuscatedSSHServerPort: generateObfuscatedSSHServerPort,
  78. })
  79. if err != nil {
  80. fmt.Errorf("generate failed: %s", err)
  81. os.Exit(1)
  82. }
  83. err = ioutil.WriteFile(configFilename, configFileContents, 0600)
  84. if err != nil {
  85. fmt.Errorf("error writing configuration file: %s", err)
  86. os.Exit(1)
  87. }
  88. err = ioutil.WriteFile(serverEntryFilename, serverEntryFileContents, 0600)
  89. if err != nil {
  90. fmt.Errorf("error writing server entry file: %s", err)
  91. os.Exit(1)
  92. }
  93. } else if args[0] == "run" {
  94. if len(runConfigFilenames) == 0 {
  95. runConfigFilenames = []string{configFilename}
  96. }
  97. var configFileContents [][]byte
  98. for _, configFilename := range runConfigFilenames {
  99. ccontents, err := ioutil.ReadFile(configFilename)
  100. if err != nil {
  101. fmt.Errorf("error loading configuration file: %s", err)
  102. os.Exit(1)
  103. }
  104. configFileContents = append(configFileContents, ccontents)
  105. }
  106. err := server.RunServices(configFileContents)
  107. if err != nil {
  108. fmt.Errorf("run failed: %s", err)
  109. os.Exit(1)
  110. }
  111. }
  112. }
  113. type stringListFlag []string
  114. func (list *stringListFlag) String() string {
  115. return strings.Join(*list, ", ")
  116. }
  117. func (list *stringListFlag) Set(flagValue string) error {
  118. *list = append(*list, flagValue)
  119. return nil
  120. }