跳转到内容

Mikrotik

5 posts with the tag “Mikrotik”

基于 RouterOS、OpenWrt、SmartDNS 与 FRP 的家庭网络架构

基于 RouterOS、OpenWrt、SmartDNS 与 FRP 的家庭网络架构

这篇文章记录一个围绕 MikroTik RouterOS、OpenWrt 旁路由、SmartDNS、Tailscale 和 FRP 搭建的家庭网络架构。它不是一份可以直接复制粘贴的配置教程,更关注“职责归属”:哪个设备应该负责路由、DNS、代理、IPv6、公网服务和恢复。

使用 SmartDNS 进行 DNS 分配

0. 简介Mikrotik的DNS始终是一个令人困惑的问题。我们将引入智能DNS来克服它。

Section titled “0. 简介Mikrotik的DNS始终是一个令人困惑的问题。我们将引入智能DNS来克服它。”

2. 智能DNS配置在本节中,我们将演示 DNS 组和流程。

Section titled “2. 智能DNS配置在本节中,我们将演示 DNS 组和流程。”

3. DNS 组和请求DNS 记录将分为 3 组:- [中国名单][中国名单] - 使用 SmartDNS 配置文件中配置的 CHINA 组的公共 DNS 服务器进行解析- [GFW 列表][GFW 列表] - 使用 SmartDNS 配置文件中配置的 GFW 组的外部 DNS 服务器进行解析- 其他的 - 使用 SmartDNS 配置文件中配置的 OverSea 组的混合 DNS 服务器进行解析

Section titled “3. DNS 组和请求DNS 记录将分为 3 组:- [中国名单][中国名单] - 使用 SmartDNS 配置文件中配置的 CHINA 组的公共 DNS 服务器进行解析- [GFW 列表][GFW 列表] - 使用 SmartDNS 配置文件中配置的 GFW 组的外部 DNS 服务器进行解析- 其他的 - 使用 SmartDNS 配置文件中配置的 OverSea 组的混合 DNS 服务器进行解析”
server-name SmartDNS
bind :53 -force-https-soa
cache-size 131072
cache-persist yes
cache-file /tmp/smartdns.cache
cache-checkpoint-time 120
prefetch-domain yes
serve-expired yes
serve-expired-ttl 0
serve-expired-reply-ttl 10
serve-expired-prefetch-time 86400
force-qtype-SOA 65
dualstack-ip-selection-threshold 10
dualstack-ip-allow-force-AAAA no
dualstack-ip-selection yes
rr-ttl 60
rr-ttl-min 1
rr-ttl-max 60
max-reply-ip-num 6
max-query-limit 0
response-mode fastest-response
log-level notice
log-file /var/log/smartdns/smartdns.log
log-size 128k
log-num 2
log-file-mode 644
audit-enable yes
audit-file /var/log/smartdns/smartdns-audit.log
audit-size 128k
audit-num 2
<!-- proxy-server socks5://127.0.0.1:1080 -name Proxy -->
server-https https://1.1.1.1/dns-query -group GFW -exclude-default-group -proxy Proxy -http-host one.one.one.one
server-tls 223.5.5.5:853 -group China -exclude-default-group
server-tls 8.8.8.8:853 -group OverSea
expand-ptr-from-address yes
mdns-lookup yes
speed-check-mode ping,tcp:80,tcp:443
domain-set -name chinalist -file /etc/smartdns/chinalist.conf
domain-set -name gfwlist -file /etc/smartdns/gfwlist.conf
nameserver /domain-set:chinalist/China
nameserver /domain-set:gfwlist/GFW
domain-rules /domain-set:gfwlist/ -speed-check-mode none

[2]. 基于RouterOS和Openwrt的IP地址列表分配 [中国列表]:https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf[GFW列表]:https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/gfw.txt

Mikrotik 上的容器

0. 简介从 Mikrotik ROS 7.4 Beta 4 开始,Mikrotik 添加了 Container 的新功能。

Section titled “0. 简介从 Mikrotik ROS 7.4 Beta 4 开始,Mikrotik 添加了 Container 的新功能。”

配置基于 Mikrotik RouterOS 7.13.1 。

使用以下命令登录 RouterOS 中的终端:``` /system/device-mode/update container=yes

> 如果在 X86 上使用容器,您将需要按下重置按钮或冷重启来确认设备模式。
## 2. 创建网络### 桥接模式#### (1).为容器添加bridge veth接口:```
/interface/veth/add name=veth1 address=172.17.0.2/24 gateway=172.17.0.1

(2)。为容器创建一个桥并向其中添加 veth:

