Skip to content

Commit

Permalink
updates based on review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lekhan05 committed Jun 15, 2021
1 parent b6c77b7 commit 6d9164a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions orangemiddleware/ipfilter.go → middleware/ipfilter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package orangemiddleware
package middleware

import (
"net"
Expand All @@ -11,18 +11,18 @@ func IpFilter(handler http.Handler) http.Handler {
// go-chi's middleware/realip.go already parses for RealIp and xForwardedFor, further sets RemoteAddr to xForwardedFor ip if available.
ipAddress, _, splitHostPortError := net.SplitHostPort(r.RemoteAddr)
if splitHostPortError != nil {
w.WriteHeader(http.StatusUnauthorized)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}

parsedIp := net.ParseIP(ipAddress)
if parsedIp == nil {
w.WriteHeader(http.StatusUnauthorized)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}

if checkIfIpIsBlocked(parsedIp.String()) {
w.WriteHeader(http.StatusUnauthorized)
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
handler.ServeHTTP(w, r)
Expand Down
4 changes: 2 additions & 2 deletions views/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/go-chi/chi/middleware"
"github.com/go-chi/jwtauth"
"github.com/gorilla/csrf"
orangemiddleware "github.com/s-gv/orangeforum/middleware"
"github.com/s-gv/orangeforum/models"
"github.com/s-gv/orangeforum/orangemiddleware"
)

var tokenAuth *jwtauth.JWTAuth
Expand All @@ -29,10 +29,10 @@ func GetRouter() *chi.Mux {
// A good base middleware stack
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(orangemiddleware.IpFilter)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(30 * time.Second))
r.Use(orangemiddleware.IpFilter)

csrfMiddleware := csrf.Protect([]byte(SecretKey), csrf.Secure(false))
r.Use(csrfMiddleware)
Expand Down

0 comments on commit 6d9164a

Please sign in to comment.