优雅地使用 WireGuard:从“能用”到“好用”

17 天前(已编辑)
/ , , ,
4
1

优雅地使用 WireGuard:从“能用”到“好用”

众所周知,南邮的新教务系统 和录播课堂 都会限制校外 IP 访问。以校外 IP 访问,前者会得到 Empty reply,后者则拿不到播放器的媒体地址。

飞书 WireGuard 组网 文档已经列出了 Allowed IPs,你应当在配置的 [peer] 中添加它们,而不是默认 0.0.0.0/0,否则所有流量都会被 TUN 转发回学校。

但实际使用过程中还存在一些问题。截至 2026~2027 秋季学期,你至少需要进行以下配置,才能比较舒服地用好 wg。

方案一:将 WG 节点写入 mihomo

如果你的代理核心(以 mihomo 为例,其他核心请自行查阅文档)支持 WireGuard 节点,那么直接参考 本地 JavaScript 覆写 方案。

function main(config) {
  const nodeName = 'SASTv6-outer-net';
  const alternateName = 'SAST-school-local';
  const directName = 'Direct';
  const groupName = '校园网络';
  const testUrl = 'http://jwglxt.njupt.edu.cn/xtgl/login_slogin.html';
  const privateKey = 'REPLACE_PRIVATE_KEY';
  const publicKey = 'REPLACE_SERVER_PUBLIC_KEY';
  const presharedKey = 'REPLACE_PRESHARED_KEY';
  if ([privateKey, publicKey, presharedKey].some(k => k.startsWith('REPLACE_'))) {
    throw new Error('请先在本机填入 WireGuard 三项密钥,再启用此覆写');
  }
  const prefixes = [
    '172.16.0.0/24', '192.168.101.0/24', '192.168.114.0/24',
    '10.0.0.0/8', '202.119.224.0/20', '103.244.235.63/32',
    '115.28.139.59/32', '180.209.99.28/32'
  ];
  const node = {
    name: nodeName, type: 'wireguard', ip: '172.16.0.21',
    'private-key': privateKey,
    peers: [{
      server: 'wg6.sast.fun', port: 61820,
      'public-key': publicKey, 'pre-shared-key': presharedKey,
      'allowed-ips': prefixes
    }],
    'persistent-keepalive': 15, mtu: 1400, udp: true,
    'remote-dns-resolve': true, dns: ['192.168.114.1']
  };
  const alternateNode = {
    ...node, name: alternateName,
    peers: node.peers.map(peer => ({...peer, server: 'wg.sast.fun'}))
  };
  config.proxies = [
    ...(config.proxies || []).filter(p => ![nodeName, alternateName, directName].includes(p.name)),
    node, alternateNode, {name: directName, type: 'direct', udp: true}
  ];
  const groups = (config['proxy-groups'] || []).filter(g => g.name !== groupName);
  // 避免将来订阅改成自动纳入全部节点后,把校园 WG 混入国外测速组。
  for (const g of groups) {
    if (g['include-all'] || g['include-all-proxies']) {
      const old = g['exclude-filter'];
      if (!old || !old.includes('^SAST-WG$')) {
        g['exclude-filter'] = old ? `(${old})|^SAST-WG$` : '^SAST-WG$';
      }
      if (!g['exclude-filter'].includes('^SAST-直连$')) {
        g['exclude-filter'] += '|^SAST-直连$';
      }
      if (!g['exclude-filter'].includes('^SAST-WG-wg$')) {
        g['exclude-filter'] += '|^SAST-WG-wg$';
      }
    }
  }
  config['proxy-groups'] = [...groups, {
    name: groupName, type: 'select', proxies: [nodeName, alternateName, directName],
    url: testUrl, interval: 300, lazy: true
  }];
  const rules = [
    'DOMAIN,wg6.sast.fun,DIRECT',
    'DOMAIN,wg.sast.fun,DIRECT',
    `DOMAIN,jwglxt.njupt.edu.cn,${groupName}`,
    `DOMAIN-SUFFIX,smartclass.cn,${groupName}`,
    ...prefixes.map(p => `IP-CIDR,${p},${groupName},no-resolve`)
  ];
  config.rules = [...rules, ...(config.rules || []).filter(r => !rules.includes(r))];
  return config;
}
Warning
如果你不使用共存方案,请不要为校园网段添加 TUN 排除项。

---

方案二:共存

与代理软件共存

我使用 Sparkle(mihomo 核心)作为主力代理软件,使用习惯是:System Proxy + TUN 常开。 而 wg 工作方式也是 TUN,且 mihomo 的 Tun 网卡的跃点为 1。很有可能所有流量都直接走 mihomo 代理,WireGuard 形同虚设(系统先比较路由前缀长度,再比较相同前缀的代价)。

解决方法 1: 短暂关闭 Sparkle 的 TUN。但这一点也不优雅。 解决方法 2: 在 Sparkle TUN 设置中排除自定义网段:

tun:
  route-exclude-address:
    - 172.16.0.0/24
    - 192.168.101.0/24
    - 192.168.114.0/24
    - 10.0.0.0/8
    - 202.119.224.0/20
    - 103.244.235.63/32
    - 115.28.139.59/32        # smartclass
    - 180.209.99.28/32        # 校内DNS解析的新版教务

在系统代理 - 绕过列表 / DNS FakeIP 过滤列表

jwglxt.njupt.edu.cn
njupt.smartclass.cn

与易安连 App 共存

南邮信息办给出的校外巡课流程本身也是先连接易安联的 ENLINK CASB,再访问 njupt.smartclass.cn。所以 SmartClass 与易安联本身并不冲突,真正打架的是 易安联和 WireGuard 同时建立隧道后,对同一批校内地址产生重叠路由。

Tip
Windows / macOS 上两个隧道可以同时存在,但命中同一个目标地址时,最终走哪个接口取决于系统路由;iOS 上只允许一个 Packet Tunnel 接管系统流量,WireGuard 和易安联 App 必有一个被强制关闭。

因此使用 WireGuard 时保持易安联断开,反之亦然。这样比改双方的默认路由、metric 或强行套两层 TUN 稳定得多。(加上 mihomo,你的电脑里有三个 TUN Be like)

smartclass

前文已提。 wg config 中追加: ini [Peer] AllowedIPs = ..., 115.28.139.59/32

mihomo TUN 排除:115.28.139.59/32 ,System Proxy 绕过列表和 DNS FakeIP 过滤列表:njupt.smartclass.cn

近期发现,教师账号所用校园网出口被 smartclass 限制,原因未知。建议切换 CMCC 或 CHINANET 出口或使用易安连 App。

新版教务系统

前文已提。

[Peer]
AllowedIPs = ..., 180.209.99.28/32

System Proxy 绕过列表和 DNS FakeIP 过滤列表: jwglxt.njupt.edu.cn


至此,实际需要处理的是三层互不相干的问题:WireGuard 的 AllowedIPs 决定哪些目的地址回学校;mihomo 的 TUN / System Proxy 决定这些流量会不会在到达 WireGuard 之前被代理截走;易安联则是另一套 VPN,使用时不要和 WireGuard 重复接管同一批校内资源。

使用社交账号登录

  • Loading...
  • Loading...
  • Loading...
  • Loading...
  • Loading...