2017年2月15日星期三

EdgeRouter 使用dnsmasq 进行广告过滤

参考:https://help.ubnt.com/hc/en-us/articles/205223340-EdgeRouter-Ad-blocking-content-filtering-using-EdgeRouter


第一步,获取并生成conf配置文件:
#!/bin/bash
# define variables
host_version=0
host_new=0
host_url="
https://raw.githubusercontent.com/vokins/yhosts/master/hosts"

cd /root/hosts
echo
date
echo
echo +++ Delete old files...
echo
rm -f temp1 temp2 temp3 result host
rm -f hosts*

echo +++ Download new hosts file...
echo
wget $host_url

echo +++ Check if download succeeded...
echo
if [ ! -f hosts ]; then
    echo --- Download failed... Exit.
    exit
fi

host_version=$(sed -n '1p' version | awk -F"=" '{print $2}')
cat hosts|grep version|sed -n 1p > version
host_new=$(sed -n '1p' version | awk -F"=" '{print $2}')

echo +++ Version check:
echo +++ Old: $host_version, New: $host_new

# check hosts version
if [ "$host_version"x = "$host_new"x ]; then
    echo --- No update for hosts. Exit
    exit
fi



########################################################
# following lines are just shell learning, ignore.
# get column 2, space as delimiter.
# cat hosts|cut -d ' ' -f 2
#
# compress extra space
# cat hosts|tr -s ' '
#
# compress extra space and get column 2, space as delimiter
# cat hosts|tr -s ' '|cut -d ' ' -f 2
#
# remove comment line start with "#"
# cat hosts| grep -v ^# | grep -v ^$
########################################################

echo
echo +++ Split hosts file to two column file.
cat hosts| grep -v ^# | grep -v ^$|tr -s ' '|cut -d ' ' -f 1 >temp1
cat hosts| grep -v ^# | grep -v ^$|tr -s ' '|cut -d ' ' -f 2 >temp2

# combine two files and add "address=/" at beginning of each line
echo +++ Combine two column file to one.
paste temp2 temp1 -d "/" | sed 's/^/address=\/&/g' >temp3
echo +++ Add self defined hosts
cat self temp3 > result
# remove ipv6 address and useless hosts
echo +++ Remove useless hosts.
sed -e '/\:/d' result | sed -e '/XiaoQiang/d' | sed -e '/localhost/d' > conf

echo +++ Delete temp files.
rm -f temp1 temp2 temp3 result

echo
echo +++ Copy files to web folder
cp -f ./version /usr/share/nginx/html/hosts/
cp -f ./conf /usr/share/nginx/html/hosts/
echo --- Hosts conf file updated.




将上述脚本设置为Linux开机启动。


第二步,配置EdgeRouter

移动光猫改桥接模式

使用以下管理用户名和密码登录移动光猫: 用户名:CMCCAdmin 密码:aDm8H%MdA 登陆后,查看“网络-宽带设置”,选择名称中含有“INTERNET”的连接名称,截图记录原有INTERNET设置: 截图记录后,删除此配置。 参考原有配置,新建一个INTERNET连接,并...