|
|
@@ -1,6 +1,8 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
+ "io"
|
|
|
"log"
|
|
|
"net/http"
|
|
|
"net/http/httputil"
|
|
|
@@ -59,90 +61,62 @@ var serverPortMap = map[int]int{
|
|
|
func proxyHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
start := time.Now()
|
|
|
|
|
|
- // 先解析 form,无论是 GET 还是 POST 都能取到
|
|
|
- r.ParseForm()
|
|
|
- serverIDStr := r.FormValue("server")
|
|
|
-
|
|
|
- if serverIDStr == "" {
|
|
|
- http.Error(w, "Missing serverId parameter", http.StatusBadRequest)
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- serverID, err := strconv.Atoi(serverIDStr)
|
|
|
+ // 读取原始 body
|
|
|
+ bodyBytes, err := io.ReadAll(r.Body)
|
|
|
if err != nil {
|
|
|
- http.Error(w, "Invalid serverId", http.StatusBadRequest)
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- port, ok := serverPortMap[serverID]
|
|
|
- if !ok {
|
|
|
- http.Error(w, "Unknown serverId", http.StatusBadRequest)
|
|
|
+ log.Printf("[ERROR] Failed to read body: %v", err)
|
|
|
+ http.Error(w, "Failed to read request body", http.StatusBadRequest)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- targetURL, _ := url.Parse("http://127.0.0.1:" + strconv.Itoa(port))
|
|
|
- proxy := httputil.NewSingleHostReverseProxy(targetURL)
|
|
|
+ // 恢复 body 以便 ParseForm 使用
|
|
|
+ r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
|
|
|
|
|
|
- // 修改请求信息
|
|
|
- originalDirector := proxy.Director
|
|
|
- proxy.Director = func(req *http.Request) {
|
|
|
- originalDirector(req)
|
|
|
- req.Host = targetURL.Host
|
|
|
- }
|
|
|
-
|
|
|
- proxy.ModifyResponse = func(resp *http.Response) error {
|
|
|
- duration := time.Since(start)
|
|
|
- log.Printf("[OK] %s | method=%s | serverId=%d -> %d | %s | status=%d | %.2fms\n",
|
|
|
- start.Format("2006-01-02 15:04:05"),
|
|
|
- r.Method,
|
|
|
- serverID,
|
|
|
- port,
|
|
|
- r.URL.RequestURI(),
|
|
|
- resp.StatusCode,
|
|
|
- float64(duration.Milliseconds()))
|
|
|
- return nil
|
|
|
+ // 解析表单数据(application/x-www-form-urlencoded)
|
|
|
+ if err := r.ParseForm(); err != nil {
|
|
|
+ log.Printf("[ERROR] Failed to parse form: %v", err)
|
|
|
+ http.Error(w, "Invalid form data", http.StatusBadRequest)
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
- log.Printf("[REQ] %s | method=%s | serverId=%d -> %d | %s\n",
|
|
|
- start.Format("2006-01-02 15:04:05"),
|
|
|
- r.Method,
|
|
|
- serverID, port, r.URL.RequestURI())
|
|
|
-
|
|
|
- proxy.ServeHTTP(w, r)
|
|
|
-}
|
|
|
-
|
|
|
-func proxyCreateOrderHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
- start := time.Now()
|
|
|
-
|
|
|
- // 先解析 form,无论是 GET 还是 POST 都能取到
|
|
|
- r.ParseForm()
|
|
|
- serverIDStr := r.FormValue("serverId")
|
|
|
-
|
|
|
+ // 获取 game_area 参数
|
|
|
+ serverIDStr := r.FormValue("game_area")
|
|
|
if serverIDStr == "" {
|
|
|
- http.Error(w, "Missing serverId parameter", http.StatusBadRequest)
|
|
|
+ http.Error(w, "Missing game_area parameter", http.StatusBadRequest)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
serverID, err := strconv.Atoi(serverIDStr)
|
|
|
if err != nil {
|
|
|
- http.Error(w, "Invalid serverId", http.StatusBadRequest)
|
|
|
+ http.Error(w, "Invalid game_area", http.StatusBadRequest)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
port, ok := serverPortMap[serverID]
|
|
|
if !ok {
|
|
|
- http.Error(w, "Unknown serverId", http.StatusBadRequest)
|
|
|
+ http.Error(w, "Unknown game_area", http.StatusBadRequest)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
targetURL, _ := url.Parse("http://127.0.0.1:" + strconv.Itoa(port))
|
|
|
proxy := httputil.NewSingleHostReverseProxy(targetURL)
|
|
|
|
|
|
- // 修改请求信息
|
|
|
+ // 自定义 Director,恢复原始 body
|
|
|
originalDirector := proxy.Director
|
|
|
proxy.Director = func(req *http.Request) {
|
|
|
originalDirector(req)
|
|
|
req.Host = targetURL.Host
|
|
|
+
|
|
|
+ // 恢复 body 和 Content-Length
|
|
|
+ if len(bodyBytes) > 0 {
|
|
|
+ req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
|
|
|
+ req.ContentLength = int64(len(bodyBytes))
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保 Content-Type 正确
|
|
|
+ if req.Header.Get("Content-Type") == "" {
|
|
|
+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
proxy.ModifyResponse = func(resp *http.Response) error {
|
|
|
@@ -158,18 +132,25 @@ func proxyCreateOrderHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- log.Printf("[REQ] %s | method=%s | serverId=%d -> %d | %s\n",
|
|
|
+ // 错误处理
|
|
|
+ proxy.ErrorHandler = func(w http.ResponseWriter, req *http.Request, err error) {
|
|
|
+ log.Printf("[ERROR] Proxy error: %v", err)
|
|
|
+ http.Error(w, "Proxy error: "+err.Error(), http.StatusBadGateway)
|
|
|
+ }
|
|
|
+
|
|
|
+ log.Printf("[REQ] %s | method=%s | serverId=%d -> %d | %s | body_size=%d\n",
|
|
|
start.Format("2006-01-02 15:04:05"),
|
|
|
r.Method,
|
|
|
- serverID, port, r.URL.RequestURI())
|
|
|
+ serverID,
|
|
|
+ port,
|
|
|
+ r.URL.RequestURI(),
|
|
|
+ len(bodyBytes))
|
|
|
|
|
|
proxy.ServeHTTP(w, r)
|
|
|
}
|
|
|
|
|
|
func main() {
|
|
|
log.Println("Reverse proxy started on :8155 ...")
|
|
|
- http.HandleFunc("/v1/pay/hwDn", proxyHandler)
|
|
|
- http.HandleFunc("/v1/pay/hwDn2", proxyHandler)
|
|
|
- http.HandleFunc("/v1/create-order", proxyHandler)
|
|
|
+ http.HandleFunc("/v1/pay/xiaoqi", proxyHandler)
|
|
|
log.Fatal(http.ListenAndServe(":8155", nil))
|
|
|
}
|