Upgraded to latest aero version
This commit is contained in:
@ -20,10 +20,10 @@ package middleware
|
||||
|
||||
// // Firewall middleware detects malicious requests.
|
||||
// func Firewall() aero.Middleware {
|
||||
// return func(ctx *aero.Context, next func()) {
|
||||
// return func(ctx aero.Context, next func()) {
|
||||
// var stats *IPStats
|
||||
|
||||
// ip := ctx.RealIP()
|
||||
// ip := ctx.IP()
|
||||
|
||||
// // Allow localhost
|
||||
// if ip == "127.0.0.1" {
|
||||
@ -44,7 +44,7 @@ package middleware
|
||||
// }
|
||||
|
||||
// // Add requested URI to the list of requests
|
||||
// stats.Requests = append(stats.Requests, ctx.URI())
|
||||
// stats.Requests = append(stats.Requests, ctx.Path())
|
||||
|
||||
// if len(stats.Requests) > requestThreshold {
|
||||
// stats.Requests = stats.Requests[len(stats.Requests)-requestThreshold:]
|
||||
@ -69,7 +69,7 @@ package middleware
|
||||
// }
|
||||
|
||||
// // Disallow request
|
||||
// request.Error("[guest]", ip, "BLOCKED BY FIREWALL", ctx.URI())
|
||||
// request.Error("[guest]", ip, "BLOCKED BY FIREWALL", ctx.Path())
|
||||
// return
|
||||
// }
|
||||
|
||||
|
@ -2,15 +2,15 @@ package middleware
|
||||
|
||||
// // HTTPSRedirect middleware redirects to HTTPS if needed.
|
||||
// func HTTPSRedirect() aero.Middleware {
|
||||
// return func(ctx *aero.Context, next func()) {
|
||||
// return func(ctx aero.Context, next func()) {
|
||||
// request := ctx.Request()
|
||||
// userAgent := request.Header().Get("User-Agent")
|
||||
// isBrowser := strings.Contains(userAgent, "Mozilla/") || strings.Contains(userAgent, "Chrome/") || strings.Contains(userAgent, "AppleWebKit/")
|
||||
|
||||
// if !strings.HasPrefix(request.Protocol(), "HTTP/2") && isBrowser {
|
||||
// fmt.Println("Redirect to HTTPS")
|
||||
// ctx.Redirect("https://" + request.Host() + request.URL().Path)
|
||||
// ctx.Response().WriteHeader(ctx.StatusCode)
|
||||
// ctx.Redirect(http.StatusFound, "https://" + request.Host() + request.URL().Path)
|
||||
// ctx.Response().WriteHeader(ctx.Status())
|
||||
// return
|
||||
// }
|
||||
|
||||
|
@ -32,18 +32,19 @@ func init() {
|
||||
}
|
||||
|
||||
// Log middleware logs every request into logs/request.log and errors into logs/error.log.
|
||||
func Log() aero.Middleware {
|
||||
return func(ctx *aero.Context, next func()) {
|
||||
func Log(next aero.Handler) aero.Handler {
|
||||
return func(ctx aero.Context) error {
|
||||
start := time.Now()
|
||||
next()
|
||||
err := next(ctx)
|
||||
responseTime := time.Since(start)
|
||||
|
||||
go logRequest(ctx, responseTime)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Logs a single request
|
||||
func logRequest(ctx *aero.Context, responseTime time.Duration) {
|
||||
func logRequest(ctx aero.Context, responseTime time.Duration) {
|
||||
responseTimeString := strconv.Itoa(int(responseTime.Nanoseconds()/1000000)) + " ms"
|
||||
repeatSpaceCount := 8 - len(responseTimeString)
|
||||
|
||||
@ -54,7 +55,7 @@ func logRequest(ctx *aero.Context, responseTime time.Duration) {
|
||||
responseTimeString = strings.Repeat(" ", repeatSpaceCount) + responseTimeString
|
||||
|
||||
user := utils.GetUser(ctx)
|
||||
ip := ctx.RealIP()
|
||||
ip := ctx.IP()
|
||||
hostNames, cached := GetHostsForIP(ip)
|
||||
|
||||
if !cached && len(hostNames) > 0 {
|
||||
@ -70,20 +71,20 @@ func logRequest(ctx *aero.Context, responseTime time.Duration) {
|
||||
nick = user.Nick
|
||||
}
|
||||
|
||||
requestLog.Info("%s | %s | %s | %s | %d | %s", nick, id, ip, responseTimeString, ctx.StatusCode, ctx.URI())
|
||||
requestLog.Info("%s | %s | %s | %s | %d | %s", nick, id, ip, responseTimeString, ctx.Status(), ctx.Path())
|
||||
|
||||
// Log all requests that failed
|
||||
switch ctx.StatusCode {
|
||||
switch ctx.Status() {
|
||||
case http.StatusOK, http.StatusFound, http.StatusMovedPermanently, http.StatusPermanentRedirect, http.StatusTemporaryRedirect:
|
||||
// Ok.
|
||||
|
||||
default:
|
||||
errorLog.Error("%s | %s | %s | %s | %d | %s (%s)", nick, id, ip, responseTimeString, ctx.StatusCode, ctx.URI(), ctx.ErrorMessage)
|
||||
errorLog.Error("%s | %s | %s | %s | %d | %s", nick, id, ip, responseTimeString, ctx.Status(), ctx.Path())
|
||||
}
|
||||
|
||||
// Notify us about long requests.
|
||||
// However ignore requests under /auth/ because those depend on 3rd party servers.
|
||||
if responseTime >= 500*time.Millisecond && !strings.HasPrefix(ctx.URI(), "/auth/") && !strings.HasPrefix(ctx.URI(), "/sitemap/") && !strings.HasPrefix(ctx.URI(), "/api/sse/") {
|
||||
errorLog.Error("%s | %s | %s | %s | %d | %s (long response time)", nick, id, ip, responseTimeString, ctx.StatusCode, ctx.URI())
|
||||
if responseTime >= 500*time.Millisecond && !strings.HasPrefix(ctx.Path(), "/auth/") && !strings.HasPrefix(ctx.Path(), "/sitemap/") && !strings.HasPrefix(ctx.Path(), "/api/sse/") {
|
||||
errorLog.Error("%s | %s | %s | %s | %d | %s (long response time)", nick, id, ip, responseTimeString, ctx.Status(), ctx.Path())
|
||||
}
|
||||
}
|
||||
|
24
middleware/OpenGraph.go
Normal file
24
middleware/OpenGraph.go
Normal file
@ -0,0 +1,24 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/arn"
|
||||
)
|
||||
|
||||
// OpenGraphContext is a context with open graph data.
|
||||
type OpenGraphContext struct {
|
||||
aero.Context
|
||||
*arn.OpenGraph
|
||||
}
|
||||
|
||||
// OpenGraph middleware modifies the context to be an OpenGraphContext.
|
||||
func OpenGraph(next aero.Handler) aero.Handler {
|
||||
return func(ctx aero.Context) error {
|
||||
ctx = &OpenGraphContext{
|
||||
Context: ctx,
|
||||
OpenGraph: nil,
|
||||
}
|
||||
|
||||
return next(ctx)
|
||||
}
|
||||
}
|
34
middleware/Recover.go
Normal file
34
middleware/Recover.go
Normal file
@ -0,0 +1,34 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"runtime"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
)
|
||||
|
||||
// Recover recovers from panics and shows them as the response body.
|
||||
func Recover(next aero.Handler) aero.Handler {
|
||||
return func(ctx aero.Context) error {
|
||||
defer func() {
|
||||
r := recover()
|
||||
|
||||
if r == nil {
|
||||
return
|
||||
}
|
||||
|
||||
err, ok := r.(error)
|
||||
|
||||
if !ok {
|
||||
err = fmt.Errorf("%v", r)
|
||||
}
|
||||
|
||||
stack := make([]byte, 4096)
|
||||
length := runtime.Stack(stack, true)
|
||||
_ = ctx.Error(http.StatusInternalServerError, err, stack[:length])
|
||||
}()
|
||||
|
||||
return next(ctx)
|
||||
}
|
||||
}
|
@ -3,14 +3,16 @@ package middleware
|
||||
import "github.com/aerogo/aero"
|
||||
|
||||
// Session middleware saves an existing session if it has been modified.
|
||||
func Session() aero.Middleware {
|
||||
return func(ctx *aero.Context, next func()) {
|
||||
func Session(next aero.Handler) aero.Handler {
|
||||
return func(ctx aero.Context) error {
|
||||
// Handle the request first
|
||||
next()
|
||||
err := next(ctx)
|
||||
|
||||
// Update session if it has been modified
|
||||
if ctx.HasSession() && ctx.Session().Modified() {
|
||||
ctx.App.Sessions.Store.Set(ctx.Session().ID(), ctx.Session())
|
||||
_ = ctx.App().Sessions.Store.Set(ctx.Session().ID(), ctx.Session())
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -14,33 +14,35 @@ import (
|
||||
)
|
||||
|
||||
// UserInfo updates user related information after each request.
|
||||
func UserInfo() aero.Middleware {
|
||||
return func(ctx *aero.Context, next func()) {
|
||||
next()
|
||||
func UserInfo(next aero.Handler) aero.Handler {
|
||||
return func(ctx aero.Context) error {
|
||||
err := next(ctx)
|
||||
|
||||
// Ignore non-HTML requests
|
||||
contentType := ctx.Response().Header().Get("Content-Type")
|
||||
contentType := ctx.Response().Header("Content-Type")
|
||||
|
||||
if !strings.HasPrefix(contentType, "text/html") {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
user := utils.GetUser(ctx)
|
||||
|
||||
// When there's no user logged in, nothing to update
|
||||
if user == nil {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
// This works asynchronously so it doesn't block the response
|
||||
go updateUserInfo(ctx, user)
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Update browser and OS data
|
||||
func updateUserInfo(ctx *aero.Context, user *arn.User) {
|
||||
newIP := ctx.RealIP()
|
||||
newUserAgent := ctx.UserAgent()
|
||||
func updateUserInfo(ctx aero.Context, user *arn.User) {
|
||||
newIP := ctx.IP()
|
||||
newUserAgent := ctx.Request().Header("User-Agent")
|
||||
|
||||
if user.UserAgent != newUserAgent {
|
||||
user.UserAgent = newUserAgent
|
||||
@ -91,7 +93,12 @@ func updateUserLocation(user *arn.User, newIP string) {
|
||||
}
|
||||
|
||||
newLocation := arn.IPInfoDBLocation{}
|
||||
response.Unmarshal(&newLocation)
|
||||
err = response.Unmarshal(&newLocation)
|
||||
|
||||
if err != nil {
|
||||
color.Red("Couldn't deserialize location data | Status: %d | IP: %s", response.StatusCode, user.IP)
|
||||
return
|
||||
}
|
||||
|
||||
if newLocation.CountryName != "-" {
|
||||
user.Location.CountryName = newLocation.CountryName
|
||||
|
Reference in New Issue
Block a user