-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Add Websocket Upgrade Test #5613
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
Open
PoleTransformer
wants to merge
22
commits into
louislam:master
Choose a base branch
from
PoleTransformer:websocket_test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+406
−10
Open
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
fc628e3
add websocket test
587699d
Add Websocket Test v2
dcb07a5
update tests
3a61b2f
macos skip insecure test
425c78c
Merge branch 'louislam:master' into websocket_test
PoleTransformer b0fb6ab
linux skip insecure test
2beb627
windows skip insecure test
2d46a32
skip insecure test except generic arm64
1a98012
skip insecure test on CI
bf17e24
increase test verbosity
725892c
increase test verbosity
492d9f5
one assert per testcase
5bca760
local ws for unit test + touchups
2ad0d78
merge wsurl with url
5bc9a0d
add subprotocol selection + translation keys
e3e019c
variable renaming + update translation keys + additional unit tests
2f2db04
Merge branch 'louislam:master' into websocket_test
PoleTransformer 8833b0c
Merge branch 'websocket_test' of github:PoleTransformer/uptime-kuma i…
722a081
fix linter
ee4a34a
add hyperlinks + open new tabs
1f058cb
Merge branch 'master' into websocket_test
CommanderStorm ba7ff48
Merge branch 'master' into websocket_test
CommanderStorm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Add websocket URL | ||
exports.up = function (knex) { | ||
return knex.schema | ||
.alterTable("monitor", function (table) { | ||
table.text("wsurl"); | ||
table.boolean("ws_ignore_headers").notNullable().defaultTo(false); | ||
}); | ||
}; | ||
|
||
exports.down = function (knex) { | ||
return knex.schema.alterTable("monitor", function (table) { | ||
table.dropColumn("wsurl"); | ||
table.dropColumn("ws_ignore_headers"); | ||
}); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const { MonitorType } = require("./monitor-type"); | ||
const WebSocket = require("ws"); | ||
const { UP, DOWN } = require("../../src/util"); | ||
const childProcessAsync = require("promisify-child-process"); | ||
|
||
class websocket extends MonitorType { | ||
name = "websocket"; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async check(monitor, heartbeat, _server) { | ||
let statusCode = await this.attemptUpgrade(monitor); | ||
//let statusCode = await this.curlTest(monitor.url); | ||
this.updateStatus(heartbeat, statusCode); | ||
} | ||
|
||
/** | ||
* Attempts to upgrade HTTP/HTTPs connection to Websocket. Use curl to send websocket headers to server and returns response code. Close the connection after 1 second and wrap command in bash to return exit code 0 instead of 28. | ||
* @param {string} url Full URL of Websocket server | ||
* @returns {string} HTTP response code | ||
*/ | ||
async curlTest(url) { | ||
let res = await childProcessAsync.spawn("bash", [ "-c", "curl -s -o /dev/null -w '%{http_code}' --http1.1 -N --max-time 1 -H 'Upgrade: websocket' -H 'Sec-WebSocket-Key: test' -H 'Sec-WebSocket-Version: 13' " + url + " || true" ], { | ||
timeout: 5000, | ||
encoding: "utf8", | ||
}); | ||
return res.stdout.toString(); | ||
} | ||
|
||
/** | ||
* Checks if status code is 1000(Normal Closure) and sets status and message | ||
* @param {object} heartbeat The heartbeat object to update. | ||
* @param {[ string, int ]} status Array containing a status message and response code | ||
* @returns {void} | ||
*/ | ||
updateStatus(heartbeat, [ message, code ]) { | ||
heartbeat.status = code === 1000 ? UP : DOWN; | ||
heartbeat.msg = message; | ||
} | ||
PoleTransformer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Uses the builtin Websocket API to establish a connection to target server | ||
* @param {object} monitor The monitor object for input parameters. | ||
* @returns {[ string, int ]} Array containing a status message and response code | ||
*/ | ||
async attemptUpgrade(monitor) { | ||
return new Promise((resolve) => { | ||
const ws = new WebSocket(monitor.wsurl); | ||
|
||
ws.addEventListener("open", (event) => { | ||
ws.close(1000); | ||
}); | ||
|
||
ws.onerror = (error) => { | ||
// console.log(error.message); | ||
PoleTransformer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// Give user the choice to ignore Sec-WebSocket-Accept header | ||
if (monitor.wsIgnoreHeaders && error.message === "Invalid Sec-WebSocket-Accept header") { | ||
resolve([ "101 - OK", 1000 ]); | ||
} | ||
resolve([ error.message, error.code ]); | ||
}; | ||
|
||
ws.onclose = (event) => { | ||
// console.log(event.message); | ||
// console.log(event.code); | ||
resolve([ "101 - OK", event.code ]); | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { | ||
websocket, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
const { describe, test } = require("node:test"); | ||
const assert = require("node:assert"); | ||
const { websocket } = require("../../server/monitor-types/websocket"); | ||
const { UP, DOWN, PENDING } = require("../../src/util"); | ||
|
||
describe("Websocket Test", { | ||
}, () => { | ||
test("Non Websocket Server", {}, async () => { | ||
const websocketMonitor = new websocket(); | ||
|
||
const monitor = { | ||
wsurl: "wss://example.org", | ||
wsIgnoreHeaders: false, | ||
}; | ||
|
||
const heartbeat = { | ||
msg: "", | ||
status: PENDING, | ||
}; | ||
|
||
await websocketMonitor.check(monitor, heartbeat, {}); | ||
assert.strictEqual(heartbeat.status, DOWN); | ||
assert.strictEqual(heartbeat.msg, "Unexpected server response: 200"); | ||
}); | ||
|
||
test("Secure Websocket", async () => { | ||
const websocketMonitor = new websocket(); | ||
|
||
const monitor = { | ||
wsurl: "wss://echo.websocket.org", | ||
wsIgnoreHeaders: false, | ||
}; | ||
|
||
const heartbeat = { | ||
msg: "", | ||
status: PENDING, | ||
}; | ||
|
||
await websocketMonitor.check(monitor, heartbeat, {}); | ||
assert.strictEqual(heartbeat.status, UP); | ||
assert.strictEqual(heartbeat.msg, "101 - OK"); | ||
}); | ||
|
||
test("Insecure Websocket", { | ||
skip: !!process.env.CI, | ||
}, async () => { | ||
const websocketMonitor = new websocket(); | ||
|
||
const monitor = { | ||
wsurl: "ws://ws.ifelse.io", | ||
wsIgnoreHeaders: false, | ||
}; | ||
|
||
const heartbeat = { | ||
msg: "", | ||
status: PENDING, | ||
}; | ||
|
||
await websocketMonitor.check(monitor, heartbeat, {}); | ||
assert.strictEqual(heartbeat.status, UP); | ||
assert.strictEqual(heartbeat.msg, "101 - OK"); | ||
}); | ||
|
||
test("Test a non compliant WS server without ignore", async () => { | ||
const websocketMonitor = new websocket(); | ||
|
||
const monitor = { | ||
wsurl: "wss://c.img-cdn.net/yE4s7KehTFyj/", | ||
wsIgnoreHeaders: false, | ||
}; | ||
|
||
const heartbeat = { | ||
msg: "", | ||
status: PENDING, | ||
}; | ||
|
||
await websocketMonitor.check(monitor, heartbeat, {}); | ||
assert.strictEqual(heartbeat.status, DOWN); | ||
assert.strictEqual(heartbeat.msg, "Invalid Sec-WebSocket-Accept header"); | ||
}); | ||
|
||
test("Test a non compliant WS server with ignore", async () => { | ||
const websocketMonitor = new websocket(); | ||
|
||
const monitor = { | ||
wsurl: "wss://c.img-cdn.net/yE4s7KehTFyj/", | ||
wsIgnoreHeaders: true, | ||
}; | ||
|
||
const heartbeat = { | ||
msg: "", | ||
status: PENDING, | ||
}; | ||
|
||
await websocketMonitor.check(monitor, heartbeat, {}); | ||
assert.strictEqual(heartbeat.status, UP); | ||
assert.strictEqual(heartbeat.msg, "101 - OK"); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.