smartproxy/ts/smartproxy.classes.proxyworker.ts

278 lines
11 KiB
TypeScript
Raw Normal View History

2019-08-22 13:09:48 +00:00
import { expose } from '@pushrocks/smartspawn';
2019-08-21 13:09:32 +00:00
import * as plugins from './smartproxy.plugins';
2019-08-21 21:54:55 +00:00
import { SmartproxyRouter } from './smartproxy.classes.router';
2019-08-21 13:09:32 +00:00
export class ProxyWorker {
2019-09-23 17:20:53 +00:00
public proxyConfigs: plugins.tsclass.network.IReverseProxyConfig[] = [];
2019-08-22 13:09:48 +00:00
public httpsServer: plugins.https.Server; // | plugins.http.Server;
public port = 8001;
2019-08-21 21:54:55 +00:00
public router = new SmartproxyRouter();
2021-02-02 15:55:25 +00:00
public socketMap = new plugins.lik.ObjectMap<plugins.net.Socket>();
2019-08-21 13:09:32 +00:00
2019-08-21 21:54:55 +00:00
/**
* starts the proxyInstance
*/
public async start() {
2019-11-03 02:04:23 +00:00
this.httpsServer = plugins.https.createServer(
2019-11-03 02:05:49 +00:00
// ================
// Spotted this keypair in the code?
// Don't get exited:
// It is an invalid default keypair.
2019-11-03 02:06:56 +00:00
// For proper requests custom domain level keypairs are used that are provided in the reverse config
2019-11-03 02:05:49 +00:00
// ================
2019-11-03 02:04:23 +00:00
{
key: `-----BEGIN PRIVATE KEY-----
2019-09-20 16:40:55 +00:00
MIIJRQIBADANBgkqhkiG9w0BAQEFAASCCS8wggkrAgEAAoICAQDi2F/0kQr96mhe
3yEWvy2mRHOZoSSBtIqg6Bre4ZcMu901/cHNIjFnynNGFl9Se61yZbW2F3PfCt7+
kQlHug1Cx+LFssvz+hLlB5cqJQZfRKx92DhbROygtxG9r7UBmx/fwx+JQ+HOHX9R
b+szLBZqxrNDBFl2SRdviconYgVnHbaqcAPj/lK6D6x94qgUEX+vMjbIruuiCe3u
RbYse/quzAednVnY/+BuGVn8SEb2EVVFnBEsOxxYpy5ZzGR48O3YnWkM2oPpJhrp
mMYLcARMnDmIQDVstD1i+MM2lVhx/pm9xKKUgWNJC7lyz2x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Fwj+fCArYBck2NZannDPKA6X8V
TzJTbTCteOUUJTrcfZ0gGhGkF4nYLmX5OI+TPqrDJf0fZ+mzAEHzDDVXcBYpYRDr
r8d9QwrK+WaqVi2ofbMfMByVF72jgeJNa4nxwT9bVbu/Q1T2Lt+YPb4pQ7yCoUgS
JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
-----END CERTIFICATE-----
2021-02-02 15:55:25 +00:00
`,
2019-11-03 02:04:23 +00:00
},
async (req, res) => {
console.log('got request');
const destinationConfig = this.router.routeReq(req);
// endRequest function
const endRequest = (
statusArg: number = 404,
messageArg: string = 'This route is not available on this server.',
headers: plugins.http.OutgoingHttpHeaders = {}
) => {
res.writeHead(statusArg, messageArg);
res.end(messageArg);
};
// authentication
if (destinationConfig.authentication) {
const authInfo = destinationConfig.authentication;
switch (authInfo.type) {
case 'Basic':
const authHeader = req.headers.authorization;
if (authHeader) {
if (!authHeader.includes('Basic ')) {
return endRequest(401, 'Authentication required', {
2021-02-02 15:55:25 +00:00
'WWW-Authenticate': 'Basic realm="Access to the staging site", charset="UTF-8"',
2019-11-03 02:04:23 +00:00
});
}
const authStringBase64 = req.headers.authorization.replace('Basic ', '');
const authString: string = plugins.smartstring.base64.decode(authStringBase64);
const userPassArray = authString.split(':');
const user = userPassArray[0];
const pass = userPassArray[1];
if (user === authInfo.user && pass === authInfo.pass) {
console.log('request successfully authenticated');
} else {
return endRequest(403, 'Forbidden: Wrong credentials');
}
}
break;
default:
return endRequest(
403,
'Forbidden: unsupported authentication method configured. Please report to the admin.'
);
}
}
let destinationUrl: string;
if (destinationConfig) {
destinationUrl = `http://${destinationConfig.destinationIp}:${destinationConfig.destinationPort}${req.url}`;
} else {
return endRequest();
2019-09-28 23:06:07 +00:00
}
2019-11-03 02:04:23 +00:00
console.log(destinationUrl);
const response = await plugins.smartrequest.request(
destinationUrl,
{
method: req.method,
2021-02-02 15:55:25 +00:00
headers: req.headers,
2019-11-03 02:04:23 +00:00
},
true, // lets make this streaming
2021-02-02 15:55:25 +00:00
(request) => {
req.on('data', (data) => {
2019-11-03 02:04:23 +00:00
request.write(data);
});
2021-02-02 15:55:25 +00:00
req.on('end', (data) => {
2019-11-03 02:04:23 +00:00
request.end();
});
}
);
res.statusCode = response.statusCode;
console.log(response.statusCode);
for (const header of Object.keys(response.headers)) {
res.setHeader(header, response.headers[header]);
}
2021-02-02 15:55:25 +00:00
response.on('data', (data) => {
2019-11-03 02:04:23 +00:00
res.write(data);
});
response.on('end', () => {
res.end();
});
2019-08-21 21:54:55 +00:00
}
2019-11-03 02:04:23 +00:00
);
2019-08-21 21:54:55 +00:00
// Enable websockets
const wss = new plugins.ws.Server({ server: this.httpsServer });
2019-09-28 23:06:07 +00:00
wss.on('connection', (ws: plugins.wsDefault) => {
2019-09-20 16:40:55 +00:00
console.log('got connection for wsc');
2019-08-21 21:54:55 +00:00
const wscConnected = plugins.smartpromise.defer();
2019-08-22 22:48:14 +00:00
2019-09-28 23:06:07 +00:00
const wsc = new plugins.wsDefault(this.router.routeWs(ws), {
headers: {
2021-02-02 15:55:25 +00:00
Host: ws.url,
},
});
2019-08-21 21:54:55 +00:00
wsc.on('open', () => {
wscConnected.resolve();
});
2021-02-02 15:55:25 +00:00
ws.on('message', async (message) => {
2019-08-21 21:54:55 +00:00
await wscConnected.promise;
wsc.emit('message', message);
});
2021-02-02 15:55:25 +00:00
wsc.on('message', (message) => {
2019-08-21 21:54:55 +00:00
ws.emit('message', message);
});
// handle closing
2021-02-02 15:55:25 +00:00
ws.on('close', (message) => {
2019-08-21 21:54:55 +00:00
wsc.close();
});
2021-02-02 15:55:25 +00:00
wsc.on('close', (message) => {
2019-08-21 21:54:55 +00:00
ws.close();
});
});
2019-09-28 23:06:07 +00:00
this.httpsServer.keepAliveTimeout = 61000;
this.httpsServer.headersTimeout = 65000;
2019-09-29 15:18:40 +00:00
2021-02-02 15:55:25 +00:00
this.httpsServer.on('connection', (connection) => {
2019-09-29 15:18:40 +00:00
this.socketMap.add(connection);
connection.on('close', () => {
this.socketMap.remove(connection);
});
});
2019-08-22 13:09:48 +00:00
this.httpsServer.listen(this.port);
2020-02-07 13:04:11 +00:00
console.log(`ProxyWorker -> OK: now listening for new connections on port ${this.port}`);
2019-08-21 21:54:55 +00:00
}
2020-02-07 16:06:11 +00:00
public async updateProxyConfigs(proxyConfigsArg: plugins.tsclass.network.IReverseProxyConfig[]) {
2019-09-23 17:20:53 +00:00
this.proxyConfigs = proxyConfigsArg;
this.router.setNewProxyConfigs(proxyConfigsArg);
for (const hostCandidate of this.proxyConfigs) {
2019-09-20 16:40:55 +00:00
// console.log(hostCandidate);
2019-08-22 10:49:29 +00:00
this.httpsServer.addContext(hostCandidate.hostName, {
cert: hostCandidate.publicKey,
2021-02-02 15:55:25 +00:00
key: hostCandidate.privateKey,
2019-08-22 10:49:29 +00:00
});
}
2019-09-20 16:40:55 +00:00
/* this.httpsServer.close();
this.httpsServer.listen(this.port); */
2019-08-21 21:54:55 +00:00
}
public async stop() {
const done = plugins.smartpromise.defer();
this.httpsServer.close(() => {
done.resolve();
});
2021-02-02 15:55:25 +00:00
await this.socketMap.forEach(async (socket) => {
2019-09-29 15:18:40 +00:00
socket.destroy();
});
2019-08-22 13:09:48 +00:00
await done.promise;
2019-08-21 21:54:55 +00:00
}
2019-08-21 13:09:32 +00:00
}
2019-08-22 13:09:48 +00:00
const proxyWorkerInstance = new ProxyWorker();
// the following is interesting for the master process only
const proxyWorkerCalls = {
stop: async () => {
await proxyWorkerInstance.stop();
},
start: async () => {
await proxyWorkerInstance.start();
},
2019-09-20 16:40:55 +00:00
updateReverseConfigs: async (configArray: plugins.tsclass.network.IReverseProxyConfig[]) => {
2019-09-23 17:20:53 +00:00
await proxyWorkerInstance.updateProxyConfigs(configArray);
2021-02-02 15:55:25 +00:00
},
2019-08-22 13:09:48 +00:00
};
export type TProxyWorkerCalls = typeof proxyWorkerCalls;
expose(proxyWorkerCalls);
2019-08-22 14:14:50 +00:00
console.log('ProxyWorker initialized');