mirror of
https://github.com/aljazceru/njump.git
synced 2025-12-17 22:34:25 +01:00
a more stupid way of doing middle chaining, but at least it is clearer.
This commit is contained in:
4
block.go
4
block.go
@@ -8,7 +8,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func agentBlock(next http.Handler) http.Handler {
|
func agentBlock(next http.HandlerFunc) http.HandlerFunc {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
ua := r.Header.Get("User-Agent")
|
ua := r.Header.Get("User-Agent")
|
||||||
for _, bua := range []string{"Amazonbot", "semrush", "Bytespider", "AhrefsBot"} {
|
for _, bua := range []string{"Amazonbot", "semrush", "Bytespider", "AhrefsBot"} {
|
||||||
@@ -22,7 +22,7 @@ func agentBlock(next http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func cloudflareBlock(next http.Handler) http.Handler {
|
func cloudflareBlock(next http.HandlerFunc) http.HandlerFunc {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
ip := net.ParseIP(actualIP(r))
|
ip := net.ParseIP(actualIP(r))
|
||||||
if ip != nil {
|
if ip != nil {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
func loggingMiddleware(next http.Handler) http.Handler {
|
func loggingMiddleware(next http.HandlerFunc) http.HandlerFunc {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
path := r.URL.Path
|
path := r.URL.Path
|
||||||
if r.URL.RawQuery != "" {
|
if r.URL.RawQuery != "" {
|
||||||
|
|||||||
24
main.go
24
main.go
@@ -144,15 +144,21 @@ func main() {
|
|||||||
mux.HandleFunc("/{code}", renderEvent)
|
mux.HandleFunc("/{code}", renderEvent)
|
||||||
mux.HandleFunc("/{$}", renderHomepage)
|
mux.HandleFunc("/{$}", renderHomepage)
|
||||||
|
|
||||||
var mainHandler http.Handler = relay
|
corsH := cors.Default()
|
||||||
// apply http middlewares
|
corsM := func(next http.HandlerFunc) http.HandlerFunc {
|
||||||
for _, middleware := range []func(http.Handler) http.Handler{
|
return corsH.Handler(next).ServeHTTP
|
||||||
cors.Default().Handler,
|
}
|
||||||
loggingMiddleware,
|
|
||||||
agentBlock,
|
var mainHandler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
|
||||||
cloudflareBlock,
|
cloudflareBlock(
|
||||||
} {
|
agentBlock(
|
||||||
mainHandler = middleware(mainHandler)
|
loggingMiddleware(
|
||||||
|
corsM(
|
||||||
|
relay.ServeHTTP,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Print("listening at http://0.0.0.0:" + s.Port)
|
log.Print("listening at http://0.0.0.0:" + s.Port)
|
||||||
|
|||||||
Reference in New Issue
Block a user