forked from smogon/pokemon-showdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverifier.js
67 lines (57 loc) · 1.72 KB
/
verifier.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
/**
* Verifier process
* Pokemon Showdown - http://pokemonshowdown.com/
*
* This is just an asynchronous implementation of a verifier for a
* signed key, because Node.js's crypto functions are synchronous,
* strangely, considering how everything else is asynchronous.
*
* I wrote this one day hoping it would help with performance, but
* I don't think it had any noticeable effect.
*
* @license MIT license
*/
'use strict';
// Because I don't want two files, we're going to fork ourselves.
if (!process.send) {
// This is the parent
let guid = 1;
let callbacks = {};
let callbackData = {};
let child = exports.child = require('child_process').fork('verifier.js', {cwd: __dirname});
exports.verify = function (data, signature, callback) {
let localGuid = guid++;
callbacks[localGuid] = callback;
callbackData[localGuid] = data;
child.send({data: data, sig: signature, guid: localGuid});
};
child.on('message', response => {
if (callbacks[response.guid]) {
callbacks[response.guid](response.success, callbackData[response.guid]);
delete callbacks[response.guid];
delete callbackData[response.guid];
}
});
} else {
// This is the child
global.Config = require('./config/config.js');
let crypto = require('crypto');
let keyalgo = Config.loginserverkeyalgo;
let pkey = Config.loginserverpublickey;
process.on('message', message => {
let verifier = crypto.createVerify(keyalgo);
verifier.update(message.data);
let success = false;
try {
success = verifier.verify(pkey, message.sig, 'hex');
} catch (e) {}
process.send({
success: success,
guid: message.guid,
});
});
process.on('disconnect', () => {
process.exit();
});
require('./repl.js').start('verifier', cmd => eval(cmd));
}