~mser/pkg.mser.at

585cfb360a510528f5a48151c45cb1c9d3a8637a — cryzed 2 years ago 8b7d308
unshare-net: parse mount tab manually
2 files changed, 16 insertions(+), 9 deletions(-)

M packages/unshare-net/PKGBUILD
M packages/unshare-net/unshare-net
M packages/unshare-net/PKGBUILD => packages/unshare-net/PKGBUILD +3 -3
@@ 1,14 1,14 @@
# Maintainer: Michael Serajnik <m at mser dot at>
pkgname=unshare-net
pkgver=9
pkgrel=2
pkgver=10
pkgrel=1
pkgdesc="Selectively whitelist traffic to specified IPs and domains for target applications"
arch=("any")
url="https://git.sr.ht/~mser/pkg.mser.at/tree/master/item/packages/unshare-net"
license=("AGPL3")
depends=("python")
source=("unshare-net")
sha512sums=('6c0bbaaf70c9fbdab5489d8076a6555e2318fdb2637c28133d3f30d47203bdcc2e08e273404b2809a0cdd99a10b36904890389b31f46d5ff888eb90ff45e397d')
sha512sums=('0ec67b9a2706dbb386d428d4bf534fc74eb044cc1fb1db3875cb2ba205ec17bbb8824023617eb6c5c56a869c688fa51babc6bd3a62ddc9377d62656e761a43eb')

package() {
  install -D --mode 755 "${srcdir}/unshare-net" --target-directory "${pkgdir}/usr/bin"

M packages/unshare-net/unshare-net => packages/unshare-net/unshare-net +13 -6
@@ 31,6 31,7 @@ import time
import typing as T

IPTABLES_MAX_CHAIN_NAME_LENGTH = 28
MOUNT_TABLE_PATH = pathlib.Path("/etc/mtab")


class ExitCode(enum.IntEnum):


@@ 111,10 112,14 @@ def umount(path: pathlib.Path) -> subprocess.CompletedProcess:
    return run_command(("umount", str(path)), check=True)


def find_cgroup_mount_path() -> T.Optional[pathlib.Path]:
    process = run_command(("mount", "--types", "cgroup2"), capture_output=True, check=True, text=True)
    mounts = process.stdout.splitlines()
    return pathlib.Path(mounts[0].split(" on ", 1)[1].split(" type ", 1)[0]) if mount else None
def find_cgroup_mount_paths() -> list[pathlib.Path]:
    paths = []
    for line in MOUNT_TABLE_PATH.read_text(encoding="utf-8").splitlines():
        name, path, type_, options, fs_freq, fs_passno = line.split()
        if type_ == "cgroup2":
            paths.append(pathlib.Path(path))

    return paths


def get_identifier() -> str:


@@ 139,10 144,12 @@ def run(arguments: argparse.Namespace, command: list[str]) -> ExitCode:
        return ExitCode.FAILURE

    # Try to find an existing cgroup2 mount path
    cgroup_mount_path = find_cgroup_mount_path()
    cgroup_mount_paths = find_cgroup_mount_paths()
    cgroup_mount_created = False
    identifier = get_identifier()
    if not cgroup_mount_path:
    if cgroup_mount_paths:
        cgroup_mount_path = cgroup_mount_paths[0]
    else:
        cgroup_mount_path = pathlib.Path(tempfile.mkdtemp(prefix="unshare-net-"))
        stderr(f"created {str(cgroup_mount_path)!r}")
        # These are the mount options used by Arch Linux (systemd), so I assume they are fine