#! /usr/bin/python3 # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # Copyright © 2019 Hugo Levy-Falk """ Creates the nat set. """ import logging import time from configparser import ConfigParser import netaddr from firewall import NAT CONFIG = ConfigParser() CONFIG.read('config.ini') def create_nat_adherent(): range_in = CONFIG['NAT']['range_in_adherent'] range_out = CONFIG['NAT']['range_out_adherent'] first_port = int(CONFIG['NAT']['first_port_adherent']) last_port = int(CONFIG['NAT']['last_port_adherent']) return NAT( 'adherent', range_in, range_out, first_port, last_port ) def create_nat_federez(): range_in = CONFIG['NAT']['range_in_federez'] range_out = CONFIG['NAT']['range_out_federez'] first_port = int(CONFIG['NAT']['first_port_federez']) last_port = int(CONFIG['NAT']['last_port_federez']) return NAT( 'federez', range_in, range_out, first_port, last_port ) def create_nat_aloes(): range_in = CONFIG['NAT']['range_in_aloes'] range_out = CONFIG['NAT']['range_out_aloes'] first_port = int(CONFIG['NAT']['first_port_aloes']) last_port = int(CONFIG['NAT']['last_port_aloes']) return NAT( 'aloes', range_in, range_out, first_port, last_port ) def create_nat_admin(): range_in = CONFIG['NAT']['range_in_admin'] range_out = CONFIG['NAT']['range_out_admin'] first_port = int(CONFIG['NAT']['first_port_admin']) last_port = int(CONFIG['NAT']['last_port_admin']) return NAT( 'admin', range_in, range_out, first_port, last_port ) def main(): nat_log = time.ctime() + "\n" logging.info("Creating adherent nat...") nat_adherent = create_nat_adherent() nat_log += "Adherents :\n" nat_log += nat_adherent.manage() logging.info("Done.") logging.info("Creating federez nat...") nat_federez = create_nat_federez() nat_log += "Federez :\n" nat_log += nat_federez.manage() logging.info("Done.") logging.info("Creating aloes nat...") aloes_nat = create_nat_aloes() nat_log += "Aloes :\n" nat_log += aloes_nat.manage() logging.info("Done.") logging.info("Creating admin nat...") admin_nat = create_nat_admin() nat_log += "Admin :\n" nat_log += admin_nat.manage() logging.info("Done.") logging.info("Saving nat table into /var/log/nat.log") with open('/var/log/nat.log', 'a') as f: f.write(nat_log) if __name__=='__main__': logging.info('Updating the NAT table.') main()