Skip to content

Commit

Permalink
add mcs server side
Browse files Browse the repository at this point in the history
  • Loading branch information
citronneur committed Jul 3, 2015
1 parent 938da69 commit c28a08c
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 49 deletions.
6 changes: 4 additions & 2 deletions bin/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ var rdp = require("../lib");


rdp.createServer({
key : '/home/sylvain/dev/node-rdp-cert/ryans-key.pem',
cert : '/home/sylvain/dev/node-rdp-cert/ryans-cert.pem'
//key : '/home/sylvain/dev/node-rdp-cert/ryans-key.pem',
//cert : '/home/sylvain/dev/node-rdp-cert/ryans-cert.pem'
key : '/home/speyrefitte/dev/node-rdp-cert/ryans-key.pem',
cert : '/home/speyrefitte/dev/node-rdp-cert/ryans-cert.pem'
}, function (server) {

}).listen(33390);
2 changes: 1 addition & 1 deletion lib/asn1/univ.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ Enumerate.prototype.decode = function(s, decoder) {
* @returns {type.Component}
*/
Enumerate.prototype.encode = function(encoder) {
return encoder.encode(this.tag, new UInt8(this.value));
return encoder.encode(this.tag, new type.UInt8(this.value));
};

/**
Expand Down
1 change: 1 addition & 0 deletions lib/protocol/rdp.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ function RdpServer(config, socket) {
this.bufferLayer = new layer.BufferLayer(socket);
this.tpkt = new TPKT(this.bufferLayer);
this.x224 = new x224.Server(this.tpkt, config.key, config.cert);
this.mcs = new t125.mcs.Server(this.x224);
};

inherits(RdpServer, events.EventEmitter);
Expand Down
166 changes: 141 additions & 25 deletions lib/protocol/t125/gcc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var h221_sc_key = "McDn";
/**
* @see http://msdn.microsoft.com/en-us/library/cc240509.aspx
*/
var MESSAGE_TYPE = {
var MessageType = {
//server -> client
SC_CORE : 0x0C01,
SC_SECURITY : 0x0C02,
Expand Down Expand Up @@ -217,29 +217,37 @@ function block(data) {
}),
// data block
data : data || new type.Factory(function(s){
log.debug('read gcc block : ' + self.type.value);
var options = {
readLength : new type.CallableValue( function () {
return self.length.value - 4;
})
};
switch(self.type.value) {
case MESSAGE_TYPE.SC_CORE:
self.data = serverCoreData({ readLength : new type.CallableValue(self.length.value - 4) }).read(s);
case MessageType.SC_CORE:
self.data = serverCoreData(options).read(s);
break;
case MESSAGE_TYPE.SC_SECURITY:
self.data = serverSecurityData({ readLength : new type.CallableValue(self.length.value - 4) }).read(s);
break;;
case MESSAGE_TYPE.CS_CORE:
self.data = clientCoreData({ readLength : new type.CallableValue(self.length.value - 4) }).read(s);
case MessageType.SC_SECURITY:
self.data = serverSecurityData(options).read(s);
break;
case MESSAGE_TYPE.CS_SECURITY:
self.data = clientSecurityData({ readLength : new type.CallableValue(self.length.value - 4) }).read(s);
case MessageType.SC_NET:
self.data = serverNetworkData(null, options).read(s);
break;
case MESSAGE_TYPE.CS_NET:
self.data = clientSecurityData({ readLength : new type.CallableValue(self.length.value - 4) }).read(s);
case MessageType.CS_CORE:
self.data = clientCoreData(options).read(s);
break;
case MessageType.CS_SECURITY:
self.data = clientSecurityData(options).read(s);
break;
case MessageType.CS_NET:
self.data = clientNetworkData(null, options).read(s);
break;
default:
log.warn("unknown gcc block type " + self.type.value);
self.data = new type.BinaryString(null, { readLength : new type.CallableValue(self.length.value - 4) }).read(s);
log.debug("unknown gcc block type " + self.type.value);
self.data = new type.BinaryString(null, options).read(s);
}
})
};

return new type.Component(self);
}

