forked from citronneur/node-rdpjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtpkt.js
More file actions
172 lines (157 loc) · 3.95 KB
/
tpkt.js
File metadata and controls
172 lines (157 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright (c) 2014-2015 Sylvain Peyrefitte
*
* This file is part of node-rdpjs.
*
* node-rdpjs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var inherits = require('util').inherits;
var type = require('../core').type;
var events = require('events');
/**
* Type of tpkt packet
* Fastpath is use to shortcut RDP stack
* @see http://msdn.microsoft.com/en-us/library/cc240621.aspx
* @see http://msdn.microsoft.com/en-us/library/cc240589.aspx
*/
var Action = {
FASTPATH_ACTION_FASTPATH : 0x0,
FASTPATH_ACTION_X224 : 0x3
};
/**
* TPKT layer of rdp stack
*/
function TPKT(transport) {
this.transport = transport;
// wait 2 bytes
this.transport.expect(2);
// next state is receive header
var self = this;
this.transport.once('data', function(s) {
self.recvHeader(s);
}).on('close', function() {
self.emit('close');
}).on('error', function (err) {
self.emit('error', err);
});
}
/**
* inherit from a packet layer
*/
inherits(TPKT, events.EventEmitter);
/**
* Receive correct packet as expected
* @param s {type.Stream}
*/
TPKT.prototype.recvHeader = function(s) {
var version = new type.UInt8().read(s).value;
var self = this;
if(version === Action.FASTPATH_ACTION_X224) {
new type.UInt8().read(s);
this.transport.expect(2);
this.transport.once('data', function(s) {
self.recvExtendedHeader(s);
});
}
else {
this.secFlag = ((version >> 6) & 0x3);
var length = new type.UInt8().read(s).value;
if (length & 0x80) {
this.transport.expect(1);
this.transport.once('data', function(s) {
self.recvExtendedFastPathHeader(s, length);
});
}
else {
this.transport.expect(length - 2);
this.transport.once('data', function(s) {
self.recvFastPath(s);
});
}
}
};
/**
* Receive second part of header packet
* @param s {type.Stream}
*/
TPKT.prototype.recvExtendedHeader = function(s) {
var size = new type.UInt16Be().read(s);
this.transport.expect(size.value - 4);
//next state receive packet
var self = this;
this.transport.once('data', function(s) {
self.recvData(s);
});
};
/**
* Receive data available for presentation layer
* @param s {type.Stream}
*/
TPKT.prototype.recvData = function(s) {
this.emit('data', s);
this.transport.expect(2);
//next state receive header
var self = this;
this.transport.once('data', function(s) {
self.recvHeader(s);
});
};
/**
* Read extended fastpath header
* @param s {type.Stream}
*/
TPKT.prototype.recvExtendedFastPathHeader = function (s, length) {
var rightPart = new type.UInt8().read(s).value;
var leftPart = length & ~0x80;
var packetSize = (leftPart << 8) + rightPart;
var self = this;
this.transport.expect(packetSize - 3);
this.transport.once('data', function(s) {
self.recvFastPath(s);
});
};
/**
* Read fast path data
* @param s {type.Stream}
*/
TPKT.prototype.recvFastPath = function (s) {
this.emit('fastPathData', this.secFlag, s);
var self = this;
this.transport.expect(2);
this.transport.once('data', function(s) {
self.recvHeader(s);
});
};
/**
* Send message throught TPKT layer
* @param message {type.*}
*/
TPKT.prototype.send = function(message) {
this.transport.send(new type.Component([
new type.UInt8(Action.FASTPATH_ACTION_X224),
new type.UInt8(0),
new type.UInt16Be(message.size() + 4),
message
]));
};
/**
* close stack
*/
TPKT.prototype.close = function() {
this.transport.close();
};
/**
* Module exports
*/
module.exports = TPKT;