firewall/nat.py

121 lines
3.2 KiB
Python
Raw Normal View History

2019-01-16 21:57:50 +00:00
#! /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
2019-03-30 17:33:06 +00:00
import time
2019-01-16 21:57:50 +00:00
from configparser import ConfigParser
import netaddr
2019-03-26 21:02:43 +00:00
from firewall import NAT
2019-01-16 21:57:50 +00:00
CONFIG = ConfigParser()
2019-03-31 18:17:31 +00:00
CONFIG.read('/usr/local/firewall/config.ini')
2019-01-16 21:57:50 +00:00
def create_nat_adherent():
2019-03-26 21:02:43 +00:00
range_in = CONFIG['NAT']['range_in_adherent']
range_out = CONFIG['NAT']['range_out_adherent']
2019-03-12 21:06:21 +00:00
first_port = int(CONFIG['NAT']['first_port_adherent'])
last_port = int(CONFIG['NAT']['last_port_adherent'])
2019-03-26 21:02:43 +00:00
return NAT(
2019-01-16 21:57:50 +00:00
'adherent',
range_in,
range_out,
first_port,
last_port
)
def create_nat_federez():
2019-03-26 21:02:43 +00:00
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(
2019-01-16 21:57:50 +00:00
'federez',
range_in,
range_out,
first_port,
last_port
)
def create_nat_aloes():
2019-03-26 21:02:43 +00:00
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(
2019-01-16 21:57:50 +00:00
'aloes',
range_in,
range_out,
first_port,
last_port
)
def create_nat_admin():
2019-03-26 21:02:43 +00:00
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(
2019-01-16 21:57:50 +00:00
'admin',
range_in,
range_out,
first_port,
last_port
)
def main():
2019-03-30 17:33:06 +00:00
nat_log = time.ctime() + "\n"
2019-01-16 21:57:50 +00:00
logging.info("Creating adherent nat...")
2019-03-26 21:02:43 +00:00
nat_adherent = create_nat_adherent()
2019-03-30 17:33:06 +00:00
nat_log += "Adherents :\n"
nat_log += nat_adherent.manage()
2019-03-26 21:02:43 +00:00
logging.info("Done.")
logging.info("Creating federez nat...")
nat_federez = create_nat_federez()
2019-03-30 17:33:06 +00:00
nat_log += "Federez :\n"
nat_log += nat_federez.manage()
2019-03-26 21:02:43 +00:00
logging.info("Done.")
logging.info("Creating aloes nat...")
aloes_nat = create_nat_aloes()
2019-03-30 17:33:06 +00:00
nat_log += "Aloes :\n"
nat_log += aloes_nat.manage()
2019-03-26 21:02:43 +00:00
logging.info("Done.")
logging.info("Creating admin nat...")
admin_nat = create_nat_admin()
2019-03-30 17:33:06 +00:00
nat_log += "Admin :\n"
nat_log += admin_nat.manage()
2019-01-16 21:57:50 +00:00
logging.info("Done.")
2019-03-12 21:06:21 +00:00
2019-03-30 17:33:06 +00:00
logging.info("Saving nat table into /var/log/nat.log")
with open('/var/log/nat.log', 'a') as f:
f.write(nat_log)
2019-03-12 21:06:21 +00:00
if __name__=='__main__':
logging.info('Updating the NAT table.')
main()