1
0
Fork 0
mirror of https://github.com/Ysurac/openmptcprouter-feeds.git synced 2025-03-09 15:40:03 +00:00

Add shadowsocks-Rust luci interface

This commit is contained in:
Ycarus (Yannick Chabanois) 2023-09-29 21:33:48 +02:00
parent d07ccb47ff
commit a049f0f415
38 changed files with 10479 additions and 0 deletions

View file

@ -0,0 +1,294 @@
'use strict';
'require baseclass';
'require uci';
'require form';
'require network';
var names_options_server = [
'server',
'server_port',
'method',
'password',
'plugin',
'plugin_opts',
];
var names_options_client = [
'server',
'local_address',
'local_port',
];
var names_options_common = [
'verbose',
'ipv6_first',
'fast_open',
'no_delay',
'reuse_port',
'mode',
'mtu',
'timeout',
'user',
'mptcp',
];
var modes = [
'tcp_only',
'tcp_and_udp',
'udp_only',
];
var methods = [
'none',
// aead
'aes-128-gcm',
'aes-256-gcm',
'chacha20-ietf-poly1305',
'2022-blake3-aes-128-gcm',
'2022-blake3-aes-256-gcm',
'2022-blake3-chacha8-poly1305',
'2022-blake3-chacha20-poly1305',
];
function ucival_to_bool(val) {
return val === 'true' || val === '1' || val === 'yes' || val === 'on';
}
return L.Class.extend({
values_actions: function(o) {
o.value('bypass');
o.value('forward');
if (o.option !== 'dst_default') {
o.value('checkdst');
}
},
values_redir: function(o, xmode) {
uci.sections('shadowsocks-rust', 'ss_redir', function(sdata) {
var disabled = ucival_to_bool(sdata['disabled']),
sname = sdata['.name'],
mode = sdata['mode'] || 'tcp_only';
if (!disabled && mode.indexOf(xmode) !== -1) {
o.value(sname, sname + ' - ' + mode);
}
});
o.value('', '<unset>');
o.value('all', 'all');
o.default = '';
},
values_serverlist: function(o) {
uci.sections('shadowsocks-rust', 'server', function(sdata) {
var sname = sdata['.name'],
server = sdata['server'],
server_port = sdata['server_port'];
if (server && server_port) {
var disabled = ucival_to_bool(sdata['.disabled']) ? ' - disabled' : '',
desc = '%s - %s:%s%s'.format(sname, server, server_port, disabled);
o.value(sname, desc);
}
});
},
values_ipaddr: function(o, netDevs) {
netDevs.forEach(function(v) {
v.getIPAddrs().forEach(function(a) {
var host = a.split('/')[0];
o.value(host, '%s (%s)'.format(host, v.getShortName()));
});
});
},
options_client: function(s, tab, netDevs) {
var o = s.taboption(tab, form.ListValue, 'server', _('Remote server'));
this.values_serverlist(o);
o = s.taboption(tab, form.Value, 'local_address', _('Local address'));
o.datatype = 'ipaddr';
o.placeholder = '0.0.0.0';
this.values_ipaddr(o, netDevs);
o = s.taboption(tab, form.Value, 'local_port', _('Local port'));
o.datatype = 'port';
},
options_server: function(s, opts) {
var o, optfunc,
tab = opts && opts.tab || null;
if (!tab) {
optfunc = function(/* ... */) {
var o = s.option.apply(s, arguments);
o.editable = true;
return o;
};
} else {
optfunc = function(/* ... */) {
var o = s.taboption.apply(s, L.varargs(arguments, 0, tab));
o.editable = true;
return o;
};
}
o = optfunc(form.Value, 'label', _('Label'));
o = optfunc(form.Value, 'server', _('Server'));
o.datatype = 'host';
o.size = 16;
o = optfunc(form.Value, 'server_port', _('Server port'));
o.datatype = 'port';
o.size = 5;
o = optfunc(form.ListValue, 'method', _('Method'));
methods.forEach(function(m) {
o.value(m);
});
o = optfunc(form.Value, 'password', _('Password (Base64)'));
o.datatype = 'base64';
o.password = true;
o.size = 12;
optfunc(form.Value, 'plugin', _('Plugin')).modalonly = true;
optfunc(form.Value, 'plugin_opts', _('Plugin Options')).modalonly = true;
},
options_common: function(s, tab) {
var o = s.taboption(tab, form.ListValue, 'mode', _('Mode of operation'));
modes.forEach(function(m) {
o.value(m);
});
o.default = 'tcp_and_udp';
o = s.taboption(tab, form.Value, 'mtu', _('MTU'));
o.datatype = 'uinteger';
o = s.taboption(tab, form.Value, 'timeout', _('Timeout (sec)'));
o.datatype = 'uinteger';
s.taboption(tab, form.Value, 'user', _('Run as'));
s.taboption(tab, form.Flag, 'verbose', _('Verbose'));
s.taboption(tab, form.Flag, 'ipv6_first', _('IPv6 First'), _('Prefer IPv6 addresses when resolving names'));
s.taboption(tab, form.Flag, 'fast_open', _('Enable TCP Fast Open'));
s.taboption(tab, form.Flag, 'no_delay', _('Enable TCP_NODELAY'));
s.taboption(tab, form.Flag, 'reuse_port', _('Enable SO_REUSEPORT'));
s.taboption(tab, form.Flag, 'mptcp', _('Enable MPTCP'));
},
ucival_to_bool: function(val) {
return ucival_to_bool(val);
},
cfgvalue_overview: function(sdata) {
var stype = sdata['.type'],
lines = [];
if (stype === 'ss_server') {
this.cfgvalue_overview_(sdata, lines, names_options_server);
this.cfgvalue_overview_(sdata, lines, names_options_common);
this.cfgvalue_overview_(sdata, lines, ['bind_address']);
} else if (stype === 'ss_local' || stype === 'ss_redir' || stype === 'ss_tunnel') {
this.cfgvalue_overview_(sdata, lines, names_options_client);
if (stype === 'ss_tunnel') {
this.cfgvalue_overview_(sdata, lines, ['tunnel_address']);
}
this.cfgvalue_overview_(sdata, lines, names_options_common);
} else {
return [];
}
return lines;
},
cfgvalue_overview_: function(sdata, lines, names) {
names.forEach(function(n) {
var v = sdata[n];
if (v) {
if (n === 'password') {
v = _('<hidden>');
}
var fv = E('var', [v]);
if (sdata['.type'] !== 'ss_server' && n === 'server') {
fv = E('a', {
class: 'label',
href: L.url('admin/services/shadowsocks-rust/servers') + '#edit=' + v,
target: '_blank',
rel: 'noopener'
}, fv);
}
lines.push(n + ': ', fv, E('br'));
}
});
},
option_install_package: function(s, tab) {
var bin = s.sectiontype.replace('_', '-'),
opkg_package = 'shadowsocks-rust-' + bin, o;
if (tab) {
o = s.taboption(tab, form.Button, '_install');
} else {
o = s.option(form.Button, '_install');
}
o.title = _('Package is not installed');
o.inputtitle = _('Install package ' + opkg_package);
o.inputstyle = 'apply';
o.onclick = function() {
window.open(L.url('admin/system/opkg') +
'?query=' + opkg_package, '_blank', 'noopener');
};
},
parse_uri: function(uri) {
var scheme = 'ss://';
if (uri && uri.indexOf(scheme) === 0) {
var atPos = uri.indexOf('@'), hashPos = uri.lastIndexOf('#'), tag;
if (hashPos === -1) {
hashPos = undefined;
} else {
tag = uri.slice(hashPos + 1);
}
if (atPos !== -1) { // SIP002 format https://shadowsocks.org/en/spec/SIP002-URI-Scheme.html
var colonPos = uri.indexOf(':', atPos + 1), slashPos = uri.indexOf('/', colonPos + 1);
if (colonPos === -1) return null;
if (slashPos === -1) slashPos = undefined;
var userinfo = atob(uri.slice(scheme.length, atPos)
.replace(/-/g, '+').replace(/_/g, '/')),
i = userinfo.indexOf(':');
if (i === -1) return null;
var config = {
server: uri.slice(atPos + 1, colonPos),
server_port: uri.slice(colonPos + 1, slashPos ? slashPos : hashPos),
password: userinfo.slice(i + 1),
method: userinfo.slice(0, i)
};
if (slashPos) {
var search = uri.slice(slashPos + 1, hashPos);
if (search[0] === '?') search = search.slice(1);
search.split('&').forEach(function(s) {
var j = s.indexOf('=');
if (j !== -1) {
var k = s.slice(0, j), v = s.slice(j + 1);
if (k === 'plugin') {
v = decodeURIComponent(v);
var k = v.indexOf(';');
if (k !== -1) {
config['plugin'] = v.slice(0, k);
config['plugin_opts'] = v.slice(k + 1);
}
}
}
});
}
return [config, tag];
} else { // Legacy format https://shadowsocks.org/en/config/quick-guide.html
var plain = atob(uri.slice(scheme.length, hashPos)),
firstColonPos = plain.indexOf(':'),
lastColonPos = plain.lastIndexOf(':'),
atPos = plain.lastIndexOf('@', lastColonPos);
if (firstColonPos === -1 ||
lastColonPos === -1 ||
atPos === -1) return null;
var config = {
server: plain.slice(atPos + 1, lastColonPos),
server_port: plain.slice(lastColonPos + 1),
password: plain.slice(firstColonPos + 1, atPos),
method: plain.slice(0, firstColonPos)
};
return [config, tag];
}
}
return null;
}
});

