mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2026-04-19 02:05:20 +08:00
管理系统安全设置增加“自定义客户端IP报头”
This commit is contained in:
@@ -3,12 +3,15 @@
|
||||
package loginutils
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
|
||||
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||
"net"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// CalculateClientFingerprint 计算客户端指纹
|
||||
@@ -17,8 +20,26 @@ func CalculateClientFingerprint(action *actions.ActionObject) string {
|
||||
}
|
||||
|
||||
// RemoteIP 获取客户端IP
|
||||
// TODO 将来增加是否使用代理设置(即从X-Real-IP中获取IP)
|
||||
func RemoteIP(action *actions.ActionObject) string {
|
||||
securityConfig, _ := configloaders.LoadSecurityConfig()
|
||||
|
||||
if securityConfig != nil {
|
||||
if len(securityConfig.ClientIPHeaderNames) > 0 {
|
||||
var headerNames = regexp.MustCompile(`[,;\s,、;]`).Split(securityConfig.ClientIPHeaderNames, -1)
|
||||
for _, headerName := range headerNames {
|
||||
headerName = http.CanonicalHeaderKey(strings.TrimSpace(headerName))
|
||||
if len(headerName) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
var ipValue = action.Request.Header.Get(headerName)
|
||||
if net.ParseIP(ipValue) != nil {
|
||||
return ipValue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ip, _, _ := net.SplitHostPort(action.Request.RemoteAddr)
|
||||
return ip
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user