From 2e8af2c0b466e864d75ae315ee913a3ff9d52e3f Mon Sep 17 00:00:00 2001 From: Anya Lin Date: Wed, 15 Nov 2023 20:49:29 +0800 Subject: [PATCH] feat(client): Add Clash Dashboard button --- .../resources/view/homeproxy/client.js | 28 ++++++++++++++----- root/usr/share/rpcd/ucode/luci.homeproxy | 7 +++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/htdocs/luci-static/resources/view/homeproxy/client.js b/htdocs/luci-static/resources/view/homeproxy/client.js index 92a8d82f..7377a394 100644 --- a/htdocs/luci-static/resources/view/homeproxy/client.js +++ b/htdocs/luci-static/resources/view/homeproxy/client.js @@ -38,6 +38,13 @@ var callWriteDomainList = rpc.declare({ expect: { '': {} } }); +var callGetAPISecret = rpc.declare({ + object: 'luci.homeproxy', + method: 'clash_api_get_secret', + params: [], + expect: { '': {} } +}); + function getServiceStatus() { return L.resolveDefault(callServiceList('homeproxy'), {}).then((res) => { var isRunning = false; @@ -48,12 +55,14 @@ function getServiceStatus() { }); } -function renderStatus(isRunning) { +function renderStatus(isRunning, args) { var spanTemp = '%s %s'; var renderHTML; - if (isRunning) - renderHTML = spanTemp.format('green', _('HomeProxy'), _('RUNNING')); - else + if (isRunning) { + var button = String.format(' %s', + 'http://' + window.location.hostname + ':' + args.api_port + '/ui/' + '?hostname=' + window.location.hostname + '&port=' + args.api_port + '&secret=' + args.api_secret, _('Open Clash Dashboard')); + renderHTML = spanTemp.format('green', _('HomeProxy'), _('RUNNING')) + button; + } else renderHTML = spanTemp.format('red', _('HomeProxy'), _('NOT RUNNING')); return renderHTML; @@ -96,7 +105,8 @@ return view.extend({ return Promise.all([ uci.load('homeproxy'), hp.getBuiltinFeatures(), - network.getHostHints() + network.getHostHints(), + L.resolveDefault(callGetAPISecret(), {}) ]); }, @@ -104,7 +114,9 @@ return view.extend({ var m, s, o, ss, so; var features = data[1], - hosts = data[2]?.hosts; + hosts = data[2]?.hosts, + api_port = uci.get(data[0], 'experimental', 'clash_api_port'), + api_secret = data[3]?.secret || ''; m = new form.Map('homeproxy', _('HomeProxy'), _('The modern ImmortalWrt proxy platform for ARM64/AMD64.')); @@ -114,7 +126,7 @@ return view.extend({ poll.add(function () { return L.resolveDefault(getServiceStatus()).then((res) => { var view = document.getElementById('service_status'); - view.innerHTML = renderStatus(res); + view.innerHTML = renderStatus(res, {api_port, api_secret}); }); }); @@ -875,6 +887,8 @@ return view.extend({ so = ss.option(form.Value, 'clash_api_secret', _('Secret'), _('Automatically generated if empty')); so.password = true; + if (api_secret) + so.description = _('The current Secret is ' + api_secret + ''); /* Clash API settings end */ /* ACL settings start */ diff --git a/root/usr/share/rpcd/ucode/luci.homeproxy b/root/usr/share/rpcd/ucode/luci.homeproxy index 8b156ea5..fdff9c95 100755 --- a/root/usr/share/rpcd/ucode/luci.homeproxy +++ b/root/usr/share/rpcd/ucode/luci.homeproxy @@ -212,6 +212,13 @@ const methods = { } else return { status: 255, error: 'illegal type' }; } + }, + + clash_api_get_secret: { + call: function() { + const client_json = json(trim(readfile(`${RUN_DIR}/sing-box-c.json`))); + return { secret: client_json.experimental.clash_api.secret }; + } } };