| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- package main
- import (
- "log"
- "net/http"
- "net/http/httputil"
- "net/url"
- "strconv"
- "time"
- )
- // serverId -> port 对应表
- var serverPortMap = map[int]int{
- 1: 8086,
- 2: 8003,
- 3: 8005,
- 4: 8007,
- 5: 8009,
- 6: 8011,
- 7: 8013,
- 8: 8015,
- 9: 8017,
- 10: 8019,
- 11: 8021,
- 12: 8023,
- 13: 8025,
- 14: 8027,
- 15: 8029,
- 16: 8031,
- 17: 8033,
- 18: 8035,
- 19: 8037,
- 20: 8039,
- 21: 8041,
- 22: 8043,
- 23: 8045,
- 24: 8047,
- 25: 8049,
- 26: 8051,
- }
- func proxyHandler(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 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 :8155 ...")
- http.HandleFunc("/v1/pay/hwDn", proxyHandler)
- http.HandleFunc("/v1/create-order", proxyHandler)
- log.Fatal(http.ListenAndServe(":8155", nil))
- }
|