mihomo-kit/tools/graph.html
2026-10-29 20:52:06 +03:00

125 lines
5.1 KiB
HTML
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.

<!doctype html>
<!-- The config graph (luci/graph.js) without the router: open this file from the repo,
pick or drop any mihomo config. Live state: the router's mihomo API address and secret
(LuCI → mihomo-kit, the dashboard link has both); mihomo allows any origin by default. -->
<html lang="ru">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>mihomo graph</title>
<style>
:root { color-scheme: light dark; --bg: #fff; --fg: #1f2328; --mute: #6b7280; --b: #c9ced6; }
@media (prefers-color-scheme: dark) { :root { --bg: #16181d; --fg: #e6e8eb; --mute: #9aa1ab; --b: #454c57; } }
body { margin: 0; padding: 12px 16px; background: var(--bg); color: var(--fg); font: 13px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif; }
h1 { font-size: 16px; margin: 0 0 8px; }
.top { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; margin-bottom: 8px; }
.top input[type=text], .top input[type=password] { width: 14em; padding: 3px 6px; background: var(--bg); color: var(--fg); border: 1px solid var(--b); border-radius: 4px; }
button { padding: 3px 10px; background: var(--bg); color: var(--fg); border: 1px solid var(--b); border-radius: 4px; cursor: pointer; }
.hint { color: var(--mute); }
#drop { border: 2px dashed var(--b); border-radius: 6px; padding: 24px; text-align: center; color: var(--mute); }
#drop.over { border-color: #4c7cf0; }
textarea { width: 100%; box-sizing: border-box; font: 12px monospace; background: var(--bg); color: var(--fg); border: 1px solid var(--b); border-radius: 4px; }
#err { color: #e03131; white-space: pre-wrap; }
</style>
</head>
<body>
<h1>Граф конфига mihomo</h1>
<div class="top">
<input type="file" id="file" accept=".yaml,.yml,.txt">
<button id="paste">Вставить текст</button>
<span class="hint">API:</span>
<input type="text" id="api" placeholder="http://192.168.1.1:9090">
<input type="password" id="secret" placeholder="secret">
<button id="connect">Подключить</button>
<span id="apiState" class="hint"></span>
</div>
<div id="pasteBox" style="display:none;margin-bottom:8px">
<textarea id="text" rows="10" placeholder="proxies: …"></textarea>
<button id="show">Показать</button>
</div>
<div id="drop">Перетащи сюда конфиг mihomo (.yaml) или выбери файл</div>
<div id="err"></div>
<div id="graph"></div>
<script src="../luci/servers.js"></script>
<script src="../luci/graph.js"></script>
<script>
(function() {
var $ = function(id) { return document.getElementById(id); };
var g = null, timer = null;
var store = function(k, v) {
try {
if (v === undefined)
return localStorage.getItem('mkg-' + k) || '';
localStorage.setItem('mkg-' + k, v);
} catch (e) {
return '';
}
};
$('api').value = store('api');
var show = function(text) {
$('err').textContent = '';
try {
var cfg = mkServers.yamlLoad(text);
if (!cfg || typeof cfg != 'object' || !cfg.rules && !cfg['proxy-groups'])
throw new Error('не похоже на конфиг mihomo: нет rules и proxy-groups');
if (!g)
g = mkGraph.view($('graph'));
g.show(cfg);
$('drop').style.display = 'none';
} catch (e) {
$('err').textContent = 'Не разобрал: ' + e.message;
}
};
var read = function(f) {
var r = new FileReader();
r.onload = function() { show(r.result); };
r.readAsText(f);
};
$('file').addEventListener('change', function() { if (this.files[0]) read(this.files[0]); });
$('paste').addEventListener('click', function() {
$('pasteBox').style.display = $('pasteBox').style.display == 'none' ? '' : 'none';
});
$('show').addEventListener('click', function() { show($('text').value); });
var drop = $('drop');
document.addEventListener('dragover', function(e) { e.preventDefault(); drop.classList.add('over'); });
document.addEventListener('dragleave', function() { drop.classList.remove('over'); });
document.addEventListener('drop', function(e) {
e.preventDefault();
drop.classList.remove('over');
if (e.dataTransfer.files[0])
read(e.dataTransfer.files[0]);
});
// live: /proxies every 10 s, /connections every 2 s
var api = function(path) {
var base = $('api').value.trim().replace(/\/+$/, ''), sec = $('secret').value;
return fetch(base + path, { headers: sec ? { Authorization: 'Bearer ' + sec } : {} }).then(function(r) {
if (!r.ok)
throw new Error(path + ': HTTP ' + r.status);
return r.json();
});
};
$('connect').addEventListener('click', function() {
store('api', $('api').value.trim());
if (timer)
clearTimeout(timer);
if (!g)
return ($('apiState').textContent = 'сначала конфиг');
var tick = 0;
var loop = function() {
var want = tick++ % 5 == 0 ? api('/proxies') : Promise.resolve(null);
Promise.all([ want, api('/connections') ]).then(function(r) {
g.live(r[0] && r[0].proxies, r[1].connections || []);
$('apiState').textContent = 'подключено';
}).catch(function(e) {
$('apiState').textContent = 'ошибка: ' + e.message;
}).then(function() { timer = setTimeout(loop, 2000); });
};
loop();
});
})();
</script>
</body>
</html>