lt hai 2 semanas
pai
achega
1b774b432e
Modificáronse 1 ficheiros con 174 adicións e 0 borrados
  1. 174 0
      RO_Server_Trunk-branch_0.1.39/transfor2/main.go

+ 174 - 0
RO_Server_Trunk-branch_0.1.39/transfor2/main.go

@@ -0,0 +1,174 @@
+package main
+
+import (
+	"log"
+	"net/http"
+	"net/http/httputil"
+	"net/url"
+	"strconv"
+	"time"
+)
+
+// serverId -> port 对应表
+var serverPortMap = map[int]int{
+	1:  8003,
+	2:  8005,
+	3:  8007,
+	4:  8009,
+	5:  8011,
+	6:  8013,
+	7:  8015,
+	8:  8017,
+	9:  8019,
+	10: 8021,
+	11: 8023,
+	12: 8025,
+	13: 8027,
+	14: 8029,
+	15: 8031,
+	16: 8033,
+	17: 8035,
+	18: 8037,
+	19: 8039,
+	20: 8041,
+	21: 8043,
+	22: 8045,
+	23: 8047,
+	24: 8049,
+	25: 8051,
+	26: 8053,
+	27: 8055,
+	28: 8057,
+	29: 8059,
+	30: 8061,
+	31: 8063,
+	32: 8065,
+	33: 8067,
+	34: 8069,
+	35: 8071,
+	36: 8073,
+	37: 8075,
+	38: 8077,
+	39: 8079,
+	40: 8081,
+	41: 8083,
+	42: 8085,
+}
+
+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)
+	if err != nil {
+		http.Error(w, "Invalid serverId", http.StatusBadRequest)
+		return
+	}
+
+	port, ok := serverPortMap[serverID]
+	if !ok {
+		http.Error(w, "Unknown serverId", http.StatusBadRequest)
+		return
+	}
+
+	targetURL, _ := url.Parse("http://127.0.0.1:" + strconv.Itoa(port))
+	proxy := httputil.NewSingleHostReverseProxy(targetURL)
+
+	// 修改请求信息
+	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
+	}
+
+	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")
+
+	if serverIDStr == "" {
+		http.Error(w, "Missing serverId parameter", http.StatusBadRequest)
+		return
+	}
+
+	serverID, err := strconv.Atoi(serverIDStr)
+	if err != nil {
+		http.Error(w, "Invalid serverId", http.StatusBadRequest)
+		return
+	}
+
+	port, ok := serverPortMap[serverID]
+	if !ok {
+		http.Error(w, "Unknown serverId", http.StatusBadRequest)
+		return
+	}
+
+	targetURL, _ := url.Parse("http://127.0.0.1:" + strconv.Itoa(port))
+	proxy := httputil.NewSingleHostReverseProxy(targetURL)
+
+	// 修改请求信息
+	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
+	}
+
+	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 main() {
+	log.Println("Reverse proxy started on :8156 ...")
+	http.HandleFunc("/v1/pay/hwDn", proxyHandler)
+	http.HandleFunc("/v1/pay/hwDn2", proxyHandler)
+	http.HandleFunc("/v1/create-order", proxyHandler)
+	log.Fatal(http.ListenAndServe(":8156", nil))
+}