main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "net/http/httputil"
  6. "net/url"
  7. "strconv"
  8. "time"
  9. )
  10. // serverId -> port 对应表
  11. var serverPortMap = map[int]int{
  12. 1: 8086,
  13. 2: 8003,
  14. 3: 8005,
  15. 4: 8007,
  16. 5: 8009,
  17. 6: 8011,
  18. 7: 8013,
  19. 8: 8015,
  20. 9: 8017,
  21. 10: 8019,
  22. 11: 8021,
  23. 12: 8023,
  24. 13: 8025,
  25. 14: 8027,
  26. 15: 8029,
  27. 16: 8031,
  28. 17: 8033,
  29. 18: 8035,
  30. 19: 8037,
  31. 20: 8039,
  32. 21: 8041,
  33. 22: 8043,
  34. 23: 8045,
  35. 24: 8047,
  36. 25: 8049,
  37. 26: 8051,
  38. }
  39. func proxyHandler(w http.ResponseWriter, r *http.Request) {
  40. start := time.Now()
  41. // 先解析 form,无论是 GET 还是 POST 都能取到
  42. r.ParseForm()
  43. serverIDStr := r.FormValue("serverId")
  44. if serverIDStr == "" {
  45. http.Error(w, "Missing serverId parameter", http.StatusBadRequest)
  46. return
  47. }
  48. serverID, err := strconv.Atoi(serverIDStr)
  49. if err != nil {
  50. http.Error(w, "Invalid serverId", http.StatusBadRequest)
  51. return
  52. }
  53. port, ok := serverPortMap[serverID]
  54. if !ok {
  55. http.Error(w, "Unknown serverId", http.StatusBadRequest)
  56. return
  57. }
  58. targetURL, _ := url.Parse("http://127.0.0.1:" + strconv.Itoa(port))
  59. proxy := httputil.NewSingleHostReverseProxy(targetURL)
  60. // 修改请求信息
  61. originalDirector := proxy.Director
  62. proxy.Director = func(req *http.Request) {
  63. originalDirector(req)
  64. req.Host = targetURL.Host
  65. }
  66. proxy.ModifyResponse = func(resp *http.Response) error {
  67. duration := time.Since(start)
  68. log.Printf("[OK] %s | method=%s | serverId=%d -> %d | %s | status=%d | %.2fms\n",
  69. start.Format("2006-01-02 15:04:05"),
  70. r.Method,
  71. serverID,
  72. port,
  73. r.URL.RequestURI(),
  74. resp.StatusCode,
  75. float64(duration.Milliseconds()))
  76. return nil
  77. }
  78. log.Printf("[REQ] %s | method=%s | serverId=%d -> %d | %s\n",
  79. start.Format("2006-01-02 15:04:05"),
  80. r.Method,
  81. serverID, port, r.URL.RequestURI())
  82. proxy.ServeHTTP(w, r)
  83. }
  84. func proxyCreateOrderHandler(w http.ResponseWriter, r *http.Request) {
  85. start := time.Now()
  86. // 先解析 form,无论是 GET 还是 POST 都能取到
  87. r.ParseForm()
  88. serverIDStr := r.FormValue("serverId")
  89. if serverIDStr == "" {
  90. http.Error(w, "Missing serverId parameter", http.StatusBadRequest)
  91. return
  92. }
  93. serverID, err := strconv.Atoi(serverIDStr)
  94. if err != nil {
  95. http.Error(w, "Invalid serverId", http.StatusBadRequest)
  96. return
  97. }
  98. port, ok := serverPortMap[serverID]
  99. if !ok {
  100. http.Error(w, "Unknown serverId", http.StatusBadRequest)
  101. return
  102. }
  103. targetURL, _ := url.Parse("http://127.0.0.1:" + strconv.Itoa(port))
  104. proxy := httputil.NewSingleHostReverseProxy(targetURL)
  105. // 修改请求信息
  106. originalDirector := proxy.Director
  107. proxy.Director = func(req *http.Request) {
  108. originalDirector(req)
  109. req.Host = targetURL.Host
  110. }
  111. proxy.ModifyResponse = func(resp *http.Response) error {
  112. duration := time.Since(start)
  113. log.Printf("[OK] %s | method=%s | serverId=%d -> %d | %s | status=%d | %.2fms\n",
  114. start.Format("2006-01-02 15:04:05"),
  115. r.Method,
  116. serverID,
  117. port,
  118. r.URL.RequestURI(),
  119. resp.StatusCode,
  120. float64(duration.Milliseconds()))
  121. return nil
  122. }
  123. log.Printf("[REQ] %s | method=%s | serverId=%d -> %d | %s\n",
  124. start.Format("2006-01-02 15:04:05"),
  125. r.Method,
  126. serverID, port, r.URL.RequestURI())
  127. proxy.ServeHTTP(w, r)
  128. }
  129. func main() {
  130. log.Println("Reverse proxy started on :8155 ...")
  131. http.HandleFunc("/v1/pay/hwDn", proxyHandler)
  132. http.HandleFunc("/v1/create-order", proxyHandler)
  133. log.Fatal(http.ListenAndServe(":8155", nil))
  134. }