Xiangxiang's Personal Site

Machine Learning & Security Engineer
生命不息,折腾不止,留下一点活着的记录.

View on GitHub
1 November 2019

[网络折腾]Openwrt settings(持续更新中)

by xiangxiang

上网究竟有多难,看完本文才知道

0x00 首次设置

# SSH至openwrt
# 使用sed替换opkg源地址
sed -i 's_downloads\.openwrt\.org_mirrors.ustc.edu.cn/lede_' /etc/opkg/distfeeds.conf

# 验证更换opkg源地址成功
opkg update
opkg install luci
/etc/init.d/uhttpd enable
/etc/init.d/uhttpd start

0x01 科学上网

opkg install shadowsocks-libev-ss-local shadowsocks-libev-ss-redir shadowsocks-libev-ss-rules shadowsocks-libev-config shadowsocks-libev-ss-tunnel shadowsocks-libev-ss-server
opkg install luci-app-shadowsocks-libev
touch /root/update_ignore_list
chmod +x /root/update_ignore_list

使用如下的脚本并加入定时任务

#!/bin/sh

set -e -o pipefail

wget -O- 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | awk -F\| '/CN\|ipv4/ { printf("%s/%d\n", $4, 32-log($5)/log(2)) }' >  /tmp/ignore.list

mv /tmp/ignore.list /etc/

if pidof ss-redir>/dev/null; then
    /etc/init.d/shadowsocks-libev restart
fi
Source settings -> Scr default设置为checkdst
Destination settings -> Dst ip/net bypass file设置为/etc/ignore.list
                     -> Dst default设置为forward

0x02 DNS

dnscrypt-proxy

opkg update
opkg install dnscrypt-proxy2
# 安装依赖的CA证书
opkg install ca-bundle
cd /tmp
# 根据实际情况选择版本
wget https://github.com/DNSCrypt/dnscrypt-proxy/releases/download/2.0.45/dnscrypt-proxy-linux_arm64-2.0.45.tar.gz -O dnscrypt-proxy.tar.gz 
tar -zxvf dnscrypt-proxy.tar.gz 
cp linux-arm64/dnscrypt-proxy /usr/sbin/dnscrypt-proxy # 注意路径
wget https://raw.githubusercontent.com/etam/DNS-over-HTTPS-for-OpenWRT/master/dnscrypt-proxy -O /etc/init.d/dnscrypt-proxy
chmod +x /usr/sbin/dnscrypt-proxy
chmod +x /etc/init.d/dnscrypt-proxy
cp linux-arm64/example-dnscrypt-proxy.toml /etc/config/dnscrypt-proxy.toml
server_names = ['google', 'yandex', 'cloudflare']     # 取消对于server_names的注释
listen_addresses = ['127.0.0.1:55553', '[::1]:55553'] # 端口不要和其它服务冲突
force_tcp = false                                     # 国内网络环境下可以全部走tcp
proxy = 'socks5://127.0.0.1:1080'                     # 建议使用代理
fallback_resolver = ['223.5.5.5:53', '1.1.1.1:53']    # fallback的DNS也可以设置为国内DNS
ignore_system_dns = true                              # 不使用ISP提供的DNS
netprobe_address = '114.114.114.114:53'               # 用于检测网络的地址,也建议更换为国内的DNS
cache = false                                         # ChinaDNS的机制要求上游DNS服务器禁用缓存
# 验证配置文件语法
dnscrypt-proxy -config /etc/config/dnscrypt-proxy.toml -check
dnscrypt-proxy -config /etc/config/dnscrypt-proxy.toml -resolve www.google.com
/etc/init.d/dnscrypt-proxy enable
/etc/init.d/dnscrypt-proxy start

china-dns

wget http://openwrt-dist.sourceforge.net/openwrt-dist.pub
opkg-key add openwrt-dist.pub

# use the following command to get architecture:
opkg print-architecture | awk '{print $2}'
# 比如树莓派是aarch64_cortex-a53

