|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import setup |
| 3 | +import subprocess |
| 4 | +import netfilterqueue |
| 5 | +import atexit |
| 6 | + |
| 7 | + |
| 8 | +class SNFQ: |
| 9 | + def __init__(self, process_packet_func, qnum=0, destination="forward", apache=True): |
| 10 | + self.qnum = qnum |
| 11 | + __class__.apache = apache |
| 12 | + if destination == "forward": |
| 13 | + subprocess.call("iptables -I FORWARD -j NFQUEUE --queue-num {}".format(self.qnum), shell=True) |
| 14 | + elif destination == "local": |
| 15 | + subprocess.call("iptables -I OUTPUT -j NFQUEUE --queue-num {}".format(self.qnum), shell=True) |
| 16 | + subprocess.call("iptables -I INPUT -j NFQUEUE --queue-num {}".format(self.qnum), shell=True) |
| 17 | + else: |
| 18 | + raise DestinationIncorrectException |
| 19 | + self.queue = netfilterqueue.NetfilterQueue() |
| 20 | + self.bind_queue(process_packet_func, qnum) |
| 21 | + if __class__.apache: |
| 22 | + self.apache_start() |
| 23 | + self.run_queue() |
| 24 | + |
| 25 | + def bind_queue(self, process_packet_func, qnum): |
| 26 | + self.queue.bind(qnum, process_packet_func) |
| 27 | + |
| 28 | + def run_queue(self): |
| 29 | + print("Running a Queue...") |
| 30 | + self.queue.run() |
| 31 | + |
| 32 | + @staticmethod |
| 33 | + def apache_start(): |
| 34 | + print("Starting apache2 service...") |
| 35 | + try: |
| 36 | + subprocess.check_output(["service", "apache2", "start"]) |
| 37 | + except subprocess.CalledProcessError: |
| 38 | + print("Installing and starting apache2 service...") |
| 39 | + subprocess.call("apt-get install apache2 -y", shell=True) |
| 40 | + subprocess.call("service apache2 start", shell=True) |
| 41 | + print("Completed.") |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + @atexit.register |
| 45 | + def exit(): |
| 46 | + # subprocess.call("clear", shell=True) |
| 47 | + print("Restoring normal connections...") |
| 48 | + subprocess.call("iptables --flush", shell=True) |
| 49 | + if __class__.apache: |
| 50 | + subprocess.call("service apache2 stop", shell=True) |
| 51 | + print("Quitting.") |
| 52 | + |
| 53 | + |
| 54 | +class DestinationIncorrectException(Exception): |
| 55 | + pass |
0 commit comments