-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathttpapi.js
128 lines (116 loc) · 4.6 KB
/
ttpapi.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
var https = require('https')
, io = require('socket.io');
var TTPAPI = function () {
var self = this;
this._port = (typeof arguments[0] === 'number' && arguments[0] % 1 === 0) ? arguments[0] : 80;
this._options = (Object.prototype.toString.call(arguments[1]) === '[object Object]') ? arguments[1] : {};
this._io = io.listen(this._port, this._options);
this._users = {};
this._events = [];
this._io.disable('browser client');
this._io.set('log level', 1);
this._io.set('transports', ['websocket', 'xhr-polling']);
this._io.set('authorization', function (data, callback) {
if (self._options.room_url !== undefined && data.headers.referer.substr(data.headers.referer.lastIndexOf('/') + 1) !== self._options.room_url) {
callback(null, false);
} else {
callback(null, true);
}
});
this._io.sockets.on('connection', function (socket) {
var id = socket.id,
x;
self._users[id] = {
socket: socket,
auth: false,
userid: null
};
socket.on('disconnect', function() {
delete self._users[id];
});
socket.on('auth', function (data, resObj) {
var authValid;
if (resObj === undefined) {
socket.emit('message', {success: false, re: 'auth', error: "You require an update to TT+. Please install - http://turntableplus.fm/downloads/latest.crx", log: true, alert: true});
socket.disconnect();
return;
}
if (typeof self._options.auth === 'function' && self._options.auth(data.userid) !== true) {
resObj({success: false, error: "Bot-level authentication failed.", log: true, alert: false});
socket.disconnect();
return;
} else if (typeof data.auth !== "string" || data.auth.length !== 40) {
resObj({success: false, error: "Auth is not valid.", log: true, alert: false});
socket.disconnect();
return;
}
https.get({host: "bots.turntableplus.fm", port: 443, path: "/auth/u/" + data.userid + "/a/" + data.auth}, function (authResponse) {
var json = '';
authResponse.on('data', function (chunk) {
json += chunk.toString();
});
authResponse.on('end', function () {
try {
json = JSON.parse(json);
if (json.success === true) {
self._users[id].auth = true;
self._users[id].userid = data.userid;
socket.set('auth', true);
socket.set('userid', data.userid);
resObj({success: true});
} else {
json.log = true;
json.alert = false;
resObj(json);
socket.disconnect();
}
} catch (e) {
resObj({success: false, error: "Unknown authentication error.", log: true, alert: false});
socket.disconnect();
}
});
});
});
});
};
TTPAPI.prototype.on = function (eventType, listener) {
this._io.on('connection', function (socket) {
socket.on(eventType, function () {
listener.apply(socket, arguments);
});
});
};
TTPAPI.prototype.emit = function () {
var args = [].slice.call(arguments), socket;
socket = this.getSocket(args.shift());
if (socket !== null) {
socket.emit.apply(socket, args);
}
};
TTPAPI.prototype.broadcast = function () {
var x, user;
for (x in this._users) {
user = this._users[x];
if (user.hasOwnProperty('auth') === true && user.auth === true) {
user.socket.emit.apply(user.socket, arguments);
}
}
};
TTPAPI.prototype.getSocket = function (userid) {
var x, user;
for (x in this._users) {
user = this._users[x];
if (user.hasOwnProperty('userid') === true && user.userid === userid && user.hasOwnProperty('auth') === true && user.auth === true) {
return user.socket;
}
}
return null;
};
TTPAPI.prototype.getUserid = function (socket) {
if (typeof socket === 'object' && socket.hasOwnProperty('id') === true && this._users[socket.id] !== undefined) {
return this._users[socket.id].userid;
} else {
return false;
}
}
exports.TTPAPI = TTPAPI;