mirror of
https://github.com/pi-hole/dnsmasq.git
synced 2025-12-19 10:18:25 +00:00
import of dnsmasq-2.34.tar.gz
This commit is contained in:
68
contrib/port-forward/dnsmasq-portforward
Executable file
68
contrib/port-forward/dnsmasq-portforward
Executable file
@@ -0,0 +1,68 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# /usr/sbin/dnsmasq-portforward
|
||||
#
|
||||
# A script which gets run when the dnsmasq DHCP lease database changes.
|
||||
# It logs to $LOGFILE, if it exists, and maintains port-forwards using
|
||||
# IP-tables so that they always point to the correct host. See
|
||||
# $PORTSFILE for details on configuring this. dnsmasq must be version 2.34
|
||||
# or later.
|
||||
#
|
||||
# To enable this script, add
|
||||
# dhcp-script=/usr/sbin/dnsmasq-portforward
|
||||
# to /etc/dnsmasq.conf
|
||||
#
|
||||
# To enable logging, touch $LOGFILE
|
||||
#
|
||||
|
||||
PORTSFILE=/etc/portforward
|
||||
LOGFILE=/var/log/dhcp.log
|
||||
IPTABLES=/sbin/iptables
|
||||
|
||||
action=${1:-0}
|
||||
hostname=${4}
|
||||
|
||||
# log what's going on.
|
||||
if [ -f ${LOGFILE} ] ; then
|
||||
date +"%D %T $*" >>${LOGFILE}
|
||||
fi
|
||||
|
||||
# If a lease gets stripped of a name, we see that as an "old" action
|
||||
# with DNSMASQ_OLD_HOSTNAME set, convert it into a "del"
|
||||
if [ ${DNSMASQ_OLD_HOSTNAME} ] && [ ${action} = old ] ; then
|
||||
action=del
|
||||
hostname=${DNSMASQ_OLD_HOSTNAME}
|
||||
fi
|
||||
|
||||
# action init is not relevant, and will only be seen when leasefile-ro is set.
|
||||
if [ ${action} = init ] ; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ ${hostname} ]; then
|
||||
ports=$(sed -n -e "/^${hostname}\ .*/ s/^.* //p" ${PORTSFILE})
|
||||
|
||||
for port in $ports; do
|
||||
verb=removed
|
||||
protocol=tcp
|
||||
if [ ${port:0:1} = u ] ; then
|
||||
protocol=udp
|
||||
port=${port/u/}
|
||||
fi
|
||||
src=${port/:*/}
|
||||
dst=${port/*:/}
|
||||
# delete first, to avoid multiple copies of rules.
|
||||
${IPTABLES} -t nat -D PREROUTING -p $protocol --destination-port $src -j DNAT --to-destination ${3}:$dst
|
||||
if [ ${action} != del ] ; then
|
||||
${IPTABLES} -t nat -A PREROUTING -p $protocol --destination-port $src -j DNAT --to-destination ${3}:$dst
|
||||
verb=added
|
||||
fi
|
||||
if [ -f ${LOGFILE} ] ; then
|
||||
echo " DNAT $protocol $src to ${3}:$dst ${verb}." >>${LOGFILE}
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
exit 0
|
||||
|
||||
|
||||
28
contrib/port-forward/portforward
Normal file
28
contrib/port-forward/portforward
Normal file
@@ -0,0 +1,28 @@
|
||||
# This file is read by /usr/sbin/dnsmasq-portforward and used to set up port
|
||||
# forwarding to hostnames. If the dnsmasq-determined hostname matches the
|
||||
# first column of this file, then a DNAT port-forward will be set up
|
||||
# to the address which has just been allocated by DHCP . The second field
|
||||
# is port number(s). If there is only one, then the port-forward goes to
|
||||
# the same port on the DHCP-client, if there are two seperated with a
|
||||
# colon, then the second number is the port to which the connection
|
||||
# is forwarded on the DHCP-client. By default, forwarding is set up
|
||||
# for TCP, but it can done for UDP instead by prefixing the port to "u".
|
||||
# To forward both TCP and UDP, two lines are required.
|
||||
#
|
||||
# eg.
|
||||
# wwwserver 80
|
||||
# will set up a port forward from port 80 on this host to port 80
|
||||
# at the address allocated to wwwserver whenever wwwserver gets a DHCP lease.
|
||||
#
|
||||
# wwwserver 8080:80
|
||||
# will set up a port forward from port 8080 on this host to port 80
|
||||
# on the DHCP-client.
|
||||
#
|
||||
# dnsserver 53
|
||||
# dnsserver u53
|
||||
# will port forward port 53 UDP and TCP from this host to port 53 on dnsserver.
|
||||
#
|
||||
# Port forwards will recreated when dnsmasq restarts after a reboot, and
|
||||
# removed when DHCP leases expire. After editing this file, restart dnsmasq
|
||||
# to install new iptables entries in the kernel.
|
||||
|
||||
Reference in New Issue
Block a user