2021-08-05 20:26:48 +02:00
|
|
|
import click
|
2022-08-15 06:12:27 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from exec import run_root_cmd
|
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'])
|
|
|
|
|
2022-08-15 06:12:27 +02:00
|
|
|
logging.info("Enabling ipv4 forwarding with sysctl")
|
|
|
|
result = run_root_cmd([
|
2021-08-08 18:32:42 +02:00
|
|
|
'sysctl',
|
|
|
|
'net.ipv4.ip_forward=1',
|
|
|
|
])
|
2021-08-05 20:26:48 +02:00
|
|
|
if result.returncode != 0:
|
2022-08-15 06:12:27 +02:00
|
|
|
click.Abort('Failed to enable ipv4 forward via sysctl')
|
2021-08-05 20:26:48 +02:00
|
|
|
|
2022-08-15 06:12:27 +02:00
|
|
|
logging.info("Enabling ipv4 forwarding with iptables")
|
|
|
|
result = run_root_cmd([
|
2021-08-08 18:32:42 +02:00
|
|
|
'iptables',
|
|
|
|
'-P',
|
|
|
|
'FORWARD',
|
|
|
|
'ACCEPT',
|
|
|
|
])
|
2021-08-05 20:26:48 +02:00
|
|
|
if result.returncode != 0:
|
2022-08-15 06:12:27 +02:00
|
|
|
click.Abort('Failed set iptables rule')
|
2021-08-05 20:26:48 +02:00
|
|
|
|
2022-08-15 06:12:27 +02:00
|
|
|
logging.info("Enabling ipv4 NATting with iptables")
|
|
|
|
result = run_root_cmd([
|
2021-08-08 18:32:42 +02:00
|
|
|
'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:
|
2022-08-15 06:12:27 +02:00
|
|
|
click.Abort('Failed set iptables rule')
|
2021-08-05 20:26:48 +02:00
|
|
|
|
2022-08-15 06:12:27 +02:00
|
|
|
logging.info("Setting default route on device via ssh")
|
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:
|
2022-08-15 06:12:27 +02:00
|
|
|
click.Abort('Failed to add gateway over ssh')
|