Skip to content

Commit

Permalink
Linting.
Browse files Browse the repository at this point in the history
  • Loading branch information
geozeke committed Mar 12, 2024
1 parent a528c1d commit 5856802
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 42 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ cp sample-custom_blacklist.txt ./data/custom_blacklist.txt

The source database of banned IPs isn't perfect. You may determine that
there's an IP address you want to ban this is not found in `ipsum.txt`.
Also, the `ipsum.txt` file only contains IP addesses, and you may want
Also, the `ipsum.txt` file only contains IP addresses, and you may want
to ban an entire subnet. The custom blacklist allows you to capture
specific IP addresses or subnets (in [CIDR][def] format), one per line,
that you want to block. Some of your custom blacklist IPs may be found
Expand All @@ -174,24 +174,24 @@ blacklisted IPs, save a copy of it somewhere outside the repository.*
When you're done, the `~/banip/data` directory should look like this:

```text
├── data
│   ├── custom_blacklist.txt (optional)
│   ├── custom_whitelist.txt (optional)
│   ├── geolite (required)
│   │   ├── COPYRIGHT.txt
│   │   ├── GeoLite2-Country-Blocks-IPv4.csv
│   │   ├── GeoLite2-Country-Blocks-IPv6.csv
│   │   ├── GeoLite2-Country-Locations-de.csv
│   │   ├── GeoLite2-Country-Locations-en.csv
│   │   ├── GeoLite2-Country-Locations-es.csv
│   │   ├── GeoLite2-Country-Locations-fr.csv
│   │   ├── GeoLite2-Country-Locations-ja.csv
│   │   ├── GeoLite2-Country-Locations-pt-BR.csv
│   │   ├── GeoLite2-Country-Locations-ru.csv
│   │   ├── GeoLite2-Country-Locations-zh-CN.csv
│   │   └── LICENSE.txt
│   ├── ipsum.txt (required)
│   └── targets.txt (required)
data
├── custom_blacklist.txt (optional)
├── custom_whitelist.txt (optional)
├── geolite (required)
│   ├── COPYRIGHT.txt
│   ├── GeoLite2-Country-Blocks-IPv4.csv
│   ├── GeoLite2-Country-Blocks-IPv6.csv
│   ├── GeoLite2-Country-Locations-de.csv
│   ├── GeoLite2-Country-Locations-en.csv
│   ├── GeoLite2-Country-Locations-es.csv
│   ├── GeoLite2-Country-Locations-fr.csv
│   ├── GeoLite2-Country-Locations-ja.csv
│   ├── GeoLite2-Country-Locations-pt-BR.csv
│   ├── GeoLite2-Country-Locations-ru.csv
│   ├── GeoLite2-Country-Locations-zh-CN.csv
│   └── LICENSE.txt
├── ipsum.txt (required)
└── targets.txt (required)
```

## Running
Expand Down
6 changes: 3 additions & 3 deletions src/banip/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ def main() -> None:
)

subparsers = parser.add_subparsers(title="Commands")
msg = """Check to see if a single IP address is found in the
blacklist. Run \"banip check -h" for more."""
subparser_check = subparsers.add_parser(name="check", help=msg)
msg = """Create a list of banned (blacklisted) client IP addresses
to be used with a proxy server (like HAProxy) to block network
access from those clients. Run \"banip build -h" for more."""
subparser_build = subparsers.add_parser(name="build", help=msg)
msg = """Check to see if a single IP address is found in the
blacklist. Run \"banip check -h" for more."""
subparser_check = subparsers.add_parser(name="check", help=msg)

msg = """Output file that will contain the generated list of
blacklisted IP addresses. If not provided, results will be saved to
Expand Down
20 changes: 13 additions & 7 deletions src/banip/build_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,20 @@ def banned_ips(args: Namespace) -> None:
for ip in D[key]:
args.outfile.write(f"{format(ip)}\n")

now = dt.now().strftime("%Y-%m-%d %H:%M:%S")
args.outfile.write("\n# ------------custom entries -------------\n")
args.outfile.write(f"# Added on: {now}\n")
args.outfile.write("# ----------------------------------------\n\n")

custom_present = False
for key in c_keys:
for chunk in D[key]:
args.outfile.write(f"{format(chunk)}\n")
if len(D[key]) > 0:
custom_present = True
break

if custom_present:
now = dt.now().strftime("%Y-%m-%d %H:%M:%S")
args.outfile.write("\n# ------------custom entries -------------\n")
args.outfile.write(f"# Added on: {now}\n")
args.outfile.write("# ----------------------------------------\n\n")
for key in c_keys:
for chunk in D[key]:
args.outfile.write(f"{format(chunk)}\n")

# Save a copy of the generated IP blacklist to
# ./data/ip_blacklist.txt. This will be used when running banip to
Expand Down
48 changes: 35 additions & 13 deletions src/banip/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def tag_networks() -> None:
country_id = countries_D[int(net[2])]
ipv6_D[ipa.IPv6Network(net[0])] = country_id

print("\nGenerating files...", end="")
print("\nGenerating interim build products...", end="")
keys_4 = list(ipv4_D.keys())
keys_6 = list(ipv6_D.keys())
keys_4.sort()
Expand Down Expand Up @@ -298,18 +298,40 @@ def check_ip(ip: str) -> None:
def ip_in_network(
ip: Any,
networks: list[Any],
start: int,
finish: int,
first: int,
last: int,
) -> bool:
"""Do this."""
if start > finish:
"""Check if a single IP is in a list of networks.
This is a recursive binary search across a list of networks (either
all IPv4 or all IPv6) to see if a single IP address is contained in
any of the networks.
Parameters
----------
ip : Any
This will be either an IPv4 or IPv6 address, in ip_address()
format.
networks : list[Any]
This is a homogenous list of networks. The type of items in the
list with be either IPv4Network or IPv6Network.
first : int
The starting index in the binary search.
last : int
The ending index in the binary search.
Returns
-------
bool
True if ip is in any of the networks in the list; False
otherwise.
"""
if first > last:
return False
mid = (start + finish) // 2
# print(type(ip), type(networks[mid]))
if ip in networks[mid]:
mid = (first + last) // 2
clients = ipa.ip_network(networks[mid])
if ip in clients:
return True
inner_edge = ipa.ip_network(networks[mid])[0]
if ip < inner_edge:
return ip_in_network(ip, networks, start, mid - 1)
else:
return ip_in_network(ip, networks, mid + 1, finish)
if ip < clients[0]:
return ip_in_network(ip, networks, first, mid - 1)
return ip_in_network(ip, networks, mid + 1, last)

0 comments on commit 5856802

Please sign in to comment.