-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
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
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 |
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
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 |
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
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 |
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
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 |
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
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.
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
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" |
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
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 | ||
} |