fix webrtcconfig and allow stun servers #6309

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

si458 committed Sep 3, 2024 at 13:42 UTC ac0d805378887afdfb933aac1620ea1d59059777
5 files changed +27 -7
meshcentral.js
+11 -1
@@ -1520,7 +1520,17 @@ function CreateMeshCentralServer(config, args) {
1520 if (obj.args.redirport == null) obj.args.redirport = 80;
1521 if (obj.args.minifycore == null) obj.args.minifycore = false;
1522 if (typeof obj.args.agentidletimeout != 'number') { obj.args.agentidletimeout = 150000; } else { obj.args.agentidletimeout *= 1000 } // Default agent idle timeout is 2m, 30sec.
1523 - if ((obj.args.lanonly != true) && (obj.args.webrtconfig == null)) { obj.args.webrtconfig = { iceservers: [{ urls: 'stun:stun.l.google.com:19302' }, { urls: 'stun:stun.services.mozilla.com' }] }; } // Setup default WebRTC STUN servers
1523 + if ((obj.args.lanonly != true) && (typeof obj.args.webrtconfig == 'object')) { // fix incase you are using an old mis-spelt webrtconfig
1524 + obj.args.webrtcconfig = obj.args.webrtconfig;
1525 + delete obj.args.webrtconfig;
1526 + }
1527 + if ((obj.args.lanonly != true) && (obj.args.webrtcconfig == null)) { obj.args.webrtcconfig = { iceServers: [{ urls: 'stun:stun.l.google.com:19302' }, { urls: 'stun:stun.cloudflare.com:3478' }] }; } // Setup default WebRTC STUN servers
1528 + else if ((obj.args.lanonly != true) && (typeof obj.args.webrtcconfig == 'object')) {
1529 + if (obj.args.webrtcconfig.iceservers) { // webrtc is case-sensitive, so must rename iceservers to iceServers!
1530 + obj.args.webrtcconfig.iceServers = obj.args.webrtcconfig.iceservers;
1531 + delete obj.args.webrtcconfig.iceservers;
1532 + }
1533 + }
1534 if (typeof obj.args.ignoreagenthashcheck == 'string') { if (obj.args.ignoreagenthashcheck == '') { delete obj.args.ignoreagenthashcheck; } else { obj.args.ignoreagenthashcheck = obj.args.ignoreagenthashcheck.split(','); } }
1535
1536 // Setup a site administrator
public/scripts/agent-redir-ws-0.1.1.js
+2 -1
@@ -23,6 +23,7 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
23 obj.ctrlMsgAllowed = true;
24 obj.attemptWebRTC = false;
25 obj.webRtcActive = false;
26 + obj.webrtcconfig = null;
27 obj.webSwitchOk = false;
28 obj.webchannel = null;
29 obj.webrtc = null;
@@ -158,7 +159,7 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
159
160 if (obj.attemptWebRTC == true) {
161 // Try to get WebRTC setup
161 - var configuration = null; //{ "iceServers": [ { 'urls': 'stun:stun.services.mozilla.com' }, { 'urls': 'stun:stun.l.google.com:19302' } ] };
162 + var configuration = obj.webrtcconfig; //{ "iceServers": [ { 'urls': 'stun:stun.cloudflare.com:3478' }, { 'urls': 'stun:stun.l.google.com:19302' } ] };
163 if (typeof RTCPeerConnection !== 'undefined') { obj.webrtc = new RTCPeerConnection(configuration); }
164 else if (typeof webkitRTCPeerConnection !== 'undefined') { obj.webrtc = new webkitRTCPeerConnection(configuration); }
165 if ((obj.webrtc != null) && (obj.webrtc.createDataChannel)) {
views/default.handlebars
+3
@@ -1560,6 +1560,8 @@
1560 var debugmode = 0;
1561 var windowsBrowser = detectWindowsBrowser();
1562 var attemptWebRTC = ((features & 128) != 0);
1563 + var webrtcconfiguration = '{{{webrtcconfig}}}';
1564 + if (webrtcconfiguration == '') { webrtcconfiguration = null; } else { try { webrtcconfiguration = JSON.parse(decodeURIComponent(webrtcconfiguration)); } catch (ex) { console.log('Invalid WebRTC config: "' + webrtcconfiguration + '".'); webrtcconfiguration = null; } }
1565 var passRequirements = '{{{passRequirements}}}';
1566 if (passRequirements != '') { passRequirements = JSON.parse(decodeURIComponent(passRequirements)); }
1567 var customui = '{{{customui}}}';
@@ -9292,6 +9294,7 @@
9294 desktop.debugmode = debugmode;
9295 desktop.m.debugmode = debugmode;
9296 desktop.attemptWebRTC = attemptWebRTC;
9297 + desktop.webrtcconfig = webrtcconfiguration;
9298 desktop.options = {};
9299 if (tsid != null) { desktop.options.tsid = tsid; }
9300 if (consent != null) { desktop.options.consent = consent; }
views/messenger.handlebars
+1 -1
@@ -83,7 +83,7 @@
83 var meshMessengerImage = '{{{meshMessengerImage}}}';
84 var remoteUserName = '{{{username}}}';
85 var remoteUserId = '{{{userid}}}';
86 - var webrtcconfiguration = '{{{webrtconfig}}}';
86 + var webrtcconfiguration = '{{{webrtcconfig}}}';
87 if (webrtcconfiguration == '') { webrtcconfiguration = null; } else { try { webrtcconfiguration = JSON.parse(decodeURIComponent(webrtcconfiguration)); } catch (ex) { console.log('Invalid WebRTC config: "' + webrtcconfiguration + '".'); webrtcconfiguration = null; } }
88 var windowFocus = true;
89 var chatTextSession = new Date().toString() + '\r\n';
webserver.js
+10 -4
@@ -3127,6 +3127,11 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
3127 if ((serverFeatures & 2) != 0) { serverFeatures -= 2; } // Disallow simple server restore
3128 }
3129
3130 + // Get WebRTC configuration
3131 + var webRtcConfig = null;
3132 + if (obj.parent.config.settings && obj.parent.config.settings.webrtcconfig && (typeof obj.parent.config.settings.webrtcconfig == 'object')) { webRtcConfig = encodeURIComponent(JSON.stringify(obj.parent.config.settings.webrtcconfig)).replace(/'/g, '%27'); }
3133 + else if (args.webrtcconfig && (typeof args.webrtcconfig == 'object')) { webRtcConfig = encodeURIComponent(JSON.stringify(args.webrtcconfig)).replace(/'/g, '%27'); }
3134 +
3135 // Refresh the session
3136 render(dbGetFunc.req, dbGetFunc.res, getRenderPage('default', dbGetFunc.req, domain), getRenderArgs({
3137 authCookie: authCookie,
@@ -3155,7 +3160,8 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
3160 webRelayDns: ((args.relaydns != null) ? args.relaydns[0] : ''),
3161 hidePowerTimeline: (domain.hidepowertimeline ? 'true' : 'false'),
3162 showNotesPanel: (domain.shownotespanel ? 'true' : 'false'),
3158 - userSessionsSort: (domain.usersessionssort ? domain.usersessionssort : 'SessionId')
3163 + userSessionsSort: (domain.usersessionssort ? domain.usersessionssort : 'SessionId'),
3164 + webrtcconfig: webRtcConfig
3165 }, dbGetFunc.req, domain), user);
3166 }
3167 xdbGetFunc.req = req;
@@ -3632,11 +3638,11 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
3638
3639 // Get WebRTC configuration
3640 var webRtcConfig = null;
3635 - if (obj.parent.config.settings && obj.parent.config.settings.webrtconfig && (typeof obj.parent.config.settings.webrtconfig == 'object')) { webRtcConfig = encodeURIComponent(JSON.stringify(obj.parent.config.settings.webrtconfig)).replace(/'/g, '%27'); }
3636 - else if (args.webrtconfig && (typeof args.webrtconfig == 'object')) { webRtcConfig = encodeURIComponent(JSON.stringify(args.webrtconfig)).replace(/'/g, '%27'); }
3641 + if (obj.parent.config.settings && obj.parent.config.settings.webrtcconfig && (typeof obj.parent.config.settings.webrtcconfig == 'object')) { webRtcConfig = encodeURIComponent(JSON.stringify(obj.parent.config.settings.webrtcconfig)).replace(/'/g, '%27'); }
3642 + else if (args.webrtcconfig && (typeof args.webrtcconfig == 'object')) { webRtcConfig = encodeURIComponent(JSON.stringify(args.webrtcconfig)).replace(/'/g, '%27'); }
3643
3644 // Setup other options
3639 - var options = { webrtconfig: webRtcConfig };
3645 + var options = { webrtcconfig: webRtcConfig };
3646 if (typeof domain.meshmessengertitle == 'string') { options.meshMessengerTitle = domain.meshmessengertitle; } else { options.meshMessengerTitle = '!'; }
3647
3648 // Get the userid and name