#!/bin/bash

echo "請先幫忙輸入 sudo 密碼"
echo "(輸入密碼時不會顯示出來 因此不要管螢幕畫面 直接照打 輸入完後直接按下 enter 鍵)"
sudo echo ""

FILE=linux-amd64.tar.gz
URI=`curl -s https://api.github.com/repos/AdguardTeam/dnsproxy/releases/latest | grep browser_download_url | grep linux-amd64 | cut -d'"' -f4`
wget -O $FILE $URI
tar -zxf $FILE
sudo mv linux-amd64/dnsproxy /usr/bin
rm $FILE
rm -R linux-amd64

echo "
[Unit]
Description=華麥 DNS 服務 - 提供安全查詢的好服務
After=default.target

[Service]
ExecStart=/usr/bin/dnsproxy -l 127.0.0.1 -u https://chdn.chunghwamc.com/chdn-query --http3 -b tls://1.0.0.1 -f tls://1.0.0.1 --cache --cache-size=16777216 --timeout=1s

[Install]
WantedBy=default.target
" > chunghwamc_dns.service
sudo mv chunghwamc_dns.service /etc/systemd/system

sudo systemctl daemon-reload
sudo systemctl enable --now chunghwamc_dns
sudo systemctl disable --now systemd-resolved

echo "nameserver 127.0.0.1
nameserver 8.8.8.8
options edns0 trust-ad
search .
" > resolv.conf
sudo mv /etc/resolv.conf /etc/resolv.conf.backup
sudo mv resolv.conf /etc

echo 完成
