Merge pull request 'python-rewrite' (#2) from python-rewrite into master

Reviewed-on: https://git.disroot.org/pranav/naxalnet/pulls/2
This commit is contained in:
Pranav Jerry 2021-06-09 07:35:41 +00:00
commit 804f6f0792
6 changed files with 199 additions and 76 deletions

View File

@ -3,7 +3,14 @@ PREFIX := /usr
install: naxalnet install: naxalnet
install -d $(DESTDIR)$(PREFIX)/bin install -d $(DESTDIR)$(PREFIX)/bin
install -d $(DESTDIR)$(PREFIX)/lib/systemd/system/ install -d $(DESTDIR)$(PREFIX)/lib/systemd/system/
install naxalnet@.service $(DESTDIR)$(PREFIX)/lib/systemd/system/ install -m644 naxalnet.service $(DESTDIR)$(PREFIX)/lib/systemd/system/
install naxalnet $(DESTDIR)$(PREFIX)/bin/ install naxalnet $(DESTDIR)$(PREFIX)/bin/
install -d $(DESTDIR)$(PREFIX)/share/naxalnet install -d $(DESTDIR)$(PREFIX)/share/naxalnet/networkd
install systemd-networkd/* $(DESTDIR)$(PREFIX)/share/naxalnet/ install -m644 systemd-networkd/* $(DESTDIR)$(PREFIX)/share/naxalnet/networkd
testdeps:
@for i in networkctl systemctl python3; do \
echo "Checking for $$i"; \
which $$i > /dev/null && echo " $$i found" || \
(echo " $$i not found"; exit 1); \
done

View File

@ -1,21 +1,27 @@
# naxalnet # naxalnet
Naxalnet is an experiment to create an intranet and use it for **naxalnet** is an experiment to create a wireless mesh network for
communicating with each other during an internet shutdown. It tries to use communicating with each other during an internet shutdown, using
existing software and tech as much as possible. Currently existing software and tech as much as possible. Currently
you can only communicate with other peers running the same software. you can only communicate with other peers running naxalnet or
using the same network as naxalnet. It uses
[B.A.T.M.A.N. Advanced](https://www.open-mesh.org/projects/batman-adv/wiki),
an implementation of the B.A.T.M.A.N. routing protocol to
communicate with peers.
The name naxal comes from Naxalbari, a village in Darjeeling, The name naxal comes from Naxalbari, a village in Darjeeling,
West Bengal. West Bengal.
A rewrite in python is in progress in the branch python-rewrite. This is the ongoing python rewrite branch, which uses
[dasbus](https://github.com/rhinstaller/dasbus) to communicate
with iwd.
<!-- UNCOMMENT WHEN NECESSARY <!-- UNCOMMENT WHEN NECESSARY
**Disclaimer**: **Disclaimer**:
In case you are either 1) a complete idiot; or 2) a member of parliament; In case you are either 1) a complete idiot; or 2) a member of parliament;
or 3) both, please be aware that this project is not affiliated with or 3) both, please be aware that this project is not affiliated with
any groups designated as terrorist groups in India. any groups designated as "terrorist" groups in India.
Using the name Naxal does not imply any form of connection Using the name Naxal does not imply any form of connection
with anyone the government collectively calles Urban Naxals (human rights with anyone the government collectively calles Urban Naxals (human rights
@ -31,18 +37,21 @@ into networkd's runtime configuration directory. It uses iwctl to start
an ad-hoc network named "Hello World". The wireless interface is linked to an ad-hoc network named "Hello World". The wireless interface is linked to
`bat0`, the batman interface. A bridge `bridge0` is created so that other `bat0`, the batman interface. A bridge `bridge0` is created so that other
devices such as wired connections and wireless ap can be bridged. `bat0` is devices such as wired connections and wireless ap can be bridged. `bat0` is
added to the bridge, and the bridge gets an ip address (link-local, or DHCP if added to the bridge, and the bridge gets an ip address (link-local, or
any of the computers have a DHCP server configured). If these were successful, DHCP if any of the computers have a DHCP server configured). If these
an intranet is created. You can now use services like [IPFS](https://ipfs.io), were successful, an intranet is created. You can now use services
[Jami](https://jami.net), [Secure Scuttlebutt](https://scuttlebutt.nz) like [IPFS](https://ipfs.io), [Jami](https://jami.net),
and others which can work without internet access. [Secure Scuttlebutt](https://scuttlebutt.nz) and others which can work
without internet access.
## Requirements ## Requirements
- systemd v248 or more (for batman support) - systemd v248 or more (for batman support)
- Linux kernel with batman-adv module (if `modinfo batman-adv` shows no error - Linux kernel with batman-adv module (if `modinfo batman-adv` shows
then you already have it) no error then you already have it)
- iwd (for starting ad-hoc network) - iwd (for starting ad-hoc network)
- python3
- [python-dasbus](https://github.com/rhinstaller/dasbus)
- wifi adapter with ad-hoc support - wifi adapter with ad-hoc support
- two or more computers with wifi adapter - two or more computers with wifi adapter
@ -53,47 +62,55 @@ and others which can work without internet access.
Clone the repo and cd into it. Clone the repo and cd into it.
Run `sudo make install` to install naxalnet. This will install naxalnet in Run `sudo make install` to install naxalnet. This will install naxalnet in
`/usr/{bin/naxalnet,share/naxalnet/,lib/systemd/system/naxalnet@.service}`. `/usr/bin/naxalnet`.
<!-- commented out as you'll have to edit the systemd service for this to work After installing, reload systemd so that you can enable `naxalnet.service`
To install in /usr/local instead, use `sudo make install PREFIX=/usr/local`.
After installing, reload systemd so that you can enable `naxalnet@.service`
without rebooting: without rebooting:
```
```sh
sudo systemctl daemon-reload sudo systemctl daemon-reload
``` ```
## Running the program ## Running the program
You need more than one computer running for the connection to work. You need more than one computer running for the connection to work.
Any network managers such as NetworkManager and wifi daemons like Start the naxalnet service:
wpa_supplicant should be disabled:
``` ```sh
sudo systemctl disable --now NetworkManager sudo systemctl start naxalnet.service
``` ```
Enable the naxalnet service on the device `wlan0` (iwd replaces interface Starting the service will stop `NetworkManager.service` and
names like wlp1s0 with wlan0): `wpa_supplicant.service` if it is running. If you start either of these
``` services after naxalnet is started, naxalnet will be stopped by systemd.
sudo systemctl enable --now naxalnet@wlan0.service
To run naxalnet at boot, enable the service:
```sh
sudo systemctl enable naxalnet.service
``` ```
Now naxalnet will configure a batman interface on every boot. Now naxalnet will configure a batman interface on every boot.
## Uninstalling ## Uninstalling
Currently there is now way to uninstall naxalnet than to manually removing Currently there is now way to uninstall naxalnet than manually removing
the files: the files:
```sh
sudo rm -rf /usr/{bin,share}/naxalnet \
/usr/lib/systemd/system/naxalnet.service
``` ```
sudo rm -r /usr/{bin/naxalnet,share/naxalnet/,lib/systemd/system/naxalnet@.service}
``` ## License
<!--
Or if you installed naxalnet in /usr/local: 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
sudo rm -r /usr/local/{bin/naxalnet,share/naxalnet/,lib/systemd/system/naxalnet@.service} the Free Software Foundation, either version 3 of the License, or
``` (at your option) any later version.
-->
See [LICENSE.md](LICENSE.md) for the complete version of the
license.
## TODO ## TODO
@ -101,4 +118,4 @@ sudo rm -r /usr/local/{bin/naxalnet,share/naxalnet/,lib/systemd/system/naxalnet@
Add list of things here. Add list of things here.
This project is in pre-alpha stage. Documentation is incomplete. This project is in alpha stage. Documentation is incomplete.

137
naxalnet
View File

@ -1,35 +1,118 @@
#!/usr/bin/env bash #!/usr/bin/env python3
DATADIR="$(dirname "$0")/../share/naxalnet" """
NETWORKD_DIR=/run/systemd/network Setup a working BATMAN Advanced network
SSID="Hello World" with systemd-networkd and iwd
"""
if [[ $# != 1 ]] # Copyright (C) 2021 The Authors
then
echo "Usage: $0 {wireless device}"
echo "Example: $0 wlan0"
exit 1
fi
if [[ ! -d "$NETWORKD_DIR" ]] # This program is free software: you can redistribute it and/or modify
then # it under the terms of the GNU General Public License as published by
mkdir "$NETWORKD_DIR" # the Free Software Foundation, either version 3 of the License, or
fi # (at your option) any later version.
echo "Copying systemd-networkd config files" # This program is distributed in the hope that it will be useful,
# Copies all files in DATADIR to NETWORKD_DIR # but WITHOUT ANY WARRANTY; without even the implied warranty of
find "$DATADIR" -type f -execdir cp -t "$NETWORKD_DIR" {} + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
echo "Linking resolv.conf to systemd-resolved's stub-resolved.conf" # You should have received a copy of the GNU General Public License
ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf # along with this program. If not, see <http://www.gnu.org/licenses/>.
echo "Starting services"
systemctl reload-or-restart iwd systemd-resolved systemd-networkd
# wait five seconds for iwd to start import sys
#sleep 5 from pathlib import Path
echo "Configuring iwd" from shutil import copy
iwctl device "$1" set-property Mode ad-hoc from dasbus.connection import SystemMessageBus
iwctl ad-hoc "$1" start_open "$SSID"
echo "Done" 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 e:
print(e)
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.exists():
print(r, "already exists. Removing it")
r.unlink()
print("Linking resolv.conf")
r.symlink_to(RESOLVED_STUB_RESOLVE)
except PermissionError as e:
print(e)
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
print("Device is in", dev1.Mode)
dev1.Mode = "ad-hoc"
print("Switched to", dev1.Mode)
print("Starting ad-hoc network")
dev1.StartOpen(ADHOC_SSID)
# TODO: If there is a second device, start AP
# in it
except:
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")

21
naxalnet.service Normal file
View File

@ -0,0 +1,21 @@
[Unit]
Description=Naxalnet
Requires=systemd-networkd.service
Requires=iwd.service
Wants=systemd-resolved.service
Before=systemd-networkd.service
After=iwd.service
# Stops NetworkManager and wpa_supplicant if already running
Conflicts=NetworkManager.service
Conflicts=wpa_supplicant.service
After=NetworkManager.service
After=wpa_supplicant.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/naxalnet
[Install]
WantedBy=multi-user.target

View File

@ -1,10 +0,0 @@
[Unit]
Description=Naxalnet on %i
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/naxalnet %i
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,5 @@
[Match]
Name=en*
[Network]
Bridge=bridge0