Contents

Apple Silicon 虚拟旁路网关搭建

前言

前段时间一直在忙,而且因为你懂的原因,ip 封锁也很严重,也没太多时间弄这个 vps。就趁现在到杭州的间隙,搞一搞这个 vps 顺便用 Mac 实现一个旁路网关,省下一个软路由。

通过 UTM 方式安装 OpenWrt,会有磁盘容量过小的问题

  • 解决方案一:解压出镜像后安装之前,执行命令:qemu-img resize openwrt-22.03.4-armvirt-64-rootfs-squashfs.img +1G
  • 也可以参考:Change disk image size #2636

报错:OpenWRT br-lan: received packet on eth0 with own address as source address

  • 可能是虚拟机的虚拟网卡 mac 地址跟真实网卡 mac 地址冲突了,虚拟网卡可以随机生成一个新的 mac 地址,然后重启即可

根据t帖子:openwrt_in_qemu_aarch64_on_apple_silicon_macos_m1_hardware_native 安装后,OpenWrt 只能ping通局域网地址。

  • 在启动 Openwrt 前,需要确保-append root=/dev/vda启动参数是否已经添加,没添加的话,启动后会无限重启

  • 首先设置vi /etc/config/network ,按自己需求更改下面带注释的两行信息即可

config interface 'loopback'
	option device 'lo'
	option proto 'static'
	option ipaddr '127.0.0.1'
	option netmask '255.0.0.0'

config globals 'globals'
	option ula_prefix 'fdd6:7224:70e7::/48'

config device
	option name 'br-lan'
	option type 'bridge'
	list ports 'eth0'
	option ipv6 '0'

config interface 'lan'
	option device 'br-lan'
	option proto 'static'
	option ipaddr '192.168.1.234'   // 登陆 OpenWrt Web 的 Url
	option netmask '255.255.255.0'
	option ip6assign '60'
	option gateway '192.168.1.1'  // 指向主路由的网关
	list dns '192.168.1.1'

如果网络配置不太熟悉的小伙伴,在更改完上述的option ipaddr后,可以在浏览器输入192.168.1.234登录 OpenWrt 后台进行后续操作

虚拟机 OpenWrt 作为旁路由

1. 在“网络”-“接口”的“常规设置”中给 Openwrt 的 LAN 网络接口设置一个和现有局域网同网段的静态 IP 地址,注意不要和现有设备的 IP 地址冲突。然后应用设置。
2. 将 LAN 网络接口的“默认网关”设为主路由的 IP 地址。
3. 在“高级设置”中找到“使用自定义的 DNS 服务器”设为主路由的 IP 地址。
4. 在“DHCP 服务器”中勾选“忽略此接口”。
5. 在“DHCP 服务器”-“IPv6 设置”中禁用所有IPv6服务。
6. 点击“保存”以及“保存并应用”。
7. 在“网络”-“防火墙”中,关闭“SYN-flood 防御”,点击“保存并应用”。
8. 建议重启一次。

关于防火墙的设置,其实还是比较讲究的,但是目前主要是确保能在Apple Silicon平台上顺利运行旁路由,后续再找机会出一篇

后续关于旁路由的使用方式

  • 方式一:在指定的设备上,手动设置IP地址,将其“网关”和“首选 DNS”改为旁路网关的IP地址。
  • 方式二:在主路由上配置,全部连接到主路由的设备都会经过旁路由
    • 假设主路由是 Openwrt 固件,找到 br-lan 网络接口,在“DHCP 服务器”-“高级设置”-“DHCP选项”中添加两个选项:
      • 3,你的网关IP
      • 6,你的网关IP

OpenWrt 原生主题虽然显示内容紧凑,但是分类不太清晰,一般会选择安装其他第三方主题比如 Argon,但是如果安装之后报错怎么办?

