90 lines
4.3 KiB
Python
90 lines
4.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Render the intended OpenWrt VLAN sandbox configuration for review.
|
|
|
|
This command only writes files in an explicitly supplied output directory. It
|
|
does not create guests, change bridges, or enable the existing OpenWrt path.
|
|
"""
|
|
import argparse
|
|
import ipaddress
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
SUBNETS = {10: ipaddress.ip_network('172.29.210.0/24'),
|
|
20: ipaddress.ip_network('172.29.220.0/24')}
|
|
BLOCKED_NETWORKS = ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16',
|
|
'100.64.0.0/10', '169.254.0.0/16']
|
|
|
|
|
|
def build_configs():
|
|
network = ["config interface 'loopback'", "\toption device 'lo'",
|
|
"\toption proto 'static'", "\toption ipaddr '127.0.0.1'",
|
|
"\toption netmask '255.0.0.0'", '',
|
|
"config interface 'wan'", "\toption device 'eth0'",
|
|
"\toption proto 'dhcp'", "\toption peerdns '0'",
|
|
"\tlist dns '1.1.1.1'", "\tlist dns '9.9.9.9'", '']
|
|
dhcp = ["config dnsmasq", "\toption domainneeded '1'",
|
|
"\toption boguspriv '1'", "\toption rebind_protection '1'",
|
|
"\toption leasefile '/tmp/dhcp.leases'",
|
|
"\toption resolvfile '/tmp/resolv.conf.d/resolv.conf.auto'", '']
|
|
firewall = ["config defaults", "\toption input 'REJECT'",
|
|
"\toption output 'ACCEPT'", "\toption forward 'REJECT'", '',
|
|
"config zone", "\toption name 'wan'", "\tlist network 'wan'",
|
|
"\toption input 'REJECT'", "\toption output 'ACCEPT'",
|
|
"\toption forward 'REJECT'", "\toption masq '1'", '',
|
|
"config rule", "\toption name 'Allow-WAN-DHCP'",
|
|
"\toption src 'wan'", "\toption proto 'udp'",
|
|
"\toption dest_port '68'", "\toption family 'ipv4'",
|
|
"\toption target 'ACCEPT'", '']
|
|
for vid, subnet in SUBNETS.items():
|
|
name = f'lab{vid}'
|
|
gateway = str(next(subnet.hosts()))
|
|
network += [f"config interface '{name}'", f"\toption device 'eth1.{vid}'",
|
|
"\toption proto 'static'", f"\toption ipaddr '{gateway}'",
|
|
f"\toption netmask '{subnet.netmask}'", '']
|
|
dhcp += [f"config dhcp '{name}'", f"\toption interface '{name}'",
|
|
"\toption start '100'", "\toption limit '100'",
|
|
"\toption leasetime '12h'", '']
|
|
firewall += ["config zone", f"\toption name '{name}'",
|
|
f"\tlist network '{name}'", "\toption input 'REJECT'",
|
|
"\toption output 'ACCEPT'", "\toption forward 'REJECT'", '',
|
|
"config rule", f"\toption name 'Allow-{name}-DNS-DHCP'",
|
|
f"\toption src '{name}'", "\toption proto 'tcp udp'",
|
|
"\toption dest_port '53 67'", "\toption family 'ipv4'",
|
|
"\toption target 'ACCEPT'", '']
|
|
for blocked in BLOCKED_NETWORKS:
|
|
firewall += ["config rule", f"\toption name 'Block-{name}-{blocked.replace('/', '-')}'",
|
|
f"\toption src '{name}'", "\toption dest 'wan'",
|
|
f"\toption dest_ip '{blocked}'", "\toption family 'ipv4'",
|
|
"\toption proto 'all'", "\toption target 'REJECT'", '']
|
|
firewall += ["config forwarding", f"\toption src '{name}'",
|
|
"\toption dest 'wan'", '']
|
|
return {'network': '\n'.join(network), 'dhcp': '\n'.join(dhcp),
|
|
'firewall': '\n'.join(firewall)}
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--output-dir', type=Path, required=True)
|
|
args = parser.parse_args()
|
|
try:
|
|
if not args.output_dir.is_dir():
|
|
raise ValueError('Output directory must already exist')
|
|
configs = build_configs()
|
|
paths = {name: args.output_dir / name for name in configs}
|
|
if any(path.exists() for path in paths.values()):
|
|
raise ValueError('Refusing to overwrite existing configuration files')
|
|
for name, content in configs.items():
|
|
paths[name].write_text(content, encoding='utf-8')
|
|
print(json.dumps({'output_dir': str(args.output_dir),
|
|
'files': list(configs), 'applied': False}, indent=2))
|
|
except (OSError, ValueError) as exc:
|
|
print(f'Network sandbox config: {exc}', file=sys.stderr)
|
|
return 1
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|