main.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. "context"
  22. "encoding/json"
  23. "flag"
  24. "fmt"
  25. "io"
  26. "io/ioutil"
  27. "os"
  28. "runtime/debug"
  29. "strconv"
  30. "strings"
  31. "syscall"
  32. "time"
  33. "github.com/Psiphon-Inc/rotate-safe-writer"
  34. udsipc "github.com/Psiphon-Inc/uds-ipc"
  35. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  36. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo"
  37. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  38. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/server"
  39. pb "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/server/pb/psiphond"
  40. "github.com/mitchellh/panicwrap"
  41. "google.golang.org/protobuf/proto"
  42. "google.golang.org/protobuf/types/known/timestamppb"
  43. )
  44. var loadedConfigJSON []byte
  45. func main() {
  46. var configFilename string
  47. var generateServerIPaddress string
  48. var generateServerNetworkInterface string
  49. var generateProtocolPorts stringListFlag
  50. var generateWebServerPort int
  51. var generateLogFilename string
  52. var generateTrafficRulesConfigFilename string
  53. var generateOSLConfigFilename string
  54. var generateTacticsConfigFilename string
  55. var generateServerEntryFilename string
  56. flag.StringVar(
  57. &configFilename,
  58. "config",
  59. server.SERVER_CONFIG_FILENAME,
  60. "run or generate with this config `filename`")
  61. flag.StringVar(
  62. &generateServerIPaddress,
  63. "ipaddress",
  64. server.DEFAULT_SERVER_IP_ADDRESS,
  65. "generate with this server `IP address`")
  66. flag.StringVar(
  67. &generateServerNetworkInterface,
  68. "interface",
  69. "",
  70. "generate with server IP address from this `network-interface`")
  71. flag.Var(
  72. &generateProtocolPorts,
  73. "protocol",
  74. "generate with `protocol:port`; flag may be repeated to enable multiple protocols")
  75. flag.IntVar(
  76. &generateWebServerPort,
  77. "web",
  78. 0,
  79. "generate with web server `port`; 0 for no web server")
  80. flag.StringVar(
  81. &generateLogFilename,
  82. "logFilename",
  83. "",
  84. "set application log file name and path; blank for stderr")
  85. flag.StringVar(
  86. &generateTrafficRulesConfigFilename,
  87. "trafficRules",
  88. server.SERVER_TRAFFIC_RULES_CONFIG_FILENAME,
  89. "generate with this traffic rules config `filename`")
  90. flag.StringVar(
  91. &generateOSLConfigFilename,
  92. "osl",
  93. server.SERVER_OSL_CONFIG_FILENAME,
  94. "generate with this OSL config `filename`")
  95. flag.StringVar(
  96. &generateTacticsConfigFilename,
  97. "tactics",
  98. server.SERVER_TACTICS_CONFIG_FILENAME,
  99. "generate with this tactics config `filename`")
  100. flag.StringVar(
  101. &generateServerEntryFilename,
  102. "serverEntry",
  103. server.SERVER_ENTRY_FILENAME,
  104. "generate with this server entry `filename`")
  105. flag.Usage = func() {
  106. fmt.Fprintf(os.Stderr,
  107. "Usage:\n\n"+
  108. "%s <flags> generate generates configuration files\n"+
  109. "%s <flags> run runs configured services\n\n",
  110. os.Args[0], os.Args[0])
  111. flag.PrintDefaults()
  112. }
  113. flag.Parse()
  114. args := flag.Args()
  115. if len(args) < 1 {
  116. flag.Usage()
  117. os.Exit(1)
  118. } else if args[0] == "generate" {
  119. serverIPaddress := generateServerIPaddress
  120. if generateServerNetworkInterface != "" {
  121. // TODO: IPv6 support
  122. serverIPv4Address, _, err := common.GetInterfaceIPAddresses(generateServerNetworkInterface)
  123. if err == nil && serverIPv4Address == nil {
  124. err = fmt.Errorf("no IPv4 address for interface %s", generateServerNetworkInterface)
  125. }
  126. if err != nil {
  127. fmt.Printf("generate failed: %s\n", err)
  128. os.Exit(1)
  129. }
  130. serverIPaddress = serverIPv4Address.String()
  131. }
  132. tunnelProtocolPorts := make(map[string]int)
  133. for _, protocolPort := range generateProtocolPorts {
  134. parts := strings.Split(protocolPort, ":")
  135. if len(parts) == 2 {
  136. port, err := strconv.Atoi(parts[1])
  137. if err != nil {
  138. fmt.Printf("generate failed: %s\n", err)
  139. os.Exit(1)
  140. }
  141. tunnelProtocolPorts[parts[0]] = port
  142. }
  143. }
  144. configJSON, trafficRulesConfigJSON, OSLConfigJSON,
  145. tacticsConfigJSON, encodedServerEntry, err :=
  146. server.GenerateConfig(
  147. &server.GenerateConfigParams{
  148. LogFilename: generateLogFilename,
  149. ServerIPAddress: serverIPaddress,
  150. TunnelProtocolPorts: tunnelProtocolPorts,
  151. TrafficRulesConfigFilename: generateTrafficRulesConfigFilename,
  152. OSLConfigFilename: generateOSLConfigFilename,
  153. TacticsConfigFilename: generateTacticsConfigFilename,
  154. })
  155. if err != nil {
  156. fmt.Printf("generate failed: %s\n", err)
  157. os.Exit(1)
  158. }
  159. err = ioutil.WriteFile(configFilename, configJSON, 0600)
  160. if err != nil {
  161. fmt.Printf("error writing configuration file: %s\n", err)
  162. os.Exit(1)
  163. }
  164. err = ioutil.WriteFile(generateTrafficRulesConfigFilename, trafficRulesConfigJSON, 0600)
  165. if err != nil {
  166. fmt.Printf("error writing traffic rule config file: %s\n", err)
  167. os.Exit(1)
  168. }
  169. err = ioutil.WriteFile(generateOSLConfigFilename, OSLConfigJSON, 0600)
  170. if err != nil {
  171. fmt.Printf("error writing OSL config file: %s\n", err)
  172. os.Exit(1)
  173. }
  174. err = ioutil.WriteFile(generateTacticsConfigFilename, tacticsConfigJSON, 0600)
  175. if err != nil {
  176. fmt.Printf("error writing tactics config file: %s\n", err)
  177. os.Exit(1)
  178. }
  179. err = ioutil.WriteFile(generateServerEntryFilename, encodedServerEntry, 0600)
  180. if err != nil {
  181. fmt.Printf("error writing server entry file: %s\n", err)
  182. os.Exit(1)
  183. }
  184. } else if args[0] == "run" {
  185. configJSON, err := ioutil.ReadFile(configFilename)
  186. if err != nil {
  187. fmt.Printf("error loading configuration file: %s\n", err)
  188. os.Exit(1)
  189. }
  190. loadedConfigJSON = configJSON
  191. // The initial call to panicwrap.Wrap will spawn a child process
  192. // running the same program.
  193. //
  194. // The parent process waits for the child to terminate and
  195. // panicHandler logs any panics from the child.
  196. //
  197. // The child will return immediately from Wrap without spawning
  198. // and fall through to server.RunServices.
  199. // Unhandled panic wrapper. Logs it, then re-executes the current executable
  200. exitStatus, err := panicwrap.Wrap(&panicwrap.WrapConfig{
  201. Handler: panicHandler,
  202. ForwardSignals: []os.Signal{os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGTSTP, syscall.SIGCONT},
  203. })
  204. if err != nil {
  205. fmt.Printf("failed to set up the panic wrapper: %s\n", err)
  206. os.Exit(1)
  207. }
  208. // Note: panicwrap.Wrap documentation states that exitStatus == -1
  209. // should be used to determine whether the process is the child.
  210. // However, we have found that this exitStatus is returned even when
  211. // the process is the parent. Likely due to panicwrap returning
  212. // syscall.WaitStatus.ExitStatus() as the exitStatus, which _can_ be
  213. // -1. Checking panicwrap.Wrapped(nil) is more reliable.
  214. if !panicwrap.Wrapped(nil) {
  215. os.Exit(exitStatus)
  216. }
  217. // Else, this is the child process.
  218. // As of Go 1.19.10, programs with Linux capabilities or setuid do not
  219. // dump panic stacks by default. See:
  220. // https://github.com/golang/go/commit/a7b1cd452ddc69a6606c2f35ac5786dc892e62cb.
  221. // To restore panic stacks, we call SetTraceback("single"), restoring
  222. // the default GOTRACKBACK value. The server program is run as a
  223. // non-privileged user and with CAP_NET capabilities; neither the
  224. // panic stack traces nor register dumps are expected to expose any
  225. // unexpected sensitive information.
  226. debug.SetTraceback("single")
  227. err = server.RunServices(configJSON)
  228. if err != nil {
  229. fmt.Printf("run failed: %s\n", err)
  230. os.Exit(1)
  231. }
  232. }
  233. }
  234. type stringListFlag []string
  235. func (list *stringListFlag) String() string {
  236. return strings.Join(*list, ", ")
  237. }
  238. func (list *stringListFlag) Set(flagValue string) error {
  239. *list = append(*list, flagValue)
  240. return nil
  241. }
  242. func panicHandler(output string) {
  243. // As this is the parent panicwrap process, no config or logging is
  244. // initialized. Each of the JSON and protobuf panic logging helpers
  245. // directly initialize the bare minimum required to emit the single panic
  246. // log.
  247. if len(loadedConfigJSON) == 0 {
  248. fmt.Printf("no configuration JSON was loaded, cannot continue\n%s\n", output)
  249. os.Exit(1)
  250. }
  251. config, err := server.LoadConfig([]byte(loadedConfigJSON))
  252. if err != nil {
  253. fmt.Printf("error parsing configuration file: %s\n%s\n", err, output)
  254. os.Exit(1)
  255. }
  256. if config.LogFormat == "protobuf" || config.LogFormat == "both" {
  257. err := panicHandlerProtobuf(config, output)
  258. if err != nil {
  259. fmt.Printf("error logging panic: %s\n%s\n", err, output)
  260. // continue
  261. }
  262. }
  263. // To maximize the chance of recording a panic log, always emit a classic
  264. // log even in "protobuf" mode.
  265. err = panicHandlerJSON(config, output)
  266. if err != nil {
  267. fmt.Printf("error logging panic: %s\n%s\n", err, output)
  268. // continue
  269. }
  270. os.Exit(1)
  271. }
  272. func panicHandlerJSON(config *server.Config, output string) error {
  273. logEvent := make(map[string]string)
  274. logEvent["timestamp"] = time.Now().Format(time.RFC3339)
  275. logEvent["host_id"] = config.HostID
  276. logEvent["build_rev"] = buildinfo.GetBuildInfo().BuildRev
  277. if config.HostProvider != "" {
  278. logEvent["provider"] = config.HostProvider
  279. }
  280. logEvent["event_name"] = "server_panic"
  281. // ELK has a maximum field length of 32766 UTF8 bytes. Over that length, the
  282. // log won't be delivered. Truncate the panic field, as it may be much longer.
  283. maxLen := 32766
  284. if len(output) > maxLen {
  285. output = output[:maxLen]
  286. }
  287. logEvent["panic"] = output
  288. // Logs are written to the configured file name. If no name is specified,
  289. // logs are written to stderr.
  290. var jsonWriter io.Writer
  291. if config.LogFilename != "" {
  292. retries, create, mode := config.GetLogFileReopenConfig()
  293. panicLog, err := rotate.NewRotatableFileWriter(
  294. config.LogFilename, retries, create, mode)
  295. if err != nil {
  296. return errors.Trace(err)
  297. }
  298. defer panicLog.Close()
  299. jsonWriter = panicLog
  300. } else {
  301. jsonWriter = os.Stderr
  302. }
  303. err := json.NewEncoder(jsonWriter).Encode(logEvent)
  304. if err != nil {
  305. return errors.Trace(err)
  306. }
  307. return nil
  308. }
  309. func panicHandlerProtobuf(config *server.Config, output string) error {
  310. metricSocketWriter, err := udsipc.NewWriter(config.MetricSocketPath)
  311. if err != nil {
  312. return errors.Trace(err)
  313. }
  314. metricSocketWriter.Start()
  315. // No stop deadline; prioritize recording the panic
  316. defer metricSocketWriter.Stop(context.Background())
  317. // Avoid shipping a huge set of panic stacks. The main stack, which is
  318. // first, and a selection of other stacks is sufficient.
  319. maxLen := 32766
  320. if len(output) > maxLen {
  321. output = output[:maxLen]
  322. }
  323. psiphondMsg := &pb.Psiphond{
  324. Timestamp: timestamppb.Now(),
  325. HostId: config.HostID,
  326. HostBuildRev: buildinfo.GetBuildInfo().BuildRev,
  327. Provider: config.HostProvider,
  328. Metric: &pb.Psiphond_ServerPanic{
  329. ServerPanic: &pb.ServerPanic{
  330. Panic: &output,
  331. }},
  332. }
  333. routedMsg, err := server.NewProtobufRoutedMessage(
  334. config.LogDestinationPrefix,
  335. psiphondMsg)
  336. if err != nil {
  337. return errors.Trace(err)
  338. }
  339. serialized, err := proto.Marshal(routedMsg)
  340. if err != nil {
  341. return errors.Trace(err)
  342. }
  343. err = metricSocketWriter.WriteMessage(serialized)
  344. if err != nil {
  345. return errors.Trace(err)
  346. }
  347. return nil
  348. }