forked from OpenUserJS/OpenUserJS.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
294 lines (250 loc) · 13.3 KB
/
routes.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
'use strict';
// Define some pseudo module globals
var isPro = require('./libs/debug').isPro;
var isDev = require('./libs/debug').isDev;
var isDbg = require('./libs/debug').isDbg;
var rateLimit = require('express-rate-limit');
var MongoStore = require('rate-limit-mongo');
var exec = require('child_process').exec;
var hcaptcha = require('express-hcaptcha');
var SITEKEY = process.env.HCAPTCHA_SITE_KEY;
var SECRET = process.env.HCAPTCHA_SECRET_KEY;
//
var main = require('./controllers/index');
var authentication = require('./controllers/auth');
var admin = require('./controllers/admin');
var user = require('./controllers/user');
var script = require('./controllers/script');
var flag = require('./controllers/flag');
var vote = require('./controllers/vote');
var remove = require('./controllers/remove');
var moderation = require('./controllers/moderation');
var group = require('./controllers/group');
var discussion = require('./controllers/discussion');
var issue = require('./controllers/issue');
var scriptStorage = require('./controllers/scriptStorage');
var document = require('./controllers/document');
var svgCaptcha = require('svg-captcha');
var statusCodePage = require('./libs/templateHelpers').statusCodePage;
//--- Configuration inclusions
var settings = require('./models/settings.json');
//--
var waitInstallMin = isDev ? 1 : 60;
var installLimiter = rateLimit({
store: (isDev ? undefined : new MongoStore({
uri: 'mongodb://127.0.0.1:27017/installLimiter',
resetExpireDateOnChange: true, // Rolling
expireTimeMs: waitInstallMin * 60 * 1000 // n minutes for mongo store
})),
windowMs: waitInstallMin * 60 * 1000, // n minutes for all stores
max: 50, // limit each IP to n requests per windowMs for memory store or expireTimeMs for mongo store
handler: function (aReq, aRes, aNext, aOptions) {
aRes.header('Retry-After', waitInstallMin * 60 + 60);
aRes.status(429).send();
}
});
var waitApiMin = isDev ? 1: 15;
var apiLimiter = rateLimit({
store: (isDev ? undefined : new MongoStore({
uri: 'mongodb://127.0.0.1:27017/apiLimiter',
resetExpireDateOnChange: true, // Rolling
expireTimeMs: waitApiMin * 60 * 1000 // n minutes for mongo store
})),
windowMs: waitApiMin * 60 * 1000, // n minutes for all stores
max: 100, // limit each IP to n requests per windowMs for memory store or expireTimeMs for mongo store
handler: function (aReq, aRes, aNext, aOptions) {
aRes.header('Retry-After', waitApiMin * 60 + 60);
aRes.status(429).send();
}
});
var waitCaptchaMin = isDev ? 1: 120;
var captchaLimiter = rateLimit({
store: (isDev ? undefined : new MongoStore({
uri: 'mongodb://127.0.0.1:27017/captchaLimiter',
resetExpireDateOnChange: true, // Rolling
expireTimeMs: waitCaptchaMin * 60 * 1000 // n minutes for mongo store
})),
windowMs: waitCaptchaMin * 60 * 1000, // n minutes for all stores
max: 2, // limit each IP to n requests per windowMs for memory store or expireTimeMs for mongo store
handler: function (aReq, aRes, aNext, aOptions) {
aRes.type('svg').status(200).send(
svgCaptcha('429 Too Many Requests', Object.assign(settings.captchaOpts, {
width: 350
}))
);
}
});
var listMin = isDev ? 1: 60;
var listLimiter = rateLimit({
store: (isDev ? undefined : new MongoStore({
uri: 'mongodb://127.0.0.1:27017/listLimiter',
resetExpireDateOnChange: true, // Rolling
expireTimeMs: listMin * 60 * 1000 // n minutes for mongo store
})),
windowMs: listMin * 60 * 1000, // n minutes for all stores
max: 115, // limit each IP to n requests per windowMs for memory store or expireTimeMs for mongo store
handler: function (aReq, aRes, aNext, aOptions) {
var cmd = null;
if (aReq.rateLimit.current < aReq.rateLimit.limit + 4) {
// Midddlware options
if (!aRes.oujsOptions) {
aRes.oujsOptions = {};
}
aRes.oujsOptions.showReminderListLimit = 4 - (aReq.rateLimit.current - aReq.rateLimit.limit);
aNext();
} else if (aReq.rateLimit.current < aReq.rateLimit.limit + 10) {
aRes.header('Retry-After', listMin * 60 + 60);
statusCodePage(aReq, aRes, aNext, {
statusCode: 429,
statusMessage: 'Too many requests.',
suppressNavigation: true,
isCustomView: true,
statusData: {
isListView: true,
retryAfter: listMin * 60 + 60
}
});
} else if (aReq.rateLimit.current < aReq.rateLimit.limit + 15) {
aRes.header('Retry-After', listMin * 60 + 60);
aRes.status(429).send('Too many requests. Please try again later');
} else if (aReq.rateLimit.current < aReq.rateLimit.limit + 20) {
aRes.header('Retry-After', listMin * 60 + 60);
aRes.status(429).send();
} else {
cmd = (isPro && process.env.AUTOBAN ? process.env.AUTOBAN : 'echo SIMULATING AUTOBAN') +
' ' + aReq.connection.remoteAddress;
exec(cmd, function (aErr, aStdout, aStderr) {
if (aErr) {
console.error('FAIL AUTOBAN', cmd);
// fallthrough
} else {
console.log('AUTOBAN', aReq.connection.remoteAddress);
// fallthrough
}
aRes.connection.destroy();
});
}
}
});
module.exports = function (aApp) {
//--- Middleware
//--- Routes
// Authentication routes
aApp.route('/auth/').post(authentication.preauth,
hcaptcha.middleware.validate(SECRET, SITEKEY),
authentication.errauth,
authentication.auth);
aApp.route('/auth/:strategy').get(authentication.auth);
aApp.route('/auth/:strategy/callback/:junk?').get(authentication.callback);
aApp.route('/login').get(main.register);
aApp.route('/register').get(function (aReq, aRes) {
aRes.redirect(301, '/login');
});
aApp.route('/logout').get(main.logout);
// User routes
aApp.route('/users').get(listLimiter, user.userListPage);
aApp.route('/users/:username').get(user.view);
aApp.route('/users/:username/comments').get(listLimiter, user.userCommentListPage);
aApp.route('/users/:username/scripts').get(listLimiter, user.userScriptListPage);
aApp.route('/users/:username/syncs').get(listLimiter, user.userSyncListPage);
aApp.route('/users/:username/github/repos').get(authentication.validateUser, user.userGitHubRepoListPage);
aApp.route('/users/:username/github/repo').get(authentication.validateUser, user.userGitHubRepoPage);
aApp.route('/users/:username/github/import').post(authentication.validateUser, user.userGitHubImportScriptPage);
aApp.route('/users/:username/profile/edit').get(authentication.validateUser, user.userEditProfilePage).post(authentication.validateUser, user.update);
aApp.route('/users/:username/profile/captcha').get(captchaLimiter, authentication.validateUser, user.userEditProfilePageCaptcha);
aApp.route('/users/:username/update').post(authentication.validateUser, admin.adminUserUpdate);
// NOTE: Some below inconsistent with priors
aApp.route('/user/preferences').get(authentication.validateUser, user.userEditPreferencesPage);
aApp.route('/user').get(function (aReq, aRes) {
aRes.redirect(302, '/users');
});
aApp.route('/api/user/exist/:username').head(apiLimiter, user.exist);
aApp.route('/api/user/session/extend').post(apiLimiter, authentication.validateUser, user.extend);
aApp.route('/api/user/session/destroyOne').post(apiLimiter, authentication.validateUser, user.destroyOne);
// Adding script/library routes
aApp.route('/user/add/scripts').get(listLimiter, authentication.validateUser, user.newScriptPage);
aApp.route('/user/add/scripts/new').get(authentication.validateUser, script.new(user.editScript)).post(authentication.validateUser, script.new(user.submitSource));
aApp.route('/user/add/scripts/upload').post(authentication.validateUser, user.uploadScript);
aApp.route('/user/add/lib').get(authentication.validateUser, user.newLibraryPage);
aApp.route('/user/add/lib/new').get(authentication.validateUser, script.new(script.lib(user.editScript))).post(authentication.validateUser, script.new(script.lib(user.submitSource)));
aApp.route('/user/add/lib/upload').post(authentication.validateUser, script.lib(user.uploadScript));
aApp.route('/user/add').get(function (aReq, aRes) {
aRes.redirect(301, '/user/add/scripts');
});
// Script routes
aApp.route('/scripts/:username/:scriptname').get(script.view);
aApp.route('/scripts/:username/:scriptname/edit').get(authentication.validateUser, script.edit).post(authentication.validateUser, script.edit);
aApp.route('/scripts/:username/:scriptname/source').get(user.editScript);
aApp.route('/scripts/:username').get(function (aReq, aRes) {
aRes.redirect(301, '/users/' + aReq.params.username + '/scripts'); // NOTE: Watchpoint
});
aApp.route('/install/:username/:scriptname').get(installLimiter, scriptStorage.unlockScript, scriptStorage.sendScript);
aApp.route('/meta/:username/:scriptname').get(scriptStorage.sendMeta);
// Github hook routes
aApp.route('/github/hook').post(scriptStorage.webhook);
aApp.route('/github/service').post(function (aReq, aRes, aNext) { aNext(); });
// Library routes
aApp.route('/libs/:username/:scriptname').get(script.lib(script.view));
aApp.route('/libs/:username/:scriptname/edit').get(authentication.validateUser, script.lib(script.edit)).post(authentication.validateUser, script.lib(script.edit));
aApp.route('/libs/:username/:scriptname/source').get(script.lib(user.editScript));
// Raw source
aApp.route('/src/:type(scripts|libs)/:username/:scriptname').get(installLimiter, scriptStorage.unlockScript, scriptStorage.sendScript);
// Issues routes
aApp.route('/:type(scripts|libs)/:username/:scriptname/issues/:open(open|closed|all)?').get(listLimiter, issue.list);
aApp.route('/:type(scripts|libs)/:username/:scriptname/issue/new').get(authentication.validateUser, issue.open).post(authentication.validateUser, issue.open);
aApp.route('/:type(scripts|libs)/:username/:scriptname/issues/:topic').get(listLimiter, issue.view).post(authentication.validateUser, issue.comment);
aApp.route('/:type(scripts|libs)/:username/:scriptname/issues/:topic/:action(close|reopen)').get(authentication.validateUser, issue.changeStatus);
// Admin routes
aApp.route('/admin').get(authentication.validateUser, admin.adminPage);
aApp.route('/admin/npm/version').get(authentication.validateUser, admin.adminNpmVersionView);
aApp.route('/admin/git/short').get(authentication.validateUser, admin.adminGitShortView);
aApp.route('/admin/git/branch').get(authentication.validateUser, admin.adminGitBranchView);
aApp.route('/admin/process/clone').get(authentication.validateUser, admin.adminProcessCloneView);
aApp.route('/admin/session/active').get(authentication.validateUser, admin.adminSessionActiveView);
aApp.route('/admin/npm/package').get(authentication.validateUser, admin.adminNpmPackageView);
aApp.route('/admin/npm/list').get(authentication.validateUser, admin.adminNpmListView);
aApp.route('/admin/api').get(authentication.validateUser, admin.adminApiKeysPage);
aApp.route('/admin/authas').get(authentication.validateUser, admin.authAsUser);
aApp.route('/admin/json').get(authentication.validateUser, admin.adminJsonView);
aApp.route('/admin/api/update').post(authentication.validateUser, admin.apiAdminUpdate);
// Moderation routes
aApp.route('/mod').get(authentication.validateUser, moderation.modPage);
aApp.route('/mod/removed').get(authentication.validateUser, moderation.removedItemListPage);
aApp.route('/mod/removed/:id').get(authentication.validateUser, moderation.removedItemPage);
// Vote route
aApp.route(/^\/vote\/(scripts|libs)\/((.+?)(?:\/(.+))?)$/).post(authentication.validateUser, vote.vote);
// Flag route
aApp.route(/^\/flag\/(users|scripts|libs)\/((.+?)(?:\/(.+))?)$/).post(authentication.validateUser, flag.flag);
// Remove route
aApp.route(/^\/remove\/(users|scripts|libs)\/((.+?)(?:\/(.+))?)$/).post(authentication.validateUser, remove.rm);
// Group routes
aApp.route('/groups').get(listLimiter, group.list);
aApp.route('/group/:groupname').get(listLimiter, group.view);
aApp.route('/group').get(function (aReq, aRes) { aRes.redirect('/groups'); });
aApp.route('/api/group/search/:term/:addTerm?').get(group.search);
// Discussion routes
// TODO: Update templates for new discussion routes
aApp.route('/forum').get(listLimiter, discussion.categoryListPage);
aApp.route('/:p(forum)?/:category(announcements|corner|garage|discuss|issues|all)').get(listLimiter, discussion.list);
aApp.route('/:p(forum)?/:category(announcements|corner|garage|discuss)/:topic').get(listLimiter, discussion.show).post(authentication.validateUser, discussion.createComment);
aApp.route('/:p(forum)?/:category(announcements|corner|garage|discuss)/new').get(authentication.validateUser, discussion.newTopic).post(authentication.validateUser, discussion.createTopic);
// dupe
aApp.route('/post/:category(announcements|corner|garage|discuss)').get(authentication.validateUser, discussion.newTopic).post(authentication.validateUser, discussion.createTopic);
// About document routes
aApp.route('/about/:document?').get(document.view);
// Home route
aApp.route('/').get(listLimiter, main.home);
// Misc API
aApp.route('/api').head(function (aReq, aRes, aNext) {
aRes.status(200).send();
});
// Static Routes
require('./routesStatic')(aApp);
// Fallback routes
aApp.use(function (aReq, aRes, aNext) {
statusCodePage(aReq, aRes, aNext, {
statusCode: 404,
statusMessage: 'This is not the page you\'re are looking for.'
});
});
};