// Wrap the firewall around an HTTP handler function
Wrap the firewall around an HTTP handler function
// Wrap the firewall around an HTTP handler function
func (fw *Firewall) Wrap(h func(http.ResponseWriter, *http.Request)) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		srcIP := net.ParseIP(strings.Split(r.RemoteAddr, ":")[0])
		rule, hasRule := fw.Rules.PathToNetblocks[r.URL.Path]
		authorized := (hasRule && IPIsTrusted(rule, srcIP)) || (fw.Rules.FailOpen)
		if !authorized {
			log.Println(fmt.Sprintf("[FIREWALL] blocked request from %s for %s", srcIP.String(), r.URL.Path))
			http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
			return
		}
		h(w, r)
	})
}
func ( fw * Firewall ) Wrap ( h func ( http . ResponseWriter , * http . Request ) ) http . Handler { return http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) { srcIP := net . ParseIP ( strings . Split ( r . RemoteAddr , " " ) [ 0 ] ) rule , hasRule := fw . Rules . PathToNetblocks [ r . URL . Path ] authorized := ( hasRule && IPIsTrusted ( rule , srcIP ) ) || ( fw . Rules . FailOpen ) if ! authorized { log . Println ( fmt . Sprintf ( " " , srcIP . String ( ) , r . URL . Path ) ) http . Error ( w , http . StatusText ( http . StatusForbidden ) , http . StatusForbidden ) return } h ( w , r ) } ) }
Wrap the firewall around an HTTP handler function
Wrap the firewall around an HTTP handler function
// extract IP from http.Request // get rule for path