This repository has been archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtil.js
134 lines (109 loc) · 4.21 KB
/
Util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const config = require('./config.json');
const Webhook = require('discord-webhooks');
class Util {
constructor() {
throw new Error('This class cannot be instantiated!');
}
static get config() {
return config;
}
/**
* Log to a webhook
* @param {string} message
*/
static log(message) {
if (!message) return false;
console.log(message);
let url = process.env.LOG_WEBHOOK_URL;
if (!url) return false;
url = url.replace('https://discordapp.com/api/webhooks/', '').replace('https://discord.com/api/webhooks/', '');
let split = url.split('/');
if (split.length < 2) return false;
let client = new Webhook(split[0], split[1]);
if (typeof(message) == 'string') client.send(message, { avatar: Util.config.avatar, username: 'Express-Logs' });
else client.send({ embeds: [message], avatar: Util.config.avatar, username: 'Express-Logs' });
return true;
}
static get HTTP_Codes() {
return {
101: '101: Switching Protocols',
200: '200: OK',
201: '201: Created',
204: '204: No Content',
301: '301: Moved Permanently',
303: '303: See Other',
304: '304: Not Modified',
400: '400: Bad Request',
401: '401: Unauthorized',
403: '403: Forbidden',
404: '404: Not Found',
405: '405: Method Not Allowed',
406: '406: Not Acceptable',
409: '409: Conflict',
429: '429: Too Many Requests',
500: '500: Internal Server Error',
501: '501: Not Implemented',
502: '502: Bad Gateway',
503: '503: Service Unavailable'
};
}
/**
* @param {Response} res
* @param {Number} code
* @param {Object} object
*/
static SendResponse(res, code, obj = null, pretty = true) {
if (!res || !code) throw new Error('Invalid Args');
if (!(code in Util.HTTP_Codes)) throw new Error(code + ' is not a valid HTTP status code');
if (obj == null || obj == undefined) {
return res.status(code).set('Content-Type', 'application/json').send(JSON.stringify({code: code, message: Util.HTTP_Codes[code]}));
}
return res.status(code).set('Content-Type', 'application/json').send(JSON.stringify(obj, null, pretty ? 2 : 0));
}
static GetJSON(str) {
if (!str || typeof(str) != 'string') return null;
try { return JSON.parse(str); }
catch (ex) { return null; }
}
static GenerateSnowflake() {
let rv = '';
const possible = '1234567890';
for (let i = 0; i < 20; i++) rv += possible.charAt(Math.floor(Math.random() * possible.length));
return rv;
}
/**
* @returns {Promise<number>}
* @param {string} host
*/
static GetCertExpirationDays(host) {
return new Promise((resolve, reject) => {
if (!host) return reject('No Host');
if (host.startsWith('http://')) return reject('Host Uses HTTP');
if (host.startsWith('https://')) host = host.replace('https://', '');
let port = 443;
if (host.includes(':')) {
let split = host.split(':');
if (split.length > 1) {
host = split[0];
if (!isNaN(split[split.length - 1])) port = Number(split[split.length - 1]);
}
}
let client = require('tls').connect(port, {host: host, timeout: 5e3});
client.on('error', error => reject(error));
client.on('timeout', () => reject(new Error('Host Timed Out')));
client.on('session', () => {
let valid_to = new Date(client.getPeerCertificate().valid_to);
let days = (valid_to - Date.now()) / (1000 * 3600 * 24);
client.end();
resolve(days);
});
});
}
static IPFromRequest(req) {
let IP = req.ip;
if (!IP) return 'MISSING IP';
IP = IP.replace('::ffff:', '').replace('::1', '');
return !IP ? '127.0.0.1' : IP;
}
}
module.exports = Util;