make a firewall in go that blocks all http requests appart from ones from cloudflares ips
- User
make a firewall in go that blocks all http requests appart from ones from cloudflares ips
- Assistant
To create a firewall in Go that blocks all HTTP requests except for the ones from Cloudflare's IPs, you can use the following code as a starting point.
Please note that this code only demonstrates the basic structure and logic of how you can implement a firewall. It may require modifications and additional validations based on your specific needs and environment.
package main import ( "fmt" "net/http" "strings" ) // List of Cloudflare IPs var allowedIPs = []string{ "103.21.244.0/22", "103.22.200.0/22", // Add more Cloudflare IPs here } // Middleware to check if the request is from a Cloudflare IP func cloudflareMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ip := getIPAddress(r) if !isCloudflareIP(ip) { w.WriteHeader(http.StatusForbidden) return } // Pass the request to the next handler next.ServeHTTP(w, r) }) } // Function to get the client's IP address from the request func getIPAddress(r *http.Request) string { ip := r.Header.Get("CF-Connecting-IP") if ip == "" { ip = strings.Split(r.RemoteAddr, ":")[0] } return ip } // Function to check if the given IP is a Cloudflare IP func isCloudflareIP(ip string) bool { for _, cidr := range allowedIPs { if isIPInCIDR(ip, cidr) { return true } } return false } // Function to check if an IP address is within the given CIDR range func isIPInCIDR(ip, cidr string) bool { ipAddr := parseIP(ip) _, ipNet, _ := net.ParseCIDR(cidr) return ipNet.Contains(ipAddr) } // Function to parse the IP address string to net.IP type func parseIP(ip string) net.IP { parsedIP := net.ParseIP(ip) if parsedIP == nil { parsedIP = net.ParseIP(strings.Split(ip, ":")[0]) } return parsedIP } // Example handler to handle valid requests func myHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, this is a valid request!") } func main() { // Create a new router router := http.NewServeMux() // Attach the middleware router.Handle("/", cloudflareMiddleware(http.HandlerFunc(myHandler))) // Start the server http.ListenAndServe(":8080", router) }In this code, we define a list of Cloudflare IPs in the
allowedIPsvariable. ThecloudflareMiddlewarefunction is used as a middleware to check if the request's IP is from Cloudflare. If it is, the request is passed to the next handler (in this case,myHandler). If it's not, a403 Forbiddenresponse is sent.The
getIPAddressfunction extracts the client's IP address from the request by first checking theCF-Connecting-IPheader. If it's not present (when running locally or outside of Cloudflare), it falls back to getting the IP from theRemoteAddrfield.The
isCloudflareIPfunction checks if the given IP is present in the list of allowed IPs.The
isIPInCIDRfunction uses thenetpackage to parse the IP and CIDR strings and checks if the IP is within the CIDR range.The
parseIPfunction attempts to parse the IP address string and handles cases where the IP contains a port number.The example
myHandlerfunction is a placeholder for any valid request handler logic you would like to implement.Finally, we create a new router using
http.NewServeMux, attach thecloudflareMiddlewareto the root path, and start the server usinghttp.ListenAndServe.Remember to update the
allowedIPslist with the actual Cloudflare IPs before deploying this code in a production environment.