Skip to content

Commit

Permalink
feat(client): Add Clash API options
Browse files Browse the repository at this point in the history
  • Loading branch information
muink committed Dec 2, 2023
1 parent d0b0861 commit ae88dd7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ LUCI_DEPENDS:= \
+sing-box \
+chinadns-ng \
+firewall4 \
+kmod-nft-tproxy
+kmod-nft-tproxy \
+unzip

PKG_NAME:=luci-app-homeproxy

Expand Down
23 changes: 23 additions & 0 deletions htdocs/luci-static/resources/view/homeproxy/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,29 @@ return view.extend({
/* DNS rules end */
/* Custom routing settings end */

/* Clash API settings start */
s.tab('clash', _('Clash API settings'));
o = s.taboption('clash', form.SectionValue, '_clash', form.NamedSection, 'experimental', 'homeproxy');

ss = o.subsection;
so = ss.option(form.Flag, 'clash_api_enabled', _('Enable Clash API'));
so.default = so.disabled;

so = ss.option(form.ListValue, 'dashboard_repo', _('Select Clash Dashboard'));
so.value('', _('Use Online Dashboard'));
so.value('metacubex/yacd-meta', _('yacd-meta'));
so.value('metacubex/metacubexd', _('metacubexd'));
so.default = '';

so = ss.option(form.Value, 'clash_api_port', _('Port'));
so.datatype = "and(port, min(1))";
so.default = '9090';
so.rmempty = false;

so = ss.option(form.Value, 'clash_api_secret', _('Secret'), _('Automatically generated if empty'));
so.password = true;
/* Clash API settings end */

/* ACL settings start */
s.tab('control', _('Access Control'));

Expand Down
3 changes: 3 additions & 0 deletions root/etc/config/homeproxy
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ config homeproxy 'config'
option proxy_mode 'redirect_tproxy'
option ipv6_support '1'

config homeproxy 'experimental'
option clash_api_port '9090'

config homeproxy 'control'
option lan_proxy_mode 'disabled'
list wan_proxy_ipv4_ips '91.108.4.0/22'
Expand Down
29 changes: 28 additions & 1 deletion root/etc/homeproxy/scripts/generate_client.uc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ uci.load(uciconfig);

const uciinfra = 'infra',
ucimain = 'config',
uciexp = 'experimental',
ucicontrol = 'control';

const ucidnssetting = 'dns',
Expand Down Expand Up @@ -80,6 +81,11 @@ const proxy_mode = uci.get(uciconfig, ucimain, 'proxy_mode') || 'redirect_tproxy
ipv6_support = uci.get(uciconfig, ucimain, 'ipv6_support') || '0',
default_interface = uci.get(uciconfig, ucicontrol, 'bind_interface');

const clash_api_enabled = uci.get(uciconfig, uciexp, 'clash_api_enabled'),
dashboard_repo = uci.get(uciconfig, uciexp, 'dashboard_repo'),
clash_api_port = uci.get(uciconfig, uciexp, 'clash_api_port') || '9090',
clash_api_secret = uci.get(uciconfig, uciexp, 'clash_api_secret') || trim(readfile('/proc/sys/kernel/random/uuid'));

const mixed_port = uci.get(uciconfig, uciinfra, 'mixed_port') || '5330';
let self_mark, redirect_port, tproxy_port,
tun_name, tun_addr4, tun_addr6, tun_mtu,
Expand Down Expand Up @@ -605,8 +611,29 @@ if (!isEmpty(main_node)) {
});

config.route.final = get_outbound(default_outbound);
}
};
/* Routing rules end */

/* Experimental start */
/* Clash API start */
if (dashboard_repo) {
system('rm -rf ' + RUN_DIR + '/ui');
const dashpkg = HP_DIR + '/resources/' + replace(dashboard_repo, '/', '_') + '.zip';
system('unzip -qo ' + dashpkg + ' -d ' + RUN_DIR + '/');
system('mv ' + RUN_DIR + '/*-gh-pages/ ' + RUN_DIR + '/ui/');
}
config.experimental = {
clash_api: {
external_controller: (clash_api_enabled === '1') ? '[::]:'+ clash_api_port : null,
external_ui: dashboard_repo ? RUN_DIR + '/ui' : null,
secret: clash_api_secret,
store_mode: true,
store_selected: true,
cache_file: HP_DIR + '/clash_cache.db'
}
};
/* Clash API end */
/* Experimental end */

system('mkdir -p ' + RUN_DIR);
writefile(RUN_DIR + '/sing-box-c.json', sprintf('%.J\n', removeBlankAttrs(config)));

0 comments on commit ae88dd7

Please sign in to comment.