Expand All @@ -254,7 +262,7 @@ function block(data) {
*/
function clientCoreData(opt) {
var self = {
__TYPE__ : MESSAGE_TYPE.CS_CORE,
__TYPE__ : MessageType.CS_CORE,
rdpVersion : new type.UInt32Le(VERSION.RDP_VERSION_5_PLUS),
desktopWidth : new type.UInt16Le(1280),
desktopHeight : new type.UInt16Le(800),
Expand All @@ -278,6 +286,7 @@ function clientCoreData(opt) {
pad1octet : new type.UInt8(0, { optional : true }),
serverSelectedProtocol : new type.UInt32Le(0, { optional : true })
};

return new type.Component(self, opt);
}

Expand All @@ -288,11 +297,12 @@ function clientCoreData(opt) {
*/
function serverCoreData(opt) {
var self = {
__TYPE__ : MESSAGE_TYPE.SC_CORE,
__TYPE__ : MessageType.SC_CORE,
rdpVersion : new type.UInt32Le(VERSION.RDP_VERSION_5_PLUS),
clientRequestedProtocol : new type.UInt32Le(null, { optional : true }),
earlyCapabilityFlags : new type.UInt32Le(null, { optional : true })
};

return new type.Component(self, opt);
}

Expand All @@ -303,10 +313,11 @@ function serverCoreData(opt) {
*/
function clientSecurityData(opt) {
var self = {
__TYPE__ : MESSAGE_TYPE.CS_SECURITY,
__TYPE__ : MessageType.CS_SECURITY,
encryptionMethods : new type.UInt32Le(ENCRYPTION_METHOD.ENCRYPTION_FLAG_40BIT | ENCRYPTION_METHOD.ENCRYPTION_FLAG_56BIT | ENCRYPTION_METHOD.ENCRYPTION_FLAG_128BIT),
extEncryptionMethods : new type.UInt32Le()
};

return new type.Component(self, opt);
}

Expand All @@ -318,10 +329,25 @@ function clientSecurityData(opt) {
*/
function serverSecurityData(opt) {
var self = {
__TYPE__ : MESSAGE_TYPE.SC_SECURITY,
__TYPE__ : MessageType.SC_SECURITY,
encryptionMethod : new type.UInt32Le(),
encryptionLevel : new type.UInt32Le()
};

return new type.Component(self, opt);
}

/**
* Channel definition
* @param opt {object} Classic type options
* @returns {type.Component}
*/
function channelDef (opt) {
var self = {
name : new type.BinaryString(null, { readLength : new type.CallableValue(8) }),
options : new type.UInt32Le()
};

return new type.Component(self, opt);
}

Expand All @@ -330,11 +356,47 @@ function serverSecurityData(opt) {
* @param opt {object} Classic type options
* @returns {type.Component}
*/
function clientNetworkData(opt) {
function clientNetworkData(channelDefArray, opt) {
var self = {
__TYPE__ : MESSAGE_TYPE.CS_NET,
channelCount : new type.UInt32Le()
__TYPE__ : MessageType.CS_NET,
channelCount : new type.UInt32Le( function () {
return self.channelDefArray.obj.length;
}),
channelDefArray : channelDefArray || new type.Factory( function (s) {
self.channelDefArray = new type.Component([]);

for (var i = 0; i < self.channelCount.value; i++) {
self.channelDefArray.obj.push(channelDef().read(s));
}
})
};

return new type.Component(self, opt);
}

/**
* @param channelIds {type.Component} list of available channels
* @param opt {object} Classic type options
* @returns {type.Component}
*/
function serverNetworkData (channelIds, opt) {
var self = {
__TYPE__ : MessageType.SC_NET,
MCSChannelId : new type.UInt16Le(1003, { constant : true }),
channelCount : new type.UInt16Le(function () {
return self.channelIdArray.obj.length;
}),
channelIdArray : channelIds || new type.Factory( function (s) {
self.channelIdArray = new type.Component([]);
for (var i = 0; i < self.channelCount.value; i++) {
self.channelIdArray.obj.push(new type.UInt16Le().read(s));
}
}),
pad : new type.UInt16Le(null, { conditional : function () {
return (self.channelCount.value % 2) === 1;
}})
};

return new type.Component(self, opt);
}

Expand All @@ -354,6 +416,7 @@ function settings(blocks, opt) {
}
}),
};

return new type.Component(self, opt);
}

Expand Down Expand Up @@ -390,12 +453,47 @@ function readConferenceCreateResponse(s) {
});
}

/**
* Read GCC request
* @param s {type.Stream}
* @returns {Array(type.Component)} list of client block
*/
function readConferenceCreateRequest (s) {
per.readChoice(s);
if (!per.readObjectIdentifier(s, t124_02_98_oid)) {
throw new error.ProtocolError('NODE_RDP_PROTOCOL_T125_GCC_BAD_H221_SC_KEY');
}
per.readLength(s);
per.readChoice(s);
per.readSelection(s);
per.readNumericString(s, 1);
per.readPadding(s, 1);

if (per.readNumberOfSet(s) !== 1) {
throw new error.ProtocolError('NODE_RDP_PROTOCOL_T125_GCC_BAD_SET');
}

if (per.readChoice(s) !== 0xc0) {
throw new error.ProtocolError('NODE_RDP_PROTOCOL_T125_GCC_BAD_CHOICE');
}

per.readOctetStream(s, h221_cs_key, 4);

length = per.readLength(s);
var clientSettings = settings(null, { readLength : new type.CallableValue(length) });

// Object magic
return clientSettings.read(s).obj.blocks.obj.map(function(e) {
return e.obj.data;
});
}

/**
* Built {type.Componen} from gcc user data
* @param userData {type.Component} GCC data from client
* @returns {type.Component} GCC encoded client user data
*/
function writeConferenceCreateRequest(userData) {
function writeConferenceCreateRequest (userData) {
var userDataStream = new type.Stream(userData.size());
userData.write(userDataStream);

Expand All @@ -408,16 +506,34 @@ function writeConferenceCreateRequest(userData) {
]);
}

function writeConferenceCreateResponse (userData) {
var userDataStream = new type.Stream(userData.size());
userData.write(userDataStream);

return new type.Component([
per.writeChoice(0), per.writeObjectIdentifier(t124_02_98_oid),
per.writeLength(userData.size() + 14), per.writeChoice(0x14),
per.writeInteger16(0x79F3, 1001), per.writeInteger(1), per.writeEnumerates(0),
per.writeNumberOfSet(1), per.writeChoice(0xc0),
per.writeOctetStream(new Buffer(h221_sc_key), 4), per.writeOctetStream(userDataStream.getValue())
]);
}

/**
* Module exports
*/
module.exports = {
MESSAGE_TYPE : MESSAGE_TYPE,
MessageType : MessageType,
VERSION : VERSION,
block : block,
clientCoreData : clientCoreData,
clientNetworkData : clientNetworkData,
clientSecurityData : clientSecurityData,
serverCoreData : serverCoreData,
serverSecurityData : serverSecurityData,
serverNetworkData : serverNetworkData,
readConferenceCreateResponse : readConferenceCreateResponse,
writeConferenceCreateRequest : writeConferenceCreateRequest
readConferenceCreateRequest : readConferenceCreateRequest,
writeConferenceCreateRequest : writeConferenceCreateRequest,
writeConferenceCreateResponse : writeConferenceCreateResponse
};
Loading

0 comments on commit c28a08c

Please sign in to comment.