Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update requestHandler.js #589

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 48 additions & 5 deletions lib/requestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ function getWsReqInfo(wsReq) {
const hostName = host.split(':')[0];
const port = host.split(':')[1];

// https://github.com/alibaba/anyproxy/pull/450
const portPart = port ? `:${port}` : '';
// TODO 如果是windows机器,url是不是全路径?需要对其过滤,取出
const path = wsReq.url || '/';

Expand All @@ -243,6 +245,7 @@ function getWsReqInfo(wsReq) {


return {
url: `${isEncript ? 'wss' : 'ws'}://${hostname}${portPart}${path}`,
headers: headers, // the full headers of origin ws connection
noWsHeaders: getNoWsHeaders(),
hostName: hostName,
Expand Down Expand Up @@ -526,7 +529,7 @@ function getConnectReqHandler(userRule, recorder, httpsServerMgr) {
const reqHandlerCtx = this; reqHandlerCtx.conns = new Map(); reqHandlerCtx.cltSockets = new Map()

return function (req, cltSocket, head) {
const host = req.url.split(':')[0],
let host = req.url.split(':')[0],
targetPort = req.url.split(':')[1];
let shouldIntercept;
let interceptWsRequest = false;
Expand All @@ -552,6 +555,10 @@ function getConnectReqHandler(userRule, recorder, httpsServerMgr) {
// the return value in default rule is null
// so if the value is null, will take it as final value
shouldIntercept = yield userRule.beforeDealHttpsRequest(requestDetail);
if(requestDetail.host != req.url){
host = requestDetail.host.split(':')[0],
targetPort = requestDetail.host.split(':')[1];
}

// otherwise, will take the passed in option
if (shouldIntercept === null) {
Expand Down Expand Up @@ -797,6 +804,24 @@ function getWsHandler(userRule, recorder, wsClient, wsReq) {
recorder && recorder.updateRecordWsMessage(resourceInfoId, message);
};

/**
* prepare messageDetail object for intercept hooks
* @param {object} messageEvent
* @returns {object}
*/
const prepareMessageDetail = (messageEvent) => {
return {
requestOptions: {
port: serverInfo.port,
hostname: serverInfo.hostname,
path: serverInfo.path,
secure: serverInfo.secure,
},
url: serverInfo.url,
data: messageEvent.data,
};
};

proxyWs.onopen = () => {
consumeMsgQueue();
}
Expand Down Expand Up @@ -825,8 +850,18 @@ function getWsHandler(userRule, recorder, wsClient, wsReq) {
}

proxyWs.onmessage = (event) => {
recordMessage(event, false);
wsClient.readyState === 1 && wsClient.send(event.data);

// recordMessage(event, false);
// wsClient.readyState === 1 && wsClient.send(event.data);
co(function *() {
const modifiedMsg = (yield userRule.beforeSendWsMessageToClient(prepareMessageDetail(event))) || {};
const finalMsg = {
data: modifiedMsg.data || event.data,
};
recordMessage(finalMsg, false);
wsClient.readyState === 1 && wsClient.send(finalMsg.data);
});

}

proxyWs.onclose = (event) => {
Expand All @@ -836,8 +871,16 @@ function getWsHandler(userRule, recorder, wsClient, wsReq) {
}

wsClient.onmessage = (event) => {
recordMessage(event, true);
sendProxyMessage(event);
// recordMessage(event, true);
// sendProxyMessage(event);
co(function *() {
const modifiedMsg = (yield userRule.beforeSendWsMessageToServer(prepareMessageDetail(event))) || {};
const finalMsg = {
data: modifiedMsg.data || event.data,
};
recordMessage(finalMsg, true);
sendProxyMessage(finalMsg);
});
}

wsClient.onclose = (event) => {
Expand Down