2022-07-28 22:49:46 +00:00
|
|
|
import * as plugins from './smartproxy.plugins.js';
|
|
|
|
import { ProxyRouter } from './smartproxy.classes.router.js';
|
2019-08-21 13:09:32 +00:00
|
|
|
|
2022-07-28 22:49:46 +00:00
|
|
|
export interface INetworkProxyOptions {
|
|
|
|
port: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class NetworkProxy {
|
|
|
|
// INSTANCE
|
|
|
|
public options: INetworkProxyOptions;
|
2019-09-23 17:20:53 +00:00
|
|
|
public proxyConfigs: plugins.tsclass.network.IReverseProxyConfig[] = [];
|
2022-07-28 22:49:46 +00:00
|
|
|
public httpsServer: plugins.https.Server;
|
|
|
|
public router = new ProxyRouter();
|
2021-02-02 15:55:25 +00:00
|
|
|
public socketMap = new plugins.lik.ObjectMap<plugins.net.Socket>();
|
2021-02-03 19:42:27 +00:00
|
|
|
public defaultHeaders: { [key: string]: string } = {};
|
|
|
|
|
|
|
|
public alreadyAddedReverseConfigs: {
|
|
|
|
[hostName: string]: plugins.tsclass.network.IReverseProxyConfig;
|
|
|
|
} = {};
|
2019-08-21 13:09:32 +00:00
|
|
|
|
2022-07-28 22:49:46 +00:00
|
|
|
constructor(optionsArg: INetworkProxyOptions) {
|
|
|
|
this.options = optionsArg;
|
|
|
|
}
|
|
|
|
|
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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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) => {
|
2022-07-28 23:23:17 +00:00
|
|
|
/**
|
|
|
|
* endRequest function
|
|
|
|
* can be used to prematurely end a request
|
|
|
|
*/
|
2019-11-03 02:04:23 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2022-07-30 06:20:20 +00:00
|
|
|
console.log(`got request: ${req.headers.host}${plugins.url.parse(req.url).path}`);
|
2022-07-29 02:25:01 +00:00
|
|
|
const destinationConfig = this.router.routeReq(req);
|
|
|
|
|
|
|
|
if (!destinationConfig) {
|
|
|
|
console.log(`${req.headers.host} can't be routed properly. Terminating request.`);
|
|
|
|
endRequest();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-03 02:04:23 +00:00
|
|
|
// 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,
|
2021-02-03 19:42:27 +00:00
|
|
|
keepAlive: true,
|
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();
|
|
|
|
});
|
2022-07-30 16:29:20 +00:00
|
|
|
req.on('error', () => {
|
|
|
|
request.end();
|
|
|
|
});
|
|
|
|
req.on('close', () => {
|
|
|
|
request.end();
|
|
|
|
});
|
|
|
|
req.on('timeout', () => {
|
2022-08-01 10:31:29 +00:00
|
|
|
req.destroy();
|
2022-07-30 16:29:20 +00:00
|
|
|
request.end();
|
|
|
|
})
|
2019-11-03 02:04:23 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
res.statusCode = response.statusCode;
|
|
|
|
console.log(response.statusCode);
|
2021-02-03 00:23:28 +00:00
|
|
|
for (const defaultHeader of Object.keys(this.defaultHeaders)) {
|
|
|
|
res.setHeader(defaultHeader, this.defaultHeaders[defaultHeader]);
|
2021-02-03 19:42:27 +00:00
|
|
|
}
|
2019-11-03 02:04:23 +00:00
|
|
|
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();
|
|
|
|
});
|
2022-07-30 15:32:58 +00:00
|
|
|
response.on('error', () => {
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
response.on('close', () => {
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
response.on('timeout', () => {
|
|
|
|
res.end();
|
2022-08-01 10:31:29 +00:00
|
|
|
res.destroy()
|
2022-07-30 15:32:58 +00:00
|
|
|
});
|
2022-07-30 20:20:31 +00:00
|
|
|
|
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
|
2022-07-28 22:49:46 +00:00
|
|
|
const wss = new plugins.ws.WebSocketServer({ 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');
|
2022-07-30 17:28:23 +00:00
|
|
|
let wscConnectedDeferred: plugins.smartpromise.Deferred<plugins.wsDefault>;
|
2019-08-21 21:54:55 +00:00
|
|
|
|
2021-02-02 15:55:25 +00:00
|
|
|
ws.on('message', async (message) => {
|
2022-07-30 17:28:23 +00:00
|
|
|
if (!wscConnectedDeferred) {
|
|
|
|
wscConnectedDeferred = plugins.smartpromise.defer();
|
|
|
|
const wsc = new plugins.wsDefault(this.router.routeWs(ws), {
|
|
|
|
headers: {
|
|
|
|
Host: ws.url,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
wsc.on('open', () => {
|
|
|
|
wscConnectedDeferred.resolve(wsc);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const wsc = await wscConnectedDeferred.promise;
|
2019-08-21 21:54:55 +00:00
|
|
|
wsc.emit('message', message);
|
2022-07-30 17:28:23 +00:00
|
|
|
wsc.on('message', (message) => {
|
|
|
|
ws.emit('message', message);
|
|
|
|
});
|
|
|
|
|
|
|
|
// handle closing
|
|
|
|
const cleanUp = () => {
|
|
|
|
ws.close();
|
|
|
|
wsc.close();
|
|
|
|
};
|
|
|
|
|
|
|
|
ws.on('close', (message) => {
|
|
|
|
cleanUp();
|
|
|
|
});
|
2021-02-03 19:42:27 +00:00
|
|
|
|
2022-07-30 17:28:23 +00:00
|
|
|
wsc.on('close', (message) => {
|
|
|
|
cleanUp();
|
|
|
|
});
|
2019-08-21 21:54:55 +00:00
|
|
|
});
|
|
|
|
});
|
2019-09-28 23:06:07 +00:00
|
|
|
this.httpsServer.keepAliveTimeout = 61000;
|
2022-07-28 23:52:34 +00:00
|
|
|
this.httpsServer.headersTimeout = 65000;
|
2019-09-29 15:18:40 +00:00
|
|
|
|
2021-02-03 11:02:26 +00:00
|
|
|
this.httpsServer.on('connection', (connection: plugins.net.Socket) => {
|
2019-09-29 15:18:40 +00:00
|
|
|
this.socketMap.add(connection);
|
2022-07-29 13:26:02 +00:00
|
|
|
console.log(`added connection. now ${this.socketMap.getArray().length} sockets connected.`);
|
2021-02-03 19:42:27 +00:00
|
|
|
const cleanupConnection = () => {
|
2022-07-28 22:49:46 +00:00
|
|
|
if (this.socketMap.checkForObject(connection)) {
|
|
|
|
this.socketMap.remove(connection);
|
2022-07-29 13:26:02 +00:00
|
|
|
console.log(`removed connection. ${this.socketMap.getArray().length} sockets remaining.`);
|
2022-07-31 06:30:13 +00:00
|
|
|
connection.destroy();
|
2022-07-28 22:49:46 +00:00
|
|
|
}
|
2021-02-03 19:42:27 +00:00
|
|
|
};
|
2022-07-29 13:26:02 +00:00
|
|
|
connection.on('close', () => {
|
2021-02-03 19:42:27 +00:00
|
|
|
cleanupConnection();
|
2021-02-03 11:02:26 +00:00
|
|
|
});
|
2022-07-29 13:26:02 +00:00
|
|
|
connection.on('error', () => {
|
2021-02-03 19:42:27 +00:00
|
|
|
cleanupConnection();
|
2021-02-03 11:02:26 +00:00
|
|
|
});
|
2022-07-29 13:26:02 +00:00
|
|
|
connection.on('end', () => {
|
2021-02-03 19:42:27 +00:00
|
|
|
cleanupConnection();
|
2021-02-03 11:02:26 +00:00
|
|
|
});
|
2022-07-30 20:29:31 +00:00
|
|
|
connection.on('timeout', () => {
|
|
|
|
cleanupConnection();
|
|
|
|
})
|
2019-09-29 15:18:40 +00:00
|
|
|
});
|
|
|
|
|
2022-07-28 22:49:46 +00:00
|
|
|
this.httpsServer.listen(this.options.port);
|
2022-07-28 23:52:34 +00:00
|
|
|
console.log(
|
|
|
|
`ProxyWorker -> OK: now listening for new connections on port ${this.options.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[]) {
|
2022-07-30 06:20:20 +00:00
|
|
|
console.log(`got new proxy configs`);
|
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);
|
2021-02-03 19:42:27 +00:00
|
|
|
|
|
|
|
const existingHostNameConfig = this.alreadyAddedReverseConfigs[hostCandidate.hostName];
|
|
|
|
|
|
|
|
if (!existingHostNameConfig) {
|
|
|
|
this.alreadyAddedReverseConfigs[hostCandidate.hostName] = hostCandidate;
|
|
|
|
} else {
|
|
|
|
if (
|
|
|
|
existingHostNameConfig.publicKey === hostCandidate.publicKey &&
|
|
|
|
existingHostNameConfig.privateKey === hostCandidate.privateKey
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
this.alreadyAddedReverseConfigs[hostCandidate.hostName] = 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
|
|
|
});
|
2021-02-03 19:42:27 +00:00
|
|
|
this.httpsServer;
|
2019-08-22 10:49:29 +00:00
|
|
|
}
|
2019-08-21 21:54:55 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 23:52:34 +00:00
|
|
|
public async addDefaultHeaders(headersArg: { [key: string]: string }) {
|
2022-07-28 23:37:47 +00:00
|
|
|
for (const headerKey of Object.keys(headersArg)) {
|
|
|
|
this.defaultHeaders[headerKey] = headersArg[headerKey];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|