大猩猩/多路复用器中间件未受到攻击

时间:2018-12-19 06:56:19

标签: go mux

我在go version go1.10.4 linux/amd64

我正在注册中间件,但是它们似乎没有受到攻击。

package main

import (
    "encoding/json"
    "fmt"
    "github.com/dgrijalva/jwt-go"
    "github.com/gorilla/context"
    "github.com/mitchellh/mapstructure"
    "huru/migrations"
    "huru/models"
    "huru/models/person"
    "huru/routes"
    "net/http"
    "os"

    "github.com/gorilla/mux"
    _ "github.com/lib/pq"
    log "github.com/sirupsen/logrus"
)

func loggingMiddleware(next http.Handler) http.Handler {

    log.Println("logging middleware registered");

    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        // Do stuff here
        log.Println("Here is the request URI:",r.RequestURI)
        // Call the next handler, which can be another middleware in the chain, or the final handler.
        next.ServeHTTP(w, r)
    })
}

type Exception struct {
    Message string `json:"message"`
}

func authMiddleware(next http.Handler) http.Handler {

    log.Println("auth middleware registered");

    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

        params := r.URL.Query()
        fmt.Println("the params are:", params);

        token, _ := jwt.Parse(params["token"][0], func(token *jwt.Token) (interface{}, error) {
            if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
                return nil, fmt.Errorf("there was an error")
            }
            return []byte("secret"), nil
        })
        claims, ok := token.Claims.(jwt.MapClaims)

        if ! (ok && token.Valid) {
            json.NewEncoder(w).Encode(Exception{Message: "Invalid authorization token"})
            return;
        }

        var user person.Model
        mapstructure.Decode(claims, &user)
        context.Set(r, "logged_in_user", user)
        next.ServeHTTP(w, r)
    })
}

func errorMiddleware(next http.Handler) http.Handler {

    log.Println("error handling middleware registered");

    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        defer func() {
            if err := recover(); err != nil {

                log.Error("Caught error in defer/recover middleware: ", err)
                originalError := err.(struct{ OriginalError error }).OriginalError

                if originalError != nil {
                    log.Error("Original error in defer/recover middleware: ", originalError)
                }

                statusCode := err.(struct{ StatusCode int }).StatusCode

                if statusCode != 0 {
                    w.WriteHeader(statusCode)
                } else {
                    w.WriteHeader(http.StatusInternalServerError)
                }

                message := err.(struct{ Message string }).Message

                if message == "" {
                    message = "Unknown error message."
                }

                json.NewEncoder(w).Encode(struct {
                    ID string
                }{
                    message,
                })
            }
        }()
        next.ServeHTTP(w, r)
    })
}

func main() {

    routerParent := mux.NewRouter()
    routerParent.Use(loggingMiddleware)
    routerParent.Use(errorMiddleware)
    routerParent.Use(authMiddleware)

    router := routerParent.PathPrefix("/api/v1").Subrouter();
    router.Use(loggingMiddleware)
    router.Use(errorMiddleware)
    router.Use(authMiddleware)


    // register and login
    {
        handler := routes.LoginHandler{}
        subRouter := router.PathPrefix("/").Subrouter()
        handler.Mount(subRouter, struct{}{});
    }

    {

        handler := routes.RegisterHandler{}
        subRouter := router.PathPrefix("/").Subrouter()
        handler.Mount(subRouter, struct{}{})
    }

    {
        // people
        handler := routes.PersonHandler{}
        subRouter := router.PathPrefix("/").Subrouter()
        subRouter.Use(authMiddleware)
        handler.Mount(subRouter, routes.PersonInjection{People: models.PersonInit()})
    }

     // ...
}

没有一个被记录:

log.Println("error handling middleware registered");
log.Println("auth middleware registered");
log.Println("logging middleware registered");

在运行时,中间件路由似乎都没有受到攻击,那里没有任何记录。有人知道为什么会这样吗?

请注意,我不需要所有这些内容:

    routerParent := mux.NewRouter()
    routerParent.Use(loggingMiddleware)
    routerParent.Use(errorMiddleware)
    routerParent.Use(authMiddleware)

    router := routerParent.PathPrefix("/api/v1").Subrouter();
    router.Use(loggingMiddleware)
    router.Use(errorMiddleware)
    router.Use(authMiddleware)

实际上我可能只想要:

    routerParent := mux.NewRouter()
    router := routerParent.PathPrefix("/api/v1").Subrouter();
    router.Use(loggingMiddleware)
    router.Use(errorMiddleware)
    router.Use(authMiddleware)

但是它只是用来证明某些问题。在主要功能的最后,我有这个来启动服务器:

host := os.Getenv("huru_api_host")
port := os.Getenv("huru_api_port")

if host == "" {
    host = "localhost"
}

if port == "" {
    port = "80"
}

log.Info(fmt.Sprintf("Huru API server listening on port %s", port))
path := fmt.Sprintf("%s:%s", host, port)
log.Fatal(http.ListenAndServe(path, routerParent))

0 个答案:

没有答案