From 8440f9c548741acca9db95e4016350190a517bc2 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 1 Jun 2019 23:15:52 +0200 Subject: [PATCH] dhcp_server: Refactor (#590) * dhcp_server: Rewrite add-on onto Bashio * dhcp_server: Format config.json * dhcp_server: Adds README to add-on repository * dhcp_server: Version bump to 1.1, updated CHANGELOG --- dhcp_server/CHANGELOG.md | 7 ++ dhcp_server/Dockerfile | 1 - dhcp_server/README.md | 164 +++++++++++++++++++++++++++++++++++++++ dhcp_server/config.json | 19 ++++- dhcp_server/dhcpd.conf | 7 -- dhcp_server/run.sh | 88 +++++++++++---------- 6 files changed, 235 insertions(+), 51 deletions(-) create mode 100644 dhcp_server/README.md delete mode 100644 dhcp_server/dhcpd.conf diff --git a/dhcp_server/CHANGELOG.md b/dhcp_server/CHANGELOG.md index 5952f70..2fb2b3c 100644 --- a/dhcp_server/CHANGELOG.md +++ b/dhcp_server/CHANGELOG.md @@ -1,4 +1,11 @@ # Changelog +## 1.1 + +- Rewrite add-on onto Bashio +- Added documentation to add-on repository +- Small changes to code styling + ## 1.0 + - Update DHCP to 4.4.1 diff --git a/dhcp_server/Dockerfile b/dhcp_server/Dockerfile index a028f73..2a80216 100644 --- a/dhcp_server/Dockerfile +++ b/dhcp_server/Dockerfile @@ -9,6 +9,5 @@ RUN apk add --no-cache dhcp # Copy data COPY run.sh / -COPY dhcpd.conf /etc/ CMD [ "/run.sh" ] diff --git a/dhcp_server/README.md b/dhcp_server/README.md new file mode 100644 index 0000000..199fa64 --- /dev/null +++ b/dhcp_server/README.md @@ -0,0 +1,164 @@ +# Hass.io Core Add-on: DHCP server + +A simple DHCP server. + +![Supports aarch64 Architecture][aarch64-shield] ![Supports amd64 Architecture][amd64-shield] ![Supports armhf Architecture][armhf-shield] ![Supports armv7 Architecture][armv7-shield] ![Supports i386 Architecture][i386-shield] + +## About + +This add-on provides a simple DHCP server for your network. +It provides some basic needs, like, reserving IP addresses for your devices +to ensure they alway get assigned the same IP address. + +## Installation + +The installation of this add-on is straightforward and easy to do. + +1. Navigate in your Home Assistant frontend to **Hass.io** -> **Add-on Store**. +2. Find the "DHCP server" add-on and click it. +3. Click on the "INSTALL" button. + +## How to use + +1. Set the `domain` option, e.g., `mynetwork.local`. +2. Save the add-on configuration by clicking the "SAVE" button. +3. Start the add-on. + +## Configuration + +The DHCP server add-on can be tweaked to your likings. This section +describes each of the add-on configuration options. + +Example add-on configuration: + +```json +{ + "domain": "mynetwork.local", + "dns": ["8.8.8.8", "8.8.4.4"], + "default_lease": 86400, + "max_lease": 172800, + "networks": [ + { + "subnet": "192.168.1.0", + "netmask": "255.255.255.0", + "range_start": "192.168.1.100", + "range_end": "192.168.1.200", + "broadcast": "192.168.1.255", + "gateway": "192.168.1.1", + "interface": "eth0" + } + ], + "hosts": [ + { + "name": "webcam_xy", + "mac": "aa:bb:ee:cc", + "ip": "192.168.1.40" + } + ] +} +``` + +### Option: `domain` (required) + +Your network domain name, e.g., `mynetwork.local` or `home.local` + +### Option: `dns` (required) + +The DNS servers you DHCP server gives to your clients. This option can +contain a list of servers. By default it is configured to have Google's +public DNS servers: `"8.8.8.8", "8.8.4.4". + +### Option: `default_lease` (required) + +The default time in seconds that the IP is leased to your client. +Defaults to `86400`, which is one day. + +### Option: `max_lease` (required) + +The max time in seconds that the IP is leased to your client. +Defaults to `172800`, which is one day. + +### Option: `networks` (one item required) + +This option defines settings for one or multiple networks for the DHCP server +to hand out IP addresses for. + +At least one network definition in your configuration is required for the +DHCP server to work. + +#### Option: `networks` -> `subnet` + +Your network schema/subnet. For example, if your IP addresses are `192.168.1.x` +the subnet becomes `192.168.1.0`. + +#### Option: `networks` -> `netmask` + +Your network netmask. For example, if your IP addresses are `192.168.1.x` the +netmask becomes `255.255.255.0`. + +#### Option: `networks` -> `range_start` + +Defines the start IP address for the DHCP server to lease IPs for. +Use this together with the `range_end` option to define the range of IP +addresses the DHCP server operates in. + +#### Option: `networks` -> `range_end` + +Defines the end IP address for the DHCP server to lease IPs for. + +#### Option: `networks` -> `broadcast` + +The broadcast address specific to the lease range. For example, if your +IP addresses are `192.168.1.x`, the broadcast address is usually `192.168.1.255`. + +#### Option: `networks` -> `gateway` + +Sets the gateway address for that the DHCP server hands out to its clients. +This is usually the IP address of your router. + +#### Option: `networks` -> `interface` + +The network interface to listen to for this network, e.g., `eth0`. + +### Option: `hosts` (optional) + +This option defines settings for one or host definitions for the DHCP server. + +It allows you to fix a host to a specific IP address. + +By default, non are configured. + +#### Option: `hosts` -> `name` + +The name of the hostname you'd like to fix an address for. + +#### Option: `hosts` -> `mac` + +The MAC address of the client device. + +#### Option: `hosts` -> `ip` + +The IP address you want the DHCP server to assign. + +## Support + +Got questions? + +You have several options to get them answered: + +- The [Home Assistant Discord Chat Server][discord]. +- The Home Assistant [Community Forum][forum]. +- Join the [Reddit subreddit][reddit] in [/r/homeassistant][reddit] + +In case you've found an bug, please [open an issue on our GitHub][issue]. + +[aarch64-shield]: https://img.shields.io/badge/aarch64-yes-green.svg +[amd64-shield]: https://img.shields.io/badge/amd64-yes-green.svg +[armhf-shield]: https://img.shields.io/badge/armhf-yes-green.svg +[armv7-shield]: https://img.shields.io/badge/armv7-yes-green.svg +[discord]: https://discord.gg/c5DvZ4e +[forum]: https://community.home-assistant.io +[i386-shield]: https://img.shields.io/badge/i386-yes-green.svg +[issue]: https://github.com/home-assistant/hassio-addons/issues +[reddit]: https://reddit.com/r/homeassistant +[repository]: https://github.com/hassio-addons/repository diff --git a/dhcp_server/config.json b/dhcp_server/config.json index 279d95c..d603130 100644 --- a/dhcp_server/config.json +++ b/dhcp_server/config.json @@ -1,10 +1,16 @@ { "name": "DHCP server", - "version": "1.0", + "version": "1.1", "slug": "dhcp_server", "description": "A simple DHCP server", "url": "https://home-assistant.io/addons/dhcp_server/", - "arch": ["armhf", "armv7", "aarch64", "amd64", "i386"], + "arch": [ + "armhf", + "armv7", + "aarch64", + "amd64", + "i386" + ], "startup": "system", "boot": "auto", "host_network": true, @@ -12,7 +18,10 @@ "default_lease": 86400, "max_lease": 172800, "domain": null, - "dns": ["8.8.8.8", "8.8.4.4"], + "dns": [ + "8.8.8.8", + "8.8.4.4" + ], "networks": [ { "subnet": "192.168.1.0", @@ -30,7 +39,9 @@ "default_lease": "int", "max_lease": "int", "domain": "str", - "dns": ["str"], + "dns": [ + "str" + ], "networks": [ { "subnet": "str", diff --git a/dhcp_server/dhcpd.conf b/dhcp_server/dhcpd.conf deleted file mode 100644 index 9932ef0..0000000 --- a/dhcp_server/dhcpd.conf +++ /dev/null @@ -1,7 +0,0 @@ -option domain-name "%%DOMAIN%%"; -option domain-name-servers %%DNS_SERVERS%%; - -default-lease-time %%DEFAULT_LEASE%%; -max-lease-time %%MAX_LEASE%%; - -authoritative; diff --git a/dhcp_server/run.sh b/dhcp_server/run.sh index 4d1d077..7187cfa 100755 --- a/dhcp_server/run.sh +++ b/dhcp_server/run.sh @@ -1,59 +1,69 @@ -#!/bin/bash +#!/usr/bin/env bashio set -e -CONFIG_PATH=/data/options.json +CONFIG="/etc/dhcpd.conf" +LEASES="/data/dhcpd.lease" -DEFAULT_LEASE=$(jq --raw-output '.default_lease' $CONFIG_PATH) -MAX_LEASE=$(jq --raw-output '.max_lease' $CONFIG_PATH) -DOMAIN=$(jq --raw-output '.domain' $CONFIG_PATH) -DNS=$(jq --raw-output '.dns | join(", ")' $CONFIG_PATH) -NETWORKS=$(jq --raw-output '.networks | length' $CONFIG_PATH) -HOSTS=$(jq --raw-output '.hosts | length' $CONFIG_PATH) +bashio::log.info "Creating DHCP configuration..." -sed -i "s/%%DOMAIN%%/$DOMAIN/g" /etc/dhcpd.conf -sed -i "s/%%DNS_SERVERS%%/$DNS/g" /etc/dhcpd.conf -sed -i "s/%%DEFAULT_LEASE%%/$DEFAULT_LEASE/g" /etc/dhcpd.conf -sed -i "s/%%MAX_LEASE%%/$MAX_LEASE/g" /etc/dhcpd.conf +# Create main config +DEFAULT_LEASE=$(bashio::config 'default_lease') +DNS=$(bashio::config 'dns|join(", ")') +DOMAIN=$(bashio::config 'domain') +MAX_LEASE=$(bashio::config 'max_lease') + +{ + echo "option domain-name ${DOMAIN};" + echo "option domain-name-servers ${DNS};"; + echo "default-lease-time ${DEFAULT_LEASE};" + echo "max-lease-time ${MAX_LEASE};" + echo "authoritative;" +} > "${CONFIG}" # Create networks -for (( i=0; i < "$NETWORKS"; i++ )); do - SUBNET=$(jq --raw-output ".networks[$i].subnet" $CONFIG_PATH) - NETMASK=$(jq --raw-output ".networks[$i].netmask" $CONFIG_PATH) - RANGE_START=$(jq --raw-output ".networks[$i].range_start" $CONFIG_PATH) - RANGE_END=$(jq --raw-output ".networks[$i].range_end" $CONFIG_PATH) - BROADCAST=$(jq --raw-output ".networks[$i].broadcast" $CONFIG_PATH) - GATEWAY=$(jq --raw-output ".networks[$i].gateway" $CONFIG_PATH) - INTERFACE=$(jq --raw-output ".networks[$i].interface" $CONFIG_PATH) +for network in $(bashio::config 'networks|keys'); do + BROADCAST=$(bashio::config "networks[${network}].broadcast") + GATEWAY=$(bashio::config "networks[${network}].gateway") + INTERFACE=$(bashio::config "networks[${network}].interface") + NETMASK=$(bashio::config "networks[${network}].netmask") + RANGE_END=$(bashio::config "networks[${network}].range_end") + RANGE_START=$(bashio::config "networks[${network}].range_start") + SUBNET=$(bashio::config "networks[${network}].subnet") { - echo "subnet $SUBNET netmask $NETMASK {" - echo " range $RANGE_START $RANGE_END;" - echo " interface $INTERFACE;" - echo " option routers $GATEWAY;" - echo " option broadcast-address $BROADCAST;" + echo "subnet ${SUBNET} netmask ${NETMASK} {" + echo " interface ${INTERFACE};" + echo " range ${RANGE_START} ${RANGE_END};" + echo " option routers ${GATEWAY};" + echo " option broadcast-address ${BROADCAST};" echo "}" - } >> /etc/dhcpd.conf + } >> "${CONFIG}" done # Create hosts -for (( i=0; i < "$HOSTS"; i++ )); do - MAC=$(jq --raw-output ".hosts[$i].mac" $CONFIG_PATH) - NAME=$(jq --raw-output ".hosts[$i].name" $CONFIG_PATH) - IP=$(jq --raw-output ".hosts[$i].ip" $CONFIG_PATH) +for host in $(bashio::config 'hosts|keys'); do + IP=$(bashio::config "hosts[${host}].ip") + MAC=$(bashio::config "hosts[${host}].mac") + NAME=$(bashio::config "hosts[${host}].name") { - echo "host $NAME {" - echo " hardware ethernet $MAC;" - echo " fixed-address $IP;" - echo " option host-name \"$NAME\";" + echo "host ${NAME} {" + echo " hardware ethernet ${MAC};" + echo " fixed-address ${IP};" + echo " option host-name \"${NAME}\";" echo "}" - } >> /etc/dhcpd.conf + } >> "${CONFIG}" done # Create database -if [ ! -f /data/dhcpd.lease ]; then - touch /data/dhcpd.lease +if ! bashio::fs.file_exists "${LEASES}"; then + touch "${LEASES}" fi -# run dhcp server -exec /usr/sbin/dhcpd -4 -f -d --no-pid -lf /data/dhcpd.lease -cf /etc/dhcpd.conf < /dev/null +# Start DHCP server +bashio::log.info "Starting DHCP server..." +exec /usr/sbin/dhcpd \ + -4 -f -d --no-pid \ + -lf "${LEASES}" \ + -cf "${CONFIG}" \ + < /dev/null