Skip to content

Commit

Permalink
Add support for cloudflare
Browse files Browse the repository at this point in the history
  • Loading branch information
willk authored and 0xdade committed May 7, 2024
1 parent 72ecf6a commit 6e066b5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions sephiroth/providers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sephiroth.providers.tor import Tor
from sephiroth.providers.do import DO
from sephiroth.providers.linode import Linode
from sephiroth.providers.cloudflare import Cloudflare

# This must be imported last, circular dependencies and all that
from sephiroth.providers.provider import Provider
Expand All @@ -22,4 +23,5 @@
"DO",
"Linode",
"Provider",
"Cloudflare",
]
45 changes: 45 additions & 0 deletions sephiroth/providers/cloudflare.py
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}
3 changes: 2 additions & 1 deletion sephiroth/providers/provider.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sephiroth.providers import AWS, Azure, GCP, OCI, ASN, File, Tor, DO, Linode
from sephiroth.providers import AWS, Azure, GCP, OCI, ASN, File, Tor, DO, Linode, Cloudflare

classmap = {
"aws": AWS,
Expand All @@ -10,6 +10,7 @@
"tor": Tor,
"do": DO,
"linode": Linode,
"cloudflare": Cloudflare,
}


Expand Down

0 comments on commit 6e066b5

Please sign in to comment.