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

104 lines
4.5 KiB
JavaScript
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.

'use strict';
'require view';
'require rpc';
'require dom';
// LuCI "Службы → mihomo-kit → Граф": a built profile as a graph (luci/graph.js), with the
// live state of the running mihomo through the rpcd plugin (method api: read-only paths).
var callStatus = rpc.declare({ object: 'mihomo-kit', method: 'status', expect: { '': {} } });
var callGraph = rpc.declare({ object: 'mihomo-kit', method: 'graph', params: [ 'profile' ], expect: { '': {} } });
var callApi = rpc.declare({ object: 'mihomo-kit', method: 'api', params: [ 'path' ], nobatch: true, expect: { '': {} } });
// the YAML reader of the own servers (luci/servers.js) and the graph: globals mkServers, mkGraph
function loadScript(name, global) {
if (window[global])
return Promise.resolve(window[global]);
return new Promise(function(resolve, reject) {
var s = document.createElement('script');
s.src = L.resource('mihomo-kit/' + name);
s.onload = function() { resolve(window[global]); };
s.onerror = function() { reject(new Error(name + ' not loaded')); };
document.head.appendChild(s);
});
}
return view.extend({
load: function() {
return Promise.all([ loadScript('servers.js', 'mkServers'), loadScript('graph.js', 'mkGraph'), callGraph(''), callStatus() ]);
},
// /proxies every 10 s, /connections every 3 s; only for the running profile and a visible tab
poll: function() {
var tick = 0;
var loop = L.bind(function() {
if (this.stopped)
return;
if (document.hidden || !this.liveOn)
return void (this.timer = window.setTimeout(loop, 3000));
var want = tick++ % 3 == 0 ? callApi('/proxies') : Promise.resolve(null);
Promise.all([ want, callApi('/connections') ]).then(L.bind(function(r) {
if (r[0] && r[0].error || r[1].error)
throw new Error((r[0] && r[0].error) || r[1].error);
this.graph.live(r[0] && r[0].proxies, r[1].connections || []);
dom.content(this.state, '');
}, this)).catch(L.bind(function(e) {
dom.content(this.state, E('em', 'живое состояние недоступно: ' + (e.message || e)));
}, this)).then(L.bind(function() {
this.timer = window.setTimeout(loop, 3000);
}, this));
}, this);
loop();
},
show: function(g) {
if (g.error) {
dom.content(this.state, E('p', { 'class': 'alert-message warning' }, g.error));
return;
}
try {
this.graph.show(window.mkServers.yamlLoad(g.yaml));
} catch (e) {
dom.content(this.state, E('p', { 'class': 'alert-message warning' }, 'Не разобрал профиль: ' + e.message));
return;
}
this.liveOn = g.active && g.profile == g.running;
dom.content(this.state, this.liveOn ? '' : E('em', g.profile == g.running ? 'nikki остановлен: только схема'
: 'профиль ' + g.profile + ' сейчас не запущен: только схема, без живого состояния'));
},
render: function(data) {
var g = data[2], st = data[3];
this.state = E('div', { 'style': 'margin:.3em 0' });
var box = E('div');
var select = E('select', { 'class': 'cbi-input-select', 'change': L.bind(function(ev) {
return callGraph(ev.target.value).then(L.bind(this.show, this));
}, this) });
(st.profiles || []).forEach(function(p) {
select.appendChild(E('option', { 'value': p }, p + (p == st.profile ? ' (запущен)' : '')));
});
select.value = g.profile || st.profile;
var page = E([], [
E('h2', 'mihomo-kit: граф'),
E('div', { 'class': 'cbi-map-descr' }, 'Как устроен профиль: правила (сверху вниз — в порядке проверки) → группы → ' +
'подписки и серверы → через что они подключаются (dialer-proxy). Наведи или нажми на узел — подсветится всё, ' +
'что через него идёт; колесо — масштаб, перетаскивание — сдвиг. Для запущенного профиля видно, что выбрано ' +
'в группах сейчас и куда идут соединения. Править — на странице «Управление».'),
E('div', { 'style': 'margin:.5em 0' }, [ 'Профиль: ', select ]),
this.state,
box
]);
// once the page is in the document: the graph fits its box and takes the theme's background
window.setTimeout(L.bind(function() {
this.graph = window.mkGraph.view(box);
this.show(g);
this.poll();
}, this), 0);
return page;
},
handleSave: null,
handleSaveApply: null,
handleReset: null
});