This commit is contained in:
Daniel 2024-03-25 10:23:29 +08:00
parent ffd6459540
commit fa49681cd9
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 7 additions and 8 deletions

View file

@ -94,7 +94,7 @@ func LoginAuth(c *gin.Context) {
if Conf.AccessAuthCode != authCode {
ret.Code = -1
ret.Msg = Conf.Language(83)
logging.LogWarnf("invalid auth code")
logging.LogWarnf("invalid auth code [ip=%s]", util.GetRemoteAddr(c.Request))
util.WrongAuthCount++
workspaceSession.Captcha = gulu.Rand.String(7)
@ -113,7 +113,7 @@ func LoginAuth(c *gin.Context) {
workspaceSession.AccessAuthCode = authCode
util.WrongAuthCount = 0
workspaceSession.Captcha = gulu.Rand.String(7)
logging.LogInfof("auth success")
logging.LogInfof("auth success [ip=%s]", util.GetRemoteAddr(c.Request))
if err := session.Save(c); nil != err {
logging.LogErrorf("save session failed: " + err.Error())
c.Status(http.StatusInternalServerError)

View file

@ -457,7 +457,7 @@ func serveWebSocket(ginServer *gin.Engine) {
if !authOk {
s.CloseWithMsg([]byte(" unauthenticated"))
logging.LogWarnf("closed an unauthenticated session [%s]", util.GetRemoteAddr(s))
logging.LogWarnf("closed an unauthenticated session [%s]", util.GetRemoteAddr(s.Request))
return
}

View file

@ -27,7 +27,6 @@ import (
"github.com/88250/lute/ast"
"github.com/gin-gonic/gin"
"github.com/imroc/req/v3"
"github.com/olahol/melody"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/logging"
)
@ -125,15 +124,15 @@ func isOnline(checkURL string, skipTlsVerify bool) (ret bool) {
return
}
func GetRemoteAddr(session *melody.Session) string {
ret := session.Request.Header.Get("X-forwarded-for")
func GetRemoteAddr(req *http.Request) string {
ret := req.Header.Get("X-forwarded-for")
ret = strings.TrimSpace(ret)
if "" == ret {
ret = session.Request.Header.Get("X-Real-IP")
ret = req.Header.Get("X-Real-IP")
}
ret = strings.TrimSpace(ret)
if "" == ret {
return session.Request.RemoteAddr
return req.RemoteAddr
}
return strings.Split(ret, ",")[0]
}