Disabled old middleware
This commit is contained in:
parent
6ff9ad1594
commit
e7f30607b2
@ -1,79 +1,70 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
// const requestThreshold = 10
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/aerogo/aero"
|
// var ipToStats = cache.New(15*time.Minute, 15*time.Minute)
|
||||||
"github.com/animenotifier/notify.moe/utils"
|
|
||||||
cache "github.com/patrickmn/go-cache"
|
|
||||||
)
|
|
||||||
|
|
||||||
const requestThreshold = 10
|
// // IPStats captures the statistics for a single IP.
|
||||||
|
// type IPStats struct {
|
||||||
|
// Requests []string
|
||||||
|
// }
|
||||||
|
|
||||||
var ipToStats = cache.New(15*time.Minute, 15*time.Minute)
|
// // Firewall middleware detects malicious requests.
|
||||||
|
// func Firewall() aero.Middleware {
|
||||||
|
// return func(ctx *aero.Context, next func()) {
|
||||||
|
// var stats *IPStats
|
||||||
|
|
||||||
// IPStats captures the statistics for a single IP.
|
// ip := ctx.RealIP()
|
||||||
type IPStats struct {
|
|
||||||
Requests []string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Firewall middleware detects malicious requests.
|
// // Allow localhost
|
||||||
func Firewall() aero.Middleware {
|
// if ip == "127.0.0.1" {
|
||||||
return func(ctx *aero.Context, next func()) {
|
// next()
|
||||||
var stats *IPStats
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
ip := ctx.RealIP()
|
// statsObj, found := ipToStats.Get(ip)
|
||||||
|
|
||||||
// Allow localhost
|
// if found {
|
||||||
if ip == "127.0.0.1" {
|
// stats = statsObj.(*IPStats)
|
||||||
next()
|
// } else {
|
||||||
return
|
// stats = &IPStats{
|
||||||
}
|
// Requests: []string{},
|
||||||
|
// }
|
||||||
|
|
||||||
statsObj, found := ipToStats.Get(ip)
|
// ipToStats.Set(ip, stats, cache.DefaultExpiration)
|
||||||
|
// }
|
||||||
|
|
||||||
if found {
|
// // Add requested URI to the list of requests
|
||||||
stats = statsObj.(*IPStats)
|
// stats.Requests = append(stats.Requests, ctx.URI())
|
||||||
} else {
|
|
||||||
stats = &IPStats{
|
|
||||||
Requests: []string{},
|
|
||||||
}
|
|
||||||
|
|
||||||
ipToStats.Set(ip, stats, cache.DefaultExpiration)
|
// if len(stats.Requests) > requestThreshold {
|
||||||
}
|
// stats.Requests = stats.Requests[len(stats.Requests)-requestThreshold:]
|
||||||
|
|
||||||
// Add requested URI to the list of requests
|
// for _, uri := range stats.Requests {
|
||||||
stats.Requests = append(stats.Requests, ctx.URI())
|
// // Allow request
|
||||||
|
// if strings.Contains(uri, "/_/") || strings.Contains(uri, "/api/") || strings.Contains(uri, "/scripts") || strings.Contains(uri, "/service-worker") || strings.Contains(uri, "/images/") || strings.Contains(uri, "/favicon.ico") || strings.Contains(uri, "/extension/embed") {
|
||||||
|
// next()
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
if len(stats.Requests) > requestThreshold {
|
// // Allow logged in users
|
||||||
stats.Requests = stats.Requests[len(stats.Requests)-requestThreshold:]
|
// if ctx.HasSession() {
|
||||||
|
// user := utils.GetUser(ctx)
|
||||||
|
|
||||||
for _, uri := range stats.Requests {
|
// if user != nil {
|
||||||
// Allow request
|
// // Allow request
|
||||||
if strings.Contains(uri, "/_/") || strings.Contains(uri, "/api/") || strings.Contains(uri, "/scripts") || strings.Contains(uri, "/service-worker") || strings.Contains(uri, "/images/") || strings.Contains(uri, "/favicon.ico") || strings.Contains(uri, "/extension/embed") {
|
// next()
|
||||||
next()
|
// return
|
||||||
return
|
// }
|
||||||
}
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
// Allow logged in users
|
// // Disallow request
|
||||||
if ctx.HasSession() {
|
// request.Error("[guest]", ip, "BLOCKED BY FIREWALL", ctx.URI())
|
||||||
user := utils.GetUser(ctx)
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
if user != nil {
|
// // Allow the request if the number of requests done by the IP is below the threshold
|
||||||
// Allow request
|
// next()
|
||||||
next()
|
// }
|
||||||
return
|
// }
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Disallow request
|
|
||||||
request.Error("[guest]", ip, "BLOCKED BY FIREWALL", ctx.URI())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Allow the request if the number of requests done by the IP is below the threshold
|
|
||||||
next()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,27 +1,20 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
// // HTTPSRedirect middleware redirects to HTTPS if needed.
|
||||||
"fmt"
|
// func HTTPSRedirect() aero.Middleware {
|
||||||
"strings"
|
// 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/")
|
||||||
|
|
||||||
"github.com/aerogo/aero"
|
// 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)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
// HTTPSRedirect middleware redirects to HTTPS if needed.
|
// // Handle the request
|
||||||
func HTTPSRedirect() aero.Middleware {
|
// next()
|
||||||
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)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle the request
|
|
||||||
next()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user