Skip to content

Commit

Permalink
feat(server): add password generator
Browse files Browse the repository at this point in the history
  • Loading branch information
muink committed Aug 25, 2024
1 parent 8115d10 commit d9796a6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
25 changes: 20 additions & 5 deletions htdocs/luci-static/resources/homeproxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,26 @@ return baseclass.extend({
return L.resolveDefault(callGetSingBoxFeatures(), {});
},

generateUUIDv4: function() {
/* Thanks to https://stackoverflow.com/a/2117523 */
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, (c) =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
generateRand: function(type, length) {
var byteArr;
if (['base64', 'hex'].includes(type))
byteArr = crypto.getRandomValues(new Uint8Array(length));
switch (type) {
case 'base64':
/* Thanks to https://stackoverflow.com/questions/9267899 */
return btoa(String.fromCharCode.apply(null, byteArr));
case 'hex':
return Array.from(byteArr, (byte) =>
(byte & 255).toString(16).padStart(2, '0')
).join('');
case 'uuid':
/* Thanks to https://stackoverflow.com/a/2117523 */
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, (c) =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
default:
return null;
};
},

loadDefaultLabel: function(uciconfig, ucisection) {
Expand Down
63 changes: 63 additions & 0 deletions htdocs/luci-static/resources/view/homeproxy/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'require poll';
'require rpc';
'require uci';
'require ui';
'require view';

'require homeproxy as hp';
Expand Down Expand Up @@ -41,6 +42,46 @@ function renderStatus(isRunning) {
return renderHTML;
}

function handleGenKey(option) {
var section_id = this.section.section;
var type = this.section.getOption('type').formvalue(section_id);
var widget = this.map.findElement('id', 'widget.cbid.homeproxy.%s.%s'.format(section_id, option));
var password, required_method;

if (option === 'uuid')
required_method = 'uuid';
else if (type === 'shadowsocks')
required_method = this.section.getOption('shadowsocks_encrypt_method')?.formvalue(section_id);

switch (required_method) {
case 'aes-128-gcm':
case '2022-blake3-aes-128-gcm':
password = hp.generateRand('base64', 16);
break;
case 'aes-192-gcm':
password = hp.generateRand('base64', 24);
break;
case 'aes-256-gcm':
case 'chacha20-ietf-poly1305':
case 'xchacha20-ietf-poly1305':
case '2022-blake3-aes-256-gcm':
case '2022-blake3-chacha20-poly1305':
password = hp.generateRand('base64', 32);
break;
case 'none':
password = '';
break;
case 'uuid':
password = hp.generateRand('uuid');
break;
default:
password = hp.generateRand('hex', 16);
break;
}

return widget.value = password;
}

return view.extend({
load: function() {
return Promise.all([
Expand Down Expand Up @@ -139,6 +180,17 @@ return view.extend({
o.depends('type', 'shadowsocks');
o.depends('type', 'trojan');
o.depends('type', 'tuic');
o.renderWidget = function() {
var node = form.Value.prototype.renderWidget.apply(this, arguments);

(node.querySelector('.control-group') || node).appendChild(E('button', {
'class': 'cbi-button cbi-button-apply',
'title': _('Generate'),
'click': ui.createHandlerFn(this, handleGenKey, this.option)
}, [ _('Generate') ]));

return node;
}
o.validate = function(section_id, value) {
if (section_id) {
var type = this.map.lookupOption('type', section_id)[0].formvalue(section_id);
Expand Down Expand Up @@ -265,6 +317,17 @@ return view.extend({
o.depends('type', 'tuic');
o.depends('type', 'vless');
o.depends('type', 'vmess');
o.renderWidget = function() {
var node = form.Value.prototype.renderWidget.apply(this, arguments);

(node.querySelector('.control-group') || node).appendChild(E('button', {
'class': 'cbi-button cbi-button-apply',
'title': _('Generate'),
'click': ui.createHandlerFn(this, handleGenKey, this.option)
}, [ _('Generate') ]));

return node;
}
o.validate = hp.validateUUID;
o.modalonly = true;

Expand Down

0 comments on commit d9796a6

Please sign in to comment.