View file

@ -0,0 +1,163 @@
'use strict';
'require form';
'require uci';
'require fs';
'require network';
'require rpc';
'require shadowsocks-rust as ss';
var conf = 'shadowsocks-rust';
var cfgtypes = ['ss_local', 'ss_redir', 'ss_server', 'ss_tunnel'];
var callServiceList = rpc.declare({
object: 'service',
method: 'list',
params: [ 'name' ],
expect: { '': {} }
});
return L.view.extend({
render: function(stats) {
var m, s, o;
m = new form.Map(conf,
_('Local Instances'),
_('Instances of shadowsocks-rust components, e.g. ss-local, \
ss-redir, ss-tunnel, ss-server, etc. To enable an instance it \
is required to enable both the instance itself and the remote \
server it refers to.'));
s = m.section(form.GridSection);
s.addremove = true;
s.cfgsections = function() {
return this.map.data.sections(this.map.config)
.filter(function(s) { return cfgtypes.indexOf(s['.type']) !== -1; })
.map(function(s) { return s['.name']; });
};
s.sectiontitle = function(section_id) {
var s = uci.get(conf, section_id);
return (s ? s['.type'] + '.' : '') + section_id;
};
s.renderSectionAdd = function(extra_class) {
var el = form.GridSection.prototype.renderSectionAdd.apply(this, arguments),
optionEl = [E('option', { value: '_dummy' }, [_('-- instance type --')])];
cfgtypes.forEach(function(t) {
optionEl.push(E('option', { value: t }, [t.replace('_', '-')]));
});
var selectEl = E('select', {
class: 'cbi-input-select',
change: function(ev) {
ev.target.parentElement.nextElementSibling.nextElementSibling
.toggleAttribute('disabled', ev.target.value === '_dummy');
}
}, optionEl);
el.lastElementChild.setAttribute('disabled', '');
el.prepend(E('div', {}, selectEl));
return el;
};
s.handleAdd = function(ev, name) {
var selectEl = ev.target.parentElement.firstElementChild.firstElementChild,
type = selectEl.value;
this.sectiontype = type;
var promise = form.GridSection.prototype.handleAdd.apply(this, arguments);
this.sectiontype = undefined;
return promise;
};
s.addModalOptions = function(s, section_id, ev) {
var sdata = uci.get(conf, section_id),
stype = sdata ? sdata['.type'] : null;
if (stype) {
s.sectiontype = stype;
return Promise.all([
L.resolveDefault(fs.stat('/usr/bin/' + stype.replace('_', '-')), null),
network.getDevices()
]).then(L.bind(function(res) {
s.tab('general', _('General Settings'));
s.tab('advanced', _('Advanced Settings'));
s.taboption('general', form.Value, 'label', _('Label'));
s.taboption('general', form.Flag, 'disabled', _('Disable'));
if (!res[0]) {
ss.option_install_package(s, 'general');
}
ss.options_common(s, 'advanced');
if (stype === 'ss_server') {
ss.options_server(s, { tab: 'general' });
o = s.taboption('general', form.Value, 'bind_address',
_('Bind address'),
_('The address ss-server will initiate connection from'));
o.datatype = 'ipaddr';
o.placeholder = '0.0.0.0';
ss.values_ipaddr(o, res[1]);
} else {
ss.options_client(s, 'general', res[1]);
if (stype === 'ss_tunnel') {
o = s.taboption('general', form.Value, 'tunnel_address',
_('Tunnel address'),
_('The address ss-tunnel will forward traffic to'));
o.datatype = 'hostport';
}
}
}, this));
}
};
o = s.option(form.DummyValue, 'overview', _('Overview'));
o.modalonly = false;
o.editable = true;
o.rawhtml = true;
o.renderWidget = function(section_id, option_index, cfgvalue) {
var sdata = uci.get(conf, section_id);
if (sdata) {
return form.DummyValue.prototype.renderWidget.call(this, section_id, option_index, ss.cfgvalue_overview(sdata));
}
return null;
};
o = s.option(form.DummyValue, 'running', _('Running'));
o.modalonly = false;
o.editable = true;
o.default = '';
o = s.option(form.Button, 'disabled', _('Enable/Disable'));
o.modalonly = false;
o.editable = true;
o.inputtitle = function(section_id) {
var s = uci.get(conf, section_id);
if (ss.ucival_to_bool(s['disabled'])) {
this.inputstyle = 'reset';
return _('Disabled');
}
this.inputstyle = 'save';
return _('Enabled');
}
o.onclick = function(ev) {
var inputEl = ev.target.parentElement.nextElementSibling;
inputEl.value = ss.ucival_to_bool(inputEl.value) ? '0' : '1';
return this.map.save();
}
return m.render().finally(function() {
L.Poll.add(function() {
return L.resolveDefault(callServiceList(conf), {})
.then(function(res) {
var instances = null;
try {
instances = res[conf]['instances'];
} catch (e) {}
if (!instances) return;
uci.sections(conf)
.filter(function(s) { return cfgtypes.indexOf(s['.type']) !== -1; })
.forEach(function(s) {
var el = document.getElementById('cbi-shadowsocks-rust-' + s['.name'] + '-running');
if (el) {
var name = s['.type'] + '.' + s['.name'],
running = instances.hasOwnProperty(name)? instances[name].running : false;
el.innerText = running ? 'yes' : 'no';
}
});
});
});
});
},
});

