diff --git a/package.json b/package.json index 1dd79e905..03112518a 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,6 @@ "update-language-files": "cd extra/update-language-files && node index.js && eslint ../../src/languages/**.js --fix" }, "dependencies": { - "@alicloud/pop-core": "^1.7.10", "@fortawesome/fontawesome-svg-core": "~1.2.36", "@fortawesome/free-regular-svg-icons": "~5.15.4", "@fortawesome/free-solid-svg-icons": "~5.15.4", diff --git a/server/notification-providers/aliyun-sms.js b/server/notification-providers/aliyun-sms.js index 9111c429a..d5d59be91 100644 --- a/server/notification-providers/aliyun-sms.js +++ b/server/notification-providers/aliyun-sms.js @@ -1,6 +1,8 @@ const NotificationProvider = require("./notification-provider"); const { DOWN, UP } = require("../../src/util"); -const Core = require("@alicloud/pop-core"); +const { default: axios } = require("axios"); +const Crypto = require("crypto"); +const qs = require("qs"); class AliyunSMS extends NotificationProvider { name = "AliyunSMS"; @@ -9,52 +11,88 @@ class AliyunSMS extends NotificationProvider { let okMsg = "Sent Successfully."; try { - var client = new Core({ - accessKeyId: notification.accessKeyId, - accessKeySecret: notification.secretAccessKey, - endpoint: "https://dysmsapi.aliyuncs.com", - apiVersion: "2017-05-25", - }); - - var params = { - PhoneNumbers: notification.phonenumber, - TemplateCode: notification.templateCode, - SignName: notification.signName, - TemplateParam: JSON.stringify({ - name: "", - time: "", - status: "", - msg: msg, - }), - }; - if (heartbeatJSON != null) { - params.TemplateParam = JSON.stringify({ + var msgBody = JSON.stringify({ name: monitorJSON["name"], time: heartbeatJSON["time"], status: this.statusToString(heartbeatJSON["status"]), msg: heartbeatJSON["msg"], }); - } - - var requestOption = { - method: "POST", - }; - - await client.request("SendSms", params, requestOption).then( - (result) => { - console.log(JSON.stringify(result)); + if (this.sendSms(notification, msgBody)) { return okMsg; - }, - (ex) => { - console.log(ex); } - ); + } else { + var msgBody = JSON.stringify({ + name: "", + time: "", + status: "", + msg: msg, + }); + if (this.sendSms(notification, msgBody)) { + return okMsg; + } + } } catch (error) { this.throwGeneralAxiosError(error); } } + async sendSms(notification, msgbody) { + var 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); + var config = { + method: "POST", + url: "http://dysmsapi.aliyuncs.com/", + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + data: qs.stringify(params), + }; + + var result = await axios(config); + if (result.data.Message == "OK") { + return true; + } + return false; + } + + /** Aliyun request sign */ + sign(param, AccessKeySecret) { + var param2 = {}, + data = []; + + var oa = Object.keys(param).sort(); + + for (var i = 0; i < oa.length; i++) { + var key = oa[i]; + param2[key] = param[key]; + } + + for (var key in param2) { + data.push(`${encodeURIComponent(key)}=${encodeURIComponent(param2[key])}`); + } + + var StringToSign = `POST&${encodeURIComponent("/")}&${encodeURIComponent(data.join("&"))}`; + return Crypto + .createHmac("sha1", `${AccessKeySecret}&`) + .update(Buffer.from(StringToSign)) + .digest("base64"); + } + statusToString(status) { switch (status) { case DOWN: diff --git a/src/components/notifications/AliyunSms.vue b/src/components/notifications/AliyunSms.vue index 07dca8bbb..3f65b3464 100644 --- a/src/components/notifications/AliyunSms.vue +++ b/src/components/notifications/AliyunSms.vue @@ -16,6 +16,7 @@
+

Sms template must contain parameters:
${name} ${time} ${status} ${msg}

https://help.aliyun.com/document_detail/101414.html