-
Notifications
You must be signed in to change notification settings - Fork 746
/
server.js
102 lines (88 loc) · 2.39 KB
/
server.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
/* eslint-env node */
if (process.env.NODE_ENV === 'production') {
// eslint-disable-next-line global-require
require('newrelic');
}
// Room server
const http = require('http');
const path = require('path');
const express = require('express');
const logger = require('morgan');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const cookieSession = require('cookie-session');
const compression = require('compression');
const { v4: uuidv4 } = require('uuid');
const crypto = require('crypto');
const FirebaseTokenGenerator = require('firebase-token-generator');
const firebaseTokenGenerator = new FirebaseTokenGenerator(
process.env.FIREBASE_SECRET,
);
const app = express();
const secret = process.env.SECRET;
const base = ['dist'];
app.enable('trust proxy');
app.use(logger('combined'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(
cookieSession({
cookie: {
// secure: true,
httpOnly: true,
maxAge: 30 * 24 * 60 * 60 * 1000, // 30 days
},
secret,
proxy: true,
}),
);
app.use(compression());
//
// Web server
//
base.forEach((dir) => {
const subdirs = ['assets', '.well-known'];
subdirs.forEach((subdir) => {
app.use(
`/${subdir}`,
express.static(`${dir}/${subdir}`, {
maxAge: 31104000000, // ~1 year
}),
);
});
});
//
// API server
//
app.get('/', (req, res) => {
const root = path.join(__dirname, base[0]);
console.log({ root });
res.sendFile(`${root}/index.html`);
});
app.get('/rooms/:id', (req, res) => {
const root = path.join(__dirname, base[0]);
res.sendFile(`${root}/index.html`);
});
app.get('/room', (req, res) => {
const ip = req.headers['cf-connecting-ip'] || req.ip;
const name = crypto.createHmac('md5', secret).update(ip).digest('hex');
res.json({ name });
});
app.get('/auth', (req, res) => {
const ip = req.headers['cf-connecting-ip'] || req.ip;
const uid = uuidv4();
const token = firebaseTokenGenerator.createToken(
{ uid, id: uid }, // will be available in Firebase security rules as 'auth'
{ expires: 32503680000 }, // 01.01.3000 00:00
);
res.json({ id: uid, token, public_ip: ip });
});
http
.createServer(app)
.listen(process.env.PORT)
.on('listening', () => {
console.log(
`Started ShareDrop web server at http://localhost:${process.env.PORT}...`,
);
});