-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import requests | ||
from sephiroth.providers.base_provider import BaseProvider | ||
|
||
|
||
class Cloudflare(BaseProvider): | ||
def __init__(self, excludeip6=False): | ||
self.source_ranges = self._get_ranges() | ||
self.processed_ranges = self._process_ranges(excludeip6) | ||
|
||
def _get_ranges(self): | ||
""" | ||
Input: None | ||
Output: Dict representation of cloudflare ip ranges | ||
""" | ||
print("(cloudflare) Fetching IP ranges from cloudflare's API") | ||
cloudflare_ip_ranges_url = "https://api.cloudflare.com/client/v4/ips" | ||
r = requests.get(cloudflare_ip_ranges_url) | ||
return r.json() | ||
|
||
def _process_ranges(self, excludeip6=False): | ||
""" | ||
Input: List of ip-ranges, optionally exclude ip6 ranges | ||
Output: Dict with header_comments and list of dicts for ip ranges | ||
""" | ||
header_comments = [ | ||
f"(cloudflare) success: {self.source_ranges['success']}", | ||
f"(cloudflare) messages: {self.source_ranges['messages']}", | ||
] | ||
out_ranges = [] | ||
ip_ranges = self.source_ranges["result"] | ||
|
||
range_types = [("ipv4_cidrs", "ipv4 cloudflare")] | ||
|
||
if not excludeip6: | ||
range_types.append(("ipv6_cidrs", "ipv6 cloudflare")) | ||
|
||
for range_type, comment in range_types: | ||
for ip_range in ip_ranges[range_type]: | ||
item = { | ||
"range": ip_range, | ||
"comment": comment | ||
} | ||
out_ranges.append(item) | ||
|
||
return {"header_comments": header_comments, "ranges": out_ranges} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters