-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
188 lines (167 loc) · 6.9 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
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
/**
*
* Proxive
*
* Copyright 2016 Landstinget Dalarna Bibliotek och Informationscentral
* Copyright 2016 Emil Hemdal <https://github.com/emilhem>
*
* Proxive is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Proxive is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Proxive. If not, see <http://www.gnu.org/licenses/>.
*
*/
'use strict';
var http = require('http');
var Cookies = require('cookies');
var Unblocker = require('unblocker');
var mysql = require('mysql');
var fs = require('fs');
var qs = require('querystring');
var url = require('url');
var path = require('path');
function setupServer(config) {
var pool = mysql.createPool({
connectionLimit: 10,
host: config.mysql.host,
port: (config.mysql.port || 3306),
user: config.mysql.user,
password: config.mysql.pass,
database: config.mysql.database,
charset: 'utf8mb4'
});
pool.getConnection(function(err, connection) {
if(err) {
console.error(err);
throw 'MySQL not set up properly!';
}
connection.query('SHOW TABLES;', function(err) {
if(err) {
console.error(err);
throw 'MySQL not set up properly!';
}
console.log('MySQL activated!');
connection.release();
});
});
config.getRealUrl = Unblocker.getRealUrlPrefixed({domain: config.proxyServer.domain});
config.mysql = pool;
config.redis = require("redis");
config.redisClient = config.redis.createClient(config.redisConfig);
config.organizations = require('./lib/organizations.js')(config);
config.users = require('./lib/users.js')(config);
config.pugGen = require('./lib/pugGenerator.js')(config);
config.emailer = require('./lib/emailer.js')(config);
var get = require('./lib/get.js')(config);
var post = require('./lib/post.js')(config);
var adminDelete = require('./lib/delete.js')(config);
var unblockerConfig = {
prefix: false,
domainPrefixing: true,
domain: config.proxyServer.domain,
specialRedirects: config.specialRedirects
};
var unblockerConfigSpecial = {
prefix: false,
domainPrefixing: true,
domain: config.proxyServer.domain,
specialRedirects: config.specialRedirects,
mysql: pool,
redisClient: config.redisClient,
pugGen: config.pugGen,
webmasterEmail: config.webmasterEmail
};
unblockerConfig.requestMiddleware = [
require('./lib/whitelist.js')(unblockerConfigSpecial),
require('./lib/special-redirects.js')(unblockerConfig),
require('./lib/otool.js')(unblockerConfigSpecial)
];
unblockerConfig.responseMiddleware = [
require('./lib/logging.js')(unblockerConfigSpecial)
];
var unblocker = new Unblocker(unblockerConfig);
// Proxy server
http.createServer(function(req, res) {
res.cookies = req.cookies = new Cookies( req, res );
if(req.cookies.get('login')) {
var cookieInfo = config.users.validateHmac(req.cookies.get('login'));
if(cookieInfo) {
config.users.validateSession(cookieInfo.user, cookieInfo.data, function(err) {
if(err) {
console.error(err);
if(err === 'invalid-user' || err === 'no-user') {
res.writeHead(302, {
location: (config.https ? 'https://' : 'http://') + config.loginServer.domain + '/?redirect=' + encodeURIComponent(config.getRealUrl(req.headers.host, req.url))
});
return res.end();
} else {
res.writeHead(500, {'content-type': 'text/html; charset=utf-8'});
return res.end(config.pugGen.run['500.pug']({title: 'Ett fel inträffade vid valideringen av inloggningsuppgifterna!',
message: 'Prova att ladda om sidan om en liten stund.'}));
}
}
req.userId = cookieInfo.user;
config.organizations.getConnectOrgIdAndIp(cookieInfo.user, function(err, orgId, orgIp) {
if(err) {
console.error(err);
res.writeHead(500, {'content-type': 'text/html; charset=utf-8'});
return res.end(config.pugGen.run['500.pug']({title: 'Ett fel inträffade vid valideringen av inloggningsuppgifterna!',
message: 'Prova att ladda om sidan om en liten stund.'}));
}
req.organizationId = orgId;
req.ipToConnectWith = orgIp;
unblocker(req, res, function(err) {
// this callback will be fired for any request that unblocker does not serve
var headers = {'content-type': 'text/html; charset=utf-8'};
if (err) {
console.error(err.stack || err.message);
console.error(err);
res.writeHead(500, headers);
return res.end(config.pugGen.run['500.pug']({title: 'Ett fel hände vid hämtandet av sidan!',
message: 'Ett fel inträffade! Är du säker på att adressen ska fungera? Kontakta i så fall [email protected]'}));
}
res.writeHead(404, headers);
return res.end(config.pugGen.run['404.pug']({title: 'Ett fel hände vid hämtandet av sidan!',
message: 'Ett fel inträffade! Är du säker på att adressen ska fungera? Kontakta i så fall [email protected]'}));
});
});
});
return;
}
}
res.writeHead(302, {
location: (config.https ? 'https://' : 'http://') + config.loginServer.domain + '/?redirect=' + encodeURIComponent(config.getRealUrl(req.headers.host, req.url))
});
res.end();
}).listen(config.proxyServer.port, '127.0.0.1');
// Loginserver
http.createServer(function(req, res) {
res.cookies = req.cookies = new Cookies( req, res );
if(req.method === 'POST') {
post(req, res);
} else if(req.method === 'GET') {
get(req, res);
} else if(req.method === 'DELETE') {
adminDelete(req, res);
} else {
res.writeHead(405, {
'content-type': 'text/plain; charset=utf-8'
});
res.end('Method not supported!');
}
}).listen(config.loginServer.port, '127.0.0.1');
}
fs.readFile(path.join(__dirname, 'config.json'), function(err, data) {
if(err) {
throw 'config.json couldn\'t be loaded!';
}
setupServer(JSON.parse(data));
});