2021-08-05 20:26:48 +02:00
|
|
|
import click
|
|
|
|
import subprocess
|
2021-09-09 20:23:23 +02:00
|
|
|
from logger import logging
|
2021-10-01 12:31:08 +02:00
|
|
|
from ssh import run_ssh_command
|
2021-09-29 23:49:46 +02:00
|
|
|
from wrapper import check_programs_wrap
|
2021-08-05 20:26:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
@click.command(name='forwarding')
|
2021-09-09 20:23:23 +02:00
|
|
|
def cmd_forwarding():
|
2022-02-13 19:57:04 +01:00
|
|
|
"""Enable network forwarding for a usb-attached device"""
|
2021-09-29 23:49:46 +02:00
|
|
|
check_programs_wrap(['syctl', 'iptables'])
|
|
|
|
|
2021-08-08 18:32:42 +02:00
|
|
|
result = subprocess.run([
|
|
|
|
'sysctl',
|
|
|
|
'net.ipv4.ip_forward=1',
|
|
|
|
])
|
2021-08-05 20:26:48 +02:00
|
|
|
if result.returncode != 0:
|
|
|
|
logging.fatal(f'Failed to enable ipv4 forward via sysctl')
|
|
|
|
exit(1)
|
|
|
|
|
2021-08-08 18:32:42 +02:00
|
|
|
result = subprocess.run([
|
|
|
|
'iptables',
|
|
|
|
'-P',
|
|
|
|
'FORWARD',
|
|
|
|
'ACCEPT',
|
|
|
|
])
|
2021-08-05 20:26:48 +02:00
|
|
|
if result.returncode != 0:
|
|
|
|
logging.fatal(f'Failed set iptables rule')
|
|
|
|
exit(1)
|
|
|
|
|
2021-08-08 18:32:42 +02:00
|
|
|
result = subprocess.run([
|
|
|
|
'iptables',
|
|
|
|
'-A',
|
|
|
|
'POSTROUTING',
|
|
|
|
'-t',
|
|
|
|
'nat',
|
|
|
|
'-j',
|
|
|
|
'MASQUERADE',
|
|
|
|
'-s',
|
|
|
|
'172.16.42.0/24',
|
|
|
|
])
|
2021-08-05 20:26:48 +02:00
|
|
|
if result.returncode != 0:
|
|
|
|
logging.fatal(f'Failed set iptables rule')
|
|
|
|
exit(1)
|
|
|
|
|
2021-10-01 12:31:08 +02:00
|
|
|
result = run_ssh_command(cmd=['sudo -S route add default gw 172.16.42.2'])
|
2021-08-05 20:26:48 +02:00
|
|
|
if result.returncode != 0:
|
|
|
|
logging.fatal(f'Failed to add gateway over ssh')
|
|
|
|
exit(1)
|