-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.py
36 lines (30 loc) · 1.06 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3
"""Some general purpose utilities."""
import socket
import re
class f2bcUtils:
"""Utility functions for fail2ban-cluster."""
# valid IPv4 IP address?
def valid_ipv4(address):
try:
socket.inet_aton(address)
return True
except Exception:
return False
# valid jailname? must only contain a-z,A-Z,0-9 and _-
# TODO: verify fail2ban jailname constraints, including length
def valid_jailname(jailname):
match = re.match("^[a-zA-Z0-9_-]*$", jailname)
return match is not None
def is_valid_hostname(hostname):
if len(hostname) > 255:
return False
if hostname[-1] == ".":
# strip exactly one dot from the right, if present
hostname = hostname[:-1]
allowed = re.compile("(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
return all(allowed.match(x) for x in hostname.split("."))
def is_valid_action(action):
if not action.lower() in ['ban', 'unban']:
return False
return True