committed by
GitHub
23 changed files with 722 additions and 227 deletions
@ -0,0 +1,108 @@ |
|||
const NotificationProvider = require("./notification-provider"); |
|||
const { DOWN, UP } = require("../../src/util"); |
|||
const { default: axios } = require("axios"); |
|||
const Crypto = require("crypto"); |
|||
const qs = require("qs"); |
|||
|
|||
class AliyunSMS extends NotificationProvider { |
|||
name = "AliyunSMS"; |
|||
|
|||
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { |
|||
let okMsg = "Sent Successfully."; |
|||
|
|||
try { |
|||
if (heartbeatJSON != null) { |
|||
let msgBody = JSON.stringify({ |
|||
name: monitorJSON["name"], |
|||
time: heartbeatJSON["time"], |
|||
status: this.statusToString(heartbeatJSON["status"]), |
|||
msg: heartbeatJSON["msg"], |
|||
}); |
|||
if (this.sendSms(notification, msgBody)) { |
|||
return okMsg; |
|||
} |
|||
} else { |
|||
let msgBody = JSON.stringify({ |
|||
name: "", |
|||
time: "", |
|||
status: "", |
|||
msg: msg, |
|||
}); |
|||
if (this.sendSms(notification, msgBody)) { |
|||
return okMsg; |
|||
} |
|||
} |
|||
} catch (error) { |
|||
this.throwGeneralAxiosError(error); |
|||
} |
|||
} |
|||
|
|||
async sendSms(notification, msgbody) { |
|||
let params = { |
|||
PhoneNumbers: notification.phonenumber, |
|||
TemplateCode: notification.templateCode, |
|||
SignName: notification.signName, |
|||
TemplateParam: msgbody, |
|||
AccessKeyId: notification.accessKeyId, |
|||
Format: "JSON", |
|||
SignatureMethod: "HMAC-SHA1", |
|||
SignatureVersion: "1.0", |
|||
SignatureNonce: Math.random().toString(), |
|||
Timestamp: new Date().toISOString(), |
|||
Action: "SendSms", |
|||
Version: "2017-05-25", |
|||
}; |
|||
|
|||
params.Signature = this.sign(params, notification.secretAccessKey); |
|||
let config = { |
|||
method: "POST", |
|||
url: "http://dysmsapi.aliyuncs.com/", |
|||
headers: { |
|||
"Content-Type": "application/x-www-form-urlencoded", |
|||
}, |
|||
data: qs.stringify(params), |
|||
}; |
|||
|
|||
let result = await axios(config); |
|||
if (result.data.Message == "OK") { |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
/** Aliyun request sign */ |
|||
sign(param, AccessKeySecret) { |
|||
let param2 = {}; |
|||
let data = []; |
|||
|
|||
let oa = Object.keys(param).sort(); |
|||
|
|||
for (let i = 0; i < oa.length; i++) { |
|||
let key = oa[i]; |
|||
param2[key] = param[key]; |
|||
} |
|||
|
|||
for (let key in param2) { |
|||
data.push(`${encodeURIComponent(key)}=${encodeURIComponent(param2[key])}`); |
|||
} |
|||
|
|||
let StringToSign = `POST&${encodeURIComponent("/")}&${encodeURIComponent(data.join("&"))}`; |
|||
return Crypto |
|||
.createHmac("sha1", `${AccessKeySecret}&`) |
|||
.update(Buffer.from(StringToSign)) |
|||
.digest("base64"); |
|||
} |
|||
|
|||
statusToString(status) { |
|||
switch (status) { |
|||
case DOWN: |
|||
return "DOWN"; |
|||
case UP: |
|||
return "UP"; |
|||
default: |
|||
return status; |
|||
} |
|||
} |
|||
} |
|||
|
|||
module.exports = AliyunSMS; |
@ -0,0 +1,79 @@ |
|||
const NotificationProvider = require("./notification-provider"); |
|||
const { DOWN, UP } = require("../../src/util"); |
|||
const { default: axios } = require("axios"); |
|||
const Crypto = require("crypto"); |
|||
|
|||
class DingDing extends NotificationProvider { |
|||
name = "DingDing"; |
|||
|
|||
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { |
|||
let okMsg = "Sent Successfully."; |
|||
|
|||
try { |
|||
if (heartbeatJSON != null) { |
|||
let params = { |
|||
msgtype: "markdown", |
|||
markdown: { |
|||
title: monitorJSON["name"], |
|||
text: `## [${this.statusToString(heartbeatJSON["status"])}] \n > ${heartbeatJSON["msg"]} \n > Time(UTC):${heartbeatJSON["time"]}`, |
|||
} |
|||
}; |
|||
if (this.sendToDingDing(notification, params)) { |
|||
return okMsg; |
|||
} |
|||
} else { |
|||
let params = { |
|||
msgtype: "text", |
|||
text: { |
|||
content: msg |
|||
} |
|||
}; |
|||
if (this.sendToDingDing(notification, params)) { |
|||
return okMsg; |
|||
} |
|||
} |
|||
} catch (error) { |
|||
this.throwGeneralAxiosError(error); |
|||
} |
|||
} |
|||
|
|||
async sendToDingDing(notification, params) { |
|||
let timestamp = Date.now(); |
|||
|
|||
let config = { |
|||
method: "POST", |
|||
headers: { |
|||
"Content-Type": "application/json", |
|||
}, |
|||
url: `${notification.webHookUrl}×tamp=${timestamp}&sign=${encodeURIComponent(this.sign(timestamp, notification.secretKey))}`, |
|||
data: JSON.stringify(params), |
|||
}; |
|||
|
|||
let result = await axios(config); |
|||
if (result.data.errmsg == "ok") { |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
/** DingDing sign */ |
|||
sign(timestamp, secretKey) { |
|||
return Crypto |
|||
.createHmac("sha256", Buffer.from(secretKey, "utf8")) |
|||
.update(Buffer.from(`${timestamp}\n${secretKey}`, "utf8")) |
|||
.digest("base64"); |
|||
} |
|||
|
|||
statusToString(status) { |
|||
switch (status) { |
|||
case DOWN: |
|||
return "DOWN"; |
|||
case UP: |
|||
return "UP"; |
|||
default: |
|||
return status; |
|||
} |
|||
} |
|||
} |
|||
|
|||
module.exports = DingDing; |
@ -0,0 +1,25 @@ |
|||
<template> |
|||
<div class="mb-3"> |
|||
<label for="accessKeyId" class="form-label">{{ $t("AccessKeyId") }}<span style="color: red;"><sup>*</sup></span></label> |
|||
<input id="accessKeyId" v-model="$parent.notification.accessKeyId" type="text" class="form-control" required> |
|||
|
|||
<label for="secretAccessKey" class="form-label">{{ $t("SecretAccessKey") }}<span style="color: red;"><sup>*</sup></span></label> |
|||
<input id="secretAccessKey" v-model="$parent.notification.secretAccessKey" type="text" class="form-control" required> |
|||
|
|||
<label for="phonenumber" class="form-label">{{ $t("Phonenumber") }}<span style="color: red;"><sup>*</sup></span></label> |
|||
<input id="phonenumber" v-model="$parent.notification.phonenumber" type="text" class="form-control" required> |
|||
|
|||
<label for="templateCode" class="form-label">{{ $t("TemplateCode") }}<span style="color: red;"><sup>*</sup></span></label> |
|||
<input id="templateCode" v-model="$parent.notification.templateCode" type="text" class="form-control" required> |
|||
|
|||
<label for="signName" class="form-label">{{ $t("SignName") }}<span style="color: red;"><sup>*</sup></span></label> |
|||
<input id="signName" v-model="$parent.notification.signName" type="text" class="form-control" required> |
|||
|
|||
<div class="form-text"> |
|||
<p>Sms template must contain parameters: <br> <code>${name} ${time} ${status} ${msg}</code></p> |
|||
<i18n-t tag="p" keypath="Read more:"> |
|||
<a href="https://help.aliyun.com/document_detail/101414.html" target="_blank">https://help.aliyun.com/document_detail/101414.html</a> |
|||
</i18n-t> |
|||
</div> |
|||
</div> |
|||
</template> |
@ -0,0 +1,16 @@ |
|||
<template> |
|||
<div class="mb-3"> |
|||
<label for="WebHookUrl" class="form-label">{{ $t("WebHookUrl") }}<span style="color: red;"><sup>*</sup></span></label> |
|||
<input id="WebHookUrl" v-model="$parent.notification.webHookUrl" type="text" class="form-control" required> |
|||
|
|||
<label for="secretKey" class="form-label">{{ $t("SecretKey") }}<span style="color: red;"><sup>*</sup></span></label> |
|||
<input id="secretKey" v-model="$parent.notification.secretKey" type="text" class="form-control" required> |
|||
|
|||
<div class="form-text"> |
|||
<p>For safety, must use secret key</p> |
|||
<i18n-t tag="p" keypath="Read more:"> |
|||
<a href="https://developers.dingtalk.com/document/robots/custom-robot-access" target="_blank">https://developers.dingtalk.com/document/robots/custom-robot-access</a> |
|||
</i18n-t> |
|||
</div> |
|||
</div> |
|||
</template> |
@ -1,118 +1,118 @@ |
|||
"use strict"; |
|||
// Common Util for frontend and backend
|
|||
//
|
|||
// DOT NOT MODIFY util.js!
|
|||
// Need to run "tsc" to compile if there are any changes.
|
|||
//
|
|||
// Backend uses the compiled file util.js
|
|||
// Frontend uses util.ts
|
|||
Object.defineProperty(exports, "__esModule", { value: true }); |
|||
exports.getMonitorRelativeURL = exports.genSecret = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.debug = exports.ucfirst = exports.sleep = exports.flipStatus = exports.STATUS_PAGE_PARTIAL_DOWN = exports.STATUS_PAGE_ALL_UP = exports.STATUS_PAGE_ALL_DOWN = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isDev = void 0; |
|||
const _dayjs = require("dayjs"); |
|||
const dayjs = _dayjs; |
|||
exports.isDev = process.env.NODE_ENV === "development"; |
|||
exports.appName = "Uptime Kuma"; |
|||
exports.DOWN = 0; |
|||
exports.UP = 1; |
|||
exports.PENDING = 2; |
|||
exports.STATUS_PAGE_ALL_DOWN = 0; |
|||
exports.STATUS_PAGE_ALL_UP = 1; |
|||
exports.STATUS_PAGE_PARTIAL_DOWN = 2; |
|||
function flipStatus(s) { |
|||
if (s === exports.UP) { |
|||
return exports.DOWN; |
|||
} |
|||
if (s === exports.DOWN) { |
|||
return exports.UP; |
|||
} |
|||
return s; |
|||
} |
|||
exports.flipStatus = flipStatus; |
|||
function sleep(ms) { |
|||
return new Promise(resolve => setTimeout(resolve, ms)); |
|||
} |
|||
exports.sleep = sleep; |
|||
/** |
|||
* PHP's ucfirst |
|||
* @param str |
|||
*/ |
|||
function ucfirst(str) { |
|||
if (!str) { |
|||
return str; |
|||
} |
|||
const firstLetter = str.substr(0, 1); |
|||
return firstLetter.toUpperCase() + str.substr(1); |
|||
} |
|||
exports.ucfirst = ucfirst; |
|||
function debug(msg) { |
|||
if (exports.isDev) { |
|||
console.log(msg); |
|||
} |
|||
} |
|||
exports.debug = debug; |
|||
function polyfill() { |
|||
/** |
|||
* String.prototype.replaceAll() polyfill |
|||
* https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/
|
|||
* @author Chris Ferdinandi |
|||
* @license MIT |
|||
*/ |
|||
if (!String.prototype.replaceAll) { |
|||
String.prototype.replaceAll = function (str, newStr) { |
|||
// If a regex pattern
|
|||
if (Object.prototype.toString.call(str).toLowerCase() === "[object regexp]") { |
|||
return this.replace(str, newStr); |
|||
} |
|||
// If a string
|
|||
return this.replace(new RegExp(str, "g"), newStr); |
|||
}; |
|||
} |
|||
} |
|||
exports.polyfill = polyfill; |
|||
class TimeLogger { |
|||
constructor() { |
|||
this.startTime = dayjs().valueOf(); |
|||
} |
|||
print(name) { |
|||
if (exports.isDev && process.env.TIMELOGGER === "1") { |
|||
console.log(name + ": " + (dayjs().valueOf() - this.startTime) + "ms"); |
|||
} |
|||
} |
|||
} |
|||
exports.TimeLogger = TimeLogger; |
|||
/** |
|||
* Returns a random number between min (inclusive) and max (exclusive) |
|||
*/ |
|||
function getRandomArbitrary(min, max) { |
|||
return Math.random() * (max - min) + min; |
|||
} |
|||
exports.getRandomArbitrary = getRandomArbitrary; |
|||
/** |
|||
* From: https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range
|
|||
* |
|||
* Returns a random integer between min (inclusive) and max (inclusive). |
|||
* The value is no lower than min (or the next integer greater than min |
|||
* if min isn't an integer) and no greater than max (or the next integer |
|||
* lower than max if max isn't an integer). |
|||
* Using Math.round() will give you a non-uniform distribution! |
|||
*/ |
|||
function getRandomInt(min, max) { |
|||
min = Math.ceil(min); |
|||
max = Math.floor(max); |
|||
return Math.floor(Math.random() * (max - min + 1)) + min; |
|||
} |
|||
exports.getRandomInt = getRandomInt; |
|||
function genSecret(length = 64) { |
|||
let secret = ""; |
|||
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
|||
let charsLength = chars.length; |
|||
for (let i = 0; i < length; i++) { |
|||
secret += chars.charAt(Math.floor(Math.random() * charsLength)); |
|||
} |
|||
return secret; |
|||
} |
|||
exports.genSecret = genSecret; |
|||
function getMonitorRelativeURL(id) { |
|||
return "/dashboard/" + id; |
|||
} |
|||
exports.getMonitorRelativeURL = getMonitorRelativeURL; |
|||
"use strict"; |
|||
// Common Util for frontend and backend
|
|||
//
|
|||
// DOT NOT MODIFY util.js!
|
|||
// Need to run "tsc" to compile if there are any changes.
|
|||
//
|
|||
// Backend uses the compiled file util.js
|
|||
// Frontend uses util.ts
|
|||
Object.defineProperty(exports, "__esModule", { value: true }); |
|||
exports.getMonitorRelativeURL = exports.genSecret = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.debug = exports.ucfirst = exports.sleep = exports.flipStatus = exports.STATUS_PAGE_PARTIAL_DOWN = exports.STATUS_PAGE_ALL_UP = exports.STATUS_PAGE_ALL_DOWN = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isDev = void 0; |
|||
const _dayjs = require("dayjs"); |
|||
const dayjs = _dayjs; |
|||
exports.isDev = process.env.NODE_ENV === "development"; |
|||
exports.appName = "Uptime Kuma"; |
|||
exports.DOWN = 0; |
|||
exports.UP = 1; |
|||
exports.PENDING = 2; |
|||
exports.STATUS_PAGE_ALL_DOWN = 0; |
|||
exports.STATUS_PAGE_ALL_UP = 1; |
|||
exports.STATUS_PAGE_PARTIAL_DOWN = 2; |
|||
function flipStatus(s) { |
|||
if (s === exports.UP) { |
|||
return exports.DOWN; |
|||
} |
|||
if (s === exports.DOWN) { |
|||
return exports.UP; |
|||
} |
|||
return s; |
|||
} |
|||
exports.flipStatus = flipStatus; |
|||
function sleep(ms) { |
|||
return new Promise(resolve => setTimeout(resolve, ms)); |
|||
} |
|||
exports.sleep = sleep; |
|||
/** |
|||
* PHP's ucfirst |
|||
* @param str |
|||
*/ |
|||
function ucfirst(str) { |
|||
if (!str) { |
|||
return str; |
|||
} |
|||
const firstLetter = str.substr(0, 1); |
|||
return firstLetter.toUpperCase() + str.substr(1); |
|||
} |
|||
exports.ucfirst = ucfirst; |
|||
function debug(msg) { |
|||
if (exports.isDev) { |
|||
console.log(msg); |
|||
} |
|||
} |
|||
exports.debug = debug; |
|||
function polyfill() { |
|||
/** |
|||
* String.prototype.replaceAll() polyfill |
|||
* https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/
|
|||
* @author Chris Ferdinandi |
|||
* @license MIT |
|||
*/ |
|||
if (!String.prototype.replaceAll) { |
|||
String.prototype.replaceAll = function (str, newStr) { |
|||
// If a regex pattern
|
|||
if (Object.prototype.toString.call(str).toLowerCase() === "[object regexp]") { |
|||
return this.replace(str, newStr); |
|||
} |
|||
// If a string
|
|||
return this.replace(new RegExp(str, "g"), newStr); |
|||
}; |
|||
} |
|||
} |
|||
exports.polyfill = polyfill; |
|||
class TimeLogger { |
|||
constructor() { |
|||
this.startTime = dayjs().valueOf(); |
|||
} |
|||
print(name) { |
|||
if (exports.isDev && process.env.TIMELOGGER === "1") { |
|||
console.log(name + ": " + (dayjs().valueOf() - this.startTime) + "ms"); |
|||
} |
|||
} |
|||
} |
|||
exports.TimeLogger = TimeLogger; |
|||
/** |
|||
* Returns a random number between min (inclusive) and max (exclusive) |
|||
*/ |
|||
function getRandomArbitrary(min, max) { |
|||
return Math.random() * (max - min) + min; |
|||
} |
|||
exports.getRandomArbitrary = getRandomArbitrary; |
|||
/** |
|||
* From: https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range
|
|||
* |
|||
* Returns a random integer between min (inclusive) and max (inclusive). |
|||
* The value is no lower than min (or the next integer greater than min |
|||
* if min isn't an integer) and no greater than max (or the next integer |
|||
* lower than max if max isn't an integer). |
|||
* Using Math.round() will give you a non-uniform distribution! |
|||
*/ |
|||
function getRandomInt(min, max) { |
|||
min = Math.ceil(min); |
|||
max = Math.floor(max); |
|||
return Math.floor(Math.random() * (max - min + 1)) + min; |
|||
} |
|||
exports.getRandomInt = getRandomInt; |
|||
function genSecret(length = 64) { |
|||
let secret = ""; |
|||
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
|||
let charsLength = chars.length; |
|||
for (let i = 0; i < length; i++) { |
|||
secret += chars.charAt(Math.floor(Math.random() * charsLength)); |
|||
} |
|||
return secret; |
|||
} |
|||
exports.genSecret = genSecret; |
|||
function getMonitorRelativeURL(id) { |
|||
return "/dashboard/" + id; |
|||
} |
|||
exports.getMonitorRelativeURL = getMonitorRelativeURL; |
|||
|
@ -1,10 +1,37 @@ |
|||
const { genSecret } = require("../src/util"); |
|||
|
|||
beforeAll(() => { |
|||
|
|||
}); |
|||
|
|||
describe("", () => { |
|||
describe("Test genSecret", () => { |
|||
|
|||
it("should be correct length", () => { |
|||
let secret = genSecret(-1); |
|||
expect(secret).toEqual(""); |
|||
|
|||
secret = genSecret(0); |
|||
expect(secret).toEqual(""); |
|||
|
|||
secret = genSecret(1); |
|||
expect(secret.length).toEqual(1); |
|||
|
|||
it("should ", () => { |
|||
secret = genSecret(2); |
|||
expect(secret.length).toEqual(2); |
|||
|
|||
secret = genSecret(64); |
|||
expect(secret.length).toEqual(64); |
|||
|
|||
secret = genSecret(9000); |
|||
expect(secret.length).toEqual(9000); |
|||
|
|||
secret = genSecret(90000); |
|||
expect(secret.length).toEqual(90000); |
|||
}); |
|||
|
|||
it("should contain first and last possible chars", () => { |
|||
let secret = genSecret(90000); |
|||
expect(secret).toContain("A"); |
|||
expect(secret).toContain("9"); |
|||
}); |
|||
}); |
|||
|
Loading…
Reference in new issue