110 lines
3 KiB
Python
110 lines
3 KiB
Python
#! /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 <http://www.gnu.org/licenses/>.
|
|
|
|
# Copyright © 2019 Hugo Levy-Falk <me@klafyvel.me>
|
|
|
|
"""
|
|
Creates the nat set.
|
|
"""
|
|
|
|
import logging
|
|
import time
|
|
import os
|
|
from configparser import ConfigParser
|
|
|
|
import netaddr
|
|
|
|
from firewall import NAT
|
|
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
CONFIG = ConfigParser()
|
|
CONFIG.read(os.path.join(BASE_DIR, '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 main():
|
|
ctime = time.ctime()
|
|
nat_log = 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.")
|
|
|
|
nat_directory = "/var/log/nat/"
|
|
if not os.path.exists(nat_directory):
|
|
os.makedirs(nat_directory)
|
|
filename = os.path.join(nat_directory, "nat_%s.log" % time.strftime('%Y_%m_%d_%Hh_%Mm'))
|
|
|
|
logging.info("Saving nat table into " + filename)
|
|
with open(filename, 'a') as f:
|
|
f.write(nat_log)
|
|
|
|
|
|
if __name__=='__main__':
|
|
logging.info('Updating the NAT table.')
|
|
main()
|