From cc36f796fcdd201a6e1a4198d1d8cc4933f9b03f Mon Sep 17 00:00:00 2001 From: Krutyi 4el <60041069+Krutyi-4el@users.noreply.github.com> Date: Thu, 13 Apr 2023 23:30:15 +0300 Subject: [PATCH] add scripts --- customize.sh | 6 +++ default.conf | 9 +++++ post-fs-data.sh | 11 ++++++ service.sh | 41 ++++++++++++++++++++ system/bin/mydns | 86 +++++++++++++++++++++++++++++++++++++++++ system/etc/resolv.conf | 0 uninstall.sh | 6 +++ utils.sh | 88 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 247 insertions(+) create mode 100644 customize.sh create mode 100644 default.conf create mode 100644 post-fs-data.sh create mode 100644 service.sh create mode 100644 system/bin/mydns create mode 100644 system/etc/resolv.conf create mode 100644 uninstall.sh create mode 100644 utils.sh diff --git a/customize.sh b/customize.sh new file mode 100644 index 0000000..42e3380 --- /dev/null +++ b/customize.sh @@ -0,0 +1,6 @@ +#!/system/bin/sh + +# prepare data directory +. $MODPATH/utils.sh + +set_perm $MODPATH/system/bin/mydns 0 0 777 diff --git a/default.conf b/default.conf new file mode 100644 index 0000000..886727c --- /dev/null +++ b/default.conf @@ -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 diff --git a/post-fs-data.sh b/post-fs-data.sh new file mode 100644 index 0000000..7e6f0cf --- /dev/null +++ b/post-fs-data.sh @@ -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 diff --git a/service.sh b/service.sh new file mode 100644 index 0000000..634f96c --- /dev/null +++ b/service.sh @@ -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 diff --git a/system/bin/mydns b/system/bin/mydns new file mode 100644 index 0000000..d9eb576 --- /dev/null +++ b/system/bin/mydns @@ -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 & +} + +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 diff --git a/system/etc/resolv.conf b/system/etc/resolv.conf new file mode 100644 index 0000000..e69de29 diff --git a/uninstall.sh b/uninstall.sh new file mode 100644 index 0000000..15d01f3 --- /dev/null +++ b/uninstall.sh @@ -0,0 +1,6 @@ +#!/system/bin/sh +MODDIR=${0%/*} + +. "$MODDIR/utils.sh" + +rm -fr "$DATADIR" diff --git a/utils.sh b/utils.sh new file mode 100644 index 0000000..65fad3a --- /dev/null +++ b/utils.sh @@ -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 +}