Skip to content

Instantly share code, notes, and snippets.

@smx-smx
Last active June 10, 2024 00:19
Show Gist options
  • Save smx-smx/88189c268ade3a5085a5ecbcbb7e6638 to your computer and use it in GitHub Desktop.
Save smx-smx/88189c268ade3a5085a5ecbcbb7e6638 to your computer and use it in GitHub Desktop.
DVA-5592 autostart script
root@dlinkrouter:~# cp /tmp/smx.sh /etc/init.d/smx.sh
root@dlinkrouter:~# cd /etc/rc.d
root@dlinkrouter:/etc/rc.d# ln -s ../init.d/smx.sh S50smx.sh
#!/bin/sh
dnat_tcp(){
iptables -I SmxIn -p tcp -m tcp --dport $1 -j ACCEPT
iptables -t nat -I PREROUTING -p tcp -m tcp ! -s 192.168.0.0/23 --dport $1 -j DNAT --to=$2
}
dnat_udp(){
iptables -I SmxIn -p udp -m udp --dport $1 -j ACCEPT
iptables -t nat -I PREROUTING -p udp -m udp ! -s 192.168.0.0/23 --dport $1 -j DNAT --to=$2
}
mount_ifnot(){
local src="$1"
local dst="$2"
local opts="$3"
grep -q "$dst" /proc/mounts || (
echo "mounting $dst ..."
mount $opts "$src" "$dst"
)
}
init_chroot(){
mount_ifnot /dev $root/dev "-o bind"
mount_ifnot devpts $root/dev/pts "-t devpts"
mount_ifnot proc $root/proc "-t proc"
mount_ifnot sysfs $root/sys "-t sysfs"
#mount_ifnot /tmp $root/yaps-tmp "-o bind"
}
fake_fwver(){
cmclient SET Device.DeviceInfo.SoftwareVersion DVA-5592_A1_WI_20180823
}
setup_firewall(){
## Short circuit INPUT
iptables -P INPUT DROP
iptables -I INPUT -j DROP
iptables -N SmxIn
iptables -I INPUT -j SmxIn
## Allowance
iptables -A SmxIn -i lo -j ACCEPT
iptables -A SmxIn -m state --state RELATED,ESTABLISHED -j ACCEPT
#iptables -A SmxIn -m iprange --dst-range 224.0.0.0-239.255.255.255 -j ACCEPT
iptables -A SmxIn -s 127.0.0.1/8 ! -i lo -j DROP
iptables -A SmxIn -i br0 -j ACCEPT
# DLink Voip Stack
iptables -A SmxIn -p udp -m udp --dport 5060 -j ACCEPT
# Asterisk
iptables -A SmxIn -p udp -m udp --dport 5062 -j ACCEPT
# Asterisk RTP
iptables -A SmxIn -p udp -m udp --dport 10000:10100 -j ACCEPT
# StrongSwan forwarding
iptables -A SmxIn -p esp -j ACCEPT
}
export TERM=linux
root=$PWD
init_chroot
fake_fwver
setup_firewall
# continue without blocking boot
nohup ./init_post.sh &>/dev/null 2>&1 &
#!/bin/sh
/usr/sbin/sshd -f /etc/ssh/sshd_config
#!/bin/sh
ROOT=$PWD
export LD_LIBRARY_PATH="${ROOT}/lib:${ROOT}/usr/lib"
wait_xdsl_link(){
while true; do
local status=`xdslctl info --state | grep ^Status: | cut -d ' ' -f2`
if [ "$status" == "Showtime" ]; then
break
fi
sleep 2
done
}
run_ddns_monitor(){
cd "${ROOT}/ipchange"
nohup ./monitor &>/dev/null 2>&1 &
}
# a bug in cm resets the EWAN port status to disabled on each reboot
# since i use it as a normal ethernet port, it must be manually re-enabled
enable_eth5(){
cmclient SET Device.Ethernet.Interface.5.Enable true
}
sleep 5
chroot $ROOT /bin/sh -c '/init_local.sh'
wait_xdsl_link
run_ddns_monitor
sleep 20
enable_eth5
#!/bin/sh /etc/rc.common
START=50
init_chroot(){
local root="$1"
[ -f "$root/init.sh" ] && [ -x "$root/init.sh" ] && (
echo "running init.sh on $root ..."
cd $root
sh init.sh
)
}
mount_usbdevs(){
for sysdev in `ls -1d /sys/block/sd*`; do
# safety check
[ ! -e "$sysdev/queue" ] && continue
dev=$(basename "$sysdev")
echo "probing $dev ..."
for syspart in `ls -1d $sysdev/sd*`; do
# safety check
[ ! -e "$syspart/partition" ] && continue
part=$(basename "$syspart")
[ ! -d /mnt/$part ] && mkdir /mnt/$part
grep -q /mnt/$part /proc/mounts || (
echo "mounting $part ..."
mount /dev/$part /mnt/$part
[ -d /mnt/$part/yaps-rootfs ] && init_chroot /mnt/$part/yaps-rootfs
)
done
done
}
boot(){
mount_usbdevs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment