Question: Validate IP Requests
You are developing a firewall software and need to block certain requests based on blacklisted IP addresses and request history.
There are two conditions for blocking a request:
Rule 1: Blacklist Match
- If the IP address matches any of the blacklisted IP patterns, the request should be blocked.
- A blacklisted IP pattern may contain a * wildcard, which matches zero or more characters.
Rule 2: Too Many Requests
- If an IP address has made two or more unblocked requests in the last 5 seconds, the request should be blocked.
You are given:
- A list of blacklisted_ips, where each element is a string (possibly with *)
- A list of requests, where each element is an IP string
- (the index represents the timestamp in seconds, i.e. requests[i] occurs at time i)
Task:
- Implement the following function:
validate_requests(blacklisted_ips, requests)
Return a list of integers where:
- 1 means the request is blocked
- 0 means the request is allowed
Example:
blacklisted_ips = ["45.", "123.45.67.89", ".100"]
requests = [
"45.1.2.3",
"123.45.67.89",
"99.99.99.100",
"10.10.10.10",
"10.10.10.10",
"10.10.10.10",
"10.10.10.10",
"99.99.99.100"
]
Output:
[1, 1, 1, 0, 0, 1, 0, 1]