Skip to content

Commit

Permalink
Merge pull request #7 from Krutyi-4el/universal
Browse files Browse the repository at this point in the history
Add linux_install.sh
  • Loading branch information
solaluset authored Jan 16, 2024
2 parents 035c9f5 + 8a83432 commit 75c2b75
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 106 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ This module allows you to:
- Run `mydns config` and update the file to your needs.
- Run `mydns restart` for changes to take effect.
- Run `mydns` to check resource usage. If CPU usage is high, wait a minute and check again. If it's still high, please open an issue in this repository.

## Installation (non-Android)
You can use `linux_install.sh` to install MyDNS for any Linux system.
Keep in mind that you will need to add `mydns start` to autostart manually.
4 changes: 2 additions & 2 deletions customize.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/system/bin/sh

# prepare data directory
. $MODPATH/utils.sh
. "$MODPATH/utils.sh"

set_perm $MODPATH/system/bin/mydns 0 0 777
set_perm "$MODPATH/mydns" 0 0 755
36 changes: 36 additions & 0 deletions linux_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh

if [ "$1" = "" ]; then
echo "Usage: $0 <installation directory>"
exit 1
fi

dest=$(realpath "$1")
if [ "$PREFIX" = "" ]; then
PREFIX=/usr/local
fi
PREFIX=$(realpath "$PREFIX")
echo "Installing to $dest"
echo "Prefix set to $PREFIX"

mkdir -p "$dest"
cd "$(dirname "$0")"

for file in *; do
if [ "$file" = "META-INF" ]; then
continue
fi
if [ "$file" = "default.conf" ]; then
sed -Ez 's/(upstream_server=[^\n]+\n)+/upstream_server=inherit\n/' default.conf > "$dest/default.conf"
continue
fi
if [ -f "$file" -a -x "$file" ]; then
install -o 0 -g 0 -m 755 "$file" "$dest"
sed -z -i 's|^#!/system/|#!/|' "$dest/$file"
else
cp -r "$file" "$dest"
fi
done

sed -i "s/ANDROID=1/ANDROID=0/;s/PREFIX=/PREFIX='$(echo "$PREFIX" | sed 's|/|\\/|g')'/" "$dest/utils.sh"
ln -fs "$dest/mydns" "$PREFIX/bin"
108 changes: 108 additions & 0 deletions mydns
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/system/bin/sh

path=$(readlink -f "$0")
MODDIR=${path%/*}
. "$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" ] && [ -d "/proc/$(cat $PIDFILE)" ]; then
return 0
else
return 1
fi
}

exit_code=0

case "$1" in
"start")
if check_running; then
echo "The service is already running."
exit_code=1
else
start
fi
;;
"stop")
if check_running; then
stop
else
echo "The service is not running."
exit_code=1
fi
;;
"restart")
if check_running; then
stop && start
else
echo "The service is not running."
exit_code=1
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."
exit_code=1
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."
exit_code=1
fi
fi
rm -r "$temp_dir"
;;
*)
echo -n "Status: "
if check_running; then
echo "running"
echo "Resources consumed:"

pid=$(cat "$PIDFILE")
ticks1=$(awk '{ print $14 + $15 }' /proc/$pid/stat)
sleep 0.2
ticks2=$(awk '{ print $14 + $15 }' /proc/$pid/stat)

TOTAL_MEM=$(awk '$1 == "MemTotal:" { print $2 }' /proc/meminfo)
used_mem=$(awk '$1 == "Pss:" { total += $2 }; END { print total }' /proc/$pid/smaps)

awk '
BEGIN {
"getconf CLK_TCK" | getline TICKS_PER_SEC
printf "CPU %4.0f%%\n", ('$ticks2' - '$ticks1') * 5 * 100 / TICKS_PER_SEC
printf "RAM %4.1f%%\n", '$used_mem' / '$TOTAL_MEM' * 100
}
'
else
echo "stopped"
fi
echo "Available commands: start, restart, stop, config"
if [ "$1" != "" ]; then
exit_code=1
fi
;;
esac

exit $exit_code
12 changes: 8 additions & 4 deletions post-fs-data.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#!/system/bin/sh
MODDIR=${0%/*}
. $MODDIR/utils.sh
. "$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"
# link back to module directory
# (to not depend on mount point)
real=$MODDIR/mydns
linked=$MODDIR/system/bin/mydns
if [ "$(readlink -f $linked)" != "$real" ]; then
ln -fs "$real" "$linked"
fi

# generate resolv.conf
upstream_servers=$(load_cfg_val upstream_server)
Expand Down
2 changes: 1 addition & 1 deletion service.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# This will make sure your module will still work
# if Magisk change its mount point in the future
MODDIR=${0%/*}
. $MODDIR/utils.sh
. "$MODDIR/utils.sh"

# This script will be executed in late_start service mode

Expand Down
86 changes: 0 additions & 86 deletions system/bin/mydns

This file was deleted.

1 change: 1 addition & 0 deletions system/bin/mydns
3 changes: 3 additions & 0 deletions uninstall.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ MODDIR=${0%/*}
. "$MODDIR/utils.sh"

rm -fr "$DATADIR"
if [ $ANDROID = 0 ]; then
rm -fr "$PREFIX/bin/mydns"
fi
47 changes: 34 additions & 13 deletions utils.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,43 @@ if [ "$MODPATH" != "" ]; then
elif [ "$MODDIR" = "" ]; then
MODDIR=${0%/*}
fi
DATADIR=/data/mydns
ANDROID=1
if [ $ANDROID = 1 ]; then
DATADIR=/data/mydns
SYSDIR=$MODDIR/system
else
DATADIR=$MODDIR
SYSDIR=""
PREFIX=
fi
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
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."
if [ ! -x "$DNSMASQ" ]; then
if [ $ANDROID = 1 ]; 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
else
DNSMASQ=dnsmasq
fi
fi
inherit_servers=0

load_cfg_val() {
if [ "$2" != "" ]; then
Expand All @@ -41,6 +54,10 @@ 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")
if [ "$upstream_servers" = "inherit" ]; then
inherit_servers=1
upstream_servers=$(grep -Po '(?<=nameserver )[^$]+$' /etc/resolv.conf)
fi
dnsmasq_args=$(load_cfg_val dnsmasq_args "$1")
}

Expand Down Expand Up @@ -76,13 +93,17 @@ check_config() {
}

write_resolv_conf() {
echo -n > "$MODDIR/system/etc/resolv.conf"
if [ "$inherit_servers" = "1" ]; then
return
fi
local resolv=$SYSDIR/etc/resolv.conf
echo -n > "$resolv"
local server
for server in $upstream_servers; do
echo "nameserver $server" >> "$MODDIR/system/etc/resolv.conf"
echo "nameserver $server" >> "$resolv"
done
}

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

0 comments on commit 75c2b75

Please sign in to comment.