-
-
Notifications
You must be signed in to change notification settings - Fork 417
Expand file tree
/
Copy pathapp.js
More file actions
210 lines (189 loc) · 6.79 KB
/
Copy pathapp.js
File metadata and controls
210 lines (189 loc) · 6.79 KB
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
const config = require('./config.js');
const express = require('express')
const app = express()
var bodyParser = require('body-parser')
const cors = require("cors");
require('dotenv').config()
const path = require('path');
var session = require('cookie-session')
var compression = require('compression')
app.use(compression())
// Refuse to boot on the placeholder secrets checked into .env — those values
// are public (visible to anyone who has cloned this repo), so running with
// them means forgeable sessions and/or unreadable stored credentials.
;['COOKIE_KEY', 'COOKIE_KEY2', 'CREDENTIALS_ENCRYPTION_KEY'].forEach((name) => {
const value = (process.env[name] || '').trim();
if (!value || value.startsWith('CHANGE_ME')) {
console.error(
`Refusing to start: ${name} is unset or still the placeholder from .env. ` +
`Generate one (e.g. \`openssl rand -hex 32\`) and set it before running the server.`
);
process.exit(1);
}
});
// TEMP diagnostic: log every incoming request before any routing/auth, so
// nothing can go unlogged while tracking down a request that isn't reaching
// any of the normal route handlers.
app.use((req, res, next) => {
console.log(`>>> INCOMING ${req.method} ${req.originalUrl} from ${req.ip}`);
next();
});
var expiryDate = new Date(Date.now() + 60 * 60 * (1000 * 12 * 30)) // 30 day
app.use(session({
name: 'session',
keys: [process.env.COOKIE_KEY, process.env.COOKIE_KEY2],
cookie: {
secure: true,
httpOnly: true,
expires: expiryDate
}
}))
let setCache = function (req, res, next) {
const period = 60 * 60 * 24
if (req.method == 'GET') {
res.set('Cache-control', `public, max-age=${period}`)
} else {
res.set('Cache-control', `no-store`)
}
next()
}
app.use(setCache)
var RateLimit = require('express-rate-limit');
const helmet = require("helmet");
app.use(
helmet.contentSecurityPolicy({
useDefaults: true,
reportOnly: false,
directives: {
"default-src": ["'self'", "sdk.twilio.com","wss:","ws:","eventgw.twilio.com"
],
"object-src": ["'self'"],
"script-src": ["'self'","'unsafe-eval'", "'unsafe-inline'"]
},
})
);
//sdk.twilio.com
app.use(helmet.dnsPrefetchControl());
app.use(helmet.expectCt());
app.use(helmet.frameguard());
app.use(helmet.hidePoweredBy());
app.use(helmet.hsts());
app.use(helmet.ieNoOpen());
app.use(helmet.noSniff());
app.use(helmet.permittedCrossDomainPolicies());
app.use(helmet.referrerPolicy());
app.use(helmet.xssFilter());
app.disable('x-powered-by');
app.set('trust proxy', 1)
const server = require('http').createServer(app);
global.io = require('socket.io')(server,{ cors: { origin: '*' } });
var mongoose = require('./config/db.config');
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('database connected successfully!');
});
//app.use(cors());
app.use(cors({ origin: ['http://localhost:8080'], }))
var limiter = new RateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 100,
message: "Slow down your requests!",
headers: false
});
// apply rate limiter to all requests
app.use(limiter);
io.on('connection', socket => {
console.log('a user connected');
socket.on('join_channel', (channel) => {
console.log(`${channel} user joined channel`);
socket.join(channel);
});
socket.on('join_profile_channel', (channel) => {
console.log(`${channel} user joined channel`);
socket.join(channel);
});
});
app.use('/frontend/dist/index.html', express.static('frontend/dist/index.html'));
app.use('/version.md', express.static('version.md'));
// app.enable('trust proxy')
if( process.env.HTTPS.trim() === 'true'){
app.use((req, res, next) => {
if (req.header('x-forwarded-proto') !== 'https'){
if(req.url == '/get-base-url'){
next()
// res.status(200).json({url: process.env.BASE_URL.trim()});
}else{
res.sendFile(path.join(__dirname, './error/index.html'));
}
} else {
next()
}
})
/* app.use((req, res, next) => {
console.log(req.url)
console.log(req.secure)
if (req.secure || req.url === '/error') {
next()
} else if(req.url == '/get-base-url'){
res.status(200).json({url: process.env.BASE_URL.trim()});
}else{
// res.sendFile(path.join(__dirname, './error/index.html'));
}
}) */
}
// parse requests of content-type - application/json
// The `verify` callback stashes the raw bytes on the request — Telnyx webhook
// signatures are computed over the exact bytes sent, so the re-serialized
// req.body wouldn't reliably match.
app.use(bodyParser.json({
limit: '500mb',
parameterLimit: 10000000,
verify: (req, res, buf) => { req.rawBody = buf; },
}));
// parse requests of content-type - application/x-www-form-urlencoded
// Same rawBody capture as above — Telnyx's TeXML voice webhooks (call.telnyx,
// call.statusTelnyx) arrive form-encoded, matching Twilio's format for
// drop-in compatibility, but Telnyx still signs them Ed25519-over-raw-bytes
// the same as its JSON Call Control webhooks.
app.use(bodyParser.urlencoded({
extended: true,
limit: '500mb',
parameterLimit: 10000000,
verify: (req, res, buf) => { req.rawBody = buf; },
}));
app.use('/uploads', express.static('uploads'));
// Removed: `/src` (empty, unused) and a blanket `/frontend` mount that served
// the entire uncompiled source tree (frontend/config, frontend/src, frontend/test)
// alongside the build output. Only the built `frontend/dist` needs to be public,
// and it already is via the mounts below.
app.use('/frontend/dist/static/', express.static('frontend/dist/static'));
app.get(`/error`, function (req, res) {
res.sendFile(path.join(__dirname, './error/index.html'));
})
app.use(express.static(path.join(__dirname, './frontend/dist')));
require("./app/routes/auth.route")(app);
require("./app/routes/setting.route")(app);
require("./app/routes/profile.route")(app);
require("./app/routes/media.route")(app);
require("./app/routes/contact.route")(app);
require("./app/routes/email.route")(app);
require("./app/routes/call.route")(app);
require("./app/routes/hardwarekey.route")(app);
app.get('/api/users/', function (req, res) {
res.status(200).json({message: 'success'});
})
app.get('/get-base-url', function(req, res) {
res.status(200).json({url: process.env.BASE_URL.trim()});
});
// These SPA catch-alls must be registered last: Express matches routes in
// registration order, and `/:id` / `/:id/:name` would otherwise swallow any
// two-segment API path (confirmed live: GET /api/users/ returned the SPA's
// index.html, not JSON, before this reordering).
app.get(`/:id`, function (req, res) {
res.sendFile(path.join(__dirname, './frontend/dist/index.html'));
})
app.get(`/:id/:name`, function (req, res) {
res.sendFile(path.join(__dirname, './frontend/dist/index.html'));
})
server.listen(process.env.PORT)