|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "encoding/json" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "log" |
| 10 | + "net/http" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/imroc/req/v3" |
| 14 | +) |
| 15 | + |
| 16 | +// ProxyRequest 定义客户端请求结构 |
| 17 | +type ProxyRequest struct { |
| 18 | + Method string `json:"method"` // GET/POST/PUT/DELETE |
| 19 | + URL string `json:"url"` // 目标 URL |
| 20 | + Headers map[string]string `json:"headers"` // 可选请求头 |
| 21 | + Body interface{} `json:"body"` // 可选 body |
| 22 | + Timeout int `json:"timeout"` // 毫秒,可选 |
| 23 | +} |
| 24 | + |
| 25 | +func main() { |
| 26 | + port := flag.Int("p", 57571, "port to listen on") |
| 27 | + flag.Parse() |
| 28 | + |
| 29 | + // 创建 req/v3 客户端,忽略 HTTPS 证书验证 |
| 30 | + client := req.C(). |
| 31 | + SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) |
| 32 | + |
| 33 | + // /proxy 接口 |
| 34 | + http.HandleFunc("/proxy", func(w http.ResponseWriter, r *http.Request) { |
| 35 | + var reqBody ProxyRequest |
| 36 | + |
| 37 | + if r.Method == http.MethodGet { |
| 38 | + // GET 请求参数从 query |
| 39 | + reqBody.Method = r.URL.Query().Get("method") |
| 40 | + reqBody.URL = r.URL.Query().Get("url") |
| 41 | + if h := r.URL.Query().Get("headers"); h != "" { |
| 42 | + var hdr map[string]string |
| 43 | + if err := json.Unmarshal([]byte(h), &hdr); err == nil { |
| 44 | + reqBody.Headers = hdr |
| 45 | + } |
| 46 | + } |
| 47 | + if t := r.URL.Query().Get("timeout"); t != "" { |
| 48 | + var ms int |
| 49 | + fmt.Sscanf(t, "%d", &ms) |
| 50 | + reqBody.Timeout = ms |
| 51 | + } |
| 52 | + // GET 请求 body 可以用 query 参数传 JSON |
| 53 | + if b := r.URL.Query().Get("body"); b != "" { |
| 54 | + var body interface{} |
| 55 | + if err := json.Unmarshal([]byte(b), &body); err == nil { |
| 56 | + reqBody.Body = body |
| 57 | + } |
| 58 | + } |
| 59 | + } else { |
| 60 | + // POST/PUT/DELETE 从 JSON body |
| 61 | + data, err := io.ReadAll(r.Body) |
| 62 | + if err != nil { |
| 63 | + http.Error(w, "failed to read body: "+err.Error(), http.StatusBadRequest) |
| 64 | + return |
| 65 | + } |
| 66 | + if err := json.Unmarshal(data, &reqBody); err != nil { |
| 67 | + http.Error(w, "invalid JSON: "+err.Error(), http.StatusBadRequest) |
| 68 | + return |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // 默认 method 为 GET |
| 73 | + if reqBody.Method == "" { |
| 74 | + reqBody.Method = http.MethodGet |
| 75 | + } |
| 76 | + |
| 77 | + // 设置请求超时 |
| 78 | + if reqBody.Timeout > 0 { |
| 79 | + client.SetTimeout(time.Duration(reqBody.Timeout) * time.Millisecond) |
| 80 | + } else { |
| 81 | + client.SetTimeout(30 * time.Second) |
| 82 | + } |
| 83 | + |
| 84 | + // 构造请求 |
| 85 | + request := client.R() |
| 86 | + if reqBody.Headers != nil { |
| 87 | + request.SetHeaders(reqBody.Headers) |
| 88 | + } |
| 89 | + if reqBody.Body != nil && (reqBody.Method == http.MethodPost || reqBody.Method == http.MethodPut) { |
| 90 | + request.SetBodyJsonMarshal(reqBody.Body) |
| 91 | + } |
| 92 | + |
| 93 | + // 执行请求 |
| 94 | + resp, err := request.Send(reqBody.Method, reqBody.URL) |
| 95 | + if err != nil { |
| 96 | + http.Error(w, "request failed: "+err.Error(), http.StatusInternalServerError) |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + // 返回响应 |
| 101 | + result := map[string]interface{}{ |
| 102 | + "status": resp.StatusCode, |
| 103 | + "headers": resp.Header, |
| 104 | + "body": resp.String(), |
| 105 | + } |
| 106 | + |
| 107 | + w.Header().Set("Content-Type", "application/json") |
| 108 | + json.NewEncoder(w).Encode(result) |
| 109 | + }) |
| 110 | + |
| 111 | + // 健康检查 |
| 112 | + http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) { |
| 113 | + fmt.Fprintln(w, "pong") |
| 114 | + }) |
| 115 | + |
| 116 | + addr := fmt.Sprintf(":%d", *port) |
| 117 | + log.Println("Go server running on", addr) |
| 118 | + log.Fatal(http.ListenAndServe(addr, nil)) |
| 119 | +} |
0 commit comments