This repository has been archived by the owner on Nov 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
verifier.js
103 lines (88 loc) · 2.37 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
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
/**
* 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';
const crypto = require('crypto');
const ProcessManager = require('./process-manager');
class VerifierManager extends ProcessManager {
/**
* @param {string} message
*/
onMessageUpstream(message) {
// Protocol:
// success: "[id]|1"
// failure: "[id]|0"
let pipeIndex = message.indexOf('|');
let id = +message.substr(0, pipeIndex);
let result = Boolean(~~message.slice(pipeIndex + 1));
// @ts-ignore
if (this.pendingTasks.has(id)) {
// @ts-ignore
this.pendingTasks.get(id)(result);
// @ts-ignore
this.pendingTasks.delete(id);
// @ts-ignore
this.release();
}
}
/**
* @param {string} message
*/
onMessageDownstream(message) {
// protocol:
// "[id]|{data, sig}"
let pipeIndex = message.indexOf('|');
let id = message.substr(0, pipeIndex);
let data = JSON.parse(message.slice(pipeIndex + 1));
if (process.send) process.send(`${id}|${this.receive(data)}`);
}
/**
* @param {{data: string, sig: string}} data
* @return {number}
*/
receive(data) {
let verifier = crypto.createVerify(Config.loginserverkeyalgo);
verifier.update(data.data);
let success = false;
try {
success = verifier.verify(Config.loginserverpublickey, data.sig, 'hex');
} catch (e) {}
return success ? 1 : 0;
}
}
const PM = new VerifierManager({
execFile: __filename,
maxProcesses: ('Config' in global) ? Config.verifierprocesses : 1,
isChatBased: false,
});
if (process.send && module === process.mainModule) {
// This is a child process!
// @ts-ignore
global.Config = require('./config/config');
process.on('message', message => PM.onMessageDownstream(message));
process.on('disconnect', () => process.exit(0));
require('./repl').start('verifier', /** @param {string} cmd */ cmd => eval(cmd));
}
/**
* @param {string} data
* @param {string} signature
* @return {Promise<boolean>}
*/
function verify(data, signature) {
return PM.send({data: data, sig: signature});
}
module.exports = {
VerifierManager,
PM,
verify,
};