aboutsummaryrefslogtreecommitdiff
path: root/linux/reroute.py
blob: 2e0064773765037737a41a73a93731a97f68297d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3

DESCRIPTION = """
This script runs a command with a different network configuration using firejail.

THIS MIGHT BE UNSAFE. USE AT YOUR OWN CAUTION:

* Input might not be correctly validated.
* Use of firejail might not be correct.
"""

import argparse
import pathlib
import shlex
import subprocess
import tempfile
import textwrap


def main():
    parser = argparse.ArgumentParser(description=DESCRIPTION)

    parser.add_argument("network_interface")
    parser.add_argument("dns")
    parser.add_argument("gateway")
    parser.add_argument("ip")
    parser.add_argument("command", nargs="+")

    parser.add_argument("--route", action="append", help="destination,gateway")

    args = parser.parse_args()

    routes = "\n".join([_make_route(r) for r in args.route])

    command = shlex.join(args.command)

    with tempfile.TemporaryDirectory() as tempdir:
        script = pathlib.Path(tempdir) / "script"

        script.write_text(textwrap.dedent(
            f"""
            #!/bin/sh

            {routes}
            {command}
            """
        ).lstrip())
        script.chmod(0o555)
        command = ["sudo", "firejail", f"--net={args.network_interface}", f"--dns={args.dns}", f"--defaultgw={args.gateway}", f"--ip={args.ip}", f"--whitelist={script}", "--", script]

        subprocess.run(command, check=True)


def _make_route(argument):
    destination, gateway = argument.split(",")
    return shlex.join(["ip", "route", "add", destination, "via", gateway, "dev", "eth0"])

if __name__ == "__main__":
    main()