View file

@ -0,0 +1,141 @@
'use strict';
'require view';
'require uci';
'require fs';
'require form';
'require tools.widgets as widgets';
'require shadowsocks-rust as ss';
var conf = 'shadowsocks-rust';
var cfgtypes = ['ss_rules'];
function src_dst_option(s /*, ... */) {
var o = s.taboption.apply(s, L.varargs(arguments, 1));
o.datatype = 'or(ipaddr,cidr)';
}
return L.view.extend({
load: function() {
return Promise.all([
L.resolveDefault(fs.stat('/usr/lib/iptables/libxt_recent.so'), {}),
L.resolveDefault(fs.stat('/usr/bin/ss-rules'), null),
uci.load(conf).then(function() {
if (!uci.get_first(conf, 'ss_rules')) {
uci.set(conf, uci.add(conf, 'ss_rules', 'ss_rules'), 'disabled', '1');
}
})
]);
},
render: function(stats) {
var m, s, o;
m = new form.Map(conf, _('Redir Rules'),
_('On this page you can configure how traffics are to be \
forwarded to ss-redir instances. \
If enabled, packets will first have their src ip addresses checked \
against <em>Src ip/net bypass</em>, <em>Src ip/net forward</em>, \
<em>Src ip/net checkdst</em> and if none matches <em>Src default</em> \
will give the default action to be taken. \
If the prior check results in action <em>checkdst</em>, packets will continue \
to have their dst addresses checked.'));
s = m.section(form.GridSection);
s.addremove = true;
s.addbtntitle = _('Add a new rule...');
s.cfgsections = function() {
return this.map.data.sections(this.map.config)
.filter(function(s) { return cfgtypes.indexOf(s['.type']) !== -1; })
.map(function(s) { return s['.name']; });
};
s.tab('general', _('General Settings'));
s.tab('src', _('Source Settings'));
s.tab('dst', _('Destination Settings'));
s.sectiontype = 'ss_rules';
s.addModalOptions = function(s, section_id, ev) {
s.taboption('general', form.Flag, 'disabled', _('Disable'));
s.taboption('general', form.Value, 'label', _('Label'));
//o = s.taboption('general', form.ListValue, 'server', _('server'));
//ss.values_serverlist(o, '');
o = s.taboption('general', form.ListValue, 'redir_tcp',
_('ss-redir for TCP'));
ss.values_redir(o, 'tcp');
o = s.taboption('general', form.ListValue, 'redir_udp',
_('ss-redir for UDP'));
ss.values_redir(o, 'udp');
o = s.taboption('general', form.ListValue, 'local_default',
_('Local-out default'),
_('Default action for locally generated TCP packets'));
ss.values_actions(o);
o = s.taboption('general', widgets.DeviceSelect, 'ifnames',
_('Ingress interfaces'),
_('Only apply rules on packets from these network interfaces'));
o.multiple = true;
o.noaliases = true;
o.noinactive = true;
s.taboption('general', form.Value, 'ipt_args',
_('Extra arguments'),
_('Passes additional arguments to iptables. Use with care!'));
src_dst_option(s, 'src', form.DynamicList, 'src_ips_bypass',
_('Src ip/net bypass'),
_('Bypass ss-redir for packets with src address in this list'));
src_dst_option(s, 'src', form.DynamicList, 'src_ips_forward',
_('Src ip/net forward'),
_('Forward through ss-redir for packets with src address in this list'));
src_dst_option(s, 'src', form.DynamicList, 'src_ips_checkdst',
_('Src ip/net checkdst'),
_('Continue to have dst address checked for packets with src address in this list'));
o = s.taboption('src', form.ListValue, 'src_default',
_('Src default'),
_('Default action for packets whose src address do not match any of the src ip/net list'));
ss.values_actions(o);
src_dst_option(s, 'dst', form.DynamicList, 'dst_ips_bypass',
_('Dst ip/net bypass'),
_('Bypass ss-redir for packets with dst address in this list'));
src_dst_option(s, 'dst', form.DynamicList, 'dst_ips_forward',
_('Dst ip/net forward'),
_('Forward through ss-redir for packets with dst address in this list'));
var dir = '/etc/shadowsocks-rust';
o = s.taboption('dst', form.FileUpload, 'dst_ips_bypass_file',
_('Dst ip/net bypass file'),
_('File containing ip/net for the purposes as with <em>Dst ip/net bypass</em>'));
o.root_directory = dir;
o = s.taboption('dst', form.FileUpload, 'dst_ips_forward_file',
_('Dst ip/net forward file'),
_('File containing ip/net for the purposes as with <em>Dst ip/net forward</em>'));
o.root_directory = dir;
o = s.taboption('dst', form.ListValue, 'dst_default',
_('Dst default'),
_('Default action for packets whose dst address do not match any of the dst ip list'));
ss.values_actions(o);
o = s.taboption('dst', form.Flag, 'dst_forward_recentrst');
o.title = _('Forward recentrst');
o.description = _('Forward those packets whose dst have recently sent to us multiple tcp-rst');
};
o = s.option(form.Button, 'disabled', _('Enable/Disable'));
o.modalonly = false;
o.editable = true;
o.inputtitle = function(section_id) {
var s = uci.get(conf, section_id);
if (ss.ucival_to_bool(s['disabled'])) {
this.inputstyle = 'reset';
return _('Disabled');
}
this.inputstyle = 'save';
return _('Enabled');
};
o.onclick = function(ev) {
var inputEl = ev.target.parentElement.nextElementSibling;
inputEl.value = ss.ucival_to_bool(inputEl.value) ? '0' : '1';
return this.map.save();
};
return m.render();
},
});

