任务二完成

This commit is contained in:
Yvan
2026-01-07 10:35:49 +08:00
parent d572a03654
commit 7d27c70453
11 changed files with 968 additions and 1 deletions
+43
View File
@@ -0,0 +1,43 @@
package middleware
import (
"net/http"
"strings"
)
// getClientIP 获取客户端IP
func getClientIP(r *http.Request) string {
// X-Forwarded-For
xff := r.Header.Get("X-Forwarded-For")
if xff != "" {
ips := strings.Split(xff, ",")
if len(ips) > 0 {
return strings.TrimSpace(ips[0])
}
}
// X-Real-IP
xri := r.Header.Get("X-Real-IP")
if xri != "" {
return xri
}
// RemoteAddr
ip := r.RemoteAddr
if idx := strings.LastIndex(ip, ":"); idx != -1 {
ip = ip[:idx]
}
return ip
}
// parseQuery 解析query参数
func parseQuery(query string) map[string]string {
m := make(map[string]string)
for _, v := range strings.Split(query, "&") {
kv := strings.Split(v, "=")
if len(kv) == 2 {
m[kv[0]] = kv[1]
}
}
return m
}