您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

113 行
3.1 KiB

  1. // Copyright 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // httpr is a proxy that can record or replay HTTP requests.
  15. // Start httpr with either the -record or -replay flags, providing a filename.
  16. // Terminate the process with an interrupt (kill -2) to write the log file when recording.
  17. // To get the CA certificate of the proxy, issue a GET to http://localhost:CP/authority.cer, where
  18. // CP is the control port.
  19. // +build go1.8
  20. package main
  21. import (
  22. "flag"
  23. "fmt"
  24. "io/ioutil"
  25. "log"
  26. "net"
  27. "net/http"
  28. "os"
  29. "os/signal"
  30. "cloud.google.com/go/httpreplay/internal/proxy"
  31. "github.com/google/martian/martianhttp"
  32. )
  33. var (
  34. port = flag.Int("port", 8080, "port of the proxy")
  35. controlPort = flag.Int("control-port", 8181, "port for controlling the proxy")
  36. record = flag.String("record", "", "record traffic and save to filename")
  37. replay = flag.String("replay", "", "read filename and replay traffic")
  38. debugHeaders = flag.Bool("debug-headers", false, "log header mismatches")
  39. )
  40. func main() {
  41. flag.Parse()
  42. if *record == "" && *replay == "" {
  43. log.Fatal("provide either -record or -replay")
  44. }
  45. if *record != "" && *replay != "" {
  46. log.Fatal("provide only one of -record and -replay")
  47. }
  48. log.Printf("httpr: starting proxy on port %d and control on port %d", *port, *controlPort)
  49. var pr *proxy.Proxy
  50. var err error
  51. if *record != "" {
  52. pr, err = proxy.ForRecording(*record, *port)
  53. } else {
  54. pr, err = proxy.ForReplaying(*replay, *port)
  55. }
  56. if err != nil {
  57. log.Fatal(err)
  58. }
  59. proxy.DebugHeaders = *debugHeaders
  60. // Expose handlers on the control port.
  61. mux := http.NewServeMux()
  62. mux.Handle("/authority.cer", martianhttp.NewAuthorityHandler(pr.CACert))
  63. mux.HandleFunc("/initial", handleInitial(pr))
  64. lControl, err := net.Listen("tcp", fmt.Sprintf(":%d", *controlPort))
  65. if err != nil {
  66. log.Fatal(err)
  67. }
  68. go http.Serve(lControl, mux)
  69. sigc := make(chan os.Signal, 1)
  70. signal.Notify(sigc, os.Interrupt)
  71. <-sigc
  72. log.Println("httpr: shutting down")
  73. if err := pr.Close(); err != nil {
  74. log.Fatal(err)
  75. }
  76. }
  77. func handleInitial(pr *proxy.Proxy) http.HandlerFunc {
  78. return func(w http.ResponseWriter, req *http.Request) {
  79. switch req.Method {
  80. case "GET":
  81. if pr.Initial != nil {
  82. w.Write(pr.Initial)
  83. }
  84. case "POST":
  85. bytes, err := ioutil.ReadAll(req.Body)
  86. req.Body.Close()
  87. if err != nil {
  88. w.WriteHeader(http.StatusInternalServerError)
  89. fmt.Fprintf(w, "reading body: %v", err)
  90. }
  91. pr.Initial = bytes
  92. default:
  93. w.WriteHeader(http.StatusBadRequest)
  94. fmt.Fprint(w, "use GET to retrieve initial or POST to set it")
  95. }
  96. }
  97. }