Skip to content

Commit

Permalink
start server side
Browse files Browse the repository at this point in the history
  • Loading branch information
citronneur committed Jun 30, 2015
1 parent 866f630 commit 6613ec0
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 13 deletions.
2 changes: 1 addition & 1 deletion bin/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
var rdp = require("../lib");


rdp.createClient({ domain : 'siradel', userName : 'speyrefitte'}).connect('54.187.36.238', 3389);
rdp.createServer().listen(33389);
9 changes: 9 additions & 0 deletions bin/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* test file
*/
var rdp = require("../lib");


rdp.createServer({}, function (server) {

}).listen(33389);
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@

var protocol = require('./protocol');
module.exports = {
createClient : protocol.rdp.createClient
createClient : protocol.rdp.createClient,
createServer : protocol.rdp.createServer
};
22 changes: 21 additions & 1 deletion lib/protocol/rdp.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,29 @@ function createClient(config) {
return new RdpClient(config);
};

/**
* RDP server side protocol
* @param config {object} configuration
* @param socket {net.Socket}
*/
function RdpServer(config, socket) {
config = config || {};
this.connected = false;
this.bufferLayer = new layer.BufferLayer(socket);
this.tpkt = new TPKT(this.bufferLayer);
this.x224 = new x224.Server(this.tpkt);
};

function createServer (config, next) {
return net.createServer(function (socket) {
next(new RdpServer(config, socket));
});
};

/**
* Module exports
*/
module.exports = {
createClient : createClient
createClient : createClient,
createServer : createServer
};
83 changes: 73 additions & 10 deletions lib/protocol/x224.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,28 @@ function negotiation(opt) {
* @param opt {object} component type options
* @see http://msdn.microsoft.com/en-us/library/cc240470.aspx
*/
function clientConnectionRequestPDU(opt) {
function clientConnectionRequestPDU(opt, cookie) {
var self = {
len : new type.UInt8(function() {
return new type.Component(self).size() - 1;
}),
code : new type.UInt8(MessageType.X224_TPDU_CONNECTION_REQUEST, { constant : true }),
padding : new type.Component([new type.UInt16Le(), new type.UInt16Le(), new type.UInt8()]),
cookie : cookie || new type.Factory( function (s) {
var offset = 0;
while (true) {
var token = s.buffer.readUint16LE(s.offset + offset);
if (token === 0x0d0a) {
self.cookie = new type.BinaryString(null, { readLength : new type.CallableValue(offset) }).read(s);
return;
}
else {
offset += 1;
}
}
}, { conditional : function () {
return self.len.value > 14;
}}),
protocolNeg : negotiation({ optional : true })
};

Expand Down Expand Up @@ -128,6 +143,13 @@ function X224(transport) {
this.transport = transport;
this.requestedProtocol = Protocols.PROTOCOL_SSL;
this.selectedProtocol = Protocols.PROTOCOL_SSL;

var self = this;
this.transport.on('close', function() {
self.emit('close');
}).on('error', function (err) {
self.emit('error', err);
});
}

//inherit from Layer
Expand Down Expand Up @@ -159,12 +181,7 @@ X224.prototype.send = function(message) {
*/
function Client(transport) {
X224.call(this, transport);
var self = this;
this.transport.on('close', function() {
self.emit('close');
}).on('error', function (err) {
self.emit('error', err);
});

}

//inherit from X224 automata
Expand All @@ -174,7 +191,7 @@ inherits(Client, X224);
* Client automata connect event
*/
Client.prototype.connect = function() {
var message = clientConnectionRequestPDU();
var message = clientConnectionRequestPDU(null, new type.BinaryString());
message.obj.protocolNeg.obj.type.value = NegotiationType.TYPE_RDP_NEG_REQ;
message.obj.protocolNeg.obj.result.value = this.requestedProtocol;
this.transport.send(message);
Expand Down Expand Up @@ -235,13 +252,59 @@ Client.prototype.recvConnectionConfirm = function(s) {
/**
* Server x224 automata
*/
function Server() {
X224.call(this);
function Server(transport) {
X224.call(this, transport);
var self = this;
this.transport.once('data', function (s) {
self.recvConnectionRequest(s);
});
}

//inherit from X224 automata
inherits(Server, X224);

/**
* @see http://msdn.microsoft.com/en-us/library/cc240470.aspx
* @param s {type.Stream}
*/
Server.prototype.recvConnectionRequest = function (s) {
var request = clientConnectionRequestPDU().read(s);
if (!request.obj.protocolNeg.isReaded) {
throw new Error('NODE_RDP_PROTOCOL_X224_NO_BASIC_SECURITY_LAYER');
}

this.requestedProtocol = request.obj.protocolNeg.obj.result.value;
this.selectedProtocol = this.requestedProtocol & Protocols.PROTOCOL_SSL;

if (!(this.selectedProtocol & Protocols.PROTOCOL_SSL)) {
var confirm = serverConnectionConfirm();
confirm.obj.protocolNeg.obj.type.value = NegociationType.TYPE_RDP_NEG_FAILURE;
confirm.obj.protocolNeg.obj.result.value = NegotiationFailureCode.SSL_REQUIRED_BY_SERVER;
this.transport.send(confirm);
}
else {
this.sendConnectionConfirm();
}
};

/**
* Start SSL connection if needed
* @see http://msdn.microsoft.com/en-us/library/cc240501.aspx
*/
Server.prototype.sendConnectionConfirm = function () {
var confirm = serverConnectionConfirm();
confirm.obj.protocolNeg.obj.type.value = NegotiationType.TYPE_RDP_NEG_RSP;
confirm.obj.protocolNeg.obj.result.value = this.selectedProtocol;
this.transport.send(confirm);

log.info('start SSL connection');

var self = this;
this.transport.transport.startTLS(true, function() {
self.emit('connect', self.selectedProtocol);
});
};

/**
* Module exports
*/
Expand Down

0 comments on commit 6613ec0

Please sign in to comment.