Skip to content

Commit

Permalink
add websocket test
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Feb 10, 2025
1 parent 7482d58 commit fc628e3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
69 changes: 69 additions & 0 deletions server/monitor-types/websocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const { MonitorType } = require("./monitor-type");
const { UP } = require("../../src/util");
const childProcessAsync = require("promisify-child-process");

class websocket extends MonitorType {
name = "websocket";

/**
* @inheritdoc
*/
async check(monitor, heartbeat, _server) {
//let status_code = await this.attemptUpgrade(monitor.url);
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 101 and sets status
* @param {object} heartbeat The heartbeat object to update.
* @param {string} statusCode Status code from curl
* @returns {void}
*/
updateStatus(heartbeat, statusCode) {
if (statusCode === "101") {
heartbeat.status = UP;
}
heartbeat.msg = statusCode;
}

// Attempt at using websocket library. Abandoned this idea because of the lack of control of headers. Certain websocket servers don't return the Sec-WebSocket-Accept, which causes websocket to error out.
// async attemptUpgrade(hostname) {
// return new Promise((resolve) => {
// const ws = new WebSocket('wss://' + hostname);

// ws.addEventListener("open", (event) => {
// ws.close();
// });

// ws.onerror = (error) => {
// console.log(error.message);
// };

// ws.onclose = (event) => {
// if (event.code === 1005) {
// resolve(true);
// } else {
// resolve(false);
// }
// };
// })
// }
}

module.exports = {
websocket,
};
2 changes: 2 additions & 0 deletions server/uptime-kuma-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class UptimeKumaServer {
// Set Monitor Types
UptimeKumaServer.monitorTypeList["real-browser"] = new RealBrowserMonitorType();
UptimeKumaServer.monitorTypeList["tailscale-ping"] = new TailscalePing();
UptimeKumaServer.monitorTypeList["websocket"] = new websocket();
UptimeKumaServer.monitorTypeList["dns"] = new DnsMonitorType();
UptimeKumaServer.monitorTypeList["mqtt"] = new MqttMonitorType();
UptimeKumaServer.monitorTypeList["snmp"] = new SNMPMonitorType();
Expand Down Expand Up @@ -549,6 +550,7 @@ module.exports = {
// Must be at the end to avoid circular dependencies
const { RealBrowserMonitorType } = require("./monitor-types/real-browser-monitor-type");
const { TailscalePing } = require("./monitor-types/tailscale-ping");
const { websocket } = require("./monitor-types/websocket");
const { DnsMonitorType } = require("./monitor-types/dns");
const { MqttMonitorType } = require("./monitor-types/mqtt");
const { SNMPMonitorType } = require("./monitor-types/snmp");
Expand Down
6 changes: 5 additions & 1 deletion src/pages/EditMonitor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
<option value="real-browser">
HTTP(s) - Browser Engine (Chrome/Chromium) (Beta)
</option>

<option value="websocket">
Websocket Upgrade
</option>
</optgroup>

<optgroup :label="$t('Passive Monitor Type')">
Expand Down Expand Up @@ -113,7 +117,7 @@
</div>

<!-- URL -->
<div v-if="monitor.type === 'http' || monitor.type === 'keyword' || monitor.type === 'json-query' || monitor.type === 'real-browser' " class="my-3">
<div v-if="monitor.type === 'websocket' || monitor.type === 'http' || monitor.type === 'keyword' || monitor.type === 'json-query' || monitor.type === 'real-browser' " class="my-3">
<label for="url" class="form-label">{{ $t("URL") }}</label>
<input id="url" v-model="monitor.url" type="url" class="form-control" pattern="https?://.+" required data-testid="url-input">
</div>
Expand Down

0 comments on commit fc628e3

Please sign in to comment.