-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkrpc.js
216 lines (163 loc) · 4.34 KB
/
krpc.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var events = require('events'),
util = require('util'),
crypto = require('crypto'),
bencode = require('bencode'),
TRANS_ID_BYTES = 2;
module.exports = exports = function(options) {
events.EventEmitter.call(this);
if(!options) options = {};
this._transIdBytes = options.transIdBytes || TRANS_ID_BYTES;
this._nextTransId = crypto.randomBytes(this._transIdBytes);
this._queryTimeout = options.queryTimeout || 2000;
this._queryTimers = {};
};
util.inherits(exports, events.EventEmitter);
var proto = exports.prototype;
/*** constants ***/
exports.Errors = {
GENERIC: 201,
SERVER: 202,
PROTOCOL: 203,
METHOD_UNKNOWN: 204
};
/*** foreign error ***/
exports.ForeignError = function(err) {
Error.call(this, 'Foreign Error'
+ (err && err.message ? ': ' + err.message : ''));
this.error = err;
};
util.inherits(exports.ForeignError, Error);
/*** public methods ***/
proto.parse = function(buffer, ip, port) {
var msg = this.decode(buffer);
if(!msg.t)
throw new Error('Missing transaction id');
var method = '_parseType_' + msg.y;
if(!this[method])
throw new Error('Invaild message type');
this[method](msg, ip, port);
return msg;
};
proto.genTransId = function(ip, port, callback, timeout) {
var self = this,
transId = this._nextTransId.toString('hex'),
transIdBuffer = new Buffer(transId, 'hex');
if(typeof ip === 'function') {
callback = ip;
timeout = port;
ip = null;
port = null;
} else if(typeof port === 'function') {
callback = port;
timeout = callback;
port = null;
}
// increase _nextTransId
for(var i = 0; i < this._nextTransId.length; i++) {
if(this._nextTransId[i] !== 0xff) {
this._nextTransId[i]++;
break;
}
this._nextTransId[i] = 0x00;
}
// if(this._queryTimers[transId]) {
// this.emit(transId, new Error('Timeout'));
// this.removeAllListeners(transId);
// }
if(typeof callback === 'function') {
var listener = function(err, rIp, rPort, res) {
if(ip !== rIp && ip)
return;
if(port !== rPort && port)
return;
if(self._queryTimers[transId])
delete self._queryTimers[transId];
callback.call(null, err, res);
};
if(timeout === undefined)
timeout = this._queryTimeout;
if(timeout) {
this._queryTimers[transId] = setTimeout(function() {
self.removeListener(transId, listener);
if(!self._queryTimers[transId])
return;
delete self._queryTimers[transId];
callback(new Error('Timeout'));
}, timeout);
}
this.on(transId, listener);
} else {
delete this._queryTimers[transId];
}
return transIdBuffer;
};
proto.query = function(transId, type, query) {
return this.encode({
t: transId,
y: 'q',
q: type,
a: query
});
};
proto.respond = function(transId, res) {
return this.encode({
t: transId,
y: 'r',
r: res
});
};
proto.error = function(transId, errorCode, errorMsg) {
return this.encode({
t: transId,
y: 'e',
e: [errorCode, errorMsg]
});
};
proto.encode = function(msg) {
return bencode.encode(msg);
};
proto.decode = function(buffer) {
return bencode.decode(buffer);
};
/*** protected methods ***/
proto._parseType_q = function(msg, ip, port) {
if(!(msg.q instanceof Buffer)) {
this.emit('parseError', msg.t, 'Missing query type', ip, port);
throw new Error('Missing query type');
}
if(!msg.a) {
this.emit('parseError', msg.t, 'Missing query data', ip, port);
throw new Error('Missing query data');
}
var type = msg.q.toString();
try {
this.emit('query', type, msg.a, msg.t, ip, port);
this.emit('query_' + type, msg.a, msg.t, ip, port);
} catch(err) {
throw new exports.ForeignError(err);
}
};
proto._parseType_r = function(msg, ip, port) {
if(!msg.r)
throw new Error('Missing respond data');
try {
this.emit('respond', msg.r, msg.t, ip, port);
this.emit(msg.t.toString('hex'), null, ip, port, msg.r);
} catch(err) {
throw new exports.ForeignError(err);
}
};
proto._parseType_e = function(msg, ip, port) {
if(!Array.isArray(msg.e) || msg.e.length !== 2 || typeof msg.e[0] !== 'number')
throw new Error('Invaild error data');
var code = msg.e[0],
message = msg.e[1] ? msg.e[1].toString() : 'Unknown Error',
err = new Error(message);
err.code = code;
try {
this.emit('error', code, message, msg.t, ip, port);
this.emit(msg.t.toString('hex'), err, ip, port);
} catch(err) {
throw new exports.ForeignError(err);
}
};