Documentation
¶
Overview ¶
Package ipfilter provides a simple package for checking GRPC/HTTP requests against a white/black list of IPs or CIDRs.
This package is a work in progress and makes no API stability promises.
Index ¶
- type Action
- type Filter
- func (f Filter) Check(ip net.IP) Action
- func (f Filter) Empty() bool
- func (f Filter) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (f Filter) StreamServerInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, ...) error
- func (f Filter) UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, ...) (interface{}, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Filter ¶
type Filter struct { // Wrapped http handler to control requests Wrapped http.Handler // Autocommented... AllowedIPs []net.IP DisallowedIPs []net.IP AllowedCIDRs []*net.IPNet DisallowedCIDRs []*net.IPNet // Policy is the action to take if no match Policy Action }
Filter is the main structure.
func Blacklist ¶
Blacklist creates a filter with the list of ips and/or cidrs passed. Returned filter has an allow policy and all ips and/or cidrs passed will be added to the disallowed lists. Strings that can't be parsed will be ignored.
func Whitelist ¶
Whitelist creates a filter with the list of ips and/or cidrs passed. Returned filter has a deny policy and all ips and/or cidrs passed will be added to the allowed lists. Strings that can't be parsed will be ignored.
Example ¶
package main import ( "fmt" "net" "github.com/luids-io/core/ipfilter" ) func main() { list := []string{ "192.168.1.0/24", "23.3.4.3", "127.0.0.0/8", } filter := ipfilter.Whitelist(list) ok := filter.Check(net.ParseIP("192.168.1.2")) if ok { fmt.Println("IP aceptada") } }
Output:
func (Filter) ServeHTTP ¶
func (f Filter) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements middleware for http servers.
func (Filter) StreamServerInterceptor ¶
func (f Filter) StreamServerInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error
StreamServerInterceptor implements middleware for grpc servers
func (Filter) UnaryServerInterceptor ¶
func (f Filter) UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)
UnaryServerInterceptor implements middleware for grpc servers.