mirror of
https://git.disroot.org/pranav/pybatmesh.git
synced 2024-12-02 11:40:44 +05:30
Pranav Jerry
1d2d4b2d88
Looks like issue #3 is fixed. Added an AUR package to the README. Fixed another bug related to linking resolv.conf (I didn't know Path.exsts() checks the destination if Path was a symlink). Changed how it deals with resolv.conf if it is a symlink or normal files. Also, added a 'RestartSec=5sec' to the systemd service.
137 lines
4.2 KiB
Python
Executable File
137 lines
4.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
"""
|
|
Setup a working BATMAN Advanced network
|
|
with systemd-networkd and iwd
|
|
"""
|
|
|
|
# Copyright (C) 2021 The Authors
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
from shutil import copy
|
|
from dasbus.connection import SystemMessageBus
|
|
from dasbus.error import DBusError
|
|
|
|
NETWORKD_CONFIGS = "/usr/share/naxalnet/networkd"
|
|
NETWORKD_VOLATILE_DIR = "/run/systemd/network"
|
|
RESOLVED_STUB_RESOLVE = "/run/systemd/resolve/stub-resolv.conf"
|
|
RESOLV_CONF = "/etc/resolv.conf"
|
|
ADHOC_SSID = "HelloWorld"
|
|
|
|
# Copy networkd configs to volatile dir.
|
|
# See man:systemd.networkm(5)
|
|
try:
|
|
print("Copying network config files")
|
|
dest = Path(NETWORKD_VOLATILE_DIR)
|
|
src = Path(NETWORKD_CONFIGS)
|
|
|
|
# Create the volatile directory if it doesn't exist
|
|
dest.mkdir(parents=True, exist_ok=True)
|
|
|
|
# Copy all files in src to dest
|
|
for i in src.iterdir():
|
|
copy(i, dest)
|
|
except PermissionError as error:
|
|
print(error)
|
|
sys.exit("Make sure you are root")
|
|
|
|
|
|
# Symlink resolvd.conf to systemd's stub-resolvd.conf
|
|
# This is needed for DNS resolution to work.
|
|
# see https://wiki.archlinux.org/title/Systemd-resolved#DNS
|
|
try:
|
|
print("Checking resolv.conf")
|
|
r = Path(RESOLV_CONF)
|
|
if r.is_symlink():
|
|
# Naxalnet will be started before resolved, so the destination
|
|
# is unlikely to exist at the time
|
|
# If r is linked to resolved's resolv.conf, dont change it
|
|
if r.samefile(RESOLVED_STUB_RESOLVE):
|
|
print(r, "is already linked to stub-resolv.conf. Not changing")
|
|
# Else if the destination exists
|
|
elif r.exists():
|
|
print(r, "is a symlink that exists. Not removing")
|
|
else:
|
|
print(r, "is a symlink to a destination that doesn't exist. Removing")
|
|
r.unlink()
|
|
elif r.exists():
|
|
print(r, "is not a symlink")
|
|
x = r.rename(RESOLV_CONF + ".naxalnet-bkp")
|
|
print(r, "was moved to", x)
|
|
print("Linking resolv.conf")
|
|
r.symlink_to(RESOLVED_STUB_RESOLVE)
|
|
except PermissionError as error:
|
|
print(error)
|
|
sys.exit("An error occured while linking resolv.conf")
|
|
|
|
|
|
# Now, the iwd part
|
|
try:
|
|
# connect to the System bus
|
|
bus = SystemMessageBus()
|
|
# iwd proxy
|
|
iwd = bus.get_proxy("net.connman.iwd", "/")
|
|
# Get list of all devices
|
|
print("Finding connected devices")
|
|
objects = iwd.GetManagedObjects()
|
|
devices = []
|
|
for name, obj in objects.items():
|
|
if "net.connman.iwd.Device" in obj:
|
|
# add all devices to the list
|
|
print("Found device:", obj["net.connman.iwd.Device"]["Name"])
|
|
devices.append(name)
|
|
|
|
# Start ad-hoc on first device
|
|
devpath = devices.pop()
|
|
print("Working on first device", devpath)
|
|
dev1 = bus.get_proxy("net.connman.iwd", devpath)
|
|
if not dev1.Powered:
|
|
print("Device is off. Turning on")
|
|
dev1.Powered = True
|
|
if dev1.Mode != "ad-hoc":
|
|
print("Device is in", dev1.Mode)
|
|
print("Switching to ad-hoc")
|
|
dev1.Mode = "ad-hoc"
|
|
# Changing Mode requires connecting to the proxy again
|
|
dev1 = bus.get_proxy("net.connman.iwd", devpath)
|
|
print("Starting ad-hoc network")
|
|
dev1.StartOpen(ADHOC_SSID)
|
|
# TODO: If there is a second device, start AP
|
|
# in it
|
|
except DBusError:
|
|
sys.exit("An error occured while communicating with iwd")
|
|
|
|
# Sleep my little baby-oh
|
|
# Sleep until you waken
|
|
# When you wake you'll see the world
|
|
# If I'm not mistaken...
|
|
#
|
|
# Kiss a lover
|
|
# Dance a measure,
|
|
# Find your name
|
|
# And buried treasure...
|
|
#
|
|
# Face your life
|
|
# Its pain,
|
|
# Its pleasure,
|
|
# Leave no path untaken.
|
|
#
|
|
# -- Neil Gaiman, The Graveyard Book
|
|
print("Bye")
|