<commit_before>#!/usr/bin/python

from ansible.module_utils.basic import *

class UFWForwards(object):
    def __init__(self, data, ipv6):
        self.ufw_chain = "ufw6" if ipv6 else 'ufw'

        self.nat_rules = []
        self.filter_rules = []
        
        self.reroute_data = []
        self.port_forward_data = []

        args = [
           'incomming_dev', 
           'incomming_network', 
           'outgoing_dev', 
           'outgoing_network', 
           'masquerading', 
           'conntrack_state'
        ]

        if 'reroute' in data:
            self.reroute_data = data['reroute'] 
        else:
            self.reroute_data = []
        
        if 'forwards' in data:
            self.port_forward_data = data['forwards'] 
        else:
            self.port_forward_data = [] 

        for arg in args:
            if arg in data:
                setattr(self, arg, data[arg])
            else:
                setattr(self, arg, None)
        
    def generate(self):
        if self.masquerading:
            for item in self.reroute_data:
                self._reroute_generate(item)

        for item in self.port_forward_data:
            self._port_forward(item)

        if self.masquerading:
            self._masquerade_generate()
        self._forward_generate()
    
    def _port_forward(self, item):
        for k in ['destination_port', 'incomming_port', 'protocol']:
            if k not in item:
                item[k] = [None]
            if not isinstance(item[k], list):
                item[k] = [str(item[k])] 

        if item['incomming_port'][0] is None and len(item['destination_port']) > 1:
            item['incomming_port'] = [None for i in range(len(item['destination_port']))]

        for protocol in item['protocol']:
            ports = zip(item['incomming_port'], item['destination_port'])
            for in_port, dport in ports:
                print(item, protocol, dport)
                self._port_forward_generate(item, protocol, dport)
                if self.masquerading:
                    self._port_forward_dnat_generate(item, protocol, in_port, dport)


    def _reroute_generate(self, item):
        rule = ["-A", "POSTROUTING"]

        if self.incomming_dev:
            rule += ['-o', self.incomming_dev]
        if 'source_ip' in item:
            rule += ['-s', item['source_ip']]
        
        rule += ['-j', 'SNAT']
        rule += ["--to-source", item['routed_ip']]
        if rule not in self.nat_rules:
            self.nat_rules.append(rule)

    def _port_forward_dnat_generate(self, item, protocol, in_port, dport):<commit_msg>Removed debug echo in library<commit_after>#!/usr/bin/python

from ansible.module_utils.basic import *

class UFWForwards(object):
    def __init__(self, data, ipv6):
        self.ufw_chain = "ufw6" if ipv6 else 'ufw'

        self.nat_rules = []
        self.filter_rules = []
        
        self.reroute_data = []
        self.port_forward_data = []

        args = [
           'incomming_dev', 
           'incomming_network', 
           'outgoing_dev', 
           'outgoing_network', 
           'masquerading', 
           'conntrack_state'
        ]

        if 'reroute' in data:
            self.reroute_data = data['reroute'] 
        else:
            self.reroute_data = []
        
        if 'forwards' in data:
            self.port_forward_data = data['forwards'] 
        else:
            self.port_forward_data = [] 

        for arg in args:
            if arg in data:
                setattr(self, arg, data[arg])
            else:
                setattr(self, arg, None)
        
    def generate(self):
        if self.masquerading:
            for item in self.reroute_data:
                self._reroute_generate(item)

        for item in self.port_forward_data:
            self._port_forward(item)

        if self.masquerading:
            self._masquerade_generate()
        self._forward_generate()
    
    def _port_forward(self, item):
        for k in ['destination_port', 'incomming_port', 'protocol']:
            if k not in item:
                item[k] = [None]
            if not isinstance(item[k], list):
                item[k] = [str(item[k])] 

        if item['incomming_port'][0] is None and len(item['destination_port']) > 1:
            item['incomming_port'] = [None for i in range(len(item['destination_port']))]

        for protocol in item['protocol']:
            ports = zip(item['incomming_port'], item['destination_port'])
            for in_port, dport in ports:
                self._port_forward_generate(item, protocol, dport)
                if self.masquerading:
                    self._port_forward_dnat_generate(item, protocol, in_port, dport)


    def _reroute_generate(self, item):
        rule = ["-A", "POSTROUTING"]

        if self.incomming_dev:
            rule += ['-o', self.incomming_dev]
        if 'source_ip' in item:
            rule += ['-s', item['source_ip']]
        
        rule += ['-j', 'SNAT']
        rule += ["--to-source", item['routed_ip']]
        if rule not in self.nat_rules:
            self.nat_rules.append(rule)

    def _port_forward_dnat_generate(self, item, protocol, in_port, dport):
{'fraction_non_alphanumeric': 0.11478859766596518, 'fraction_numerical': 0.001913143294432753, 'mean_word_length': 1.8077336197636948, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2721616', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1566, 'n_words': 342}