Improved APF control command.

This commit is contained in:
Ylian Saint-Hilaire 2020-10-12 09:29:34 -07:00
parent 80980f7089
commit e1bbd49490
3 changed files with 53 additions and 24 deletions

View File

@ -3538,8 +3538,12 @@ function createMeshCore(agent) {
case 'apf': { case 'apf': {
if (meshCoreObj.intelamt !== null) { if (meshCoreObj.intelamt !== null) {
if (args['_'].length == 1) { if (args['_'].length == 1) {
if (args['_'][0] == 'on') { var connType = -1, connTypeStr = args['_'][0].toLowerCase();
response = "Starting APF tunnel"; if (connTypeStr == 'lms') { connType = 2; }
if (connTypeStr == 'relay') { connType = 1; }
if (connTypeStr == 'cira') { connType = 0; }
if (connTypeStr == 'off') { connType = -2; }
if (connType >= 0) { // Connect
var apfarg = { var apfarg = {
mpsurl: mesh.ServerUrl.replace('agent.ashx', 'apf.ashx'), mpsurl: mesh.ServerUrl.replace('agent.ashx', 'apf.ashx'),
mpsuser: Buffer.from(mesh.ServerInfo.MeshID, 'hex').toString('base64').substring(0, 16), mpsuser: Buffer.from(mesh.ServerInfo.MeshID, 'hex').toString('base64').substring(0, 16),
@ -3548,7 +3552,7 @@ function createMeshCore(agent) {
clientname: require('os').hostname(), clientname: require('os').hostname(),
clientaddress: '127.0.0.1', clientaddress: '127.0.0.1',
clientuuid: meshCoreObj.intelamt.uuid, clientuuid: meshCoreObj.intelamt.uuid,
conntype: 2 // 0 = CIRA, 1 = Relay, 2 = LMS. The correct value is 2 since we are performing an LMS relay, other values for testing. conntype: connType // 0 = CIRA, 1 = Relay, 2 = LMS. The correct value is 2 since we are performing an LMS relay, other values for testing.
}; };
if ((apfarg.clientuuid == null) || (apfarg.clientuuid.length != 36)) { if ((apfarg.clientuuid == null) || (apfarg.clientuuid.length != 36)) {
response = "Unable to get Intel AMT UUID: " + apfarg.clientuuid; response = "Unable to get Intel AMT UUID: " + apfarg.clientuuid;
@ -3557,25 +3561,24 @@ function createMeshCore(agent) {
apftunnel = require('apfclient')(tobj, apfarg); apftunnel = require('apfclient')(tobj, apfarg);
try { try {
apftunnel.connect(); apftunnel.connect();
response += "...success"; response = "Started APF tunnel";
} catch (e) { } catch (e) {
response += JSON.stringify(e); response = JSON.stringify(e);
} }
} }
} else if (args['_'][0] == 'off') { } else if (connType == -2) { // Disconnect
response = "Stopping APF tunnel";
try { try {
apftunnel.disconnect(); apftunnel.disconnect();
response += "..success"; response = "Stopped APF tunnel";
} catch (e) { } catch (e) {
response += JSON.stringify(e); response = JSON.stringify(e);
} }
apftunnel = null; apftunnel = null;
} else { } else {
response = "Invalid command.\r\nCmd syntax: apf on|off"; response = "Invalid command.\r\nUse: apf lms|relay|cira|off";
} }
} else { } else {
response = "APF tunnel is " + (apftunnel == null ? "off" : "on"); response = "APF tunnel is " + (apftunnel == null ? "off" : "on") + "\r\nUse: apf lms|relay|cira|off";
} }
} else { } else {
response = "APF tunnel requires Intel AMT"; response = "APF tunnel requires Intel AMT";

View File

@ -1,9 +1,25 @@
/*
Copyright 2018-2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/** /**
* @description APF/CIRA Client for duktape * @description APF/CIRA Client for Duktape
* @author Joko Sastriawan * @author Joko Sastriawan & Ylian Saint-Hilaire
* @copyright Intel Corporation 2019 * @copyright Intel Corporation 2019
* @license Apache-2.0 * @license Apache-2.0
* @version v0.0.1 * @version v0.0.2
*/ */
function CreateAPFClient(parent, args) { function CreateAPFClient(parent, args) {
@ -84,7 +100,7 @@ function CreateAPFClient(parent, args) {
KEEPALIVE_REPLY: 209, KEEPALIVE_REPLY: 209,
KEEPALIVE_OPTIONS_REQUEST: 210, KEEPALIVE_OPTIONS_REQUEST: 210,
KEEPALIVE_OPTIONS_REPLY: 211, KEEPALIVE_OPTIONS_REPLY: 211,
MESH_CONNECTION_TYPE: 250 // This is a Mesh specific command that instructs the server of the connection type: 1 = Relay, 2 = LMS. JSON_CONTROL: 250 // This is a Mesh specific command that sends JSON to and from the MPS server.
} }
var APFDisconnectCode = { var APFDisconnectCode = {
@ -163,14 +179,15 @@ function CreateAPFClient(parent, args) {
}); });
obj.state = CIRASTATE.INITIAL; obj.state = CIRASTATE.INITIAL;
if (typeof obj.args.conntype == 'number') { SendConnectionType(obj.forwardClient.ws, obj.args.conntype); } if ((typeof obj.args.conntype == 'number') && (obj.args.conntype != 0)) { SendJsonControl(obj.forwardClient.ws, { action: 'connType', value: obj.args.conntype } ); }
SendProtocolVersion(obj.forwardClient.ws, obj.args.clientuuid); SendProtocolVersion(obj.forwardClient.ws, obj.args.clientuuid);
SendServiceRequest(obj.forwardClient.ws, 'auth@amt.intel.com'); SendServiceRequest(obj.forwardClient.ws, 'auth@amt.intel.com');
} }
function SendConnectionType(socket, type) { function SendJsonControl(socket, o) {
socket.write(String.fromCharCode(APFProtocol.MESH_CONNECTION_TYPE) + IntToStr(type)); var data = JSON.stringify(o)
Debug("APF: Send connection type " + type); socket.write(String.fromCharCode(APFProtocol.JSON_CONTROL) + IntToStr(data.length) + data);
Debug("APF: Send JSON control: " + data);
} }
function SendProtocolVersion(socket, uuid) { function SendProtocolVersion(socket, uuid) {

View File

@ -80,7 +80,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
KEEPALIVE_REPLY: 209, KEEPALIVE_REPLY: 209,
KEEPALIVE_OPTIONS_REQUEST: 210, KEEPALIVE_OPTIONS_REQUEST: 210,
KEEPALIVE_OPTIONS_REPLY: 211, KEEPALIVE_OPTIONS_REPLY: 211,
MESH_CONNECTION_TYPE: 250 // This is a Mesh specific command that instructs the server of the connection type: 1 = Relay, 2 = LMS. JSON_CONTROL: 250 // This is a Mesh specific command that sends JSON to and from the MPS server.
}; };
/* /*
@ -870,13 +870,22 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
removeCiraConnection(socket); removeCiraConnection(socket);
return 7; return 7;
} }
case APFProtocol.MESH_CONNECTION_TYPE: // This is a Mesh specific command to indicate the connect type. case APFProtocol.JSON_CONTROL: // This is a Mesh specific command that sends JSON to and from the MPS server.
{ {
if (len < 5) return 0; if (len < 5) return 0;
if ((socket.tag.connType == 0) && (socket.tag.SystemId == null)) { // Once set, the connection type can't be changed. var jsondatalen = common.ReadInt(data, 1);
socket.tag.connType = common.ReadInt(data, 1); // 0 = CIRA, 1 = Relay, 2 = LMS if (len < (5 + jsondatalen)) return 0;
var jsondata = null, jsondatastr = data.substring(5, 5 + jsondatalen);
try { jsondata = JSON.parse(jsondatastr); } catch (ex) { }
if ((jsondata == null) || (typeof jsondata.action != 'string')) return;
switch (jsondata.action) {
case 'connType':
if ((socket.tag.connType != 0) || (socket.tag.SystemId != null)) return; // Once set, the connection type can't be changed.
if (typeof jsondata.value != 'number') return;
socket.tag.connType = jsondata.value; // 0 = CIRA, 1 = Relay, 2 = LMS
break;
} }
return 5; return 5 + jsondatalen;
} }
default: default:
{ {