Phuong Nguyen Minh
3 years ago
committed by
GitHub
71 changed files with 2438 additions and 687 deletions
@ -0,0 +1,22 @@ |
|||
name: 'Automatically close stale issues and PRs' |
|||
on: |
|||
schedule: |
|||
- cron: '0 0 * * *' |
|||
#Run once a day at midnight |
|||
|
|||
jobs: |
|||
stale: |
|||
runs-on: ubuntu-latest |
|||
steps: |
|||
- uses: actions/stale@v4 |
|||
with: |
|||
stale-issue-message: 'We are clearing up our old issues and your ticket has been open for 6 months with no activity. Remove stale label or comment or this will be closed in 7 days.' |
|||
stale-pr-message: 'We are clearing up our old Pull Requests and yours has been open for 6 months with no activity. Remove stale label or comment or this will be closed in 7 days.' |
|||
close-issue-message: 'This issue was closed because it has been stalled for 7 days with no activity.' |
|||
close-pr-message: 'This PR was closed because it has been stalled for 7 days with no activity.' |
|||
days-before-stale: 180 |
|||
days-before-close: 7 |
|||
exempt-issue-labels: 'News,Medium,High,discussion,bug,doc,' |
|||
exempt-pr-labels: 'awaiting-approval,work-in-progress,enhancement,' |
|||
exempt-issue-assignees: 'louislam' |
|||
exempt-pr-assignees: 'louislam' |
@ -0,0 +1,13 @@ |
|||
-- You should not modify if this have pushed to Github, unless it does serious wrong with the db. |
|||
BEGIN TRANSACTION; |
|||
|
|||
ALTER TABLE monitor |
|||
ADD method TEXT default 'GET' not null; |
|||
|
|||
ALTER TABLE monitor |
|||
ADD body TEXT default null; |
|||
|
|||
ALTER TABLE monitor |
|||
ADD headers TEXT default null; |
|||
|
|||
COMMIT; |
@ -1,32 +0,0 @@ |
|||
# Uptime-Kuma K8s Deployment |
|||
|
|||
⚠ Warning: K8s deployment is provided by contributors. I have no experience with K8s and I can't fix error in the future. I only test Docker and Node.js. Use at your own risk. |
|||
|
|||
## How does it work? |
|||
|
|||
Kustomize is a tool which builds a complete deployment file for all config elements. |
|||
You can edit the files in the ```uptime-kuma``` folder except the ```kustomization.yml``` until you know what you're doing. |
|||
If you want to choose another namespace you can edit the ```kustomization.yml``` in the ```kubernetes```-Folder and change the ```namespace: uptime-kuma``` to something you like. |
|||
|
|||
It creates a certificate with the specified Issuer and creates the Ingress for the Uptime-Kuma ClusterIP-Service. |
|||
|
|||
## What do I have to edit? |
|||
|
|||
You have to edit the ```ingressroute.yml``` to your needs. |
|||
This ingressroute.yml is for the [nginx-ingress-controller](https://kubernetes.github.io/ingress-nginx/) in combination with the [cert-manager](https://cert-manager.io/). |
|||
|
|||
- Host |
|||
- Secrets and secret names |
|||
- (Cluster)Issuer (optional) |
|||
- The Version in the Deployment-File |
|||
- Update: |
|||
- Change to newer version and run the above commands, it will update the pods one after another |
|||
|
|||
## How To use |
|||
|
|||
- Install [kustomize](https://kubectl.docs.kubernetes.io/installation/kustomize/) |
|||
- Edit files mentioned above to your needs |
|||
- Run ```kustomize build > apply.yml``` |
|||
- Run ```kubectl apply -f apply.yml``` |
|||
|
|||
Now you should see some k8s magic and Uptime-Kuma should be available at the specified address. |
@ -1,10 +0,0 @@ |
|||
namespace: uptime-kuma |
|||
namePrefix: uptime-kuma- |
|||
|
|||
commonLabels: |
|||
app: uptime-kuma |
|||
|
|||
bases: |
|||
- uptime-kuma |
|||
|
|||
|
@ -1,45 +0,0 @@ |
|||
apiVersion: apps/v1 |
|||
kind: Deployment |
|||
metadata: |
|||
labels: |
|||
component: uptime-kuma |
|||
name: deployment |
|||
spec: |
|||
selector: |
|||
matchLabels: |
|||
component: uptime-kuma |
|||
replicas: 1 |
|||
strategy: |
|||
type: Recreate |
|||
|
|||
template: |
|||
metadata: |
|||
labels: |
|||
component: uptime-kuma |
|||
spec: |
|||
containers: |
|||
- name: app |
|||
image: louislam/uptime-kuma:1 |
|||
ports: |
|||
- containerPort: 3001 |
|||
volumeMounts: |
|||
- mountPath: /app/data |
|||
name: storage |
|||
livenessProbe: |
|||
exec: |
|||
command: |
|||
- node |
|||
- extra/healthcheck.js |
|||
initialDelaySeconds: 180 |
|||
periodSeconds: 60 |
|||
timeoutSeconds: 30 |
|||
readinessProbe: |
|||
httpGet: |
|||
path: / |
|||
port: 3001 |
|||
scheme: HTTP |
|||
|
|||
volumes: |
|||
- name: storage |
|||
persistentVolumeClaim: |
|||
claimName: pvc |
@ -1,39 +0,0 @@ |
|||
apiVersion: networking.k8s.io/v1 |
|||
kind: Ingress |
|||
metadata: |
|||
annotations: |
|||
kubernetes.io/ingress.class: nginx |
|||
cert-manager.io/cluster-issuer: letsencrypt-prod |
|||
nginx.ingress.kubernetes.io/proxy-read-timeout: "3600" |
|||
nginx.ingress.kubernetes.io/proxy-send-timeout: "3600" |
|||
nginx.ingress.kubernetes.io/server-snippets: | |
|||
location / { |
|||
proxy_set_header Upgrade $http_upgrade; |
|||
proxy_http_version 1.1; |
|||
proxy_set_header X-Forwarded-Host $http_host; |
|||
proxy_set_header X-Forwarded-Proto $scheme; |
|||
proxy_set_header X-Forwarded-For $remote_addr; |
|||
proxy_set_header Host $host; |
|||
proxy_set_header Connection "upgrade"; |
|||
proxy_set_header X-Real-IP $remote_addr; |
|||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
|||
proxy_set_header Upgrade $http_upgrade; |
|||
proxy_cache_bypass $http_upgrade; |
|||
} |
|||
name: ingress |
|||
spec: |
|||
tls: |
|||
- hosts: |
|||
- example.com |
|||
secretName: example-com-tls |
|||
rules: |
|||
- host: example.com |
|||
http: |
|||
paths: |
|||
- path: / |
|||
pathType: Prefix |
|||
backend: |
|||
service: |
|||
name: service |
|||
port: |
|||
number: 3001 |
@ -1,5 +0,0 @@ |
|||
resources: |
|||
- deployment.yml |
|||
- service.yml |
|||
- ingressroute.yml |
|||
- pvc.yml |
@ -1,10 +0,0 @@ |
|||
apiVersion: v1 |
|||
kind: PersistentVolumeClaim |
|||
metadata: |
|||
name: pvc |
|||
spec: |
|||
accessModes: |
|||
- ReadWriteOnce |
|||
resources: |
|||
requests: |
|||
storage: 4Gi |
@ -1,13 +0,0 @@ |
|||
apiVersion: v1 |
|||
kind: Service |
|||
metadata: |
|||
name: service |
|||
spec: |
|||
selector: |
|||
component: uptime-kuma |
|||
type: ClusterIP |
|||
ports: |
|||
- name: http |
|||
port: 3001 |
|||
targetPort: 3001 |
|||
protocol: TCP |
File diff suppressed because it is too large
@ -0,0 +1,7 @@ |
|||
const args = require("args-parser")(process.argv); |
|||
const demoMode = args["demo"] || false; |
|||
|
|||
module.exports = { |
|||
args, |
|||
demoMode |
|||
}; |
@ -0,0 +1,31 @@ |
|||
const path = require("path"); |
|||
const Bree = require("bree"); |
|||
const { SHARE_ENV } = require("worker_threads"); |
|||
|
|||
const jobs = [ |
|||
{ |
|||
name: "clear-old-data", |
|||
interval: "at 03:14", |
|||
} |
|||
]; |
|||
|
|||
const initBackgroundJobs = function (args) { |
|||
const bree = new Bree({ |
|||
root: path.resolve("server", "jobs"), |
|||
jobs, |
|||
worker: { |
|||
env: SHARE_ENV, |
|||
workerData: args, |
|||
}, |
|||
workerMessageHandler: (message) => { |
|||
console.log("[Background Job]:", message); |
|||
} |
|||
}); |
|||
|
|||
bree.start(); |
|||
return bree; |
|||
}; |
|||
|
|||
module.exports = { |
|||
initBackgroundJobs |
|||
}; |
@ -0,0 +1,40 @@ |
|||
const { log, exit, connectDb } = require("./util-worker"); |
|||
const { R } = require("redbean-node"); |
|||
const { setSetting, setting } = require("../util-server"); |
|||
|
|||
const DEFAULT_KEEP_PERIOD = 180; |
|||
|
|||
(async () => { |
|||
await connectDb(); |
|||
|
|||
let period = await setting("keepDataPeriodDays"); |
|||
|
|||
// Set Default Period
|
|||
if (period == null) { |
|||
await setSetting("keepDataPeriodDays", DEFAULT_KEEP_PERIOD, "general"); |
|||
period = DEFAULT_KEEP_PERIOD; |
|||
} |
|||
|
|||
// Try parse setting
|
|||
let parsedPeriod; |
|||
try { |
|||
parsedPeriod = parseInt(period); |
|||
} catch (_) { |
|||
log("Failed to parse setting, resetting to default.."); |
|||
await setSetting("keepDataPeriodDays", DEFAULT_KEEP_PERIOD, "general"); |
|||
parsedPeriod = DEFAULT_KEEP_PERIOD; |
|||
} |
|||
|
|||
log(`Clearing Data older than ${parsedPeriod} days...`); |
|||
|
|||
try { |
|||
await R.exec( |
|||
"DELETE FROM heartbeat WHERE time < DATETIME('now', '-' || ? || ' days') ", |
|||
[parsedPeriod] |
|||
); |
|||
} catch (e) { |
|||
log(`Failed to clear old data: ${e.message}`); |
|||
} |
|||
|
|||
exit(); |
|||
})(); |
@ -0,0 +1,39 @@ |
|||
const { parentPort, workerData } = require("worker_threads"); |
|||
const Database = require("../database"); |
|||
const path = require("path"); |
|||
|
|||
const log = function (any) { |
|||
if (parentPort) { |
|||
parentPort.postMessage(any); |
|||
} |
|||
}; |
|||
|
|||
const exit = function (error) { |
|||
if (error && error != 0) { |
|||
process.exit(error); |
|||
} else { |
|||
if (parentPort) { |
|||
parentPort.postMessage("done"); |
|||
} else { |
|||
process.exit(0); |
|||
} |
|||
} |
|||
}; |
|||
|
|||
const connectDb = async function () { |
|||
const dbPath = path.join( |
|||
process.env.DATA_DIR || workerData["data-dir"] || "./data/" |
|||
); |
|||
|
|||
Database.init({ |
|||
"data-dir": dbPath, |
|||
}); |
|||
|
|||
await Database.connect(); |
|||
}; |
|||
|
|||
module.exports = { |
|||
log, |
|||
exit, |
|||
connectDb, |
|||
}; |
@ -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,167 @@ |
|||
"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.getCryptoRandomInt = 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; |
|||
/** |
|||
* Returns either the NodeJS crypto.randomBytes() function or its |
|||
* browser equivalent implemented via window.crypto.getRandomValues() |
|||
*/ |
|||
let getRandomBytes = ((typeof window !== 'undefined' && window.crypto) |
|||
// Browsers
|
|||
? function () { |
|||
return (numBytes) => { |
|||
let randomBytes = new Uint8Array(numBytes); |
|||
for (let i = 0; i < numBytes; i += 65536) { |
|||
window.crypto.getRandomValues(randomBytes.subarray(i, i + Math.min(numBytes - i, 65536))); |
|||
} |
|||
return randomBytes; |
|||
}; |
|||
} |
|||
// Node
|
|||
: function () { |
|||
return require("crypto").randomBytes; |
|||
})(); |
|||
function getCryptoRandomInt(min, max) { |
|||
// synchronous version of: https://github.com/joepie91/node-random-number-csprng
|
|||
const range = max - min; |
|||
if (range >= Math.pow(2, 32)) |
|||
console.log("Warning! Range is too large."); |
|||
let tmpRange = range; |
|||
let bitsNeeded = 0; |
|||
let bytesNeeded = 0; |
|||
let mask = 1; |
|||
while (tmpRange > 0) { |
|||
if (bitsNeeded % 8 === 0) |
|||
bytesNeeded += 1; |
|||
bitsNeeded += 1; |
|||
mask = mask << 1 | 1; |
|||
tmpRange = tmpRange >>> 1; |
|||
} |
|||
const randomBytes = getRandomBytes(bytesNeeded); |
|||
let randomValue = 0; |
|||
for (let i = 0; i < bytesNeeded; i++) { |
|||
randomValue |= randomBytes[i] << 8 * i; |
|||
} |
|||
randomValue = randomValue & mask; |
|||
if (randomValue <= range) { |
|||
return min + randomValue; |
|||
} |
|||
else { |
|||
return getCryptoRandomInt(min, max); |
|||
} |
|||
} |
|||
exports.getCryptoRandomInt = getCryptoRandomInt; |
|||
function genSecret(length = 64) { |
|||
let secret = ""; |
|||
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
|||
const charsLength = chars.length; |
|||
for (let i = 0; i < length; i++) { |
|||
secret += chars.charAt(getCryptoRandomInt(0, charsLength - 1)); |
|||
} |
|||
return secret; |
|||
} |
|||
exports.genSecret = genSecret; |
|||
function getMonitorRelativeURL(id) { |
|||
return "/dashboard/" + id; |
|||
} |
|||
exports.getMonitorRelativeURL = getMonitorRelativeURL; |
|||
|
@ -1,10 +1,44 @@ |
|||
beforeAll(() => { |
|||
const { genSecret, sleep } = require("../src/util"); |
|||
|
|||
}); |
|||
describe("Test genSecret", () => { |
|||
|
|||
beforeAll(() => { |
|||
|
|||
}); |
|||
|
|||
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); |
|||
|
|||
describe("", () => { |
|||
secret = genSecret(2); |
|||
expect(secret.length).toEqual(2); |
|||
|
|||
it("should ", () => { |
|||
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"); |
|||
}); |
|||
|
|||
}); |
|||
|
|||
describe("Test reset-password", () => { |
|||
it("should able to run", async () => { |
|||
await require("../extra/reset-password").main(); |
|||
}, 120000); |
|||
}); |
|||
|
Loading…
Reference in new issue