Mới chuyển qua golang để tận dụng mấy cái API làm chức năng đồng bộ cho nhiều profile.
Share lại cái script cũ đã chạy mấy ngày nay cho bạn nào dùng OpenWrt nha.
Bác nào rảnh viết thành cái script full tự cài luôn cho mọi người cũng được, em đang lười quá.
Bước 1: Cài đặt https-dns-proxy
Bash:
opkg update && opkg install https-dns-proxy
Bước 2: Tạo thư mục chứa script
Bước 3: Tạo file script
Gõ lệnh sau:
Bash:
vi /opt/scripts/nextdns.sh
Tiếp tục bấm phím
để chuyển sang chế độ chỉnh sửa tập tin
Dán đoạn code bên dưới vào
Bash:
#!/bin/sh
find_next_id() {
local input_id="$1"
local next_id=""
local nextdns_ids="/opt/scripts/nextdns_ids.txt"
line_number=$(grep -n "$input_id" "$nextdns_ids" | cut -d ':' -f 1)
if [ -z "$line_number" ]; then
line_number=0
fi
next_line=$((line_number % $(wc -l < "$nextdns_ids") + 1))
next_id=$(sed -n "${next_line}p" "$nextdns_ids")
echo "$next_id"
}
config_file=$(cat /etc/config/https-dns-proxy)
current_id="${config_file#*nextdns\.io/}"; current_id="${current_id%%/*}"
next_id=$(find_next_id "$current_id")
echo "Change NextDNS from [$current_id] to [$next_id]"
logger -t NextDNS "Change NextDNS from [$current_id] to [$next_id]"
while uci -q delete https-dns-proxy.@https-dns-proxy[0]; do :; done
uci set https-dns-proxy.config.force_dns="0"
uci add https-dns-proxy https-dns-proxy > /dev/null
uci set https-dns-proxy.@https-dns-proxy[-1].resolver_url="https://dns1.nextdns.io/$next_id/OpenWrt"
uci set https-dns-proxy.@https-dns-proxy[-1].bootstrap_dns="8.8.8.8,1.1.1.1"
uci set https-dns-proxy.@https-dns-proxy[-1].listen_addr="127.0.0.1"
uci set https-dns-proxy.@https-dns-proxy[-1].listen_port="5053"
uci add https-dns-proxy https-dns-proxy > /dev/null
uci set https-dns-proxy.@https-dns-proxy[-1].resolver_url="https://dns2.nextdns.io/$next_id/OpenWrt"
uci set https-dns-proxy.@https-dns-proxy[-1].bootstrap_dns="8.8.8.8,1.1.1.1"
uci set https-dns-proxy.@https-dns-proxy[-1].listen_addr="127.0.0.1"
uci set https-dns-proxy.@https-dns-proxy[-1].listen_port="5054"
uci commit https-dns-proxy
/etc/init.d/https-dns-proxy restart
Bấm lần lượt các phím: Esc => gõ :wq => Enter
Gõ lệnh sau để chmod file nextdns.sh có thể chạy:
Bash:
chmod +x /opt/scripts/nextdns.sh
Bước 4: Tạo danh sách các id NextDNS
Gõ lệnh sau:
Bash:
vi /opt/scripts/nextdns_ids.txt
Tiếp tục bấm phím để chuyển sang chế độ chỉnh sửa tập tin
Dán các id của NextDNS vào, mỗi id 1 dòng nhé.
Ví dụ:
Bấm lần lượt các phím: Esc => gõ :wq => Enter
Để chạy test thử xem có lỗi gì không thì gõ lệnh
Bước 5: Tạo cron tự động xoay tua theo khung giờ
Vào System => Scheduled Tasks thêm dòng bên dưới và bấm Save.
Bash:
0 3 * * * /opt/scripts/nextdns.sh
Ví dụ dòng trên là script sẽ tự chạy vào lúc 3h00.