firewall/nat.py

108 lines
2.8 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-05-06 21:01:59 +00:00
import os
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
2019-05-06 21:01:59 +00:00
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
2019-01-16 21:57:50 +00:00
CONFIG = ConfigParser()
2019-05-06 21:01:59 +00:00
CONFIG.read(os.path.join(BASE_DIR, '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 main():
2019-04-29 22:12:26 +00:00
ctime = time.ctime()
nat_log = 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.")
2019-03-12 21:06:21 +00:00
2019-04-29 22:12:26 +00:00
filename = "/var/log/nat-%s.log" % ctime
logging.info("Saving nat table into " + filename)
with open(filename, 'a') as f:
2019-03-30 17:33:06 +00:00
f.write(nat_log)
2019-03-12 21:06:21 +00:00
if __name__=='__main__':
logging.info('Updating the NAT table.')
main()