diff --git a/dhcp_server/Dockerfile b/dhcp_server/Dockerfile new file mode 100644 index 0000000..44d5c74 --- /dev/null +++ b/dhcp_server/Dockerfile @@ -0,0 +1,15 @@ +FROM %%BASE_IMAGE%% + +# Add env +ENV LANG C.UTF-8 + +# Setup base +RUN apk add --no-cache tzdata jq dhcp + +# Copy data +COPY run.sh / +COPY dhcpd.conf /etc/ + +RUN chmod a+x /run.sh + +CMD [ "/run.sh" ] diff --git a/dhcp_server/config.json b/dhcp_server/config.json new file mode 100644 index 0000000..559e11a --- /dev/null +++ b/dhcp_server/config.json @@ -0,0 +1,52 @@ +{ + "name": "DHCP server", + "version": "0.1", + "slug": "dhcp_server", + "description": "A simple dhcp server", + "url": "https://home-assistant.io/addons/dhcp_server/", + "startup": "before", + "boot": "auto", + "ports": { + "67/tcp": 67, + "67/udp": 67 + }, + "options": { + "domain": null, + "domain-name-servers": [null], + "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.254", + "routers": ["192.168.1.1"] + } + ], + "hosts": [ + {} + ] + }, + "schema": { + "domain": null, + "dns": ["8.8.8.8", "8.8.4.4"], + "networks": [ + { + "subnet": "str", + "netmask": "str", + "range_start": "str", + "range_end": "str", + "broadcast": "str", + "routers": ["str"] + } + ], + "hosts": [ + { + "mac": "str", + "name": "str", + "ip": "str" + } + ] + }, + "image": "homeassistant/{arch}-addon-dhcp_server" +} diff --git a/dhcp_server/dhcpd.conf b/dhcp_server/dhcpd.conf new file mode 100644 index 0000000..7239045 --- /dev/null +++ b/dhcp_server/dhcpd.conf @@ -0,0 +1,7 @@ +option domain-name "%%DOMAIN%%"; +option domain-name-servers %%DNS_SERVERS%%; + +default-lease-time 600; +max-lease-time 7200; + +authoritative; diff --git a/dhcp_server/run.sh b/dhcp_server/run.sh new file mode 100644 index 0000000..93ec078 --- /dev/null +++ b/dhcp_server/run.sh @@ -0,0 +1,48 @@ +#!/bin/bash +set -e + +CONFIG_PATH=/data/options.json + +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) + +sed -i "s/%%DOMAIN%%/$DOMAIN/g" /etc/dhcpd.conf +sed -i "s/%%DNS_SERVERS%%/$DNS/g" /etc/dhcpd.conf + +# 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) + ROUTERS=$(jq --raw-output ".networks[$i].routers | joint(\", \")" $CONFIG_PATH) + + { + echo "subnet $SUBNET netmask $NETMASK {" + echo " range $RANGE_START $RANGE_END;" + echo " option routers $ROUTERS;" + echo " option broadcast-address $BROADCAST;" + echo "}" + } >> /etc/dhcpd.conf +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) + + { + echo "host $NAME {" + echo " hardware ethernet $MAC;" + echo " fixed-address $IP;" + echo " option host-name \"$NAME\";" + echo "}" + } >> /etc/dhcpd.conf +done + +# run dhcp server +exec /usr/sbin/dhcpd -4 -f -d --no-pid -lf /data/dhcpd.lease -cf /etc/dhcpd.conf < /dev/null diff --git a/duckdns/run.sh b/duckdns/run.sh index 7e30ac3..f7072a5 100644 --- a/duckdns/run.sh +++ b/duckdns/run.sh @@ -3,21 +3,13 @@ set -e CONFIG_PATH=/data/options.json -TOKEN=$(jq --raw-output ".token" $CONFIG_PATH) -DOMAINS=$(jq --raw-output ".domains[]" $CONFIG_PATH) -WAIT_TIME=$(jq --raw-output ".seconds" $CONFIG_PATH) +TOKEN=$(jq --raw-output '.token' $CONFIG_PATH) +DOMAINS=$(jq --raw-output '.domains | join(",")' $CONFIG_PATH) +WAIT_TIME=$(jq --raw-output '.seconds' $CONFIG_PATH) -for line in $DOMAINS; do - if [ -z "$DOMAIN_ARG" ]; then - DOMAIN_ARG="$line" - else - DOMAIN_ARG="$DOMAIN_ARG,$line" - fi -done - -# +# Run duckdns while true; do - ANSWER="$(curl -sk "https://www.duckdns.org/update?domains=$DOMAIN_ARG&token=$TOKEN&ip=&verbose=true")" + ANSWER="$(curl -sk "https://www.duckdns.org/update?domains=$DOMAINS&token=$TOKEN&ip=&verbose=true")" echo "$(date): $ANSWER" sleep "$WAIT_TIME" done