From e52e09cfd59985fa827d1a9b15cb93890eac5230 Mon Sep 17 00:00:00 2001 From: zouap Date: Tue, 26 Oct 2021 14:40:48 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=97=A5=E5=BF=97=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/models.go | 1 + models/user_login_log.go | 31 +++++++++++++++++++++++++++++++ routers/user/auth.go | 14 ++++++++------ 3 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 models/user_login_log.go diff --git a/models/models.go b/models/models.go index 0cdcee4fd..696d0949b 100755 --- a/models/models.go +++ b/models/models.go @@ -139,6 +139,7 @@ func init() { new(RepoStatistic), new(SummaryStatistic), new(UserBusinessAnalysis), + new(UserLoginLog), ) gonicNames := []string{"SSL", "UID"} diff --git a/models/user_login_log.go b/models/user_login_log.go new file mode 100644 index 000000000..849e9af0b --- /dev/null +++ b/models/user_login_log.go @@ -0,0 +1,31 @@ +package models + +import ( + "fmt" + "time" + "net/http" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/timeutil" +) + +type UserLoginLog struct { + ID int64 `xorm:"pk autoincr"` + uid int64 `xorm:"NOT NULL"` + ipAddr string `xorm:"default NULL"` + CreatedUnix timeutil.TimeStamp `xorm:"created"` +} + +func SaveLoginInfoToDb(r *http.Request,u User){ + + + +} + + +func getIP(r *http.Request) string {     + forwarded := r.Header.Get("X-FORWARDED-FOR")     + if forwarded != "" {         + return forwarded     + }     + return r.RemoteAddr +} \ No newline at end of file diff --git a/routers/user/auth.go b/routers/user/auth.go index 13e338565..749386e07 100755 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -215,6 +215,7 @@ func SignInPost(ctx *context.Context, form auth.SignInForm) { } return } + log.Info("Go this 1.") // If this user is enrolled in 2FA, we can't sign the user in just yet. // Instead, redirect them to the 2FA authentication page. _, err = models.GetTwoFactorByUID(u.ID) @@ -226,7 +227,7 @@ func SignInPost(ctx *context.Context, form auth.SignInForm) { } return } - + log.Info("Go this 2.") // User needs to use 2FA, save data and redirect to 2FA page. if err := ctx.Session.Set("twofaUid", u.ID); err != nil { ctx.ServerError("UserSignIn: Unable to set twofaUid in session", err) @@ -240,13 +241,14 @@ func SignInPost(ctx *context.Context, form auth.SignInForm) { ctx.ServerError("UserSignIn: Unable to save session", err) return } - + log.Info("Go this 3.") regs, err := models.GetU2FRegistrationsByUID(u.ID) if err == nil && len(regs) > 0 { ctx.Redirect(setting.AppSubURL + "/user/u2f") return } - + log.Info("Go this 4.") + models.SaveLoginInfoToDb(ctx.Req.Request, u) ctx.Redirect(setting.AppSubURL + "/user/two_factor") } @@ -1168,8 +1170,8 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo log.Trace("Account created: %s", u.Name, ctx.Data["MsgID"]) err := models.AddEmailAddress(&models.EmailAddress{ - UID: u.ID, - Email: form.Email, + UID: u.ID, + Email: form.Email, IsActivated: !setting.Service.RegisterEmailConfirm, }) @@ -1267,7 +1269,7 @@ func Activate(ctx *context.Context) { } email, err := models.GetEmailAddressByIDAndEmail(user.ID, user.Email) - if err != nil || email == nil{ + if err != nil || email == nil { log.Error("GetEmailAddressByIDAndEmail failed", ctx.Data["MsgID"]) } else { if err := email.Activate(); err != nil { From f3b530b24923aab2a951198ee12e1e0c8505256d Mon Sep 17 00:00:00 2001 From: zouap Date: Tue, 26 Oct 2021 15:05:06 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/user_login_log.go | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/models/user_login_log.go b/models/user_login_log.go index 849e9af0b..288f8e051 100644 --- a/models/user_login_log.go +++ b/models/user_login_log.go @@ -1,31 +1,34 @@ package models import ( - "fmt" - "time" "net/http" - "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/timeutil" ) type UserLoginLog struct { - ID int64 `xorm:"pk autoincr"` - uid int64 `xorm:"NOT NULL"` - ipAddr string `xorm:"default NULL"` - CreatedUnix timeutil.TimeStamp `xorm:"created"` + ID int64 `xorm:"pk autoincr"` + uid int64 `xorm:"NOT NULL"` + ipAddr string `xorm:"default NULL"` + CreatedUnix timeutil.TimeStamp `xorm:"created"` } -func SaveLoginInfoToDb(r *http.Request,u User){ +func SaveLoginInfoToDb(r *http.Request, u *User) { + statictisSess := xStatistic.NewSession() + defer statictisSess.Close() - + var dateRecord UserLoginLog -} + dateRecord.uid = u.ID + dateRecord.ipAddr = getIP(r) + statictisSess.Insert(&dateRecord) +} -func getIP(r *http.Request) string {     - forwarded := r.Header.Get("X-FORWARDED-FOR")     - if forwarded != "" {         - return forwarded     - }     +func getIP(r *http.Request) string { + forwarded := r.Header.Get("X-FORWARDED-FOR") + if forwarded != "" { + return forwarded + } return r.RemoteAddr -} \ No newline at end of file +} From bac11fc65363543ec9f3bc839256b586de535b67 Mon Sep 17 00:00:00 2001 From: zouap Date: Tue, 26 Oct 2021 15:11:58 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/user/auth.go | 1 + 1 file changed, 1 insertion(+) diff --git a/routers/user/auth.go b/routers/user/auth.go index 749386e07..d9811bd83 100755 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -225,6 +225,7 @@ func SignInPost(ctx *context.Context, form auth.SignInForm) { } else { ctx.ServerError("UserSignIn", err) } + log.Info("Go this 1-1.") return } log.Info("Go this 2.") From c1b3feaed871f529cfa062d85a4e8acc68f4c606 Mon Sep 17 00:00:00 2001 From: zouap Date: Tue, 26 Oct 2021 15:15:06 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- routers/user/auth.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/routers/user/auth.go b/routers/user/auth.go index d9811bd83..44c5ad97d 100755 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -215,7 +215,7 @@ func SignInPost(ctx *context.Context, form auth.SignInForm) { } return } - log.Info("Go this 1.") + models.SaveLoginInfoToDb(ctx.Req.Request, u) // If this user is enrolled in 2FA, we can't sign the user in just yet. // Instead, redirect them to the 2FA authentication page. _, err = models.GetTwoFactorByUID(u.ID) @@ -225,10 +225,8 @@ func SignInPost(ctx *context.Context, form auth.SignInForm) { } else { ctx.ServerError("UserSignIn", err) } - log.Info("Go this 1-1.") return } - log.Info("Go this 2.") // User needs to use 2FA, save data and redirect to 2FA page. if err := ctx.Session.Set("twofaUid", u.ID); err != nil { ctx.ServerError("UserSignIn: Unable to set twofaUid in session", err) @@ -242,14 +240,12 @@ func SignInPost(ctx *context.Context, form auth.SignInForm) { ctx.ServerError("UserSignIn: Unable to save session", err) return } - log.Info("Go this 3.") regs, err := models.GetU2FRegistrationsByUID(u.ID) if err == nil && len(regs) > 0 { ctx.Redirect(setting.AppSubURL + "/user/u2f") return } - log.Info("Go this 4.") - models.SaveLoginInfoToDb(ctx.Req.Request, u) + ctx.Redirect(setting.AppSubURL + "/user/two_factor") } From 6c56ed5a03864d50dd6de1d934bc6a65bb8618cb Mon Sep 17 00:00:00 2001 From: zouap Date: Tue, 26 Oct 2021 15:19:40 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/user_login_log.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/models/user_login_log.go b/models/user_login_log.go index 288f8e051..4a4c5c58c 100644 --- a/models/user_login_log.go +++ b/models/user_login_log.go @@ -1,8 +1,10 @@ package models import ( + "fmt" "net/http" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/timeutil" ) @@ -21,6 +23,8 @@ func SaveLoginInfoToDb(r *http.Request, u *User) { dateRecord.uid = u.ID dateRecord.ipAddr = getIP(r) + log.Info("uid=" + fmt.Sprint(dateRecord.uid)) + log.Info("ip=" + dateRecord.ipAddr) statictisSess.Insert(&dateRecord) } From f453c288df56c4459ff4be53ebf3efe07a705190 Mon Sep 17 00:00:00 2001 From: zouap Date: Tue, 26 Oct 2021 15:23:20 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/user_login_log.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/models/user_login_log.go b/models/user_login_log.go index 4a4c5c58c..75f43847f 100644 --- a/models/user_login_log.go +++ b/models/user_login_log.go @@ -10,8 +10,8 @@ import ( type UserLoginLog struct { ID int64 `xorm:"pk autoincr"` - uid int64 `xorm:"NOT NULL"` - ipAddr string `xorm:"default NULL"` + UId int64 `xorm:"NOT NULL"` + IpAddr string `xorm:"default NULL"` CreatedUnix timeutil.TimeStamp `xorm:"created"` } @@ -21,10 +21,10 @@ func SaveLoginInfoToDb(r *http.Request, u *User) { var dateRecord UserLoginLog - dateRecord.uid = u.ID - dateRecord.ipAddr = getIP(r) - log.Info("uid=" + fmt.Sprint(dateRecord.uid)) - log.Info("ip=" + dateRecord.ipAddr) + dateRecord.UId = u.ID + dateRecord.IpAddr = getIP(r) + log.Info("uid=" + fmt.Sprint(dateRecord.UId)) + log.Info("ip=" + dateRecord.IpAddr) statictisSess.Insert(&dateRecord) } From 225a68b995b1236d1d23ee0196632b883dc93553 Mon Sep 17 00:00:00 2001 From: zouap Date: Tue, 26 Oct 2021 15:31:20 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/user_login_log.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/models/user_login_log.go b/models/user_login_log.go index 75f43847f..46260efda 100644 --- a/models/user_login_log.go +++ b/models/user_login_log.go @@ -1,6 +1,7 @@ package models import ( + "encoding/json" "fmt" "net/http" @@ -30,9 +31,12 @@ func SaveLoginInfoToDb(r *http.Request, u *User) { } func getIP(r *http.Request) string { + headerjson, _ := json.Marshal(r.Header) + log.Info("json=" + string(headerjson)) forwarded := r.Header.Get("X-FORWARDED-FOR") if forwarded != "" { return forwarded } + return r.RemoteAddr } From 762f2e3117f3c79b6a21d12570f79efee1c44252 Mon Sep 17 00:00:00 2001 From: zouap Date: Tue, 26 Oct 2021 15:38:15 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/user_login_log.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/models/user_login_log.go b/models/user_login_log.go index 46260efda..fb6be9948 100644 --- a/models/user_login_log.go +++ b/models/user_login_log.go @@ -32,7 +32,11 @@ func SaveLoginInfoToDb(r *http.Request, u *User) { func getIP(r *http.Request) string { headerjson, _ := json.Marshal(r.Header) - log.Info("json=" + string(headerjson)) + log.Info("header json=" + string(headerjson)) + + reqjson, _ := json.Marshal(r) + log.Info("request json=" + string(reqjson)) + forwarded := r.Header.Get("X-FORWARDED-FOR") if forwarded != "" { return forwarded From 32d199a6c8d2fdee1e1633fd15044663457abc2e Mon Sep 17 00:00:00 2001 From: zouap Date: Tue, 26 Oct 2021 15:43:36 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/user_login_log.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/user_login_log.go b/models/user_login_log.go index fb6be9948..299cd48af 100644 --- a/models/user_login_log.go +++ b/models/user_login_log.go @@ -34,8 +34,8 @@ func getIP(r *http.Request) string { headerjson, _ := json.Marshal(r.Header) log.Info("header json=" + string(headerjson)) - reqjson, _ := json.Marshal(r) - log.Info("request json=" + string(reqjson)) + trailerjson, _ := json.Marshal(r.Trailer) + log.Info("trailerjson=" + string(trailerjson)) forwarded := r.Header.Get("X-FORWARDED-FOR") if forwarded != "" { From d739407e0d22e08ea0c4c5092f49672231b7f78c Mon Sep 17 00:00:00 2001 From: zouap Date: Tue, 26 Oct 2021 15:59:46 +0800 Subject: [PATCH 10/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/user_business_analysis.go | 25 +++++++++++++++++++++++++ models/user_login_log.go | 12 ------------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/models/user_business_analysis.go b/models/user_business_analysis.go index 8b7691677..406cc36f1 100644 --- a/models/user_business_analysis.go +++ b/models/user_business_analysis.go @@ -153,6 +153,7 @@ func CountData(wikiCountMap map[string]int) { CommitDatasetSizeMap := queryDatasetSize(start_unix, end_unix) SolveIssueCountMap := querySolveIssue(start_unix, end_unix) CreateRepoCountMap := queryUserCreateRepo(start_unix, end_unix) + LoginCountMap := queryLoginCount(start_unix, end_unix) for i, userRecord := range userList { var dateRecord UserBusinessAnalysis @@ -235,6 +236,12 @@ func CountData(wikiCountMap map[string]int) { dateRecord.CreateRepoCount = CreateRepoCountMap[dateRecord.ID] } + if _, ok := LoginCountMap[dateRecord.ID]; !ok { + dateRecord.LoginCount = 0 + } else { + dateRecord.LoginCount = LoginCountMap[dateRecord.ID] + } + dateRecord.CommitModelCount = 0 statictisSess := xStatistic.NewSession() @@ -421,6 +428,24 @@ func queryUserCreateRepo(start_unix int64, end_unix int64) map[int64]int { return resultMap } +func queryLoginCount(start_unix int64, end_unix int64) map[int64]int { + statictisSess := xStatistic.NewSession() + defer statictisSess.Close() + statictisSess.Select("id,u_id").Table("user_login_log").Where("created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)) + userLoginLogList := make([]*UserLoginLog, 0) + statictisSess.Find(&userLoginLogList) + resultMap := make(map[int64]int) + log.Info("query user login size=" + fmt.Sprint(len(userLoginLogList))) + for _, loginRecord := range userLoginLogList { + if _, ok := resultMap[loginRecord.UId]; !ok { + resultMap[loginRecord.UId] = 1 + } else { + resultMap[loginRecord.UId] += 1 + } + } + return resultMap +} + func subMonth(t1, t2 time.Time) (month int) { y1 := t1.Year() y2 := t2.Year() diff --git a/models/user_login_log.go b/models/user_login_log.go index 299cd48af..4a499d527 100644 --- a/models/user_login_log.go +++ b/models/user_login_log.go @@ -1,11 +1,8 @@ package models import ( - "encoding/json" - "fmt" "net/http" - "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/timeutil" ) @@ -24,23 +21,14 @@ func SaveLoginInfoToDb(r *http.Request, u *User) { dateRecord.UId = u.ID dateRecord.IpAddr = getIP(r) - log.Info("uid=" + fmt.Sprint(dateRecord.UId)) - log.Info("ip=" + dateRecord.IpAddr) statictisSess.Insert(&dateRecord) } func getIP(r *http.Request) string { - headerjson, _ := json.Marshal(r.Header) - log.Info("header json=" + string(headerjson)) - - trailerjson, _ := json.Marshal(r.Trailer) - log.Info("trailerjson=" + string(trailerjson)) - forwarded := r.Header.Get("X-FORWARDED-FOR") if forwarded != "" { return forwarded } - return r.RemoteAddr }