fix meshctrl with key=xxx and loginkey #6328

Signed-off-by: si458 <simonsmith5521@gmail.com>

si458 committed Aug 16, 2024 at 16:02 UTC fa39f8a10537982953fd7ee708c621b2d2ead6e0
2 files changed +10 -3
meshctrl.js
+4 -2
@@ -1270,10 +1270,10 @@ function serverConnect() {
1270 var domainid = '', username = 'admin';
1271 if (args.logindomain != null) { domainid = args.logindomain; }
1272 if (args.loginuser != null) { username = args.loginuser; }
1273 - url += '?auth=' + encodeCookie({ userid: 'user/' + domainid + '/' + username, domainid: domainid }, ckey);
1273 + url += (url.indexOf('?key=') >= 0 ? '&auth=' : '?auth=') + encodeCookie({ userid: 'user/' + domainid + '/' + username, domainid: domainid }, ckey);
1274 } else {
1275 if (args.logindomain != null) { console.log("--logindomain can only be used along with --loginkey."); process.exit(); return; }
1276 - if (loginCookie != null) { url += '?auth=' + loginCookie; }
1276 + if (loginCookie != null) { url += (url.indexOf('?key=') >= 0 ? '&auth=' : '?auth=') + loginCookie; }
1277 }
1278
1279 const ws = new WebSocket(url, options);
@@ -2401,6 +2401,8 @@ function serverConnect() {
2401 if (data.cause == 'noauth') {
2402 if (data.msg == 'tokenrequired') {
2403 console.log('Authentication token required, use --token [number].');
2404 + } else if (data.msg == 'nokey') {
2405 + console.log('URL key is invalid or missing, please specify ?key=xxx in url');
2406 } else {
2407 if ((args.loginkeyfile != null) || (args.loginkey != null)) {
2408 console.log('Invalid login, check the login key and that this computer has the correct time.');
webserver.js
+6 -1
@@ -6573,12 +6573,17 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
6573 try { ws.close(); } catch (ex) { }
6574 return;
6575 }
6576 - if ((domain.loginkey != null) && (domain.loginkey.indexOf(req.query.key) == -1)) { ws.close(); return; } // Check 3FA URL key
6576 + if ((domain.loginkey != null) && (domain.loginkey.indexOf(req.query.key) == -1)) { // Check 3FA URL key
6577 + try { ws.send(JSON.stringify({ action: 'close', cause: 'noauth', msg: 'nokey' })); } catch (ex) { }
6578 + try { ws.close(); } catch (ex) { }
6579 + return;
6580 + }
6581 PerformWSSessionAuth(ws, req, true, function (ws1, req1, domain, user, cookie, authData) {
6582 if (user == null) { // User is not authenticated, perform inner server authentication
6583 if (req.headers['x-meshauth'] === '*') {
6584 PerformWSSessionInnerAuth(ws, req, domain, function (ws1, req1, domain, user) { obj.meshUserHandler.CreateMeshUser(obj, obj.db, ws1, req1, obj.args, domain, user, authData); }); // User is authenticated
6585 } else {
6586 + try { ws.send(JSON.stringify({ action: 'close', cause: 'noauth', msg: 'noauth' })); } catch (ex) { }
6587 try { ws.close(); } catch (ex) { } // user is not authenticated and inner authentication was not requested, disconnect now.
6588 }
6589 } else {