mirror of
https://github.com/bjdgyc/anylink.git
synced 2025-08-11 18:59:17 +08:00
修复邮件发送密码为密文密码的Bug
随机生成的密码打印到日志 邮件通知发送明文密码 清理代码
This commit is contained in:
@@ -1,121 +0,0 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bjdgyc/anylink/base"
|
||||
)
|
||||
|
||||
// var lockManager = admin.GetLockManager()
|
||||
|
||||
const loginStatusKey = "login_status"
|
||||
|
||||
type HttpContext struct {
|
||||
LoginStatus bool // 登录状态
|
||||
}
|
||||
|
||||
// 防爆破中间件
|
||||
func antiBruteForce(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, old_r *http.Request) {
|
||||
// 防爆破功能全局开关
|
||||
if !base.Cfg.AntiBruteForce {
|
||||
next.ServeHTTP(w, old_r)
|
||||
return
|
||||
}
|
||||
|
||||
// 非并发安全
|
||||
hc := &HttpContext{}
|
||||
ctx := context.WithValue(context.Background(), loginStatusKey, hc)
|
||||
r := old_r.WithContext(ctx)
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to read request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
cr := ClientRequest{}
|
||||
err = xml.Unmarshal(body, &cr)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to parse XML", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
username := cr.Auth.Username
|
||||
if r.URL.Path == "/otp-verification" {
|
||||
sessionID, err := GetCookie(r, "auth-session-id")
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid session, please login again", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
sessionData, err := SessStore.GetAuthSession(sessionID)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid session, please login again", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
username = sessionData.ClientRequest.Auth.Username
|
||||
}
|
||||
ip, _, err := net.SplitHostPort(r.RemoteAddr) // 提取纯 IP 地址,去掉端口号
|
||||
if err != nil {
|
||||
http.Error(w, "Unable to parse IP address", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
// 检查IP是否在白名单中
|
||||
if lockManager.IsWhitelisted(ip) {
|
||||
r.Body = io.NopCloser(strings.NewReader(string(body)))
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// 检查全局 IP 锁定
|
||||
if base.Cfg.MaxGlobalIPBanCount > 0 && lockManager.CheckGlobalIPLock(ip, now) {
|
||||
base.Warn("IP", ip, "is globally locked. Try again later.")
|
||||
http.Error(w, "Account globally locked due to too many failed attempts. Try again later.", http.StatusTooManyRequests)
|
||||
return
|
||||
}
|
||||
|
||||
// 检查全局用户锁定
|
||||
if base.Cfg.MaxGlobalUserBanCount > 0 && lockManager.CheckGlobalUserLock(username, now) {
|
||||
base.Warn("User", username, "is globally locked. Try again later.")
|
||||
http.Error(w, "Account globally locked due to too many failed attempts. Try again later.", http.StatusTooManyRequests)
|
||||
return
|
||||
}
|
||||
|
||||
// 检查单个用户的 IP 锁定
|
||||
if base.Cfg.MaxBanCount > 0 && lockManager.CheckUserIPLock(username, ip, now) {
|
||||
base.Warn("IP", ip, "is locked for user", username, "Try again later.")
|
||||
http.Error(w, "Account locked due to too many failed attempts. Try again later.", http.StatusTooManyRequests)
|
||||
return
|
||||
}
|
||||
|
||||
// 重新设置请求体以便后续处理器可以访问
|
||||
r.Body = io.NopCloser(strings.NewReader(string(body)))
|
||||
|
||||
// 调用下一个处理器
|
||||
next.ServeHTTP(w, r)
|
||||
|
||||
// 检查登录状态
|
||||
// Status, _ := lockManager.LoginStatus.Load(loginStatusKey)
|
||||
// loginStatus, _ := Status.(bool)
|
||||
|
||||
loginStatus := hc.LoginStatus
|
||||
|
||||
// 更新用户登录状态
|
||||
lockManager.UpdateGlobalIPLock(ip, now, loginStatus)
|
||||
lockManager.UpdateGlobalUserLock(username, now, loginStatus)
|
||||
lockManager.UpdateUserIPLock(username, ip, now, loginStatus)
|
||||
|
||||
// 清除登录状态
|
||||
// lockManager.LoginStatus.Delete(loginStatusKey)
|
||||
})
|
||||
}
|
@@ -102,9 +102,6 @@ func LinkAuth(w http.ResponseWriter, r *http.Request) {
|
||||
ext := map[string]interface{}{"mac_addr": cr.MacAddressList.MacAddress}
|
||||
err = dbdata.CheckUser(cr.Auth.Username, cr.Auth.Password, cr.GroupSelect, ext)
|
||||
if err != nil {
|
||||
// lockManager.LoginStatus.Store(loginStatusKey, false) // 记录登录失败状态
|
||||
// hc := r.Context().Value(loginStatusKey).(*HttpContext)
|
||||
// hc.LoginStatus = false
|
||||
lockManager.UpdateLoginStatus(cr.Auth.Username, r.RemoteAddr, false) // 记录登录失败状态
|
||||
|
||||
base.Warn(err, r.RemoteAddr)
|
||||
@@ -131,9 +128,6 @@ func LinkAuth(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// 用户otp验证
|
||||
if base.Cfg.AuthAloneOtp && !v.DisableOtp {
|
||||
// lockManager.LoginStatus.Store(loginStatusKey, true) // 重置OTP验证计数
|
||||
// hc := r.Context().Value(loginStatusKey).(*HttpContext)
|
||||
// hc.LoginStatus = true
|
||||
lockManager.UpdateLoginStatus(cr.Auth.Username, r.RemoteAddr, true) // 重置OTP验证计数
|
||||
|
||||
sessionID, err := GenerateSessionID()
|
||||
|
@@ -111,9 +111,6 @@ func DeleteCookie(w http.ResponseWriter, name string) {
|
||||
http.SetCookie(w, cookie)
|
||||
}
|
||||
func CreateSession(w http.ResponseWriter, r *http.Request, authSession *AuthSession) {
|
||||
// lockManager.LoginStatus.Store(loginStatusKey, true) // 更新登录成功状态
|
||||
// hc := r.Context().Value(loginStatusKey).(*HttpContext)
|
||||
// hc.LoginStatus = true
|
||||
cr := authSession.ClientRequest
|
||||
ua := authSession.UserActLog
|
||||
|
||||
@@ -208,14 +205,6 @@ func LinkAuth_otp(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// 动态码错误
|
||||
if !dbdata.CheckOtp(username, otp, otpSecret) {
|
||||
// if sessionData.AddOtpErrCount(1) > maxOtpErrCount {
|
||||
// SessStore.DeleteAuthSession(sessionID)
|
||||
// http.Error(w, "TooManyError, please login again", http.StatusBadRequest)
|
||||
// return
|
||||
// }
|
||||
// lockManager.LoginStatus.Store(loginStatusKey, false) // 记录登录失败状态
|
||||
// hc := r.Context().Value(loginStatusKey).(*HttpContext)
|
||||
// hc.LoginStatus = false
|
||||
lockManager.UpdateLoginStatus(username, r.RemoteAddr, false) // 记录登录失败状态
|
||||
|
||||
base.Warn("OTP 动态码错误", username, r.RemoteAddr)
|
||||
|
Reference in New Issue
Block a user