Skip to content

Commit

Permalink
feat(client): Add Clash Dashboard button
Browse files Browse the repository at this point in the history
  • Loading branch information
muink committed Dec 4, 2023
1 parent 3221927 commit be67f32
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
28 changes: 21 additions & 7 deletions htdocs/luci-static/resources/view/homeproxy/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,12 +55,14 @@ function getServiceStatus() {
});
}

function renderStatus(isRunning) {
function renderStatus(isRunning, args) {
var spanTemp = '<em><span style="color:%s"><strong>%s %s</strong></span></em>';
var renderHTML;
if (isRunning)
renderHTML = spanTemp.format('green', _('HomeProxy'), _('RUNNING'));
else
if (isRunning) {
var button = String.format('&#160;<a class="btn cbi-button" href="%s" target="_blank" rel="noreferrer noopener">%s</a>',
'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;
Expand Down Expand Up @@ -96,15 +105,18 @@ return view.extend({
return Promise.all([
uci.load('homeproxy'),
hp.getBuiltinFeatures(),
network.getHostHints()
network.getHostHints(),
L.resolveDefault(callGetAPISecret(), {})
]);
},

render: function(data) {
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.'));
Expand All @@ -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});
});
});

Expand Down Expand Up @@ -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 <code>' + api_secret + '</code>');
/* Clash API settings end */

/* ACL settings start */
Expand Down
7 changes: 7 additions & 0 deletions root/usr/share/rpcd/ucode/luci.homeproxy
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
}
};

Expand Down

0 comments on commit be67f32

Please sign in to comment.