Back to Whistle

frameScript

docs/en/docs/rules/frameScript.md

2.10.62.5 KB
Original Source

frameScript

Manipulate WebSocket and regular TCP request data frames through JavaScript scripts.

Rule Syntax

txt
pattern frameScript://value [filters...]
ParametersDescriptionDetailed Documentation
patternExpression to match request URLsMatch Pattern Documentation
valueJS script to generate rules. Supports the following types:
• Directory/File Path
• Remote URL
• Inline/Embedded/Values ​​ContentOperation Instruction Documentation
filtersOptional filters. Supports matching:
• Request URL/Method/Header/Content
• Response Status Code/HeaderFilter Documentation

Configuration Example

txt
``` test-frame.js
// Send preset data
ctx.sendToServer('1 = 0x12300000');
ctx.sendToClient('1 = 0x1236666666');

// Process the data frame sent to the server
ctx.handleSendToServerFrame = function(buf, opts) {
// Can return empty, null, undefined, etc.
return (buf + '').replace(/1/g, '***');
};

// Process the data frame sent to the client
ctx.handleSendToClientFrame = function(buf, opts) {
// Can return empty, null, undefined, etc.
return (buf + '').replace(/1/g, '+++');
};
```

wss://echo.websocket.org/ frameScript://{test-frame.js}

Visiting https://echo.websocket.org/.ws Results:

Available Global Variables

Variable/MethodDescription
urlFull request URL
methodRequest method (GET/POST, etc.)
ip/clientIpClient IP address
headersRequest header object
rulesRule array, add new rules via push
valuesTemporary value storage object
render(tpl,data)Micro template rendering function
getValue(key)Get the value in Values
parseUrlSame as url.parse in Node.js
parseQuerySame as querystring.parse in Node.js
sendToClient(frame, options)Sends data (object, string, buffer) to the client. See the example below.
sendToServer(frame, options)Sends data (object, string, buffer) to the server. See the example below.
handleSendToClientFrame(buffer, options)Processes (filters or deletes) data sent to the client. See the example below.
handleSendToServerFrame(buffer, options)Processes (filters or deletes) data sent to the server. See the example below.