Skip to content

Commit

Permalink
add scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
solaluset committed Apr 13, 2023
1 parent d8be6be commit cc36f79
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 0 deletions.
6 changes: 6 additions & 0 deletions customize.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/system/bin/sh

# prepare data directory
. $MODPATH/utils.sh

set_perm $MODPATH/system/bin/mydns 0 0 777
9 changes: 9 additions & 0 deletions default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# upstream servers (may be specified several times)
upstream_server=8.8.8.8
upstream_server=8.8.4.4
# args directly passed to dnsmasq executable
dnsmasq_args=--domain-needed --bogus-priv --cache-size=1000
# port of the local server
server_port=5353
# port for outgoing connections made by server
output_port=5354
11 changes: 11 additions & 0 deletions post-fs-data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/system/bin/sh
MODDIR=${0%/*}
. $MODDIR/utils.sh

# point the script back to module directory
# (in case mount point gets changed)
sed -i "s|=.*MODDIR_PLACEHOLDER|=$MODDIR # MODDIR_PLACEHOLDER|" "$MODDIR/system/bin/mydns"

# generate resolv.conf
upstream_servers=$(load_cfg_val upstream_server)
write_resolv_conf
41 changes: 41 additions & 0 deletions service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/system/bin/sh
# Do NOT assume where your module will be located.
# ALWAYS use $MODDIR if you need to know where this script
# and module is placed.
# This will make sure your module will still work
# if Magisk change its mount point in the future
MODDIR=${0%/*}
. $MODDIR/utils.sh

# This script will be executed in late_start service mode

add_iptables_redirect() {
local protocol=$1
local address=$2
local dest_port=$3
local src_port=$4

local command="iptables -A OUTPUT -w -t nat -p $protocol --dport 53 -j DNAT --to-destination $address:$dest_port"
if [ "$src_port" != "" ]; then
command="$command --sport $src_port --destination $address"
fi
eval "$command"
echo "$command" | sed "s/-A/-D/" >> "$RESTORE_IPTABLES"
}

# clear restore file
echo -n > "$RESTORE_IPTABLES"

load_config

# only output_port will be able to communicate with outer world
for server in $upstream_servers; do
add_iptables_redirect tcp $server 53 $output_port
add_iptables_redirect udp $server 53 $output_port
done

# redirect all outgoing connections to local server
add_iptables_redirect tcp 127.0.0.1 $server_port
add_iptables_redirect udp 127.0.0.1 $server_port

run_dnsmasq
86 changes: 86 additions & 0 deletions system/bin/mydns
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/system/bin/sh

if [ "$ASH_STANDALONE" = "" ]; then
ASH_STANDALONE=1 /data/adb/magisk/busybox ash "$0" "$1"
exit
fi

MODDIR=MODDIR_PLACEHOLDER
. $MODDIR/utils.sh

start() {
sh $MODDIR/service.sh </dev/null &>/dev/null &
}

stop() {
sh $RESTORE_IPTABLES
kill $(cat $PIDFILE)
rm -f $PIDFILE
}

check_running() {
if [ -f $PIDFILE ]; then
return 0
else
return 1
fi
}

case "$1" in
"start")
if check_running; then
echo "The service is already running."
else
start
fi
;;
"stop")
if check_running; then
stop
else
echo "The service is not running."
fi
;;
"restart")
if check_running; then
stop && start
else
echo "The service is not running."
fi
;;
"config")
if [ "$EDITOR" = "" ]; then
EDITOR=nano
fi
temp_dir=$(mktemp -d)
temp_conf="$temp_dir/$(basename $CONFIG)"
cp $CONFIG "$temp_conf"
if [ "$(command -v "$EDITOR")" = "" ]; then
echo "Editor '$EDITOR' not found."
echo "Point to your text editor via EDITOR variable."
else
"$EDITOR" "$temp_conf"
load_config "$temp_conf"
if check_config; then
cp "$temp_conf" $CONFIG
write_resolv_conf
echo "Config was updated."
else
echo "Config was not updated."
fi
fi
rm -r "$temp_dir"
;;
*)
echo -n "Status: "
if check_running; then
echo "running"
echo "Resources consumed:"
top -b -n 2 -d 0.2 | grep -E "^ *$(cat $PIDFILE) " | tail -1 \
| awk '{"nproc" | getline n; print "CPU " $8 * n "%\nRAM " $6 "%"}'
else
echo "stopped"
fi
echo "Available commands: start, restart, stop, config"
;;
esac
Empty file added system/etc/resolv.conf
Empty file.
6 changes: 6 additions & 0 deletions uninstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/system/bin/sh
MODDIR=${0%/*}

. "$MODDIR/utils.sh"

rm -fr "$DATADIR"
88 changes: 88 additions & 0 deletions utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
if [ "$MODPATH" != "" ]; then
MODDIR=$MODPATH
elif [ "$MODDIR" = "" ]; then
MODDIR=${0%/*}
fi
DATADIR=/data/mydns
CONFIG=$DATADIR/mydns.conf
PIDFILE=$DATADIR/dnsmasq.pid
RESTORE_IPTABLES=$DATADIR/restore_iptables

mkdir -p $DATADIR
if [ ! -f $CONFIG ]; then
cp $MODDIR/default.conf $CONFIG
fi

if [ "$(command -v ui_print)" = "" ]; then
alias ui_print=echo
fi

DNSMASQ=$DATADIR/dnsmasq
if [ ! -x $DNSMASQ ]; then
cp /data/data/com.termux/files/usr/bin/dnsmasq $DATADIR
if [ $? != 0 ]; then
ui_print "WARNING: dnsmasq not found in Termux."
ui_print "WARNING: Standard dnsmasq may cause abnormal CPU usage."
ui_print "WARNING: Install dnsmasq in Termux and reflash the module."
DNSMASQ=dnsmasq
fi
fi

load_cfg_val() {
if [ "$2" != "" ]; then
local file="$2"
else
local file="$CONFIG"
fi
sed -n "s|^$1=||p" "$file"
}

load_config() {
server_port=$(load_cfg_val server_port "$1")
output_port=$(load_cfg_val output_port "$1")
upstream_servers=$(load_cfg_val upstream_server "$1")
dnsmasq_args=$(load_cfg_val dnsmasq_args "$1")
}

check_config() {
local has_servers=0
local server
for server in $upstream_servers; do
has_servers=1
if ! echo ".$server" | grep -Eq '^(\.[0-9]{1,3}){4}$'; then
echo "'$server' doesn't look like a valid IP address."
return 1
fi
done
if [ $has_servers = 0 ]; then
echo "No upstream servers specified."
return 1
fi
local port
for port in "$server_port" "$output_port"; do
if ! echo "$port" | grep -Eq '^[0-9]+$'; then
echo "'$port' doesn't look like a valid port."
return 1
fi
done
if [ "$server_port" = "$output_port" ]; then
echo "server_port and output_port cannot be the same."
return 1
fi
if ! run_dnsmasq --test; then
return 1
fi
return 0
}

write_resolv_conf() {
echo -n > "$MODDIR/system/etc/resolv.conf"
local server
for server in $upstream_servers; do
echo "nameserver $server" >> "$MODDIR/system/etc/resolv.conf"
done
}

run_dnsmasq() {
$DNSMASQ --pid-file=$PIDFILE --port $server_port -Q $output_port $dnsmasq_args $1
}

0 comments on commit cc36f79

Please sign in to comment.