This commit is contained in:
laodaming
2023-06-30 17:20:11 +08:00
parent 5ac62e7c55
commit c17ab5ae5c
10 changed files with 283 additions and 3 deletions

View File

@@ -37,6 +37,9 @@ var (
CodeDbCreateErr = &StatusResponse{5004, "failed to create record in database"} // 数据库中创建记录失败
CodeDbSqlErr = &StatusResponse{5005, "database error"} // 数据库错误
CodeJsonErr = &StatusResponse{5006, "JSON error"} // JSON解析错误
CodeAesCbcEncryptionErr = &StatusResponse{5106, "encryption data err"} // 加密数据失败
CodeAesCbcDecryptionErr = &StatusResponse{5107, "decryption data err"} // 解密数据失败
)
type Response struct {

View File

@@ -1 +1,25 @@
package ip
import (
"net"
"net/http"
"strings"
)
// 获取客户端ip地址
func GetClientIP(r *http.Request) (ip string, err error) {
xForwardedFor := r.Header.Get("X-Forwarded-For")
ip = strings.TrimSpace(strings.Split(xForwardedFor, ",")[0])
if ip != "" {
return ip, nil
}
ip = strings.TrimSpace(r.Header.Get("X-Real-Ip"))
if ip != "" {
return ip, nil
}
ip, _, err = net.SplitHostPort(strings.TrimSpace(r.RemoteAddr))
if err != nil {
return "", err
}
return ip, nil
}