mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-08-04 21:39:04 +00:00
pass through all the valid headers when proxing the WebSocket, and adds related test cases
This commit is contained in:
@@ -120,6 +120,7 @@ function isCommonResHeaderEqual(directHeaders, proxyHeaders, requestUrl) {
|
||||
*
|
||||
*/
|
||||
function isCommonReqEqual(url, serverInstance) {
|
||||
console.info('==> trying to get the url ', url);
|
||||
try {
|
||||
let isEqual = true;
|
||||
|
||||
@@ -139,6 +140,27 @@ function isCommonReqEqual(url, serverInstance) {
|
||||
delete directReqObj.headers['transfer-encoding'];
|
||||
delete proxyReqObj.headers['transfer-encoding'];
|
||||
|
||||
// delete the headers that should not be passed by AnyProxy
|
||||
delete directReqObj.headers.connection;
|
||||
delete proxyReqObj.headers.connection;
|
||||
|
||||
// delete the headers related to websocket establishment
|
||||
const directHeaderKeys = Object.keys(directReqObj.headers);
|
||||
directHeaderKeys.forEach((key) => {
|
||||
// if the key matchs 'sec-websocket', delete it
|
||||
if (/sec-websocket/ig.test(key)) {
|
||||
delete directReqObj.headers[key];
|
||||
}
|
||||
});
|
||||
|
||||
const proxyHeaderKeys = Object.keys(proxyReqObj.headers);
|
||||
proxyHeaderKeys.forEach((key) => {
|
||||
// if the key matchs 'sec-websocaket', delete it
|
||||
if (/sec-websocket/ig.test(key)) {
|
||||
delete proxyReqObj.headers[key];
|
||||
}
|
||||
});
|
||||
|
||||
isEqual = isEqual && directReqObj.url === proxyReqObj.url;
|
||||
isEqual = isEqual && isObjectEqual(directReqObj.headers, proxyReqObj.headers, url);
|
||||
isEqual = isEqual && directReqObj.body === proxyReqObj.body;
|
||||
|
||||
@@ -187,17 +187,20 @@ function doUpload(url, method, filepath, formParams, headers = {}, isProxy) {
|
||||
return requestTask;
|
||||
}
|
||||
|
||||
function doWebSocket(url, isProxy) {
|
||||
function doWebSocket(url, headers = {}, isProxy) {
|
||||
let ws;
|
||||
if (isProxy) {
|
||||
headers['via-proxy'] = 'true';
|
||||
const agent = new HttpsProxyAgent(SOCKET_PROXY_HOST);
|
||||
ws = new WebSocket(url, {
|
||||
agent,
|
||||
rejectUnauthorized: false
|
||||
rejectUnauthorized: false,
|
||||
headers
|
||||
});
|
||||
} else {
|
||||
ws = new WebSocket(url, {
|
||||
rejectUnauthorized: false
|
||||
rejectUnauthorized: false,
|
||||
headers
|
||||
});
|
||||
}
|
||||
|
||||
@@ -252,12 +255,12 @@ function directOptions(url, headers = {}) {
|
||||
return directRequest('OPTIONS', url, {}, headers);
|
||||
}
|
||||
|
||||
function proxyWs(url) {
|
||||
return doWebSocket(url, true);
|
||||
function proxyWs(url, headers) {
|
||||
return doWebSocket(url, headers, true);
|
||||
}
|
||||
|
||||
function directWs(url) {
|
||||
return doWebSocket(url);
|
||||
function directWs(url, headers) {
|
||||
return doWebSocket(url, headers);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user