Archived
安全设置增加“检查客户端指纹"和"检查客户端区域"选项
This commit is contained in:
@@ -4,16 +4,41 @@ package loginutils
|
||||
|
||||
import (
|
||||
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"
|
||||
)
|
||||
|
||||
// CalculateClientFingerprint 计算客户端指纹
|
||||
func CalculateClientFingerprint(action *actions.ActionObject) string {
|
||||
return stringutil.Md5(action.RequestRemoteIP() + "@" + action.Request.UserAgent())
|
||||
return stringutil.Md5(RemoteIP(action) + "@" + action.Request.UserAgent())
|
||||
}
|
||||
|
||||
// RemoteIP 获取客户端IP
|
||||
// TODO 将来增加是否使用代理设置(即从X-Real-IP中获取IP)
|
||||
func RemoteIP(action *actions.ActionObject) string {
|
||||
ip, _, _ := net.SplitHostPort(action.Request.RemoteAddr)
|
||||
return ip
|
||||
}
|
||||
|
||||
// LookupIPRegion 查找登录区域
|
||||
func LookupIPRegion(ip string) string {
|
||||
if len(ip) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
var result = iplibrary.LookupIP(ip)
|
||||
if result != nil && result.IsOk() {
|
||||
// 这里不需要网络运营商信息
|
||||
return result.CountryName() + "@" + result.ProvinceName() + "@" + result.CityName() + "@" + result.TownName()
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// SetCookie 设置Cookie
|
||||
func SetCookie(action *actions.ActionObject, remember bool) {
|
||||
if remember {
|
||||
var cookie = &http.Cookie{
|
||||
@@ -44,6 +69,7 @@ func SetCookie(action *actions.ActionObject, remember bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// UnsetCookie 重置Cookie
|
||||
func UnsetCookie(action *actions.ActionObject) {
|
||||
cookie := &http.Cookie{
|
||||
Name: teaconst.CookieSID,
|
||||
|
||||
@@ -79,6 +79,9 @@ func (this *IndexAction) RunPost(params struct {
|
||||
DenySearchEngines bool
|
||||
DenySpiders bool
|
||||
|
||||
CheckClientFingerprint bool
|
||||
CheckClientRegion bool
|
||||
|
||||
DomainsJSON []byte
|
||||
|
||||
Must *actions.Must
|
||||
@@ -150,6 +153,10 @@ func (this *IndexAction) RunPost(params struct {
|
||||
// 允许记住登录
|
||||
config.AllowRememberLogin = params.AllowRememberLogin
|
||||
|
||||
// Cookie检查
|
||||
config.CheckClientFingerprint = params.CheckClientFingerprint
|
||||
config.CheckClientRegion = params.CheckClientRegion
|
||||
|
||||
err = configloaders.UpdateSecurityConfig(config)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
|
||||
@@ -175,13 +175,32 @@ func (this *userMustAuth) BeforeAction(actionPtr actions.ActionWrapper, paramNam
|
||||
}
|
||||
|
||||
// 检查指纹
|
||||
var clientFingerprint = session.GetString("@fingerprint")
|
||||
if len(clientFingerprint) > 0 && clientFingerprint != loginutils.CalculateClientFingerprint(action) {
|
||||
loginutils.UnsetCookie(action)
|
||||
session.Delete()
|
||||
if securityConfig != nil && securityConfig.CheckClientFingerprint {
|
||||
var clientFingerprint = session.GetString("@fingerprint")
|
||||
if len(clientFingerprint) > 0 && clientFingerprint != loginutils.CalculateClientFingerprint(action) {
|
||||
loginutils.UnsetCookie(action)
|
||||
session.Delete()
|
||||
|
||||
this.login(action)
|
||||
return false
|
||||
this.login(action)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// 检查区域
|
||||
if securityConfig != nil && securityConfig.CheckClientRegion {
|
||||
var oldClientIP = session.GetString("@ip")
|
||||
var currentClientIP = loginutils.RemoteIP(action)
|
||||
if len(oldClientIP) > 0 && len(currentClientIP) > 0 && oldClientIP != currentClientIP {
|
||||
var oldRegion = loginutils.LookupIPRegion(oldClientIP)
|
||||
var newRegion = loginutils.LookupIPRegion(currentClientIP)
|
||||
if newRegion != oldRegion {
|
||||
loginutils.UnsetCookie(action)
|
||||
session.Delete()
|
||||
|
||||
this.login(action)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检查用户是否存在
|
||||
|
||||
@@ -58,6 +58,7 @@ func (this *UserShouldAuth) StoreAdmin(adminId int64, remember bool) {
|
||||
var session = this.action.Session()
|
||||
session.Write("adminId", numberutils.FormatInt64(adminId))
|
||||
session.Write("@fingerprint", loginutils.CalculateClientFingerprint(this.action))
|
||||
session.Write("@ip", loginutils.RemoteIP(this.action))
|
||||
}
|
||||
|
||||
func (this *UserShouldAuth) IsUser() bool {
|
||||
|
||||
Reference in New Issue
Block a user