This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.js
93 lines (75 loc) · 2.93 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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var http = require('http');
var https = require('https');
var pem = require('pem');
var config = require('config');
var fs = require('fs');
var actions = require('./lib/actions');
var logger = require('./lib/logger');
var keychain = require('./lib/keychain');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
logger.log("---- Mjolnir authentication server (auth) ----");
if (process.env.MJOLNIR_VERBOSE) {
logger.log("Settings:");
logger.log(JSON.stringify(config, null, 4));
logger.log("Available hashing algorithms: " + require('crypto').getHashes());
}
// Images
app.use('/images', express.static('images'));
app.use('/static', express.static('static'));
// Authserver
app.all('/', actions.root);
app.all('/authenticate', actions.authenticate);
app.all('/refresh', actions.refresh);
app.all('/validate', actions.validate);
app.all('/signout', actions.signOut);
app.all('/invalidate', actions.invalidate);
// Sessionserver
app.all('/session/minecraft/join', actions.sessionJoin);
app.all('/session/minecraft/hasJoined', actions.sessionHasJoined);
app.all('/session/minecraft/profile/:uuid', actions.sessionUUIDToProfile);
// APIserver
app.all('/users/profiles/minecraft/:name', actions.apiNameToUUID);
app.all('/user/profiles/:uuid/names', actions.apiUUIDToName);
app.all('/profiles/minecraft', actions.apiPlayernamesToUUID);
// Other
app.all('*', function (request, response) {
console.log('Unknown request: ');
console.log(request.headers.host, request.originalUrl);
console.log(request.body);
response.status(404).json({
error: "Not Found",
errorMessage: "The server has not found anything matching the request URI"
});
});
startServer('sessionserver');
startServer('authserver');
startServer('apiserver');
function startServer(server) {
var privateKey = fs.readFileSync('cert/' + server + '/server.key', 'utf8');
var certOptions = config.util.extendDeep({}, config.get(server + '.certification'), {"clientKey": privateKey});
pem.createCertificate(certOptions, function (err, keys) {
if (err) {
console.log(err);
return;
}
keychain.store(server + '.key', keys.serviceKey);
keychain.store(server + '.crt', keys.certificate);
fs.writeFile('./cert/' + server + '/certificate.key', keys.serviceKey, function (err) {
if (err) {
console.log(err);
}
});
fs.writeFile('./cert/' + server + '/certificate.crt', keys.certificate, function (err) {
if (err) {
console.log(err);
}
});
http.createServer(app).listen(config.get(server + '.httpPort'));
https.createServer({key: keys.serviceKey, cert: keys.certificate}, app).listen(config.get(server + '.httpsPort'));
logger.log(server + ' is listening...');
});
}