View file

@ -0,0 +1,82 @@
'use strict';
'require form';
'require uci';
'require ui';
'require shadowsocks-rust as ss';
var conf = 'shadowsocks-rust';
return L.view.extend({
render: function() {
var m, s, o;
m = new form.Map(conf, _('Remote Servers'),
_('Definition of remote shadowsocks servers. \
Disable any of them will also disable instances referring to it.'));
s = m.section(form.GridSection, 'server');
s.addremove = true;
s.handleLinkImport = function() {
var textarea = new ui.Textarea();
ui.showModal(_('Import Links'), [
textarea.render(),
E('div', { class: 'right' }, [
E('button', {
class: 'btn',
click: ui.hideModal
}, [ _('Cancel') ]),
' ',
E('button', {
class: 'btn cbi-button-action',
click: ui.createHandlerFn(this, function() {
textarea.getValue().split('\n').forEach(function(s) {
var config = ss.parse_uri(s);
if (config) {
var tag = config[1];
if (tag && !tag.match(/^[a-zA-Z0-9_]+$/)) tag = null;
var sid = uci.add(conf, 'server', tag);
config = config[0];
Object.keys(config).forEach(function(k) {
uci.set(conf, sid, k, config[k]);
});
}
});
return uci.save()
.then(L.bind(this.map.load, this.map))
.then(L.bind(this.map.reset, this.map))
.then(L.ui.hideModal)
.catch(function() {});
})
}, [ _('Import') ])
])
]);
};
s.renderSectionAdd = function(extra_class) {
var el = form.GridSection.prototype.renderSectionAdd.apply(this, arguments);
el.appendChild(E('button', {
'class': 'cbi-button cbi-button-add',
'title': _('Import Links'),
'click': ui.createHandlerFn(this, 'handleLinkImport')
}, [ _('Import Links') ]));
return el;
};
o = s.option(form.Flag, 'disabled', _('Disable'));
o.editable = true;
ss.options_server(s);
return m.render();
},
addFooter: function() {
var p = '#edit=';
if (location.hash.indexOf(p) === 0) {
var section_id = location.hash.substring(p.length);
var editBtn = document.querySelector('#cbi-shadowsocks-rust-' + section_id + ' button.cbi-button-edit');
if (editBtn)
editBtn.click();
}
//return this.super('addFooter', arguments);
return null;
}
});