Files
subs/scripts/check_xboard.rb
T

125 lines
5.6 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env ruby
# 校验 xboard/ 下的订阅模板:用假节点按 Xboard 的逻辑渲染,再检查结果。
#
# ruby scripts/check_xboard.rb
#
# Surge(xboard/surge.conf):Xboard 对全文做字符串替换(app/Protocols/Surge.php)
# - 注释里不能出现占位符,否则同样会被替换(写了 $proxies 会把节点插到注释位置)
# - 渲染后若本机装有 Surge,用 surge-cli --check 校验
# mihomo(xboard/clashmeta.yaml):Xboard 解析 YAML 后注入节点(app/Protocols/ClashMeta.php)
# - 策略组里没有正则条目的,追加全部节点;'/正则/u' 条目替换为名称匹配的节点
# - 最终为空的策略组会被删除
# - 检查每个策略组、规则、规则集、DNS 引用都能对上
require 'yaml'
require 'tmpdir'
# 系统自带 Ruby 在未设置 LANG 时默认 US-ASCII,surge-cli 输出含中文会报编码错误
Encoding.default_external = Encoding::UTF_8
ROOT = File.expand_path('..', __dir__)
NODES = ['🇭🇰 香港 HK 01', '🇬🇧 英国 GB 01', '🇳🇱 荷兰 NL 01', '🇩🇪 德国 DE 01', 'AWS 东京 01', '🇺🇸 美国 01'].freeze
SURGE_PLACEHOLDERS = %w[$subs_link $subs_domain $subscribe_info $proxies $proxy_group].freeze
SURGE_CLI = '/Applications/Surge.app/Contents/Applications/surge-cli'
def check_surge(dir)
errors = []
path = File.join(ROOT, 'xboard/surge.conf')
text = File.read(path, encoding: 'UTF-8')
text.each_line.with_index(1) do |line, no|
next if no == 1 || !line.lstrip.start_with?('#')
errors << "surge.conf:#{no} 注释里有占位符,会被 Xboard 替换" if SURGE_PLACEHOLDERS.any? { |p| line.include?(p) }
end
errors << 'surge.conf 第一行应为 #!MANAGED-CONFIG $subs_link ...' unless text.start_with?('#!MANAGED-CONFIG $subs_link ')
proxies = NODES.map { |n| "#{n} = trojan,node.example.com,443,password=test-only,sni=node.example.com,udp-relay=true\r\n" }.join
rendered = text
.gsub('$subs_link', 'https://sub.example.com/api/v1/client/subscribe?token=test-only')
.gsub('$subs_domain', 'sub.example.com')
.gsub('$subscribe_info', 'title=测试订阅信息, content=上传流量:1GB\\n下载流量:2GB')
.gsub('$proxy_group', NODES.join(', '))
.gsub('$proxies', proxies)
out = File.join(dir, 'surge.conf')
File.write(out, rendered)
if File.executable?(SURGE_CLI)
result = `"#{SURGE_CLI}" --check "#{out}" 2>&1`.strip.lines.last.to_s.strip
errors << "surge-cli --check:#{result}" unless result == 'OK'
else
puts '跳过 未找到 surge-cli,只做了占位符检查'
end
errors
end
# 按 PHP preg 的分隔符规则判断是否为正则,返回 Regexp 或 nil
def php_regex(str)
return nil unless str.is_a?(String) && !str.empty?
bytes = str.bytes
delim = bytes[0]
return nil if delim.zero? || (delim < 128 && delim.chr =~ /[A-Za-z0-9\\\s]/)
close = bytes[1..].rindex(delim)
return nil if close.nil?
flags = str.byteslice(close + 2..).to_s
return nil unless flags.match?(/\A[imsxuADSUXJn]*\z/)
Regexp.new(str.byteslice(1, close).force_encoding('UTF-8'))
end
def check_clashmeta
errors = []
cfg = YAML.load_file(File.join(ROOT, 'xboard/clashmeta.yaml'))
%w[proxies proxy-groups rules].each { |k| errors << "clashmeta.yaml 缺少顶层键 #{k}(Xboard 直接访问,缺失会报错)" unless cfg.key?(k) }
return errors unless errors.empty?
cfg['proxies'] = (cfg['proxies'] || []) + NODES.map { |n| { 'name' => n, 'type' => 'trojan', 'server' => 'node.example.com', 'port' => 443, 'password' => 'test-only' } }
cfg['proxy-groups'].each do |group|
unless group.key?('proxies')
errors << "策略组 #{group['name']} 缺少 proxies 键"
next
end
group['proxies'] = [] unless group['proxies'].is_a?(Array)
filtered = false
group['proxies'].dup.each do |src|
re = php_regex(src)
next unless re
filtered = true
group['proxies'].delete(src)
group['proxies'].concat(NODES.select { |n| re.match?(n) })
end
group['proxies'].concat(NODES) unless filtered
end
dropped = cfg['proxy-groups'].select { |g| g['proxies'].empty? }.map { |g| g['name'] }
cfg['proxy-groups'] -= cfg['proxy-groups'].select { |g| g['proxies'].empty? }
errors << "用假节点渲染后这些组为空被删除:#{dropped.join('、')}" unless dropped.empty?
cfg['rules'].unshift('DOMAIN,sub.example.com,DIRECT')
groups = cfg['proxy-groups'].map { |g| g['name'] }
known = groups + NODES + %w[DIRECT REJECT REJECT-DROP PASS COMPATIBLE]
cfg['proxy-groups'].each { |g| (g['proxies'] - known).each { |m| errors << "策略组 #{g['name']} 引用了不存在的 #{m}" } }
providers = (cfg['rule-providers'] || {}).keys
used = []
cfg['rules'].each do |rule|
parts = rule.split(',').map(&:strip)
policy = case parts[0]
when 'MATCH' then parts[1]
when 'RULE-SET' then used << parts[1]; parts[2]
when 'AND', 'OR', 'NOT' then parts.last == 'no-resolve' ? parts[-2] : parts.last
else parts[2]
end
errors << "规则 #{rule} 的策略 #{policy} 不存在" unless known.include?(policy)
end
(used - providers).each { |p| errors << "规则集 #{p} 未定义" }
(providers - used).each { |p| errors << "规则集 #{p} 定义了但没有使用" }
Array(cfg.dig('dns', 'nameserver')).each do |ns|
target = ns[/#(.+)\z/, 1]
errors << "DNS #{ns} 指向不存在的策略组" if target && !known.include?(target)
end
errors
end
errors = Dir.mktmpdir { |dir| check_surge(dir) } + check_clashmeta
if errors.empty?
puts '通过 xboard/surge.conf、xboard/clashmeta.yaml'
else
errors.each { |e| warn "错误 #{e}" }
exit 1
end