forked from newgrounds/slackporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·190 lines (165 loc) · 4.83 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
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
var assert = require('assert');
var path = require('path');
var util = require('util');
var Boom = require('boom');
var Hapi = require('hapi');
var Joi = require('joi');
var Q = require('q');
var winston = require('winston');
var porter = require('./porter');
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, {
level: 'debug'
});
winston.add(winston.transports.File, {
filename: './slackporter.log',
level: 'info'
});
var server = new Hapi.Server();
server.connection({ port: 2603, host: '127.0.0.1' });
var fromUserSchema = Joi.object().keys({
url: Joi.string().uri().regex(/^https:\/\/.*\.slack\.com\/?$/).required(),
token: Joi.string().allow(''),
emojiJson: Joi.string().allow('')
});
var toUserSchema = Joi.object().keys({
url: Joi.string().uri().regex(/^https:\/\/.*\.slack\.com\/?$/).required(),
email: Joi.string().email().required(),
password: Joi.string().required(),
token: Joi.string().required()
});
var loginSchema = Joi.object().keys({
userFrom: fromUserSchema,
userTo: toUserSchema
});
var transferSchema = Joi.object().keys({
userFrom: fromUserSchema,
userTo: toUserSchema,
emojiName: Joi.string().required(),
emojiUrl: Joi.string().uri().required()
});
// Just guessing at what reasonable rate limits should be.
var rateLimiter = {};
var MAX_REQUESTS = 10;
var RATE_INTERVAL = 2000;
setInterval(function() {
rateLimiter = {};
}, RATE_INTERVAL);
server.register(require('vision'), function(err) {
assert(!err, err);
server.views({
engines: {
'html': {
module: require('handlebars'),
compileMode: 'sync'
}
},
compileMode: 'async',
path: path.join(__dirname, 'templates')
});
});
server.register(require('inert'), function(err) {
server.route([{
method: 'GET',
path: '/static/{param*}',
handler: {
directory: {
path: 'static'
}
}
}]);
});
server.route([{
method: 'GET',
path: '/',
handler: function (request, reply) {
reply.view('index');
}
}, {
method: 'POST',
path: '/emojilist',
config: {
validate: {
payload: loginSchema
}
},
handler: function(request, reply) {
rateLimiter[request.info.remoteAddress] =
rateLimiter[request.info.remoteAddress] + 1 || 1;
if (rateLimiter[request.info.remoteAddress] > MAX_REQUESTS) {
return reply(Boom.tooManyRequests('too many requests'));
}
var userFrom = request.payload.userFrom;
var userTo = request.payload.userTo;
if (!userFrom.token && !userFrom.emojiJson) {
return reply(
Boom.badRequest('You must enter either a token or a JSON response.'));
}
request.payload.userFrom.info = request.info;
request.payload.userTo.info = request.info;
var fromPromise = porter.fetchEmojiList(request.payload.userFrom);
var toPromise = porter.getLoginPage(request.payload.userTo)
.then(porter.postLoginPage)
.then(porter.fetchEmojiList);
Q.all([fromPromise, toPromise])
.then(function(results) {
try {
for (var e in results[1].emoji) {
delete(results[0].emoji[e]);
}
reply.view('emojis', { emoji: results[0].emoji });
} catch(error) {
winston.error('emojilist Error:\n' + util.inspect(error));
reply(Boom.badImplementation('Internal Server Error'));
}
}, function(error) {
winston.error('emojilist all handler Error:\n' + util.inspect(error));
if (error.statusCode) {
switch(error.statusCode) {
case 404:
reply(Boom.notFound('Team page not found for ' + error.url));
break;
case 401:
reply(Boom.unauthorized('Invalid password for ' + error.url));
break;
}
} else {
reply(Boom.badImplementation('Internal Server Error'));
}
});
}
}, {
method: 'POST',
path: '/transferEmoji',
config: {
validate: {
payload: transferSchema
}
},
handler: function(request, reply) {
rateLimiter[request.info.remoteAddress] =
rateLimiter[request.info.remoteAddress] + 1 || 1;
if (rateLimiter[request.info.remoteAddress] > MAX_REQUESTS) {
return reply(Boom.tooManyRequests('too many requests'));
}
request.payload.userTo.info = request.info;
porter.getLoginPage(request.payload.userTo)
.then(porter.postLoginPage)
.then(porter.getEmojiUploadPage)
.then(function(options) {
return porter.transferEmoji(options,
request.payload.emojiName,
request.payload.emojiUrl);
})
.then(function() {
reply({success: true});
})
.fail(function(error) {
winston.error('/transferEmoji error:\n', util.inspect(error));
reply(Boom.badImplementation('Internal Server Error'));
});
}
}]);
server.start(function () {
winston.info('\n\nServer running at:', server.info.uri);
});