Compare commits

...

20 Commits

Author SHA1 Message Date
5bb065f82b 3.0.37 2022-08-04 17:16:27 +02:00
942b812f97 fix(core): update 2022-08-04 17:16:26 +02:00
59a025b308 3.0.36 2022-08-04 14:21:06 +02:00
458e7d6b58 fix(core): update 2022-08-04 14:21:05 +02:00
7b0f824d29 3.0.35 2022-08-01 18:40:45 +02:00
b5796b86d5 fix(core): update 2022-08-01 18:40:44 +02:00
1f8ea59221 3.0.34 2022-08-01 13:00:47 +02:00
d717568572 fix(core): update 2022-08-01 13:00:46 +02:00
28d050851f 3.0.33 2022-08-01 12:56:15 +02:00
acbd109985 fix(core): update 2022-08-01 12:56:14 +02:00
cc38a6d10e 3.0.32 2022-08-01 12:55:01 +02:00
748b07efe2 fix(core): update 2022-08-01 12:55:00 +02:00
be4fd0978a 3.0.31 2022-08-01 12:31:30 +02:00
4521010b82 fix(core): update 2022-08-01 12:31:29 +02:00
bd1f1a4c1c 3.0.30 2022-07-31 08:30:13 +02:00
d3bdd56660 fix(core): update 2022-07-31 08:30:13 +02:00
c38a7c4c32 3.0.29 2022-07-30 23:00:23 +02:00
858628196a fix(core): update 2022-07-30 23:00:23 +02:00
4910679058 3.0.28 2022-07-30 23:00:15 +02:00
97db2012ca fix(core): update 2022-07-30 23:00:15 +02:00
4 changed files with 567 additions and 1667 deletions

2104
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartproxy", "name": "@pushrocks/smartproxy",
"version": "3.0.27", "version": "3.0.37",
"private": false, "private": false,
"description": "a proxy for handling high workloads of proxying", "description": "a proxy for handling high workloads of proxying",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
@ -15,16 +15,16 @@
"buildDocs": "tsdoc" "buildDocs": "tsdoc"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.63", "@gitzone/tsbuild": "^2.1.65",
"@gitzone/tstest": "^1.0.72", "@gitzone/tstest": "^1.0.73",
"@pushrocks/tapbundle": "^5.0.4", "@pushrocks/tapbundle": "^5.0.4",
"@types/node": "^18.6.2" "@types/node": "^18.6.3"
}, },
"dependencies": { "dependencies": {
"@pushrocks/lik": "^6.0.0", "@pushrocks/lik": "^6.0.0",
"@pushrocks/smartdelay": "^2.0.13", "@pushrocks/smartdelay": "^2.0.13",
"@pushrocks/smartpromise": "^3.1.7", "@pushrocks/smartpromise": "^3.1.7",
"@pushrocks/smartrequest": "^2.0.6", "@pushrocks/smartrequest": "^2.0.9",
"@pushrocks/smartstring": "^4.0.2", "@pushrocks/smartstring": "^4.0.2",
"@tsclass/tsclass": "^4.0.17", "@tsclass/tsclass": "^4.0.17",
"@types/ws": "^8.5.3", "@types/ws": "^8.5.3",

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@pushrocks/smartproxy', name: '@pushrocks/smartproxy',
version: '3.0.27', version: '3.0.37',
description: 'a proxy for handling high workloads of proxying' description: 'a proxy for handling high workloads of proxying'
} }

View File

