diff options
Diffstat (limited to 'routers')
-rw-r--r-- | routers/events/events.go | 112 | ||||
-rw-r--r-- | routers/init.go | 2 | ||||
-rw-r--r-- | routers/routes/routes.go | 3 | ||||
-rw-r--r-- | routers/user/auth.go | 12 |
4 files changed, 127 insertions, 2 deletions
diff --git a/routers/events/events.go b/routers/events/events.go new file mode 100644 index 0000000000..a1131f29e3 --- /dev/null +++ b/routers/events/events.go @@ -0,0 +1,112 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package events + +import ( + "net/http" + "time" + + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/eventsource" + "code.gitea.io/gitea/modules/graceful" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/routers/user" +) + +// Events listens for events +func Events(ctx *context.Context) { + // FIXME: Need to check if resp is actually a http.Flusher! - how though? + + // Set the headers related to event streaming. + ctx.Resp.Header().Set("Content-Type", "text/event-stream") + ctx.Resp.Header().Set("Cache-Control", "no-cache") + ctx.Resp.Header().Set("Connection", "keep-alive") + ctx.Resp.Header().Set("X-Accel-Buffering", "no") + ctx.Resp.WriteHeader(http.StatusOK) + + // Listen to connection close and un-register messageChan + notify := ctx.Req.Context().Done() + ctx.Resp.Flush() + + shutdownCtx := graceful.GetManager().ShutdownContext() + + uid := ctx.User.ID + + messageChan := eventsource.GetManager().Register(uid) + + unregister := func() { + eventsource.GetManager().Unregister(uid, messageChan) + // ensure the messageChan is closed + for { + _, ok := <-messageChan + if !ok { + break + } + } + } + + if _, err := ctx.Resp.Write([]byte("\n")); err != nil { + log.Error("Unable to write to EventStream: %v", err) + unregister() + return + } + + timer := time.NewTicker(30 * time.Second) + +loop: + for { + select { + case <-timer.C: + event := &eventsource.Event{ + Name: "ping", + } + _, err := event.WriteTo(ctx.Resp) + if err != nil { + log.Error("Unable to write to EventStream for user %s: %v", ctx.User.Name, err) + go unregister() + break loop + } + ctx.Resp.Flush() + case <-notify: + go unregister() + break loop + case <-shutdownCtx.Done(): + go unregister() + break loop + case event, ok := <-messageChan: + if !ok { + break loop + } + + // Handle logout + if event.Name == "logout" { + if ctx.Session.ID() == event.Data { + _, _ = (&eventsource.Event{ + Name: "logout", + Data: "here", + }).WriteTo(ctx.Resp) + ctx.Resp.Flush() + go unregister() + user.HandleSignOut(ctx) + break loop + } + // Replace the event - we don't want to expose the session ID to the user + event = (&eventsource.Event{ + Name: "logout", + Data: "elsewhere", + }) + } + + _, err := event.WriteTo(ctx.Resp) + if err != nil { + log.Error("Unable to write to EventStream for user %s: %v", ctx.User.Name, err) + go unregister() + break loop + } + ctx.Resp.Flush() + } + } + timer.Stop() +} diff --git a/routers/init.go b/routers/init.go index 724bf84c10..72f55c809c 100644 --- a/routers/init.go +++ b/routers/init.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/modules/auth/sso" "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/cron" + "code.gitea.io/gitea/modules/eventsource" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/highlight" code_indexer "code.gitea.io/gitea/modules/indexer/code" @@ -123,6 +124,7 @@ func GlobalInit(ctx context.Context) { if err := task.Init(); err != nil { log.Fatal("Failed to initialize task scheduler: %v", err) } + eventsource.GetManager().Init() } if setting.EnableSQLite3 { log.Info("SQLite3 Supported") diff --git a/routers/routes/routes.go b/routers/routes/routes.go index ac0f3f4f14..a51c1e17af 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -27,6 +27,7 @@ import ( "code.gitea.io/gitea/routers/admin" apiv1 "code.gitea.io/gitea/routers/api/v1" "code.gitea.io/gitea/routers/dev" + "code.gitea.io/gitea/routers/events" "code.gitea.io/gitea/routers/org" "code.gitea.io/gitea/routers/private" "code.gitea.io/gitea/routers/repo" @@ -340,6 +341,8 @@ func RegisterRoutes(m *macaron.Macaron) { }) }, reqSignOut) + m.Any("/user/events", reqSignIn, events.Events) + m.Group("/login/oauth", func() { m.Get("/authorize", bindIgnErr(auth.AuthorizationForm{}), user.AuthorizeOAuth) m.Post("/grant", bindIgnErr(auth.GrantApplicationForm{}), user.GrantApplicationOAuth) diff --git a/routers/user/auth.go b/routers/user/auth.go index 169f3c453d..a8ff92ae59 100644 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -16,6 +16,7 @@ import ( "code.gitea.io/gitea/modules/auth/oauth2" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/eventsource" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/password" "code.gitea.io/gitea/modules/recaptcha" @@ -991,7 +992,8 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au ctx.Redirect(setting.AppSubURL + "/user/login") } -func handleSignOut(ctx *context.Context) { +// HandleSignOut resets the session and sets the cookies +func HandleSignOut(ctx *context.Context) { _ = ctx.Session.Delete("uid") _ = ctx.Session.Delete("uname") _ = ctx.Session.Delete("socialId") @@ -1006,7 +1008,13 @@ func handleSignOut(ctx *context.Context) { // SignOut sign out from login status func SignOut(ctx *context.Context) { - handleSignOut(ctx) + if ctx.User != nil { + eventsource.GetManager().SendMessageBlocking(ctx.User.ID, &eventsource.Event{ + Name: "logout", + Data: ctx.Session.ID(), + }) + } + HandleSignOut(ctx) ctx.Redirect(setting.AppSubURL + "/") } |