Section titled “(2)。为容器创建一个桥并向其中添加 veth:”
/interface/bridge/add name=containers
/ip/address/add address=172.17.0.1/24 interface=containers
/interface/bridge/port add bridge=containers interface=veth1
/ip/firewall/nat/add chain=srcnat action=masquerade src-address=172.17.0.0/24

主机模式(Linux 设备上的 macvlan)

Section titled “主机模式(Linux 设备上的 macvlan)”
/interface/veth/add name=veth1 address=LAN_ADDRESS gateway=GATE_WAY_OF_LAN

(2)。将 veth 添加到 LAN 桥接器

Section titled “(2)。将 veth 添加到 LAN 桥接器”
/interface/bridge/port add bridge=LAN_BRIDGE interface=veth1
/container/mounts/add name=smartdns src=/smartdns.conf dst=/etc/smartdns/smartdns.conf
/container/config/set registry-url=https://registry-1.docker.io
/container/add remote-image=dashdreams/smartdns:latest interface=veth1 \
root-dir=/smartdns hostname=smartdns mounts=smartdns workdir=/root
/container/add file=smartdns.tar interface=veth1 root-dir=/smartdns \
hostname=smartdns mounts=smartdns workdir=/root
/container/start 0

6. 将端口转发到内部容器(可选)可以使用 dst-nat 转发端口(其中 192.168.88.1 路由器 IP 地址):

Section titled “6. 将端口转发到内部容器(可选)可以使用 dst-nat 转发端口(其中 192.168.88.1 路由器 IP 地址):”
/ip firewall nat
add action=dst-nat chain=dstnat dst-address=192.168.88.1 dst-port=5353 protocol=tcp to-addresses=172.17.0.2 to-ports=53
docker pull dashdreams/smartdns:latest@sha256:XXXXXXXXXXXXXXXXXXXXXXX
docker save dashdreams/smartdns > smartdns.tar

[2]. http://www.irouteros.com/?p=2712

[3]. 使用 Podman 构建容器映像

在 Mikrotik RouterOS v7.x 上配置 IPv6## 0. 简介Mikrotik RouterOS 上的 IPv6 配置比 ASUS 上的 Merlin 复杂得多。我们将在本文中配置 IPv6。

配置基于 Mikrotik RouterOS 7.6。

1. 配置 DHCPv6 客户端使用以下命令登录 RouterOS 中的终端:```

Section titled “1. 配置 DHCPv6 客户端使用以下命令登录 RouterOS 中的终端:```”

Accept ISP RA, Get IPv6 Address with SLAAC

Section titled “Accept ISP RA, Get IPv6 Address with SLAAC”

/ipv6 settings set accept-router-advertisements=yes

- IPv6 address pool: IPv6 (You can change to whatever you want)

Section titled “- IPv6 address pool: IPv6 (You can change to whatever you want)”

- Prefix length for DHCPv6 server’s offering cascading routers: 60

Section titled “- Prefix length for DHCPv6 server’s offering cascading routers: 60”

/ipv6 dhcp-client add add-default-route=no interface=pppoe-ether1 \ pool-name=IPv6 pool-prefix-length=60
request=prefix

江苏南京移动提供/60前缀,如果前缀长度小于/63,则无法使用DHCPv6。
## 2.添加必要的IPv6防火墙规则```
# Accept DHCPv6 client prefix delegation
/ipv6 firewall filter add action=accept chain=input \
comment="Accept DHCPv6 client prefix delegation" \
dst-port=546 protocol=udp src-address=fe80::/10

如果您使用默认脚本配置了路由器,则此规则是默认的。

/ipv6 address add address=::1 from-pool=IPv6 interface=bridge

## 4.配置DHCPv6-PD服务器```
# Delegate IPv6 addresses from pool `delegation`
# Lease is valid for 1 day
/ipv6 dhcp-server add address-pool=IPv6 \
interface=bridge lease-time=7d name=Home-IPv6

基于RouterOS和Openwrt的IP地址列表分发

0. 简介通过 CNIP 分配 IP 地址列表。

Section titled “0. 简介通过 CNIP 分配 IP 地址列表。”

配置基于 Mikrotik RouterOS 7.6。

1. 要求需要 Openwrt 的旁路路由器。

Section titled “1. 要求需要 Openwrt 的旁路路由器。”

2. 获取CN IP地址列表CN IP地址列表可以从以下位置找到

Section titled “2. 获取CN IP地址列表CN IP地址列表可以从以下位置找到”

