forked from 0perationPrivacy/VoIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
165 lines (148 loc) · 4.87 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
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())
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
app.use(bodyParser.json({limit: '500mb',parameterLimit: 10000000}));
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true, limit: '500mb',parameterLimit: 10000000 }));
app.use('/uploads', express.static('uploads'));
app.use('/src', express.static('src'));
app.use('/frontend', express.static('frontend'));
// app.use('/frontend', express.static('frontend'));
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.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'));
})
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()});
});
server.listen(process.env.PORT)