diff --git a/README.md b/README.md index eb31b7f..70a16ab 100644 --- a/README.md +++ b/README.md @@ -458,7 +458,8 @@ router/ bmstu-otp.sh OpenVPN BMSTU: TOTP в пароле, cron-сторож, on/off luci/ страница LuCI «Службы → mihomo-kit»: rpcd.sh (бэкенд), view, ACL, меню, servers.js (ссылки и YAML своих серверов), qrcode.js -tools/ push.sh, test.sh, test-servers.js, core/ (ядро kit) +tools/ push.sh, test.sh, test-servers.js, servers-ref.sh + servers-ref/ (эталоны + для servers.js из Go-кода mihomo), core/ (ядро kit) tests/fixtures/ профили для test.sh (фиктивные секреты), own.servers tests/expected/ эталонный YAML каждого профиля tests/links/, yaml/ ссылки и YAML с выводом Go-кода mihomo для luci/servers.js @@ -504,3 +505,9 @@ tests/links/, yaml/ ссылки и YAML с выводом Go-кода mihomo д MIHOMO=/path/to/mihomo tools/test.sh # профили = tests/expected/, mihomo -t проходит, servers.js = Go UPDATE=1 tools/test.sh # переписать tests/expected/ (изменения — проверить в diff) ``` + +После обновления mihomo (новый `CORE_OFFICIAL`) — сверить разбор ссылок и YAML на странице с +новым ядром: `tools/servers-ref.sh` (или `tools/servers-ref.sh <тег>`) пересоздаёт эталоны +`tests/links/*.expected.json` и `tests/yaml/*.expected.json` из Go-кода этой версии mihomo, +затем `tools/test.sh`. `FAIL links/...` — mihomo теперь разбирает ссылку иначе: то же +изменение (diff `common/convert` между тегами) перенести в `luci/servers.js`. diff --git a/tests/yaml/clash.expected.json b/tests/yaml/clash.expected.json index f732d25..2354cae 100644 --- a/tests/yaml/clash.expected.json +++ b/tests/yaml/clash.expected.json @@ -1 +1,95 @@ -{"port":7890,"proxies":[{"alpn":["h2","http/1.1"],"client-fingerprint":"chrome","name":"🇩🇪 de-1 | vless","network":"xhttp","port":443,"reality-opts":{"public-key":"K-_kk","short-id":"ab"},"server":"x.example","servername":"www.example.com","tls":true,"type":"vless","udp":true,"uuid":"0b1c2d3e-0000-4000-8000-000000000001","xhttp-opts":{"headers":{"X-Forwarded-For":"1.2.3.4"},"mode":"packet-up","path":"/x"}},{"name":"b","password":"p#w","port":443,"server":"t:x","sni":"t.example","type":"trojan","udp":true},{"down":"100 mbps","name":"c","obfs":"salamander","obfs-password":"a\"b\\cé","password":"it's","ports":"2000-3000","server":"h.example","skip-cert-verify":true,"type":"hysteria2","up":"50 mbps"},{"cipher":"aes-256-gcm","name":"d","password":123456,"plugin":"obfs","plugin-opts":{"host":"bing.com","mode":"tls"},"port":8388,"server":"1.2.3.4","type":"ss"},{"alterId":0,"cipher":"auto","name":"e","port":"443","server":"v.example","smux":{"enabled":false,"padding":null},"type":"vmess","uuid":"x","ws-opts":{"headers":{"Host":"v.example"},"path":"/w?ed=2048"}}],"proxy-groups":[{"name":"G","proxies":["b","c"],"type":"select"}]} +{ + "port": 7890, + "proxies": [ + { + "alpn": [ + "h2", + "http/1.1" + ], + "client-fingerprint": "chrome", + "name": "🇩🇪 de-1 | vless", + "network": "xhttp", + "port": 443, + "reality-opts": { + "public-key": "K-_kk", + "short-id": "ab" + }, + "server": "x.example", + "servername": "www.example.com", + "tls": true, + "type": "vless", + "udp": true, + "uuid": "0b1c2d3e-0000-4000-8000-000000000001", + "xhttp-opts": { + "headers": { + "X-Forwarded-For": "1.2.3.4" + }, + "mode": "packet-up", + "path": "/x" + } + }, + { + "name": "b", + "password": "p#w", + "port": 443, + "server": "t:x", + "sni": "t.example", + "type": "trojan", + "udp": true + }, + { + "down": "100 mbps", + "name": "c", + "obfs": "salamander", + "obfs-password": "a\"b\\cé", + "password": "it's", + "ports": "2000-3000", + "server": "h.example", + "skip-cert-verify": true, + "type": "hysteria2", + "up": "50 mbps" + }, + { + "cipher": "aes-256-gcm", + "name": "d", + "password": 123456, + "plugin": "obfs", + "plugin-opts": { + "host": "bing.com", + "mode": "tls" + }, + "port": 8388, + "server": "1.2.3.4", + "type": "ss" + }, + { + "alterId": 0, + "cipher": "auto", + "name": "e", + "port": "443", + "server": "v.example", + "smux": { + "enabled": false, + "padding": null + }, + "type": "vmess", + "uuid": "x", + "ws-opts": { + "headers": { + "Host": "v.example" + }, + "path": "/w?ed=2048" + } + } + ], + "proxy-groups": [ + { + "name": "G", + "proxies": [ + "b", + "c" + ], + "type": "select" + } + ] +} diff --git a/tests/yaml/list.expected.json b/tests/yaml/list.expected.json index 6d7f950..c4d70e7 100644 --- a/tests/yaml/list.expected.json +++ b/tests/yaml/list.expected.json @@ -1 +1,39 @@ -[{"colon-in":"http://a.example:8080/p","dash":"-","empty":null,"float":1.5,"folded":"word one word two\npara","hash-in":"a#b","hex":31,"multi":["a","b","c"],"name":"one","neg":-3,"nested":[[1,2],{"k":"v","k2":["a",{"x":1}]}],"port":1080,"quoted":"two lines","server":"s.example","str-num":"443","text":"line 1\nline 2\n","type":"socks5"}] +[ + { + "colon-in": "http://a.example:8080/p", + "dash": "-", + "empty": null, + "float": 1.5, + "folded": "word one word two\npara", + "hash-in": "a#b", + "hex": 31, + "multi": [ + "a", + "b", + "c" + ], + "name": "one", + "neg": -3, + "nested": [ + [ + 1, + 2 + ], + { + "k": "v", + "k2": [ + "a", + { + "x": 1 + } + ] + } + ], + "port": 1080, + "quoted": "two lines", + "server": "s.example", + "str-num": "443", + "text": "line 1\nline 2\n", + "type": "socks5" + } +] diff --git a/tests/yaml/one.expected.json b/tests/yaml/one.expected.json index e4a0a37..a24b827 100644 --- a/tests/yaml/one.expected.json +++ b/tests/yaml/one.expected.json @@ -1 +1,7 @@ -{"name":"single","password":"pw","port":443,"server":"an.example","type":"anytls"} +{ + "name": "single", + "password": "pw", + "port": 443, + "server": "an.example", + "type": "anytls" +} diff --git a/tools/servers-ref.sh b/tools/servers-ref.sh new file mode 100755 index 0000000..bec2b03 --- /dev/null +++ b/tools/servers-ref.sh @@ -0,0 +1,31 @@ +#!/bin/sh +# Rewrite the reference outputs luci/servers.js is checked against (tools/test.sh): +# tests/links/.expected.json and tests/yaml/.expected.json, made by mihomo's own +# code (tools/servers-ref/main.go). Needs git and Go. +# tools/servers-ref.sh mihomo CORE_OFFICIAL of router/mihomo-kit.sh +# tools/servers-ref.sh v1.19.34 another tag +# After a mihomo update: run it, then tools/test.sh; a FAIL of links/... means mihomo +# now converts a link differently and luci/servers.js needs the same change (diff +# common/convert between the tags). Review the expected files in git diff. +set -eu +cd "$(dirname "$0")/.." +root=$(pwd) +tag=${1:-$(sed -n 's/^CORE_OFFICIAL=//p' router/mihomo-kit.sh)} +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +git -c advice.detachedHead=false clone -q --depth 1 --branch "$tag" https://github.com/MetaCubeX/mihomo.git "$work/mihomo" +mkdir -p "$work/mihomo/cmd/servers-ref" +cp tools/servers-ref/main.go "$work/mihomo/cmd/servers-ref/" +(cd "$work/mihomo" && go build -o "$work/servers-ref" ./cmd/servers-ref) +# the fixed ws User-Agent of luci/servers.js +ua=$(sed -n "s/^[[:space:]]*var USER_AGENT = '\(.*\)';$/\1/p" luci/servers.js) +[ -n "$ua" ] || { echo "no USER_AGENT in luci/servers.js" >&2; exit 1; } +for f in tests/links/*; do + case "$f" in *.expected.json) continue ;; esac + "$work/servers-ref" links "$ua" "$f" "${f%.*}.expected.json" >/dev/null 2>&1 + echo "mihomo $tag: ${f%.*}.expected.json" +done +for f in tests/yaml/*.yaml; do + "$work/servers-ref" yaml "$f" "${f%.yaml}.expected.json" + echo "mihomo $tag: ${f%.yaml}.expected.json" +done diff --git a/tools/servers-ref/main.go b/tools/servers-ref/main.go new file mode 100644 index 0000000..e04ddbc --- /dev/null +++ b/tools/servers-ref/main.go @@ -0,0 +1,76 @@ +// Reference outputs for luci/servers.js from mihomo's own code (see tools/servers-ref.sh): +// servers-ref links common/convert.ConvertsV2Ray of a file of +// share links; ws User-Agent set to the given one +// (mihomo puts a random one there) +// servers-ref yaml go.yaml.in/yaml/v3 of a YAML file +// Output: JSON (map keys sorted). Built inside a mihomo checkout, which it imports. +package main + +import ( + "encoding/json" + "fmt" + "os" + + "github.com/metacubex/mihomo/common/convert" + "go.yaml.in/yaml/v3" +) + +func main() { + if len(os.Args) < 2 { + usage() + } + var v any + var in, out string + switch os.Args[1] { + case "links": + if len(os.Args) != 5 { + usage() + } + in, out = os.Args[3], os.Args[4] + buf, err := os.ReadFile(in) + check(err) + proxies, err := convert.ConvertsV2Ray(buf) + if err != nil { + proxies = []map[string]any{} + } + for _, p := range proxies { + if o, ok := p["ws-opts"].(map[string]any); ok { + if h, ok := o["headers"].(map[string]any); ok { + if _, ok := h["User-Agent"]; ok { + h["User-Agent"] = os.Args[2] + } + } + } + } + v = proxies + case "yaml": + if len(os.Args) != 4 { + usage() + } + in, out = os.Args[2], os.Args[3] + buf, err := os.ReadFile(in) + check(err) + check(yaml.Unmarshal(buf, &v)) + default: + usage() + } + f, err := os.Create(out) + check(err) + enc := json.NewEncoder(f) + enc.SetEscapeHTML(false) + enc.SetIndent("", " ") + check(enc.Encode(v)) + check(f.Close()) +} + +func check(err error) { + if err != nil { + fmt.Fprintln(os.Stderr, "servers-ref:", err) + os.Exit(1) + } +} + +func usage() { + fmt.Fprintln(os.Stderr, "usage: servers-ref links | yaml ") + os.Exit(2) +}