fix(core): update
This commit is contained in:
parent
1f8ea59221
commit
b5796b86d5
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@pushrocks/smartproxy',
|
name: '@pushrocks/smartproxy',
|
||||||
version: '3.0.34',
|
version: '3.0.35',
|
||||||
description: 'a proxy for handling high workloads of proxying'
|
description: 'a proxy for handling high workloads of proxying'
|
||||||
}
|
}
|
||||||
|
@ -116,27 +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);
|
||||||
res.destroy();
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,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];
|
||||||
@ -160,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.'
|
||||||
);
|
);
|
||||||
@ -174,61 +177,64 @@ 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,
|
||||||
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', () => {
|
||||||
req.destroy();
|
proxyRequest.end();
|
||||||
request.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();
|
||||||
res.destroy()
|
originResponse.destroy()
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user