forked from syzoj/syzoj
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
260 lines (229 loc) · 8.65 KB
/
app.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
const fs = require('fs');
const serializejs = require('serialize-javascript');
const { v4: UUID } = require('uuid');
const commandLineArgs = require('command-line-args');
const jwt = require('jsonwebtoken');
const optionDefinitions = [
{ name: 'config', alias: 'c', type: String, defaultValue: './config.json' },
];
const options = commandLineArgs(optionDefinitions);
global.syzoj = {
rootDir: __dirname,
config: require('object-assign-deep')({}, require('./config-example.json'), require(options.config)),
languages: require('./language-config.json'),
configDir: options.config,
models: [],
modules: [],
db: null,
serviceID: UUID(),
log(obj) {
if (obj instanceof ErrorMessage) return;
console.log(obj);
},
async run() {
let Express = require('express');
global.app = Express();
syzoj.production = app.get('env') === 'production';
let winstonLib = require('./libs/winston');
winstonLib.configureWinston(!syzoj.production);
this.utils = require('./utility');
let sccRules = new Map(require('./libs/scc_rules'));
function codeTabsEmit(func) {
let lines = func.toString().split('\n');
let minLen = lines.slice(1).map(line => line.length - line.trimLeft().length).reduce((x, y) => Math.min(x, y));
return lines.map(line => line.slice(0, minLen).trim().length === 0 ? line.slice(minLen) : line).join('\n');
}
let ruleColorizeTask = Array.from(sccRules.values()).forEachAsync(async rule => {
let showCode = `// The following script is executed by Node.js ${process.version}.\n\n/**\n * Calculate the length of the submitted code being counted.\n * @param {string} code The content of submitted code.\n * @param {string} lang The abbr. of the language used by the code.\n * @returns {number} the length of the submitted code being counted\n */\n${codeTabsEmit(rule[1])}\n\n/**\n * Calculate the player's score for one problem.\n * @param {number} len The shortest code length of this problem for this player.\n * @param {number} minLen The shortest code length of this problem among all players.\n * @returns {number} The score of this problem for this player, can be float.\n */\n${codeTabsEmit(rule[2])}`;
rule.push(await this.utils.highlight(showCode, syzoj.languages['nodejs'].highlight));
});
this.utils.sccRules = sccRules;
// Set assets dir
app.use(Express.static(__dirname + '/static', { maxAge: syzoj.production ? '1y' : 0 }));
// Set template engine ejs
app.set('view engine', 'ejs');
// Use body parser
app.use(Express.urlencoded({
extended: true,
limit: '50mb'
}));
app.use(Express.json({ limit: '50mb' }));
// Use cookie parser
app.use(require('cookie-parser')());
app.locals.serializejs = serializejs;
let multer = require('multer');
app.multer = multer({ dest: syzoj.utils.resolvePath(syzoj.config.upload_dir, 'tmp') });
// This should before load api_v2, to init the `res.locals.user`
this.loadHooks();
// Trick to bypass CSRF for APIv2
app.use((() => {
let router = new Express.Router();
app.apiRouter = router;
require('./modules/api_v2');
return router;
})());
app.server = require('http').createServer(app);
await this.connectDatabase();
this.loadModules();
// redis and redisCache is for syzoj-renderer
const redis = require('redis');
this.redis = redis.createClient(this.config.redis);
if (require.main === module) {
// Loaded by node CLI, not by `require()`.
if (process.send) {
// if it's started by child_process.fork(), it must be requested to restart
// wait until parent process quited.
await new Promise((resolve, reject) => {
process.on('message', message => {
if (message === 'quited') resolve();
});
process.send('quit');
});
}
await this.lib('judger').connect();
await ruleColorizeTask;
app.server.listen(parseInt(syzoj.config.port), syzoj.config.hostname, () => {
this.log(`SYZOJ is listening on ${syzoj.config.hostname}:${parseInt(syzoj.config.port)}...`);
});
}
},
restart() {
console.log('Will now fork a new process.');
const child = require('child_process').fork(__filename, ['-c', options.config]);
child.on('message', (message) => {
if (message !== 'quit') return;
console.log('Child process requested "quit".')
child.send('quited', err => {
if (err) console.error('Error sending "quited" to child process:', err);
process.exit();
});
});
},
async connectDatabase() {
let Sequelize = require('sequelize');
const cls = require('cls-hooked');
const namespace = cls.createNamespace('syzoj-cls-hooked-namespace');
Sequelize.useCLS(namespace);
this.db = new Sequelize(this.config.db.database, this.config.db.username, this.config.db.password, {
host: this.config.db.host,
dialect: 'mysql',
logging: syzoj.production ? false : syzoj.log,
timezone: require('moment')().format('Z')
});
this.db.Op = Sequelize.Op;
global.Promise = require('bluebird');
this.db.countQuery = async (sql, options) => (await this.db.query(`SELECT COUNT(*) FROM (${sql}) AS \`__tmp_table\``, options))[0][0]['COUNT(*)'];
this.db.clsNameSpace = namespace;
await this.loadModels();
},
loadModules() {
fs.readdir('./modules/', (err, files) => {
if (err) {
this.log(err);
return;
}
files.filter((file) => file.endsWith('.js'))
.forEach((file) => this.modules.push(require(`./modules/${file}`)));
});
},
async loadModels() {
await new Promise(resolve => {
fs.readdir('./models/', (err, files) => {
if (err) {
this.log(err);
return;
}
files.filter((file) => file.endsWith('.js'))
.forEach((file) => require(`./models/${file}`));
resolve();
});
});
await this.db.sync();
},
lib(name) {
return require(`./libs/${name}`);
},
model(name) {
let result = require(`./models/${name}`);
if (!result || Object.keys(result).length === 0) {
console.log('loop reference detected!');
}
return result;
},
loadHooks() {
let Session = require('express-session');
let FileStore = require('session-file-store')(Session);
let sessionConfig = {
secret: this.config.session_secret,
cookie: { httpOnly: false },
rolling: true,
saveUninitialized: true,
resave: true,
store: new FileStore
};
if (syzoj.production) {
app.set('trust proxy', 1);
sessionConfig.cookie.secure = false;
}
app.use(Session(sessionConfig));
app.use((req, res, next) => {
res.locals.useLocalLibs = 'true' !== req.headers['x-remote-access'] || syzoj.config.no_cdn; // !!parseInt(req.headers['syzoj-no-cdn']);
if (req.cookies.login) {
res.clearCookie('login');
}
const User = syzoj.model('user');
if (req.session.user_id) {
User.fromID(req.session.user_id).then((user) => {
res.locals.user = user;
next();
}).catch((err) => {
this.log(err);
res.locals.user = null;
req.session.user_id = null;
next();
});
} else {
if (req.cookies.login_jwt) {
(async function() {
try {
let obj = jwt.verify(req.cookies.login_jwt, syzoj.config.session_secret + syzoj.config.email_jwt_secret, { subject: 'login' });
let user = await User.fromName(String(obj.user));
if (User.passwordEncrypt(String(obj.pwd), true) !== user.password) throw null;
res.locals.user = user;
req.session.user_id = user.id;
} catch (e) {
res.clearCookie('login_jwt');
res.locals.user = null;
req.session.user_id = null;
}
next();
})();
} else {
res.locals.user = null;
req.session.user_id = null;
next();
}
}
});
// Active item on navigator bar
app.use((req, res, next) => {
res.locals.active = req.path.split('/')[1];
next();
});
app.use((req, res, next) => {
res.locals.req = req;
res.locals.res = res;
next();
});
app.use((req, res, next) => {
if(syzoj.config.forbidden_remote_access && !res.locals.useLocalLibs && (!res.locals.user || !res.locals.user.is_admin)) {
if(!req.path.startsWith("/api/login")&&!req.path.startsWith("/login")){
res.render('error', {
err: "外部访问被临时禁止!"
});
} else next();
} else next();
});
}
};
syzoj.run();