# 加入源,注意替换aarch64_cortex-a53
echo "src/gz openwrt_dist http://openwrt-dist.sourceforge.net/packages/base/aarch64_cortex-a53" >>/etc/opkg/customfeeds.conf
echo "src/gz openwrt_dist_luci http://openwrt-dist.sourceforge.net/packages/luci" >>/etc/opkg/customfeeds.conf
opkg update
opkg install ChinaDNS
opkg install luci-app-chinadns
wget -O- 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | awk -F\| '/CN\|ipv4/ { printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > /etc/chinadns_chnroute.txt

修改/etc/dnsmasq.conf

# No forward list:
server=/lan/
server=/internal/
server=/intranet/
server=/private/
server=/workgroup/
server=/10.in-addr.arpa/
server=/16.172.in-addr.arpa/
server=/168.192.in-addr.arpa/
server=/254.169.in-addr.arpa/
server=/d.f.ip6.arpa/

0x03 上海电信桥接及IPTV设置

这个比较迷默认情况下会导致DNS行为和预期的不一致
root@OpenWrt:/etc# cat /etc/resolv.conf
# Interface wan
nameserver 116.228.111.118
nameserver 180.168.255.18
config switch_vlan
        option device 'switch0'
        option vlan '3'
        option vid '51'
        option ports '1t 2t 3t 4t 0t'

config switch_vlan
        option device 'switch0'
        option vlan '4'
        option vid '85'
        option ports '6t 1t 2t 3t 4t 0t'
# 如果要DHCP拿VLAN85的IP, 需要将CPU也tag上

config interface 'IPTV_VLAN51'
        option proto 'none'
        option ifname 'eth0.51 eth3.51'
        option type 'bridge'
        option delegate '0'

config interface 'IPTV_VLAN85'
        option proto 'none'
        option ifname 'eth0.85 eth3.85'
        option type 'bridge'
        option delegate '0'

dhcp-option-force=125,00:00:00:00:1a:02:06:48:47:57:2d:43:54:03:04:5a:58:48:4e:0a:02:20:00:0b:02:00:55:0d:02:00:2e:3c:1e:00:00:01:00:02:03:43:50:45:03:0e:45:38:20:45:50:4f:4e:20:52:4f:55:54:45:52:04:03:31:2e:30
dhcp-option=15
dhcp-option=28
dhcp-option=60,00:00:01:06:68:75:61:71:69:6E:02:0A:48:47:55:34:32:31:4E:20:76:33:03:0A:48:47:55:34:32:31:4E:20:76:33:04:10:32:30:30:2E:55:59:59:2E:30:2E:41:2E:30:2E:53:48:05:04:00:01:00:50

安装udpxy

opkg install udpxy
opkg install luci-app-udpxy

新建一个WAN_IPTV, 通过DHCP获取到VLAN85的IP

设置udpxy

设置防火墙, 放行5140端口

设置MWAN3, 使得正常流量不会走WAN_IPTV

0x04 多wan口设置

opkg update
opkg install mwan3
opkg install luci-app-mwan3

  1. step 2: 利用刚刚空余出来的网卡新建一个VLAN,也就是图中VLAN3

  1. step 3: 重启路由器

  2. step 4: 在Network->Interface中新建一个interface,取一个名字如wanb

  3. step 5: 在该interfaceFirewall settings中设置为wan的firewall zone

  4. step 6: 设置由路由器自身发起的包默认从哪里发起

  5. step 7: 在Network->Interface中两个WAN口的Advanced SettingsUse gateway metric必须不同,可以分别是10和20

0x05 端口聚合设置

opkg install proto-bonding
# 查看链路聚合状态
cat /sys/class/net/bond0/bonding/mode
cat /proc/net/bonding/bond1
ip link add bond1 type bond
ip link set eth0 master bond1
ip link set eth1 master bond1
ip link add bond2 type bond mode 802.3ad
ip link set eth2 master bond2
ip link set eth3 master bond2
exit 0

0x06 网络速度测试

opkg install iperf3
# 服务端
iperf3 -s

# client端
iperf3 -c server_ip
tags: openwrt chinadns dnscrypt-proxy dnsmasq