Skip to content

Instantly share code, notes, and snippets.

@iagoalonsomrf
Last active July 28, 2023 09:57
Show Gist options
  • Save iagoalonsomrf/0e20388444d5fe4ef23ed5968767a954 to your computer and use it in GitHub Desktop.
Save iagoalonsomrf/0e20388444d5fe4ef23ed5968767a954 to your computer and use it in GitHub Desktop.
import re
COMMENT_RE = r'(route\:allow|allow)\s(tcp|udp)\s([0-9]{1,4})\s((?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9]|)\d)\.?\b){4}/[0-9]{1,2})[\sa-zA-Z]+((?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9]|)\d)\.?\b){4})' # noqa: E501
def main():
with open('diff.txt', 'r') as f:
data = [line.strip() for line in f.readlines()]
for line in data:
action = None
if line.startswith('-### tuple ###'):
action = '-'
elif line.startswith('+### tuple ###'):
action = '+'
if action is not None:
comment = bytes.fromhex(line.split('comment=')[-1]).decode('utf-8')
try:
type_, protocol, port, _, ip = re.compile(COMMENT_RE).findall(line)[0]
print(f'{action} {"forward" if "route" in type_ else "allow "} {protocol} {port.ljust(6, " ")} {ip.ljust(16, " ")} {comment}') # noqa: E501
except IndexError:
print('Cannot parse IPV6 yet')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment