Junior — Senior
Firewall with IP address verification
livecode
Task condition
It is necessary to implement a program that simulates the operation of a firewall. The program should accept a set of rules, where each rule consists of a CIDR block and an action ("ALLOW" or "DENY"). The main function should check whether a given IP address is allowed according to these rules, returning true for allowed and false for denied. It should also correctly handle invalid CIDR blocks and unknown actions.
package main
import (
"fmt"
)
// Function to check if an IP address is allowed
func IsAllowed(rules [][2]string, ip string) bool {
// **Insert IP address check code according to rules**
return false
}
func main() {
// **Example input data:**
// **Example 1:**
rules1 := [][2]string{
{"192.168.1.0/24", "ALLOW"},
{"10.0.0.0/16", "DENY"},
{"8.8.8.8", "ALLOW"},
}
ip1 := "192.168.1.10" // expected true
// **Example 2:**
rules2 := [][2]string{
{"192.168.1.0/24", "ALLOW"},
{"10.0.0.0/16", "DENY"},
{"8.8.8.8", "ALLOW"},
}
ip2 := "10.0.0.10" // expected false
// **Example 3:**
rules3 := [][2]string{
{"192.168.1.0/24", "ALLOW"},
{"10.0.0.0/16", "DENY"},
{"8.8.8.8", "ALLOW"},
}
ip3 := "192.168.2.10" // expected false
processExample(rules1, ip1)
processExample(rules2, ip2)
processExample(rules3, ip3)
}
func processExample(rules [][2]string, ip string) {
if IsAllowed(rules, ip) {
fmt.Println("true")
} else {
fmt.Println("false")
}
}