document.addEventListener('luci-loaded', function(ev) {
                                var tree = Status: 500 Internal Server Error
/usr/lib/lua/luci/dispatcher.lua:381: Failed to execute cbi dispatcher target for entry '/admin/system/webadmin'.
The called action terminated with an exception:
/usr/lib/lua/luci/template.lua:55: Failed to execute template 'cbi/header'.
A runtime error occured: /usr/lib/lua/luci/template.lua:55: Failed to execute template 'header'.
A runtime error occured: /usr/lib/lua/luci/template.lua:55: Failed to execute template 'themes/material/header'.
A runtime error occured: [string "/usr/lib/lua/luci/view/themes/material/head..."]:13: attempt to call field 'menu_json' (a nil value)
stack traceback:
        [C]: in function 'assert'
        /usr/lib/lua/luci/dispatcher.lua:381: in function 'dispatch'
        /usr/lib/lua/luci/dispatcher.lua:95: in function </usr/lib/lua/luci/dispatcher.lua:94>
  • 可先通过命令:opkg list-installed|grep luci-theme查看已安装的主题。
  • 可通过删除命令:opkg remove luci-theme-主题名 删除相关主题,然后重启即可

安装 iStore 插件商店,便于安装富强

  • 如果报错 Connection error: Invalid SSL certificate,可以手动安装,步骤如下:
  • 执行命令:vi istore-reinstall.run ,输入以下内容:
#!/bin/sh
ISTORE_REPO=https://istore.linkease.com/repo/all/store
FCURL="curl --fail --show-error"

curl -V >/dev/null 2>&1 || {
  echo "prereq: install curl"
  opkg info curl | grep -Fqm1 curl || opkg update
  opkg install curl
}

IPK=`$FCURL "$ISTORE_REPO/Packages.gz" | zcat | grep -m1 '^Filename: luci-app-store.*\.ipk$' | sed -n -e 's/^Filename: \(.\+\)$/\1/p'`

[ -n "$IPK" ] || exit 1

$FCURL "$ISTORE_REPO/$IPK" | tar -xzO ./data.tar.gz | tar -xzO ./bin/is-opkg > /tmp/is-opkg

[ -s "/tmp/is-opkg" ] || exit 1

chmod 755 /tmp/is-opkg
/tmp/is-opkg update
# /tmp/is-opkg install taskd
/tmp/is-opkg opkg install --force-reinstall luci-lib-taskd luci-lib-xterm
/tmp/is-opkg opkg install --force-reinstall luci-app-store || exit $?
[ -s "/etc/init.d/tasks" ] || /tmp/is-opkg opkg install --force-reinstall taskd
[ -s "/usr/lib/lua/luci/cbi.lua" ] || /tmp/is-opkg opkg install luci-compat >/dev/null 2>&1
  • 授权:chmod 755 istore-reinstall.run
  • 执行:./istore-reinstall.run
  • 重启 OpenWrt

如果需要安装富强,先得进行下面操作

dnsmasq-full 依赖安装失败(冲突)

  • opkg update
    # 安装和卸载一定要同时进行
    opkg remove dnsmasq && opkg install dnsmasq-full 

如果报错:iptables 相关,则自己手动安装,因为新版本的官方固件,编译不再选中这些工具

  • opkg install ipset iptables iptables-mod-conntrack-extra iptables-mod-iprange iptables-mod-socket iptables-mod-tproxy kmod-ipt-nat [ipt2socks]

其他的一些报错

module ‘luci.cbi’ not found

Failed to execute cbi dispatcher target for entry '/admin/services/v2ray/global'.
The called action terminated with an exception:
/usr/lib/lua/luci/dispatcher.lua:938: module 'luci.cbi' not found:
no field package.preload['luci.cbi']
no file './luci/cbi.lua'
no file '/usr/share/lua/luci/cbi.lua'
no file '/usr/share/lua/luci/cbi/init.lua'
no file '/usr/lib/lua/luci/cbi.lua'
no file '/usr/lib/lua/luci/cbi/init.lua'
no file './luci/cbi.so'
no file '/usr/lib/lua/luci/cbi.so'
no file '/usr/lib/lua/loadall.so'
no file './luci.so'
.......
# 解决方法
opkg update
opkg install luci luci-base luci-compat

另外UTM还可以搭配快捷指令(Shortcuts),实现开机自启动虚拟旁路网关

具体步骤:

  1. 创建快捷指令Open URLs
  2. 输入地址utm://start?name=虚拟机名
  3. 添加这个快捷指令到自启动程序列表