[叮当网] [IPIP] [RealIP数据库] 在Linux上运行以下脚本可以获取CN IP列表:``` curl ’http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest'|\ grep ipv6 | grep CN | awk -F|
‘{ printf(“%s/%d\n”, $4, 32-log($5)/log(2)) }’ |
sed -e ‘s/^/add address=/g’ -e ‘s/$/ list=CNIP/g’|
sed -e $‘1i\\n/ipv6 firewall address-list’ \
-e $‘1i\\nremove [/ipv6 firewall address-list find list=CNIP]’|\
sed ‘$a /file remove CNIPv6.rsc’>CNIPv6.rsc

curl ’http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest'|\ grep ipv4 | grep CN | awk -F|
‘{ printf(“%s/%d\n”, $4, 32-log($5)/log(2)) }’ |\ sed -e ‘s/^/add address=/g’ -e ‘s/$/ list=CNIP/g’ \
-e ‘s/$/ comment=“CNIP”/g’ |\
sed -e $‘1i\\n/ip firewall address-list’ \
-e $‘1i\\nremove [/ip firewall address-list find list=CNIP]’ |\
sed ‘$a /’ |sed ‘$a /file remove CNIPv4.rsc’>CNIPv4.rsc

> 包含 IPv6 和 IPv4 地址。
地址列表可能如下所示:```
/ip firewall address-list
add list=CNIP address=x.x.x.x/xx
add list=CNIP address=x.x.x.x/xx
...
...
...
add list=CNIP address=x.x.x.x/xx

3. 分发配置### (1).导入 CN IP 列表打开 WinBox,并将 CNIPv4.rsc 上传到 RouterOS,然后打开Terminal,然后运行 ​​import CNIPv4.rsc。

Section titled “3. 分发配置### (1).导入 CN IP 列表打开 WinBox,并将 CNIPv4.rsc 上传到 RouterOS,然后打开Terminal,然后运行 ​​import CNIPv4.rsc。”

(2)。添加路由表在Terminal中运行命令:/routing/tables/add name=OP fib

Section titled “(2)。添加路由表在Terminal中运行命令:/routing/tables/add name=OP fib”

(3)。添加代理设备列表在Firewall中添加代理列表:```

Section titled “(3)。添加代理设备列表在Firewall中添加代理列表:```”

/ip/firewall/address-list/add name=proxy address=192.168.1.1-192.168.1.200

> 我们将 IP 地址范围 192.168.1.1--192.168.1.200 添加到代理列表中。
### (4)。 Mangle 非 CN IP 连接标记非CN IP目的地址```
add action=mark-routing chain=prerouting comment="ProxyRoute" \
src-address=proxy dst-address=!CNIP dst-address-type=!local \
new-routing-mark=OP passthrough=yes

添加另一条规则以接受openwrt的流量``` add action=accept chain=prerouting comment=“BypassOP”
src-address=192.168.1.254

> 假设openwrt的本地ip是`192.168.1.254`
### (5)。配置路由表配置到openwrt的路由表:```
/ip/route/add dst-address=0.0.0.0 gateway=192.168.1.254 \
check-getway=ping routing-table=OP distance=1 \
scope=30 target-scope=10

(6)。更改PPPOE距离将PPPOE的距离更改为2:```

Section titled “(6)。更改PPPOE距离将PPPOE的距离更改为2:```”

/interface/pppoe-client/set pppoe-CMCC default-route-distance=2

## 4. 如果绕过路由器关闭,则更改分配### (1).目的- openwrt down时的关闭分配- openwrt启动时开启分配
### (2)。配置网络监视```
/tool/netwatch/add host=192.168.1.254 type=simple interval=60 \
up-script=OPUP.rsc down-script=OPDOWN.rsc

(3)。编辑向上和向下脚本向上脚本:```

Section titled “(3)。编辑向上和向下脚本向上脚本:```”

add dont-require-permissions=yes name=OPUP.rsc owner=admin policy=
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=“/
ip firewall mangle enable number=???\r
\n/ip firewall mangle enable number=???\r
\n/ip route enable number=0\r
\n/ip/dns/cache/flush”

向下脚本:```
add dont-require-permissions=yes name=OPDOWN.rsc owner=admin policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="/\
ip firewall mangle disable number=???\r\
\n/ip firewall mangle disable number=???\r\
\n/ip route disable number=0\r\
\n/ip/dns/cache/flush"

???是[MangleRules]中设置的mangle规则的索引号

[2]. https://idndx.com/high-availability-when-split-routing-using-routeros-and-raspberrypi/

[3]. 使用 SmartDNS 进行 DNS 分配 [4]. IPv6-在 Mikrotik-RouterOS-v7.x 上配置 [Clang.CN]:https://ispip.clang.cn/all_cn_cidr.txt[IPIP]:https://github.com/17mon/china_ip_list[RealIP数据库]:https://github.com/metowolf/iplist[MangleRules]:基于 RouterOS 和 Openwrt.md#4-mangle-non-cn-ip-connections 的 IP 地址列表分发