Created
July 31, 2019 23:46
-
-
Save CanadianJeff/9bce25f261c8e72990292755faee24e3 to your computer and use it in GitHub Desktop.
OpenWRT WIP Extras
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
echo 'Content-type: text/html' | |
echo '' | |
saveIFS=$IFS | |
IFS='&' | |
eval "$QUERY_STRING" | |
IFS=$saveIFS | |
ACTION=${action} | |
IP=${ip} | |
RULESET=${ruleset} | |
TTL=${ttl} | |
RULESETS=$(ipset list -t | awk '/Name:/ {print $2}') | |
htmlrow='<div class="row">' | |
htmlcol25='<div class="col-25">' | |
htmlcol75='<div class="col-75">' | |
htmlipform() | |
{ | |
echo -e '\t<form>' | |
echo -e "\t\t$htmlrow" | |
echo -e "\t\t\t$htmlcol25" | |
echo -e '\t\t\t\t<label for="ip">IPV4 Address</label>' | |
echo -e '\t\t\t</div>' | |
echo -e "\t\t\t$htmlcol75" | |
echo -e '\t\t\t\t<input type="text" id="ip" name="ip" value="'$IP'" autocomplete="off">' | |
echo -e '\t\t\t</div>' | |
echo -e '\t\t</div>' | |
echo -e "\t\t$htmlrow" | |
echo -e "\t\t\t$htmlcol25" | |
echo -e '\t\t\t\t<label for="_ruleset">Which Ruleset</label>' | |
echo -e '\t\t\t</div>' | |
echo -e "\t\t\t$htmlcol75" | |
echo -e '\t\t\t\t<select id="_ruleset" name="ruleset">' | |
for RULESET in $RULESETS; do | |
echo -e '\t\t\t\t\t<option value="'$RULESET'">'$RULESET'</option>' | |
done | |
echo -e '\t\t\t\t</select>' | |
echo -e '\t\t\t</div>' | |
echo -e '\t\t</div>' | |
echo '<input name="action" type="submit" value="add">' | |
echo '<input name="action" type="submit" value="del">' | |
echo '<input name="action" type="submit" value="test">' | |
echo '<input name="action" type="submit" value="view">' | |
echo -e '\t</form>' | |
} | |
htmlasnform() | |
{ | |
echo -e '\t<form>' | |
echo -e "\t\t$htmlrow" | |
echo -e "\t\t\t$htmlcol25" | |
echo -e '\t\t\t\t<label for="_asn">BGP AS Number</label>' | |
echo -e '\t\t\t</div>' | |
echo -e "\t\t\t$htmlcol75" | |
echo -e '\t\t\t\t<input type="number" id="_asn" name="asn" value="'$ASN'" autocomplete="off">' | |
echo '<input name="action" type="submit" value="block">' | |
echo '<input name="action" type="submit" value="allow">' | |
} | |
download() | |
{ | |
echo 'Downloading IPs. . . .<br \>' | |
wget -q -O /tmp/"$ASN".db "http://www.enjen.net/asn-blocklist/index.php?asn=$ASN&type=iplist&api=1" | |
sed -i '/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/!d' /tmp/"$ASN".db | |
echo '' | |
} | |
## Starting The Layout Here | |
#echo '<link href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.6/simplex/bootstrap.min.css" rel="stylesheet">' | |
echo '<link href="/style.css" rel="stylesheet">' | |
echo '' | |
echo '<div class="container">' | |
if [ "$QUERY_STRING" = '' ]; then | |
htmlipform | |
htmlasnform | |
fi | |
echo '</div>' | |
echo '' | |
echo '' | |
echo '' | |
exit | |
if [ "$ACTION" = 'block|allow' ]; | |
then | |
unset _iplist | |
_iplist=/tmp/$ASN.db | |
[ ! -f "$_iplist" ] && { quit 1 } | |
[ -f "$_iplist" ] && { printf " * Loading AS%s-ruleset\\n"; | |
while IFS= read -r line; do | |
ip=$(echo "$line" | { read -r first rest ; echo "$first" ; }) | |
#set -x | |
ipset add $ASN -exist "$ip" | |
#set +x | |
done <"$_iplist"; | |
} | |
fi | |
if [ "$ACTION" = 'add' ] | |
then | |
ipset add $RULESET $IP timeout $TTL 1>&1 2>&1; | |
fi | |
if [ "$ACTION" = 'del' ] | |
then | |
ipset del $RULESET $IP 1>&1 2>&1; | |
fi | |
if [ "$ACTION" = 'list' ] | |
then | |
echo "<pre>" | |
ipset list $RULESET -o plain 1>&1 2>&1; | |
echo "</pre>" | |
fi | |
if [ "$ACTION" = 'test' ] | |
then | |
echo "<pre>" | |
ipset test $RULESET $IP 1>&1 2>&1; | |
echo "</pre>" | |
fi | |
if [ "$ACTION" = 'save' ] | |
then | |
echo "<pre>" | |
ipset save $RULESET 1>&1 2>&1; | |
echo "</pre>" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment