Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

111 lignes
3.0 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. package main
  20. import (
  21. "flag"
  22. "fmt"
  23. "io/ioutil"
  24. "log"
  25. "net"
  26. "net/http"
  27. "os"
  28. "os/signal"
  29. "cloud.google.com/go/httpreplay/internal/proxy"
  30. "github.com/google/martian/martianhttp"
  31. )
  32. var (
  33. port = flag.Int("port", 8080, "port of the proxy")
  34. controlPort = flag.Int("control-port", 8181, "port for controlling the proxy")
  35. record = flag.String("record", "", "record traffic and save to filename")
  36. replay = flag.String("replay", "", "read filename and replay traffic")
  37. debugHeaders = flag.Bool("debug-headers", false, "log header mismatches")
  38. )
  39. func main() {
  40. flag.Parse()
  41. if *record == "" && *replay == "" {
  42. log.Fatal("provide either -record or -replay")
  43. }
  44. if *record != "" && *replay != "" {
  45. log.Fatal("provide only one of -record and -replay")
  46. }
  47. log.Printf("httpr: starting proxy on port %d and control on port %d", *port, *controlPort)
  48. var pr *proxy.Proxy
  49. var err error
  50. if *record != "" {
  51. pr, err = proxy.ForRecording(*record, *port)
  52. } else {
  53. pr, err = proxy.ForReplaying(*replay, *port)
  54. }
  55. if err != nil {
  56. log.Fatal(err)
  57. }
  58. proxy.DebugHeaders = *debugHeaders
  59. // Expose handlers on the control port.
  60. mux := http.NewServeMux()
  61. mux.Handle("/authority.cer", martianhttp.NewAuthorityHandler(pr.CACert))
  62. mux.HandleFunc("/initial", handleInitial(pr))
  63. lControl, err := net.Listen("tcp", fmt.Sprintf(":%d", *controlPort))
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67. go http.Serve(lControl, mux)
  68. sigc := make(chan os.Signal, 1)
  69. signal.Notify(sigc, os.Interrupt)
  70. <-sigc
  71. log.Println("httpr: shutting down")
  72. if err := pr.Close(); err != nil {
  73. log.Fatal(err)
  74. }
  75. }
  76. func handleInitial(pr *proxy.Proxy) http.HandlerFunc {
  77. return func(w http.ResponseWriter, req *http.Request) {
  78. switch req.Method {
  79. case "GET":
  80. if pr.Initial != nil {
  81. w.Write(pr.Initial)
  82. }
  83. case "POST":
  84. bytes, err := ioutil.ReadAll(req.Body)
  85. req.Body.Close()
  86. if err != nil {
  87. w.WriteHeader(http.StatusInternalServerError)
  88. fmt.Fprintf(w, "reading body: %v", err)
  89. }
  90. pr.Initial = bytes
  91. default:
  92. w.WriteHeader(http.StatusBadRequest)
  93. fmt.Fprint(w, "use GET to retrieve initial or POST to set it")
  94. }
  95. }
  96. }