Skip to content

Commit 17b01d5

Browse files
committed
Fixed bug on stopping the program with systemd where it ended abruptly instead of cleanly.
1 parent e4350a6 commit 17b01d5

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
name = 'wireguard-subnets'
99

10-
version = '1.0.1'
10+
version = '1.0.2'
1111

1212
description = "This program performs unattended addition and removal of remote subnets accessible through its `wg` interface " \
1313
"to a WireGuard server's routing table.\nWorks great combined with systemd."

wireguard_subnets/parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class ARGS:
1111
PERIOD = None
1212
METRIC = None
1313
IPS_SUBNETS = None
14+
SYSTEMD = None
1415

1516

1617
class CustomArgumentFormatter(ArgumentDefaultsHelpFormatter, RawTextHelpFormatter):

wireguard_subnets/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ def header(title: str):
1818
return f' {hyphens}\n {title}\n {hyphens}'
1919

2020

21-
def run_command(command):
22-
return subprocess.run(command, capture_output=True, start_new_session=True, text=True)
21+
def run_command(command, systemd=False):
22+
cmd = []
23+
if systemd:
24+
cmd = ['systemd-run', '--scope', '--quiet']
25+
cmd.extend(command)
26+
return subprocess.run(cmd, capture_output=True, start_new_session=True, text=True)
2327

2428

2529
def create_thread(func: Callable, *args: Any, **kwargs: Any) -> Future:

wireguard_subnets/wireguard_subnets.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,27 @@ def close(signum, frame):
2929

3030

3131
def link_up():
32-
ret = run_command(['ip', 'link', 'show', ARGS.INTERFACE])
32+
ret = run_command(['ip', 'link', 'show', ARGS.INTERFACE], systemd=ARGS.SYSTEMD)
3333
return not ret.returncode
3434

3535

3636
def ping(ip: Union[IPv4Address, IPv6Address]):
37-
ret = run_command(['ping', '-W5', '-c3', '-I', ARGS.INTERFACE, str(ip)])
37+
ret = run_command(['ping', '-W5', '-c3', '-I', ARGS.INTERFACE, str(ip)], systemd=ARGS.SYSTEMD)
3838
return not ret.returncode
3939

4040

4141
def add_subnet(subnet: Union[IPv4Network, IPv6Network]):
42-
ret = run_command(['ip', 'route', 'add', str(subnet), 'dev', ARGS.INTERFACE, 'scope', 'link', 'metric', str(ARGS.METRIC)])
42+
ret = run_command(['ip', 'route', 'add', str(subnet), 'dev', ARGS.INTERFACE, 'scope', 'link', 'metric', str(ARGS.METRIC)], systemd=ARGS.SYSTEMD)
4343
return not ret.returncode
4444

4545

4646
def remove_subnet(subnet: Union[IPv4Network, IPv6Network]):
47-
ret = run_command(['ip', 'route', 'del', str(subnet), 'dev', ARGS.INTERFACE])
47+
ret = run_command(['ip', 'route', 'del', str(subnet), 'dev', ARGS.INTERFACE], systemd=ARGS.SYSTEMD)
4848
return not ret.returncode
4949

5050

5151
def subnet_exists(subnet: Union[IPv4Network, IPv6Network]):
52-
ret = run_command(['ip', 'route', 'show', str(subnet), 'dev', ARGS.INTERFACE])
52+
ret = run_command(['ip', 'route', 'show', str(subnet), 'dev', ARGS.INTERFACE], systemd=ARGS.SYSTEMD)
5353
return not ret.returncode and bool(ret.stdout)
5454

5555

@@ -80,6 +80,7 @@ def main():
8080
print('This program must be run with root privileges.')
8181
sys.exit(0)
8282
parse_args()
83+
ARGS.SYSTEMD = not run_command(['pidof', 'systemd'], systemd=False).returncode
8384
print(header('WireGuard Subnets'))
8485
print(f'Interface: {ARGS.INTERFACE}\nRecheck period: {ARGS.PERIOD}s\nMetric: {ARGS.METRIC}\nSubnets: ')
8586
collections.deque(print(f' • {subnet} behind {ip}') for ip_subnets in ARGS.IPS_SUBNETS for ip, subnets in ip_subnets.items() for subnet in subnets)

0 commit comments

Comments
 (0)