2021-09-06 18:24:24 +02:00
|
|
|
# This file is part of naxalnet.
|
2021-07-21 12:06:11 +02:00
|
|
|
# Copyright (C) 2021 The naxalnet 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/>.
|
|
|
|
|
|
|
|
"""
|
2021-08-14 18:30:07 +02:00
|
|
|
scripts.py
|
|
|
|
----------
|
|
|
|
|
|
|
|
The functions in this file is used for reading configs, args
|
|
|
|
and doing the things this program is supposed to do.
|
|
|
|
This file is named scripts.py because the original developer
|
|
|
|
of this program could not think of a better name that suits this file.
|
|
|
|
If you want to hack naxalnet, this is the right place to start.
|
2021-09-06 18:24:24 +02:00
|
|
|
When run from the commandline, the function main() is called.
|
2021-07-21 12:06:11 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
2021-09-27 09:13:09 +02:00
|
|
|
import time
|
2021-07-21 12:06:11 +02:00
|
|
|
from pathlib import Path
|
2021-08-09 09:24:41 +02:00
|
|
|
from dasbus.error import DBusError
|
2021-09-07 07:49:14 +02:00
|
|
|
from systemd.daemon import notify
|
2021-08-14 18:30:07 +02:00
|
|
|
from naxalnet import __version__
|
2021-09-27 06:30:59 +02:00
|
|
|
from naxalnet.default import REPORT_BUG_INFO, MESH_GLOB, TMP_NET_GLOB
|
2021-09-07 15:15:11 +02:00
|
|
|
from naxalnet.log import logger
|
2021-08-14 18:30:07 +02:00
|
|
|
from naxalnet.iwd import Adapter, Device, IWD
|
2021-09-07 15:15:11 +02:00
|
|
|
from naxalnet.config import args
|
2021-09-07 07:49:14 +02:00
|
|
|
from naxalnet.daemon import Daemon
|
2021-09-27 06:30:59 +02:00
|
|
|
from naxalnet.network import NetworkD
|
2021-09-06 11:27:54 +02:00
|
|
|
|
2021-09-27 19:00:07 +02:00
|
|
|
# List of wireless devices used as part of the mesh.
|
|
|
|
# Used to poweroff devices during cleanup.
|
|
|
|
# Though used as a variable, thi e name is
|
|
|
|
# capitalised to shut up pylint and co.
|
|
|
|
USED_DEVICES = []
|
2021-07-21 12:06:11 +02:00
|
|
|
|
2021-09-27 19:00:07 +02:00
|
|
|
|
|
|
|
def get_sorted_glob(directory: str, glob: str) -> list:
|
2021-09-27 07:33:27 +02:00
|
|
|
"""return sorted list of filenames matching glob"""
|
|
|
|
path = Path(directory)
|
2021-09-27 08:13:50 +02:00
|
|
|
glob_list = path.glob(glob)
|
2021-09-27 07:33:27 +02:00
|
|
|
sorted_list = []
|
2021-09-27 08:13:50 +02:00
|
|
|
for i in glob_list:
|
2021-09-27 07:33:27 +02:00
|
|
|
# g is a list of PosixPath objects.
|
|
|
|
# So we add their absolute path as str.
|
|
|
|
sorted_list.append(str(i))
|
|
|
|
# sorted_list is not sorted, so we sort them here
|
|
|
|
sorted_list.sort()
|
|
|
|
return sorted_list
|
|
|
|
|
|
|
|
|
2021-09-27 09:13:09 +02:00
|
|
|
def any_interface_is_routable():
|
|
|
|
"""returns true if any of the interfaces is routable"""
|
|
|
|
networkd = NetworkD(runtime_dir=args.networkd_runtime_dir)
|
|
|
|
|
|
|
|
# First, add the temporary configs to networkd.
|
|
|
|
for i in get_sorted_glob(args.networkd_config_dir, TMP_NET_GLOB):
|
|
|
|
logger.debug("Adding temporary config %s", i)
|
|
|
|
networkd.add_config(i)
|
|
|
|
# Then, wait for some time to setup the network
|
|
|
|
time.sleep(10)
|
|
|
|
routable = networkd.is_routable()
|
|
|
|
networkd.remove_all_configs()
|
|
|
|
|
|
|
|
return routable
|
|
|
|
|
|
|
|
|
|
|
|
def setup_mesh(gateway_mode: str = "off"):
|
|
|
|
"""
|
|
|
|
configure networkd to setup the mesh
|
|
|
|
|
|
|
|
gateway_mode can be client, server, or off
|
|
|
|
"""
|
2021-09-06 18:24:24 +02:00
|
|
|
try:
|
2021-09-07 07:49:14 +02:00
|
|
|
notify("STATUS=Configuring the network...")
|
2021-09-06 18:24:24 +02:00
|
|
|
logger.info("Copying network config files")
|
2021-07-21 12:06:11 +02:00
|
|
|
|
2021-09-27 06:30:59 +02:00
|
|
|
networkd = NetworkD(runtime_dir=args.networkd_runtime_dir)
|
2021-09-27 09:13:09 +02:00
|
|
|
networkd.set_vars(
|
|
|
|
batdev=args.batman_device,
|
|
|
|
bridgedev=args.bridge_device,
|
|
|
|
gateway_mode=gateway_mode,
|
|
|
|
)
|
2021-09-27 19:00:07 +02:00
|
|
|
|
2021-09-28 09:07:11 +02:00
|
|
|
# Fix for issue #19
|
|
|
|
networkd.disable_config("80-wifi-adhoc.network")
|
|
|
|
|
2021-09-27 07:33:27 +02:00
|
|
|
for i in get_sorted_glob(args.networkd_config_dir, MESH_GLOB):
|
|
|
|
logger.debug("Adding network config %s", i)
|
|
|
|
networkd.add_config(i)
|
2021-09-21 19:13:41 +02:00
|
|
|
except PermissionError:
|
|
|
|
logger.exception("A PermissionError occured while copying files")
|
|
|
|
logger.error(REPORT_BUG_INFO)
|
|
|
|
sys.exit(3)
|
|
|
|
except:
|
|
|
|
logger.exception("An unknown error occured while copying files")
|
|
|
|
logger.error(REPORT_BUG_INFO)
|
2021-09-06 18:24:24 +02:00
|
|
|
sys.exit(3)
|
2021-07-21 12:06:11 +02:00
|
|
|
|
2021-07-25 10:36:17 +02:00
|
|
|
|
2021-09-06 18:24:24 +02:00
|
|
|
def setup_devices():
|
2021-08-09 09:24:41 +02:00
|
|
|
"""
|
|
|
|
Setup wifi interfaces using iwd
|
|
|
|
This function should be called every time an interface
|
2021-08-14 18:30:07 +02:00
|
|
|
is connected or removed.
|
2021-08-09 09:24:41 +02:00
|
|
|
"""
|
2021-09-06 18:24:24 +02:00
|
|
|
try:
|
2021-09-07 07:49:14 +02:00
|
|
|
notify("STATUS=Setting up mesh...")
|
2021-09-06 18:24:24 +02:00
|
|
|
iwd = IWD()
|
|
|
|
devices = iwd.get_devices()
|
|
|
|
adhoc_devices = []
|
|
|
|
ap_devices = []
|
|
|
|
|
2021-09-27 19:00:07 +02:00
|
|
|
global USED_DEVICES
|
|
|
|
USED_DEVICES = []
|
|
|
|
|
2021-09-06 18:24:24 +02:00
|
|
|
# Find devices supporting ad-hoc and ap
|
|
|
|
for i in devices:
|
|
|
|
# For each device, check if its adapter supports
|
|
|
|
# ad-hoc or ap. Many adapters will support both,
|
|
|
|
# so we will prioritise ad-hoc over ap.
|
|
|
|
device = Device(i)
|
|
|
|
logger.debug("Found device %s", device.name)
|
|
|
|
adapter = Adapter(device.adapter)
|
|
|
|
if adapter.supports_mode("ad-hoc"):
|
|
|
|
logger.debug("The device %s can be used for ad-hoc", device.name)
|
|
|
|
adhoc_devices.append(i)
|
|
|
|
if adapter.supports_mode("ap"):
|
|
|
|
logger.debug("The device %s can be used for ap", device.name)
|
|
|
|
ap_devices.append(i)
|
|
|
|
|
|
|
|
if len(adhoc_devices) != 0:
|
|
|
|
# Start ad-hoc on first device supporting ad-hoc
|
|
|
|
adhoc_device = Device(adhoc_devices.pop())
|
|
|
|
# The same device is likely to have ap support too.
|
|
|
|
# But we can't start ad-hoc and ap on the same interface.
|
|
|
|
# So we will remove adhoc_device from ap_devices if it exists there
|
|
|
|
if adhoc_device.name in ap_devices:
|
|
|
|
ap_devices.remove(adhoc_device.name)
|
2021-09-07 15:15:11 +02:00
|
|
|
|
|
|
|
# Turn on adapter if it is off
|
|
|
|
# See issue #9
|
2021-09-06 18:24:24 +02:00
|
|
|
adhoc_adapter = Adapter(adhoc_device.adapter)
|
|
|
|
if not adhoc_adapter.is_powered_on():
|
|
|
|
logger.debug("Adapter %s is off. Turning on", adhoc_adapter.name)
|
|
|
|
adhoc_adapter.power_on()
|
2021-09-27 19:00:07 +02:00
|
|
|
|
2021-09-07 15:15:11 +02:00
|
|
|
logger.info("Starting mesh on %s", adhoc_device.name)
|
|
|
|
adhoc_device.start_adhoc_open(args.adhoc_name)
|
2021-09-27 19:00:07 +02:00
|
|
|
USED_DEVICES.append(adhoc_device.name)
|
|
|
|
|
2021-09-07 15:15:11 +02:00
|
|
|
# Start Access point if ap_device is not empty,
|
|
|
|
# ie, we have more devices
|
2021-09-06 18:24:24 +02:00
|
|
|
if len(ap_devices) != 0:
|
|
|
|
ap_device = Device(ap_devices.pop())
|
|
|
|
logger.info("Starting WiFi Access Point on %s", ap_device.name)
|
|
|
|
logger.info("Use naxalnet --print-wifi to get password")
|
|
|
|
# Turn on adapter if it is off
|
|
|
|
# See issue #9
|
|
|
|
ap_adapter = Adapter(ap_device.adapter)
|
|
|
|
if not ap_adapter.is_powered_on():
|
|
|
|
logger.debug("Adapter %s is off. Turning on", ap_adapter.name)
|
|
|
|
ap_adapter.power_on()
|
2021-09-07 15:15:11 +02:00
|
|
|
ap_device.start_ap(args.ap_ssid, args.ap_passwd)
|
2021-09-27 19:00:07 +02:00
|
|
|
USED_DEVICES.append(ap_device.name)
|
2021-09-06 18:24:24 +02:00
|
|
|
else:
|
2021-09-07 07:49:14 +02:00
|
|
|
logger.warning("Not setting up WiFi AP.")
|
2021-09-06 08:53:07 +02:00
|
|
|
else:
|
2021-09-06 18:24:24 +02:00
|
|
|
logger.warning(
|
|
|
|
"No device found to setup mesh. Make sure a WiFi adapter is connected"
|
|
|
|
)
|
2021-09-07 07:49:14 +02:00
|
|
|
|
2021-09-06 18:24:24 +02:00
|
|
|
except DBusError:
|
|
|
|
logger.exception("Error while communicating with iwd")
|
2021-09-21 19:13:41 +02:00
|
|
|
logger.error(REPORT_BUG_INFO)
|
|
|
|
sys.exit(4)
|
|
|
|
except:
|
2021-09-25 11:34:29 +02:00
|
|
|
logger.exception("An unknown error occured while setting up the mesh")
|
2021-09-21 19:13:41 +02:00
|
|
|
logger.error(REPORT_BUG_INFO)
|
2021-09-06 18:24:24 +02:00
|
|
|
sys.exit(4)
|
2021-09-04 10:54:53 +02:00
|
|
|
|
2021-08-05 08:41:31 +02:00
|
|
|
|
2021-09-27 19:18:13 +02:00
|
|
|
def cleanup():
|
2021-09-27 19:00:07 +02:00
|
|
|
"""
|
|
|
|
Remove all network config, poweroff used wireless devices and
|
|
|
|
exit with 0.
|
|
|
|
"""
|
|
|
|
networkd = NetworkD(runtime_dir=args.networkd_runtime_dir)
|
|
|
|
logger.info("Exiting gracefully")
|
|
|
|
networkd.remove_all_configs()
|
|
|
|
for i in USED_DEVICES:
|
|
|
|
device = Device(i)
|
|
|
|
device.set_mode("station")
|
|
|
|
device.power_off()
|
2021-09-27 19:18:13 +02:00
|
|
|
networkd.delete_interface(args.batman_device)
|
|
|
|
networkd.delete_interface(args.bridge_device)
|
2021-09-27 19:00:07 +02:00
|
|
|
|
|
|
|
|
2021-09-06 18:24:24 +02:00
|
|
|
def print_wifi():
|
2021-08-14 18:30:07 +02:00
|
|
|
"""
|
|
|
|
Prints the name and password of the adhoc, and ap
|
|
|
|
from the arguments
|
|
|
|
"""
|
2021-08-13 09:00:30 +02:00
|
|
|
print("Mesh name:", args.adhoc_name)
|
|
|
|
print("SSID:", args.ap_ssid)
|
|
|
|
print("Password:", args.ap_passwd)
|
|
|
|
|
|
|
|
|
2021-08-14 18:30:07 +02:00
|
|
|
def print_version():
|
|
|
|
"""Just does what the name suggests"""
|
|
|
|
print(__version__)
|
|
|
|
|
|
|
|
|
2021-09-06 18:24:24 +02:00
|
|
|
def main():
|
2021-07-25 10:36:17 +02:00
|
|
|
"""
|
2021-08-14 18:30:07 +02:00
|
|
|
This is where the magic happens!
|
2021-07-25 10:36:17 +02:00
|
|
|
This function is run every time you
|
2021-08-14 18:30:07 +02:00
|
|
|
execute naxalnet from the commandline
|
2021-07-25 10:36:17 +02:00
|
|
|
"""
|
2021-08-13 07:05:13 +02:00
|
|
|
|
2021-09-06 18:24:24 +02:00
|
|
|
if args.print_wifi:
|
|
|
|
print_wifi()
|
|
|
|
sys.exit(0)
|
|
|
|
elif args.version:
|
|
|
|
print_version()
|
|
|
|
sys.exit(0)
|
2021-08-13 09:00:30 +02:00
|
|
|
|
2021-09-07 07:49:14 +02:00
|
|
|
# Notify systemd that naxalnet is ready.
|
|
|
|
# see man:sd_notify(3)
|
|
|
|
notify("READY=1")
|
2021-09-27 09:13:09 +02:00
|
|
|
|
|
|
|
if args.gateway_mode == "auto":
|
|
|
|
logger.info("Checking for internet connection")
|
|
|
|
notify("STATUS=Checking for internet")
|
|
|
|
# If any interface is routable, set gateway mode to server
|
|
|
|
if any_interface_is_routable():
|
|
|
|
gateway_mode = "server"
|
|
|
|
else:
|
|
|
|
gateway_mode = "client"
|
|
|
|
logger.info("gateway_mode set to %s", gateway_mode)
|
|
|
|
elif args.gateway_mode in ["server", "client", "off"]:
|
|
|
|
gateway_mode = args.gateway_mode
|
|
|
|
else:
|
|
|
|
logger.error("gateway-mode has an illegal value")
|
2021-09-27 19:00:07 +02:00
|
|
|
sys.exit(5)
|
2021-09-27 09:13:09 +02:00
|
|
|
|
2021-09-27 19:00:07 +02:00
|
|
|
try:
|
|
|
|
setup_devices()
|
|
|
|
setup_mesh()
|
|
|
|
|
|
|
|
# Start the daemon so that setup_devices() is called every
|
|
|
|
# time a device is connected or removed.
|
|
|
|
daemon = Daemon()
|
|
|
|
daemon.add_callback(setup_devices)
|
|
|
|
|
|
|
|
notify("STATUS=Waiting for changes")
|
|
|
|
daemon.start()
|
2021-09-27 19:18:13 +02:00
|
|
|
# systemd uses SIGINT to kill this program
|
2021-09-27 19:00:07 +02:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
cleanup()
|