-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadb_setup.sh
119 lines (96 loc) · 3.67 KB
/
adb_setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/sh
# adblock setup for OAK 19.07
# Function to update opkg with retry logic
retry_update_opkg() {
local max_retries=3
local count=0
local success=0
while [ $count -lt $max_retries ]; do
echo "Updating opkg (attempt $(($count + 1))/$max_retries)"
opkg update && success=1 && break
count=$(($count + 1))
echo "opkg update failed. Retrying..."
done
if [ $success -eq 0 ]; then
echo "Failed to update opkg after $max_retries attempts."
return 1
fi
}
# Function to install a package with retry logic
retry_install() {
local package=$1
local max_retries=3
local count=0
local success=0
while [ $count -lt $max_retries ]; do
echo "Installing $package (attempt $(($count + 1))/$max_retries)"
opkg install $package && success=1 && break
count=$(($count + 1))
echo "$package installation failed. Retrying..."
done
if [ $success -eq 0 ]; then
echo "Failed to install $package after $max_retries attempts."
return 1
fi
}
# Update and install Adblock and Luci
retry_update_opkg || exit 1
retry_install adblock
retry_install luci-app-adblock
# Add TOFU filter
cd /etc/adblock
gunzip /etc/adblock/adblock.sources.gz
sed -i '$ d' /etc/adblock/adblock.sources # Remove the last }
echo ' ,"tofu": {
"url": "https://raw.githubusercontent.com/tofukko/filter/master/Adblock_Plus_list.txt",
"rule": "BEGIN{FS=\"[|^]\"}/^\\|\\|([[:alnum:]_-]{1,63}\\.)+[[:alpha:]]+\\^(\\$third-party)?$/{print tolower($3)}",
"size": "S",
"focus": "ads_analysis",
"descurl": "https://github.com/tofukko/filter"
}' >> /etc/adblock/adblock.sources
echo "}" >> /etc/adblock/adblock.sources
gzip /etc/adblock/adblock.sources
cd /
# uci -q del_list adblock.global.adb_sources='tofu'
uci add_list adblock.global.adb_sources='tofu'
uci commit adblock
# Get the router's current IPv4 and IPv6 addresses
ipv4_address=$(uci get network.lan.ipaddr)
ipv6_address=$(ip -6 addr show br-lan | grep 'inet6' | awk '{print $2}' | grep -v '^fe80' | cut -d'/' -f1)
# Remove existing DNS settings for LAN
uci -q delete dhcp.lan.dhcp_option
uci -q delete dhcp.lan.dns
# Add the router's IPv4 and IPv6 as DNS servers
uci add_list dhcp.lan.dhcp_option="6,$ipv4_address"
if [ -n "$ipv6_address" ]; then
uci add_list dhcp.lan.dns="$ipv6_address"
fi
uci set adblock.global.adb_trigger='lan'
uci set adblock.global.adb_dns='dnsmasq'
uci set adblock.global.adb_forcedns='1'
# Commit changes and restart services
/etc/init.d/adblock enable
/etc/init.d/adblock restart
uci commit dhcp
/etc/init.d/dnsmasq restart
# Define the cron job command
cron_job="50 3 * * * /etc/init.d/adblock reload"
# Check if the cron job already exists in the crontab
if crontab -l | grep -Fq "$cron_job"; then
echo "Cron job already exists. No changes made."
else
# If the cron job does not exist, add it
(crontab -l 2>/dev/null; echo "$cron_job") | crontab -
echo "Cron job added to refresh Adblock every day at 3:50 AM."
fi
echo "Adblock DNS filtering with TOFU enabled for LAN using $ipv4_address (IPv4) and $ipv6_address (IPv6)."
echo "The automatic configuration of Adblock is complete. It will take a few minutes after a restart for the custom filter to start working." && echo "Adblockの自動設定が完了しました。カスタムフィルタの動作反映まで再起動後、数分かかります。"
# Prompt the user for confirmation to reboot
read -p "再起動を実行しますか?(N/y): " choice
# Handle the user's input for reboot
if [[ "$choice" == "y" || "$choice" == "Y" ]]; then
echo "Rebooting the system..."
/sbin/reboot
else
echo "Reboot canceled."
fi