@ -116,26 +116,30 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
-----END CERTIFICATE----- -----END CERTIFICATE-----
`, `,
}, },
async (req, res) => { async (originRequest, originResponse) => {
/** /**
* endRequest function * endRequest function
* can be used to prematurely end a request * can be used to prematurely end a request
*/ */
const endRequest = ( const endOriginReqRes = (
statusArg: number = 404, statusArg: number = 404,
messageArg: string = 'This route is not available on this server.', messageArg: string = 'This route is not available on this server.',
headers: plugins.http.OutgoingHttpHeaders = {} headers: plugins.http.OutgoingHttpHeaders = {}
) => { ) => {
res.writeHead(statusArg, messageArg); originResponse.writeHead(statusArg, messageArg);
res.end(messageArg); originResponse.end(messageArg);
if (originRequest.socket !== originResponse.socket) {
console.log('hey, something is strange.')
}
originResponse.destroy();
}; };
console.log(`got request: ${req.headers.host}${plugins.url.parse(req.url).path}`); console.log(`got request: ${originRequest.headers.host}${plugins.url.parse(originRequest.url).path}`);
const destinationConfig = this.router.routeReq(req); const destinationConfig = this.router.routeReq(originRequest);
if (!destinationConfig) { if (!destinationConfig) {
console.log(`${req.headers.host} can't be routed properly. Terminating request.`); console.log(`${originRequest.headers.host} can't be routed properly. Terminating request.`);
endRequest(); endOriginReqRes();
return; return;
} }
@ -144,14 +148,14 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
const authInfo = destinationConfig.authentication; const authInfo = destinationConfig.authentication;
switch (authInfo.type) { switch (authInfo.type) {
case 'Basic': case 'Basic':
const authHeader = req.headers.authorization; const authHeader = originRequest.headers.authorization;
if (authHeader) { if (authHeader) {
if (!authHeader.includes('Basic ')) { if (!authHeader.includes('Basic ')) {
return endRequest(401, 'Authentication required', { return endOriginReqRes(401, 'Authentication required', {
'WWW-Authenticate': 'Basic realm="Access to the staging site", charset="UTF-8"', 'WWW-Authenticate': 'Basic realm="Access to the staging site", charset="UTF-8"',
}); });
} }
const authStringBase64 = req.headers.authorization.replace('Basic ', ''); const authStringBase64 = originRequest.headers.authorization.replace('Basic ', '');
const authString: string = plugins.smartstring.base64.decode(authStringBase64); const authString: string = plugins.smartstring.base64.decode(authStringBase64);
const userPassArray = authString.split(':'); const userPassArray = authString.split(':');
const user = userPassArray[0]; const user = userPassArray[0];
@ -159,12 +163,12 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
if (user === authInfo.user && pass === authInfo.pass) { if (user === authInfo.user && pass === authInfo.pass) {
console.log('request successfully authenticated'); console.log('request successfully authenticated');
} else { } else {
return endRequest(403, 'Forbidden: Wrong credentials'); return endOriginReqRes(403, 'Forbidden: Wrong credentials');
} }
} }
break; break;
default: default:
return endRequest( return endOriginReqRes(
403, 403,
'Forbidden: unsupported authentication method configured. Please report to the admin.' 'Forbidden: unsupported authentication method configured. Please report to the admin.'
); );
@ -173,59 +177,68 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
let destinationUrl: string; let destinationUrl: string;
if (destinationConfig) { if (destinationConfig) {
destinationUrl = `http://${destinationConfig.destinationIp}:${destinationConfig.destinationPort}${req.url}`; destinationUrl = `http://${destinationConfig.destinationIp}:${destinationConfig.destinationPort}${originRequest.url}`;
} else { } else {
return endRequest(); return endOriginReqRes();
} }
console.log(destinationUrl); console.log(destinationUrl);
const response = await plugins.smartrequest.request( const proxyResponse = await plugins.smartrequest.request(
destinationUrl, destinationUrl,
{ {
method: req.method, method: originRequest.method,
headers: req.headers, headers: {
...originRequest.headers,
'X-Forwarded-Host': originRequest.headers.host,
'X-Forwarded-Proto': 'https'
},
keepAlive: true, keepAlive: true,
}, },
true, // lets make this streaming true, // lets make this streaming
(request) => { (proxyRequest) => {
req.on('data', (data) => { originRequest.on('data', (data) => {
request.write(data); proxyRequest.write(data);
}); });
req.on('end', (data) => { originRequest.on('end', (data) => {
request.end(); proxyRequest.end();
}); });
req.on('error', () => { originRequest.on('error', () => {
request.end(); proxyRequest.end();
}); });
req.on('close', () => { originRequest.on('close', () => {
request.end(); proxyRequest.end();
}); });
req.on('timeout', () => { originRequest.on('timeout', () => {
request.end(); proxyRequest.end();
originRequest.destroy();
});
proxyRequest.on('error', () => {
endOriginReqRes();
}) })
} }
); );
res.statusCode = response.statusCode; originResponse.statusCode = proxyResponse.statusCode;
console.log(response.statusCode); console.log(proxyResponse.statusCode);
for (const defaultHeader of Object.keys(this.defaultHeaders)) { for (const defaultHeader of Object.keys(this.defaultHeaders)) {
res.setHeader(defaultHeader, this.defaultHeaders[defaultHeader]); originResponse.setHeader(defaultHeader, this.defaultHeaders[defaultHeader]);
} }
for (const header of Object.keys(response.headers)) { for (const header of Object.keys(proxyResponse.headers)) {
res.setHeader(header, response.headers[header]); originResponse.setHeader(header, proxyResponse.headers[header]);
} }
response.on('data', (data) => { proxyResponse.on('data', (data) => {
res.write(data); originResponse.write(data);
}); });
response.on('end', () => { proxyResponse.on('end', () => {
res.end(); originResponse.end();
}); });
response.on('error', () => { proxyResponse.on('error', () => {
res.end(); originResponse.destroy();
}); });
response.on('close', () => { proxyResponse.on('close', () => {
res.end(); originResponse.end();
}); });
response.on('timeout', () => { proxyResponse.on('timeout', () => {
res.end(); originResponse.end();
originResponse.destroy()
}); });
} }
@ -258,8 +271,8 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
// handle closing // handle closing
const cleanUp = () => { const cleanUp = () => {
ws.close(); ws.terminate();
wsc.close(); wsc.terminate();
}; };
ws.on('close', (message) => { ws.on('close', (message) => {
@ -282,7 +295,6 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
this.socketMap.remove(connection); this.socketMap.remove(connection);
console.log(`removed connection. ${this.socketMap.getArray().length} sockets remaining.`); console.log(`removed connection. ${this.socketMap.getArray().length} sockets remaining.`);
connection.destroy(); connection.destroy();
console.log(`socket successfully destroyed.`);
} }
}; };
connection.on('close', () => { connection.on('close', () => {
@ -301,7 +313,7 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
this.httpsServer.listen(this.options.port); this.httpsServer.listen(this.options.port);
console.log( console.log(
`ProxyWorker -> OK: now listening for new connections on port ${this.options.port}` `NetworkProxy -> OK: now listening for new connections on port ${this.options.port}`
); );
} }