Fixed AMT DNS suffix on non-Win32 agents.

Ylian Saint-Hilaire committed Jul 24, 2021 at 10:56 UTC e9e24e13dcdf5b0a625c31e163d66ca49005b2aa
1 file changed +60 -43
agents/meshcore.js
+60 -43
@@ -1236,22 +1236,24 @@ function handleServerCommand(data) {
1236 if ((apftunnel != null) || (amt == null)) return;
1237 if ((state == null) || (state.ProvisioningState == null)) return;
1238 if ((state.UUID == null) || (state.UUID.length != 36)) return; // Bad UUID
1239 - var apfarg = {
1240 - mpsurl: mesh.ServerUrl.replace('/agent.ashx', '/apf.ashx'),
1241 - mpsuser: data.user, // Agent user name
1242 - mpspass: data.pass, // Encrypted login cookie
1243 - mpskeepalive: 60000,
1244 - clientname: state.OsHostname,
1245 - clientaddress: '127.0.0.1',
1246 - clientuuid: state.UUID,
1247 - conntype: 2, // 0 = CIRA, 1 = Relay, 2 = LMS. The correct value is 2 since we are performing an LMS relay, other values for testing.
1248 - meiState: state // MEI state will be passed to MPS server
1249 - };
1250 - addAmtEvent('LMS tunnel start.');
1251 - apftunnel = require('amt-apfclient')({ debug: false }, apfarg);
1252 - apftunnel.onJsonControl = handleApfJsonControl;
1253 - apftunnel.onChannelClosed = function () { addAmtEvent('LMS tunnel closed.'); apftunnel = null; }
1254 - try { apftunnel.connect(); } catch (ex) { }
1239 + getAmtOsDnsSuffix(state, function () {
1240 + var apfarg = {
1241 + mpsurl: mesh.ServerUrl.replace('/agent.ashx', '/apf.ashx'),
1242 + mpsuser: data.user, // Agent user name
1243 + mpspass: data.pass, // Encrypted login cookie
1244 + mpskeepalive: 60000,
1245 + clientname: state.OsHostname,
1246 + clientaddress: '127.0.0.1',
1247 + clientuuid: state.UUID,
1248 + conntype: 2, // 0 = CIRA, 1 = Relay, 2 = LMS. The correct value is 2 since we are performing an LMS relay, other values for testing.
1249 + meiState: state // MEI state will be passed to MPS server
1250 + };
1251 + addAmtEvent('LMS tunnel start.');
1252 + apftunnel = require('amt-apfclient')({ debug: false }, apfarg);
1253 + apftunnel.onJsonControl = handleApfJsonControl;
1254 + apftunnel.onChannelClosed = function () { addAmtEvent('LMS tunnel closed.'); apftunnel = null; }
1255 + try { apftunnel.connect(); } catch (ex) { }
1256 + });
1257 });
1258 break;
1259 }
@@ -1337,6 +1339,19 @@ function handleServerCommand(data) {
1339 }
1340 }
1341
1342 +// On non-Windows platforms, we need to query the DHCP server for the DNS suffix
1343 +function getAmtOsDnsSuffix(mestate, func) {
1344 + if ((process.platform == 'win32') || (mestate.net0 == null) || (mestate.net0.mac == null)) { func(mestate); return; }
1345 + try { require('linux-dhcp') } catch (ex) { func(mestate); return; }
1346 + require('linux-dhcp').client.info(mestate.net0.mac).then(function (d) {
1347 + if ((typeof d.options == 'object') && (typeof d.options.domainname == 'string')) { mestate.OsDnsSuffix = d.options.domainname; }
1348 + func(mestate);
1349 + }, function (e) {
1350 + console.log('DHCP error', e);
1351 + func(mestate);
1352 + });
1353 +}
1354 +
1355 // Download a file from the server and check the hash.
1356 // This download is similar to the one used for meshcore self-update.
1357 var trustedDownloads = {};
@@ -3956,35 +3971,37 @@ function processConsoleCommand(cmd, args, rights, sessionid) {
3971 if (amt == null) { response = "Intel AMT not detected."; break; }
3972 if (apftunnel != null) { response = "Intel AMT server tunnel already active"; break; }
3973 amt.getMeiState(15, function (state) {
3959 - var rx = '';
3960 - if ((state == null) || (state.ProvisioningState == null)) { rx = "Intel AMT not ready for configuration."; } else {
3961 - var apfarg = {
3962 - mpsurl: mesh.ServerUrl.replace('agent.ashx', 'apf.ashx'),
3963 - mpsuser: Buffer.from(mesh.ServerInfo.MeshID, 'hex').toString('base64').substring(0, 16),
3964 - mpspass: Buffer.from(mesh.ServerInfo.MeshID, 'hex').toString('base64').substring(0, 16),
3965 - mpskeepalive: 60000,
3966 - clientname: state.OsHostname,
3967 - clientaddress: '127.0.0.1',
3968 - clientuuid: state.UUID,
3969 - conntype: 2, // 0 = CIRA, 1 = Relay, 2 = LMS. The correct value is 2 since we are performing an LMS relay, other values for testing.
3970 - meiState: state // MEI state will be passed to MPS server
3971 - };
3972 - if ((state.UUID == null) || (state.UUID.length != 36)) {
3973 - rx = "Unable to get Intel AMT UUID";
3974 - } else {
3975 - addAmtEvent('User LMS tunnel start.');
3976 - apftunnel = require('amt-apfclient')({ debug: false }, apfarg);
3977 - apftunnel.onJsonControl = handleApfJsonControl;
3978 - apftunnel.onChannelClosed = function () { addAmtEvent('User LMS tunnel closed.'); apftunnel = null; }
3979 - try {
3980 - apftunnel.connect();
3981 - rx = "Started Intel AMT configuration";
3982 - } catch (ex) {
3983 - rx = JSON.stringify(ex);
3974 + if ((state == null) || (state.ProvisioningState == null)) { require('MeshAgent').SendCommand({ action: 'msg', type: 'console', value: "Intel AMT not ready for configuration." }); } else {
3975 + getAmtOsDnsSuffix(state, function () {
3976 + var rx = '';
3977 + var apfarg = {
3978 + mpsurl: mesh.ServerUrl.replace('agent.ashx', 'apf.ashx'),
3979 + mpsuser: Buffer.from(mesh.ServerInfo.MeshID, 'hex').toString('base64').substring(0, 16),
3980 + mpspass: Buffer.from(mesh.ServerInfo.MeshID, 'hex').toString('base64').substring(0, 16),
3981 + mpskeepalive: 60000,
3982 + clientname: state.OsHostname,
3983 + clientaddress: '127.0.0.1',
3984 + clientuuid: state.UUID,
3985 + conntype: 2, // 0 = CIRA, 1 = Relay, 2 = LMS. The correct value is 2 since we are performing an LMS relay, other values for testing.
3986 + meiState: state // MEI state will be passed to MPS server
3987 + };
3988 + if ((state.UUID == null) || (state.UUID.length != 36)) {
3989 + rx = "Unable to get Intel AMT UUID";
3990 + } else {
3991 + addAmtEvent('User LMS tunnel start.');
3992 + apftunnel = require('amt-apfclient')({ debug: false }, apfarg);
3993 + apftunnel.onJsonControl = handleApfJsonControl;
3994 + apftunnel.onChannelClosed = function () { addAmtEvent('User LMS tunnel closed.'); apftunnel = null; }
3995 + try {
3996 + apftunnel.connect();
3997 + rx = "Started Intel AMT configuration";
3998 + } catch (ex) {
3999 + rx = JSON.stringify(ex);
4000 + }
4001 }
3985 - }
4002 + if (rx != '') { require('MeshAgent').SendCommand({ action: 'msg', type: 'console', value: rx }); }
4003 + });
4004 }
3987 - if (rx != '') { require('MeshAgent').SendCommand({ action: 'msg', type: 'console', value: rx }); }
4005 });
4006 break;
4007 }