in client/blocky.py [0:0]
def getbans(chain = 'INPUT'):
""" Gets a list of all bans in a chain """
banlist = []
# Get IPv4 list
for i in range(0,MAX_IPTABLES_TRIES):
try:
out = subprocess.check_output([IPTABLES_EXEC, '--list', chain, '-n', '--line-numbers'], stderr = subprocess.STDOUT)
except subprocess.CalledProcessError as err:
if 'you must be root' in err.output:
print("Looks like blocky doesn't have permission to access iptables, giving up completely! (are you running as root?)")
sys.exit(-1)
time.sleep(1) # write lock, probably
if out:
for line in out.split("\n"):
m = re.match(r"^(\d+)\s+([A-Z]+)\s+(all|tcp|udp)\s+(\S+)\s+([0-9a-f.:/]+)\s+([0-9a-f.:/]+)\s*(.*?)$", line)
if m:
ln = m.group(1)
action = m.group(2)
protocol = m.group(3)
option = m.group(4)
source = m.group(5)
destination = m.group(6)
extensions = m.group(7)
entry = {
'chain': chain,
'linenumber': ln,
'action': action,
'protocol': protocol,
'option': option,
'source': source,
'destination': destination,
'extensions': extensions,
}
banlist.append(entry)
break
# Get IPv6 list
if not os.path.exists(IP6TABLES_EXEC):
return banlist
for i in range(0,MAX_IPTABLES_TRIES):
try:
out = subprocess.check_output([IP6TABLES_EXEC, '--list', chain, '-n', '--line-numbers'], stderr = subprocess.STDOUT)
except subprocess.CalledProcessError as err:
if 'you must be root' in err.output:
print("Looks like blocky doesn't have permission to access iptables, giving up completely! (are you running as root?)")
sys.exit(-1)
time.sleep(1) # write lock, probably
if out:
for line in out.split("\n"):
# Unlike ipv4 iptables, the 'option' thing is blank here, so omit it
m = re.match(r"^(\d+)\s+([A-Z]+)\s+(all|tcp|udp)\s+([0-9a-f.:/]+)\s+([0-9a-f.:/]+)\s*(.*?)$", line)
if m:
ln = m.group(1)
action = m.group(2)
protocol = m.group(3)
source = m.group(4)
destination = m.group(5)
extensions = m.group(6)
entry = {
'chain': chain,
'linenumber': ln,
'action': action,
'protocol': protocol,
'option': '---',
'source': source,
'destination': destination,
'extensions': extensions,
}
banlist.append(entry)
break
return banlist