forked from iliakan/xmpp-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·155 lines (120 loc) · 4.43 KB
/
index.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
#!/usr/bin/env node
// use level: trace for debug only (plaintext passwords in logs!)
// protocol @see https://www.ejabberd.im/files/doc/dev.html#htoc9
var async = require('async');
var bunyan = require('bunyan');
var config = require('./config');
var request = require('request');
var log = bunyan.createLogger({
name: "auth",
streams: [{
level: config.logLevel,
path: config.logPath
}]
});
process.stdin.on('readable', function read() {
log.trace("readable");
var offset = 0;
var buf = process.stdin.read();
if (!buf) return;
log.trace("buffer", buf, buf.toString());
while (true) {
// offset is the current message start
if (buf.length < offset + 2) {
process.stdin.unshift(buf.slice(offset));
return;
}
var messageLength = buf.readUInt16BE(offset);
log.trace("length", messageLength);
if (offset + 2 + messageLength < buf.length) {
process.stdin.unshift(buf.slice(offset));
return;
}
offset += 2;
// message example: "auth:iliakan:x.javascript.ru:password"
var message = buf.slice(offset, messageLength + offset);
offset += messageLength;
message = message.toString();
log.trace("message", message);
message = message.split(':');
/*
Messages:
auth:User:Server:Password (check if a username/password pair is correct)
isuser:User:Server (check if it’s a valid user)
setpass:User:Server:Password (set user’s password)
tryregister:User:Server:Password (try to register an account)
removeuser:User:Server (remove this account)
removeuser3:User:Server:Password (remove this account if the password is correct)
*/
messageQueue.push({
command: message[0],
user: message[1],
server: message[2],
password: message[3] // may be undefined
});
}
});
var messageQueue = async.queue(function(message, callback) {
if (message.command !== 'auth') {
log.info("not supported command", message.command);
// success
respond(true);
callback();
} else {
log.info("send to service", message.command, message.user);
var data = message;
if (typeof config.authParamsModificationFunction === 'function') {
data = config.authParamsModificationFunction(message);
}
log.trace("sending data", data);
if (config.anonymousHosts.indexOf(message.server) !== -1) {
respond(true);
callback();
} else {
if (typeof config.authServiceUrls[message.server] === 'undefined') {
throw new Error("Undefined authServiceUrl for host" + message.server);
} else {
request.post({
url: config.authServiceUrls[message.server],
form: data
}, function(err, httpResponse, body) {
if (err) throw(err);
log.info("receive from service", body);
switch(body) {
case "1":
respond(true);
callback();
break;
case "0":
respond(false);
callback();
break;
default:
log.error("Invalid response (must be 1 or 0)", body);
throw new Error("Invalid response");
}
});
}
}
}
}, 1);
function respond(ok) {
log.info("respond to service", ok);
process.stdout.write(new Buffer.from([0, 2, 0, ok ? 1 : 0]));
}
// using uncaughtException is bad m'kay
// make sure log is flushed!
// @see https://github.com/trentm/node-bunyan/issues/37#issue-6439571
process.on('uncaughtException', function flush(err) {
// prevent infinite recursion
process.removeListener('uncaughtException', flush);
// bonus: log the exception
log.error(err);
if (typeof(log.streams[0]) !== 'object') return;
// throw the original exception once stream is closed
log.streams[0].stream.on('close', function(streamErr, stream) {
throw err;
});
// close stream, flush buffer to disk
log.streams[0].stream.end();
});