vortez 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitignore +13 -0
- package/LICENSE +201 -0
- package/Notes.md +12 -0
- package/README.md +372 -0
- package/build/Beta/JwtManager.d.ts +114 -0
- package/build/Beta/JwtManager.js +249 -0
- package/build/Beta/JwtManager.js.map +1 -0
- package/build/Beta/Mail.d.ts +60 -0
- package/build/Beta/Mail.js +192 -0
- package/build/Beta/Mail.js.map +1 -0
- package/build/Config.d.ts +39 -0
- package/build/Config.js +33 -0
- package/build/Config.js.map +1 -0
- package/build/ConsoleUI.d.ts +57 -0
- package/build/ConsoleUI.js +110 -0
- package/build/ConsoleUI.js.map +1 -0
- package/build/Debug.d.ts +154 -0
- package/build/Debug.js +256 -0
- package/build/Debug.js.map +1 -0
- package/build/LoggerManager/Logger.d.ts +23 -0
- package/build/LoggerManager/Logger.js +23 -0
- package/build/LoggerManager/Logger.js.map +1 -0
- package/build/LoggerManager/LoggerManager.d.ts +18 -0
- package/build/LoggerManager/LoggerManager.js +30 -0
- package/build/LoggerManager/LoggerManager.js.map +1 -0
- package/build/Server/BodyParser.d.ts +125 -0
- package/build/Server/BodyParser.js +162 -0
- package/build/Server/BodyParser.js.map +1 -0
- package/build/Server/Cookie.d.ts +72 -0
- package/build/Server/Cookie.js +102 -0
- package/build/Server/Cookie.js.map +1 -0
- package/build/Server/Request.d.ts +61 -0
- package/build/Server/Request.js +79 -0
- package/build/Server/Request.js.map +1 -0
- package/build/Server/Response.d.ts +90 -0
- package/build/Server/Response.js +241 -0
- package/build/Server/Response.js.map +1 -0
- package/build/Server/Rule.d.ts +81 -0
- package/build/Server/Rule.js +146 -0
- package/build/Server/Rule.js.map +1 -0
- package/build/Server/Server.d.ts +157 -0
- package/build/Server/Server.js +330 -0
- package/build/Server/Server.js.map +1 -0
- package/build/Server/Session.d.ts +66 -0
- package/build/Server/Session.js +97 -0
- package/build/Server/Session.js.map +1 -0
- package/build/Server/WebSocket/Chunk.d.ts +36 -0
- package/build/Server/WebSocket/Chunk.js +81 -0
- package/build/Server/WebSocket/Chunk.js.map +1 -0
- package/build/Server/WebSocket/WebSocket.d.ts +70 -0
- package/build/Server/WebSocket/WebSocket.js +184 -0
- package/build/Server/WebSocket/WebSocket.js.map +1 -0
- package/build/Template.d.ts +32 -0
- package/build/Template.js +69 -0
- package/build/Template.js.map +1 -0
- package/build/Utilities/Env.d.ts +75 -0
- package/build/Utilities/Env.js +123 -0
- package/build/Utilities/Env.js.map +1 -0
- package/build/Utilities/Path.d.ts +18 -0
- package/build/Utilities/Path.js +27 -0
- package/build/Utilities/Path.js.map +1 -0
- package/build/Utilities/Utilities.d.ts +147 -0
- package/build/Utilities/Utilities.js +110 -0
- package/build/Utilities/Utilities.js.map +1 -0
- package/build/Vortez.d.ts +20 -0
- package/build/Vortez.js +22 -0
- package/build/Vortez.js.map +1 -0
- package/changes.md +89 -0
- package/examples/in-docs.js +96 -0
- package/global/Source/Logo_960.png +0 -0
- package/global/Source/Logo_SM_960.png +0 -0
- package/global/Style/Template/Error.css +30 -0
- package/global/Style/Template/Folder.css +77 -0
- package/global/Style/Template/Template.css +128 -0
- package/global/Template/Error.vhtml +29 -0
- package/global/Template/Folder.vhtml +41 -0
- package/package.json +47 -0
- package/tests/Template/template.js +18 -0
- package/tests/Template/template.txt +13 -0
- package/tests/Template/template.vhtml +23 -0
- package/tests/debug.js +28 -0
- package/tests/jwtManager/jwtManager.js +110 -0
- package/tests/test.js +129 -0
- package/tests/test.vhtml +14 -0
- package/tests/utilities.js +28 -0
- package/tests/websocket.vhtml +86 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author NetFeez <codefeez.dev@gmail.com>
|
|
3
|
+
* @description add the json web token manager to the Vortez.
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
export declare class JwtManager {
|
|
7
|
+
private static validSignMethods;
|
|
8
|
+
private static validMaskMethods;
|
|
9
|
+
private static signMethodMap;
|
|
10
|
+
private static maskMethodMap;
|
|
11
|
+
private algorithm;
|
|
12
|
+
private signMethod;
|
|
13
|
+
private maskMethod;
|
|
14
|
+
private privKey;
|
|
15
|
+
private pubKey;
|
|
16
|
+
/**
|
|
17
|
+
* create a new jwt manager
|
|
18
|
+
* @param algorithm the algorithm to use
|
|
19
|
+
* @param key the private key or secret to use
|
|
20
|
+
* @throws an error if the algorithm is not valid
|
|
21
|
+
* @throws an error if the key/secret is not valid
|
|
22
|
+
*/
|
|
23
|
+
constructor(algorithm: JwtManager.jwtAlgorithms, key: string);
|
|
24
|
+
/**
|
|
25
|
+
* sign a jwt
|
|
26
|
+
* @param body the body of the jwt
|
|
27
|
+
* @param options the options to sign the jwt
|
|
28
|
+
* @returns the signed jwt
|
|
29
|
+
*/
|
|
30
|
+
sign(body: JwtManager.body, options?: JwtManager.signOptions): string;
|
|
31
|
+
/**
|
|
32
|
+
* verify a jwt
|
|
33
|
+
* @param jwt the jwt to verify
|
|
34
|
+
* @param options the options to verify the jwt
|
|
35
|
+
* @returns the verified jwt
|
|
36
|
+
*/
|
|
37
|
+
parse(jwt: string, options?: JwtManager.verifyOptions): JwtManager.jwtObject;
|
|
38
|
+
/**
|
|
39
|
+
* sign a jwt
|
|
40
|
+
* @param body the body of the jwt
|
|
41
|
+
* @param options the options to sign the jwt
|
|
42
|
+
* @returns the signed jwt
|
|
43
|
+
* @throws an error if the algorithm is not valid
|
|
44
|
+
* @throws an error if the key/secret is not valid
|
|
45
|
+
*/
|
|
46
|
+
static sign(body: JwtManager.body, algorithm: JwtManager.jwtAlgorithms, privKey: string, options: JwtManager.signOptions): string;
|
|
47
|
+
/**
|
|
48
|
+
* verify a jwt
|
|
49
|
+
* @param jwt the jwt to verify
|
|
50
|
+
* @param options the options to verify the jwt
|
|
51
|
+
* @returns the verified jwt
|
|
52
|
+
* @throws an error if the algorithm is not valid
|
|
53
|
+
* @throws an error if the key/secret is not valid
|
|
54
|
+
*/
|
|
55
|
+
static parse(jwt: string, pubKey: string, options?: JwtManager.verifyOptions): JwtManager.jwtObject;
|
|
56
|
+
/**
|
|
57
|
+
* validate an private key
|
|
58
|
+
* @param signMethod the sign method
|
|
59
|
+
* @param key the private key
|
|
60
|
+
* @param sendThrows whether to throw an error if the private key is invalid
|
|
61
|
+
* @returns true if the private key is valid
|
|
62
|
+
* @throws an error if the private key is invalid
|
|
63
|
+
*/
|
|
64
|
+
private static validateKey;
|
|
65
|
+
private static getPubKey;
|
|
66
|
+
private static getAlgorithmInfo;
|
|
67
|
+
}
|
|
68
|
+
export declare namespace JwtManager {
|
|
69
|
+
type signMethods = 'HMAC' | 'RSA' | 'ECDSA' | 'RSA-PSS';
|
|
70
|
+
type maskMethods = 'SHA256' | 'SHA384' | 'SHA512';
|
|
71
|
+
type jwtSignMethods = 'HS' | 'RS' | 'PS' | 'ES';
|
|
72
|
+
type jwtMaskMethods = '256' | '384' | '512';
|
|
73
|
+
type jwtAlgorithms = `${jwtSignMethods}${jwtMaskMethods}`;
|
|
74
|
+
interface signMethodMap {
|
|
75
|
+
HS: 'HMAC';
|
|
76
|
+
RS: 'RSA';
|
|
77
|
+
PS: 'RSA-PSS';
|
|
78
|
+
ES: 'ECDSA';
|
|
79
|
+
}
|
|
80
|
+
interface maskMethodMap {
|
|
81
|
+
'256': 'SHA256';
|
|
82
|
+
'384': 'SHA384';
|
|
83
|
+
'512': 'SHA512';
|
|
84
|
+
}
|
|
85
|
+
interface algorithmInfo {
|
|
86
|
+
signMethod?: signMethods;
|
|
87
|
+
maskMethod?: maskMethods;
|
|
88
|
+
}
|
|
89
|
+
interface headers {
|
|
90
|
+
exp?: number;
|
|
91
|
+
[key: string]: any | undefined;
|
|
92
|
+
}
|
|
93
|
+
interface head extends headers {
|
|
94
|
+
alg: jwtAlgorithms;
|
|
95
|
+
typ: 'JWT';
|
|
96
|
+
}
|
|
97
|
+
interface body {
|
|
98
|
+
[key: string]: any | undefined;
|
|
99
|
+
}
|
|
100
|
+
interface jwtObject {
|
|
101
|
+
body: body;
|
|
102
|
+
head: head;
|
|
103
|
+
signature: string;
|
|
104
|
+
verify: boolean;
|
|
105
|
+
}
|
|
106
|
+
interface verifyOptions {
|
|
107
|
+
expire?: boolean;
|
|
108
|
+
}
|
|
109
|
+
interface signOptions {
|
|
110
|
+
expire?: Date;
|
|
111
|
+
[key: string]: any | undefined;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
export default JwtManager;
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author NetFeez <codefeez.dev@gmail.com>
|
|
3
|
+
* @description add the json web token manager to the Vortez.
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import Utilities from "../Utilities/Utilities.js";
|
|
7
|
+
import CRYPTO from 'crypto';
|
|
8
|
+
export class JwtManager {
|
|
9
|
+
static validSignMethods = ['HS', 'RS', 'PS', 'ES'];
|
|
10
|
+
static validMaskMethods = ['256', '384', '512'];
|
|
11
|
+
static signMethodMap = {
|
|
12
|
+
HS: 'HMAC', RS: 'RSA', PS: 'RSA-PSS', ES: 'ECDSA'
|
|
13
|
+
};
|
|
14
|
+
static maskMethodMap = {
|
|
15
|
+
'256': 'SHA256', '384': 'SHA384', '512': 'SHA512'
|
|
16
|
+
};
|
|
17
|
+
algorithm;
|
|
18
|
+
signMethod;
|
|
19
|
+
maskMethod;
|
|
20
|
+
privKey;
|
|
21
|
+
pubKey;
|
|
22
|
+
/**
|
|
23
|
+
* create a new jwt manager
|
|
24
|
+
* @param algorithm the algorithm to use
|
|
25
|
+
* @param key the private key or secret to use
|
|
26
|
+
* @throws an error if the algorithm is not valid
|
|
27
|
+
* @throws an error if the key/secret is not valid
|
|
28
|
+
*/
|
|
29
|
+
constructor(algorithm, key) {
|
|
30
|
+
const { signMethod, maskMethod } = JwtManager.getAlgorithmInfo(algorithm);
|
|
31
|
+
if (!signMethod || !maskMethod)
|
|
32
|
+
throw new Error('the jwt algorithm method is not valid');
|
|
33
|
+
if (!JwtManager.validateKey('private', signMethod, key, true))
|
|
34
|
+
throw new Error('the key/secret is not valid');
|
|
35
|
+
this.algorithm = algorithm;
|
|
36
|
+
this.signMethod = signMethod;
|
|
37
|
+
this.maskMethod = maskMethod;
|
|
38
|
+
this.privKey = key;
|
|
39
|
+
this.pubKey = signMethod === 'HMAC' ? null : JwtManager.getPubKey(key);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* sign a jwt
|
|
43
|
+
* @param body the body of the jwt
|
|
44
|
+
* @param options the options to sign the jwt
|
|
45
|
+
* @returns the signed jwt
|
|
46
|
+
*/
|
|
47
|
+
sign(body, options = {}) {
|
|
48
|
+
return JwtManager.sign(body, this.algorithm, this.privKey, options);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* verify a jwt
|
|
52
|
+
* @param jwt the jwt to verify
|
|
53
|
+
* @param options the options to verify the jwt
|
|
54
|
+
* @returns the verified jwt
|
|
55
|
+
*/
|
|
56
|
+
parse(jwt, options = {}) {
|
|
57
|
+
return JwtManager.parse(jwt, this.pubKey ?? this.privKey, options);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* sign a jwt
|
|
61
|
+
* @param body the body of the jwt
|
|
62
|
+
* @param options the options to sign the jwt
|
|
63
|
+
* @returns the signed jwt
|
|
64
|
+
* @throws an error if the algorithm is not valid
|
|
65
|
+
* @throws an error if the key/secret is not valid
|
|
66
|
+
*/
|
|
67
|
+
static sign(body, algorithm, privKey, options) {
|
|
68
|
+
const { signMethod, maskMethod } = JwtManager.getAlgorithmInfo(algorithm);
|
|
69
|
+
if (!signMethod || !maskMethod)
|
|
70
|
+
throw new Error('the jwt algorithm is not valid');
|
|
71
|
+
if (!JwtManager.validateKey('private', signMethod, privKey))
|
|
72
|
+
throw new Error('the key/secret is not valid');
|
|
73
|
+
const head = {
|
|
74
|
+
alg: algorithm,
|
|
75
|
+
typ: 'JWT',
|
|
76
|
+
...options
|
|
77
|
+
};
|
|
78
|
+
const b64Body = Utilities.base64UrlEncode(JSON.stringify(body));
|
|
79
|
+
const b64Head = Utilities.base64UrlEncode(JSON.stringify(head));
|
|
80
|
+
const toSign = `${b64Head}.${b64Body}`;
|
|
81
|
+
switch (signMethod) {
|
|
82
|
+
case 'HMAC': {
|
|
83
|
+
const hmac = CRYPTO.createHmac(maskMethod, privKey);
|
|
84
|
+
hmac.update(toSign);
|
|
85
|
+
return toSign + '.' + hmac.digest('base64url');
|
|
86
|
+
}
|
|
87
|
+
case 'RSA': {
|
|
88
|
+
const signatureBuffer = CRYPTO.sign(maskMethod, Buffer.from(toSign), {
|
|
89
|
+
key: privKey,
|
|
90
|
+
padding: CRYPTO.constants.RSA_PKCS1_PADDING
|
|
91
|
+
});
|
|
92
|
+
return toSign + '.' + signatureBuffer.toString('base64url');
|
|
93
|
+
}
|
|
94
|
+
case 'RSA-PSS': {
|
|
95
|
+
const signatureBuffer = CRYPTO.sign(maskMethod, Buffer.from(toSign), {
|
|
96
|
+
key: privKey,
|
|
97
|
+
padding: CRYPTO.constants.RSA_PKCS1_PSS_PADDING,
|
|
98
|
+
saltLength: CRYPTO.constants.RSA_PSS_SALTLEN_DIGEST
|
|
99
|
+
});
|
|
100
|
+
return toSign + '.' + signatureBuffer.toString('base64url');
|
|
101
|
+
}
|
|
102
|
+
case 'ECDSA': {
|
|
103
|
+
const signatureBuffer = CRYPTO.sign(maskMethod, Buffer.from(toSign), {
|
|
104
|
+
key: privKey,
|
|
105
|
+
dsaEncoding: 'ieee-p1363'
|
|
106
|
+
});
|
|
107
|
+
return toSign + '.' + signatureBuffer.toString('base64url');
|
|
108
|
+
}
|
|
109
|
+
default: {
|
|
110
|
+
throw new Error('the sign method is not valid');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* verify a jwt
|
|
116
|
+
* @param jwt the jwt to verify
|
|
117
|
+
* @param options the options to verify the jwt
|
|
118
|
+
* @returns the verified jwt
|
|
119
|
+
* @throws an error if the algorithm is not valid
|
|
120
|
+
* @throws an error if the key/secret is not valid
|
|
121
|
+
*/
|
|
122
|
+
static parse(jwt, pubKey, options = {}) {
|
|
123
|
+
const [b64Head, b64Body, signature] = jwt.split('.');
|
|
124
|
+
const head = JSON.parse(Utilities.base64UrlDecode(b64Head));
|
|
125
|
+
const body = JSON.parse(Utilities.base64UrlDecode(b64Body));
|
|
126
|
+
const { signMethod, maskMethod } = JwtManager.getAlgorithmInfo(head.alg);
|
|
127
|
+
if (!signMethod || !maskMethod)
|
|
128
|
+
throw new Error('the jwt algorithm is not valid');
|
|
129
|
+
if (!JwtManager.validateKey('public', signMethod, pubKey))
|
|
130
|
+
throw new Error('the key/secret is not valid');
|
|
131
|
+
const toVerify = `${b64Head}.${b64Body}`;
|
|
132
|
+
const result = {
|
|
133
|
+
body: body, head: head,
|
|
134
|
+
signature: signature, verify: false
|
|
135
|
+
};
|
|
136
|
+
switch (signMethod) {
|
|
137
|
+
case 'HMAC': {
|
|
138
|
+
const hmac = CRYPTO.createHmac(maskMethod, pubKey);
|
|
139
|
+
hmac.update(toVerify);
|
|
140
|
+
result.verify = hmac.digest('base64url') === signature;
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
case 'RSA': {
|
|
144
|
+
result.verify = CRYPTO.verify(maskMethod, Buffer.from(toVerify), {
|
|
145
|
+
key: pubKey,
|
|
146
|
+
padding: CRYPTO.constants.RSA_PKCS1_PADDING,
|
|
147
|
+
}, Buffer.from(signature, 'base64url'));
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
case 'RSA-PSS': {
|
|
151
|
+
result.verify = CRYPTO.verify(maskMethod, Buffer.from(toVerify), {
|
|
152
|
+
key: pubKey,
|
|
153
|
+
padding: CRYPTO.constants.RSA_PKCS1_PSS_PADDING,
|
|
154
|
+
saltLength: CRYPTO.constants.RSA_PSS_SALTLEN_DIGEST
|
|
155
|
+
}, Buffer.from(signature, 'base64url'));
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
case 'ECDSA': {
|
|
159
|
+
result.verify = CRYPTO.verify(maskMethod, Buffer.from(toVerify), {
|
|
160
|
+
key: pubKey,
|
|
161
|
+
dsaEncoding: 'ieee-p1363'
|
|
162
|
+
}, Buffer.from(signature, 'base64url'));
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
default: {
|
|
166
|
+
throw new Error('the sign method is not valid');
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return result;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* validate an private key
|
|
173
|
+
* @param signMethod the sign method
|
|
174
|
+
* @param key the private key
|
|
175
|
+
* @param sendThrows whether to throw an error if the private key is invalid
|
|
176
|
+
* @returns true if the private key is valid
|
|
177
|
+
* @throws an error if the private key is invalid
|
|
178
|
+
*/
|
|
179
|
+
static validateKey(type = 'private', signMethod, key, sendThrows = false) {
|
|
180
|
+
switch (signMethod) {
|
|
181
|
+
case 'HMAC': {
|
|
182
|
+
if (key.length < 6) {
|
|
183
|
+
if (sendThrows)
|
|
184
|
+
throw new Error('the secret must be at least 6 characters');
|
|
185
|
+
else
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
else
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
case 'RSA': {
|
|
192
|
+
const keyObject = type == 'public' ? CRYPTO.createPublicKey({ key }) : CRYPTO.createPrivateKey({ key });
|
|
193
|
+
if (keyObject.asymmetricKeyType !== 'rsa'
|
|
194
|
+
|| keyObject.type !== type) {
|
|
195
|
+
if (sendThrows)
|
|
196
|
+
throw new Error('the key must be a valid RSA key');
|
|
197
|
+
else
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
else
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
case 'RSA-PSS': {
|
|
204
|
+
const keyObject = type == 'public' ? CRYPTO.createPublicKey({ key }) : CRYPTO.createPrivateKey({ key });
|
|
205
|
+
if (keyObject.asymmetricKeyType !== 'rsa-pss'
|
|
206
|
+
|| keyObject.type !== type) {
|
|
207
|
+
if (sendThrows)
|
|
208
|
+
throw new Error('the key must be a valid RSA-PSS key');
|
|
209
|
+
else
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
else
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
case 'ECDSA': {
|
|
216
|
+
const keyObject = type == 'public' ? CRYPTO.createPublicKey({ key }) : CRYPTO.createPrivateKey({ key });
|
|
217
|
+
if (keyObject.asymmetricKeyType !== 'ec'
|
|
218
|
+
|| keyObject.type !== type) {
|
|
219
|
+
if (sendThrows)
|
|
220
|
+
throw new Error('the key must be a valid ECDSA key');
|
|
221
|
+
else
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
else
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
default: {
|
|
228
|
+
if (sendThrows)
|
|
229
|
+
throw new Error('the sign method is not valid');
|
|
230
|
+
else
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
static getPubKey(privKey) {
|
|
236
|
+
const keyObject = CRYPTO.createPublicKey(privKey.replace(/\\n/g, '\n'));
|
|
237
|
+
return keyObject.export({ format: 'pem', type: 'spki' }).toString();
|
|
238
|
+
}
|
|
239
|
+
static getAlgorithmInfo(algorithm) {
|
|
240
|
+
const signMethod = algorithm.slice(0, 2);
|
|
241
|
+
const maskMethod = algorithm.slice(2, 5);
|
|
242
|
+
return {
|
|
243
|
+
signMethod: JwtManager.signMethodMap[signMethod],
|
|
244
|
+
maskMethod: JwtManager.maskMethodMap[maskMethod]
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
export default JwtManager;
|
|
249
|
+
//# sourceMappingURL=JwtManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JwtManager.js","sour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AC,KAAK,CAAC,CAAC,EAAC,CAAC,CAA8B,CAAC;QACrE,OAAO;YACH,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,UAAU,CAAC;YAChD,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,UAAU,CAAC;SACnD,CAAA;IACL,CAAC;;AAkDL,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file
|
|
3
|
+
* @author NetFeez <codefeez.dev@gmail.com>
|
|
4
|
+
* @description add the mail sender to the Vortez.
|
|
5
|
+
* @license Apache-2.0
|
|
6
|
+
*/
|
|
7
|
+
import Debug from '../Debug.js';
|
|
8
|
+
export declare const $Mail: Debug;
|
|
9
|
+
export declare class Mail {
|
|
10
|
+
private host;
|
|
11
|
+
private port;
|
|
12
|
+
private secure;
|
|
13
|
+
private useStartTLS;
|
|
14
|
+
private username;
|
|
15
|
+
private password;
|
|
16
|
+
private email;
|
|
17
|
+
/**
|
|
18
|
+
* create a new mail sender
|
|
19
|
+
* @param Options the options to create the mail sender
|
|
20
|
+
*/
|
|
21
|
+
constructor(Options: Mail.CreateOptions);
|
|
22
|
+
/**
|
|
23
|
+
* connect to the mail server
|
|
24
|
+
* @returns the socket connection
|
|
25
|
+
* @throws an error if the connection fails
|
|
26
|
+
* @returns a promise that resolves to the socket connection
|
|
27
|
+
* @private
|
|
28
|
+
*/
|
|
29
|
+
private connect;
|
|
30
|
+
/**
|
|
31
|
+
* send a mail
|
|
32
|
+
* @param destination the destination of the mail
|
|
33
|
+
* @param subject the subject of the mail
|
|
34
|
+
* @param content the content of the mail
|
|
35
|
+
* @param options the options of the mail
|
|
36
|
+
* @returns a promise that resolves to true if the mail was sent successfully
|
|
37
|
+
* @throws an error if the mail could not be sent
|
|
38
|
+
*/
|
|
39
|
+
send(destination: string, subject: string, content: string, options?: Mail.SendOptions): Promise<boolean>;
|
|
40
|
+
}
|
|
41
|
+
export declare namespace Mail {
|
|
42
|
+
type CreateOptions = {
|
|
43
|
+
email?: string;
|
|
44
|
+
host: string;
|
|
45
|
+
password: string;
|
|
46
|
+
port: number;
|
|
47
|
+
secure?: boolean;
|
|
48
|
+
username: string;
|
|
49
|
+
useStartTLS?: boolean;
|
|
50
|
+
};
|
|
51
|
+
type File = {
|
|
52
|
+
name: string;
|
|
53
|
+
path: string;
|
|
54
|
+
};
|
|
55
|
+
type SendOptions = {
|
|
56
|
+
isHtml?: boolean;
|
|
57
|
+
files?: File[];
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export default Mail;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file
|
|
3
|
+
* @author NetFeez <codefeez.dev@gmail.com>
|
|
4
|
+
* @description add the mail sender to the Vortez.
|
|
5
|
+
* @license Apache-2.0
|
|
6
|
+
*/
|
|
7
|
+
import Debug from '../Debug.js';
|
|
8
|
+
import TLS from 'tls';
|
|
9
|
+
import NET from 'net';
|
|
10
|
+
import FS from 'fs';
|
|
11
|
+
import CRYPTO from 'crypto';
|
|
12
|
+
export const $Mail = Debug.getInstance('beta.mail', { path: '.debug-beta' });
|
|
13
|
+
export class Mail {
|
|
14
|
+
host;
|
|
15
|
+
port;
|
|
16
|
+
secure;
|
|
17
|
+
useStartTLS;
|
|
18
|
+
username;
|
|
19
|
+
password;
|
|
20
|
+
email;
|
|
21
|
+
/**
|
|
22
|
+
* create a new mail sender
|
|
23
|
+
* @param Options the options to create the mail sender
|
|
24
|
+
*/
|
|
25
|
+
constructor(Options) {
|
|
26
|
+
this.host = Options.host;
|
|
27
|
+
this.port = Options.port;
|
|
28
|
+
this.secure = Options.secure ?? false;
|
|
29
|
+
this.useStartTLS = Options.useStartTLS ?? false;
|
|
30
|
+
this.username = Buffer.from(Options.username).toString('base64');
|
|
31
|
+
this.password = Buffer.from(Options.password).toString('base64');
|
|
32
|
+
this.email = Options.email ?? `${Options.username}@${Options.host}`;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* connect to the mail server
|
|
36
|
+
* @returns the socket connection
|
|
37
|
+
* @throws an error if the connection fails
|
|
38
|
+
* @returns a promise that resolves to the socket connection
|
|
39
|
+
* @private
|
|
40
|
+
*/
|
|
41
|
+
async connect() {
|
|
42
|
+
try {
|
|
43
|
+
return await new Promise((resolve, reject) => {
|
|
44
|
+
const sendSocket = (socket) => {
|
|
45
|
+
const listenData = (data) => {
|
|
46
|
+
const message = data.toString().trim();
|
|
47
|
+
if (/235.+?Authentication successful$/g.test(message)) {
|
|
48
|
+
$Mail.log('Authentication successful.');
|
|
49
|
+
socket.off('error', reject);
|
|
50
|
+
socket.off('data', listenData);
|
|
51
|
+
resolve(socket);
|
|
52
|
+
}
|
|
53
|
+
else if (/535.+?Error: authentication failed.+?$/g.test(message)) {
|
|
54
|
+
$Mail.log('Authentication failed.');
|
|
55
|
+
socket.write('QUIT\r\n');
|
|
56
|
+
reject(new Error('Authentication error, please check your credentials.'));
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
$Mail.log('Starting authentication.');
|
|
60
|
+
socket.on('data', listenData);
|
|
61
|
+
socket.write(`EHLO ${this.host}\r\n`);
|
|
62
|
+
socket.write(`AUTH LOGIN\r\n`);
|
|
63
|
+
socket.write(`${this.username}\r\n`);
|
|
64
|
+
socket.write(`${this.password}\r\n`);
|
|
65
|
+
};
|
|
66
|
+
if (!this.secure) {
|
|
67
|
+
const socket = NET.connect({ host: this.host, port: this.port }, () => {
|
|
68
|
+
$Mail.log('Connection established.');
|
|
69
|
+
sendSocket(socket);
|
|
70
|
+
});
|
|
71
|
+
socket.on('error', reject);
|
|
72
|
+
socket.on('data', (data) => $Mail.log(`[<] ${data.toString().trim()}`));
|
|
73
|
+
socket.on('end', () => $Mail.log('Connection ended'));
|
|
74
|
+
}
|
|
75
|
+
else if (this.secure && !this.useStartTLS) {
|
|
76
|
+
const secureSocket = TLS.connect({ host: this.host, port: this.port }, () => {
|
|
77
|
+
$Mail.log('Connection established.');
|
|
78
|
+
sendSocket(secureSocket);
|
|
79
|
+
});
|
|
80
|
+
secureSocket.on('error', reject);
|
|
81
|
+
secureSocket.on('data', (data) => $Mail.log(`[<] ${data.toString().trim()}`));
|
|
82
|
+
secureSocket.on('end', () => $Mail.log('Connection ended'));
|
|
83
|
+
}
|
|
84
|
+
else if (this.secure && this.useStartTLS) {
|
|
85
|
+
const socket = NET.connect({ host: this.host, port: this.port }, () => {
|
|
86
|
+
$Mail.log('Connection established.');
|
|
87
|
+
$Mail.log('Switching to SSL-TLS.');
|
|
88
|
+
socket.write('STARTTLS\r\n');
|
|
89
|
+
});
|
|
90
|
+
socket.on('data', (data) => {
|
|
91
|
+
const message = data.toString().trim();
|
|
92
|
+
$Mail.log(`[<] ${message}`);
|
|
93
|
+
if (/^220.+?Ready to start TLS$/g.test(message)) {
|
|
94
|
+
const secureSocket = TLS.connect({ socket }, () => {
|
|
95
|
+
$Mail.log('SSL-TLS connection established.');
|
|
96
|
+
sendSocket(secureSocket);
|
|
97
|
+
});
|
|
98
|
+
socket.on('error', reject);
|
|
99
|
+
secureSocket.on('data', (data) => $Mail.log(`[<] ${data.toString().trim()}`));
|
|
100
|
+
secureSocket.on('end', () => $Mail.log('Connection ended'));
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
socket.on('error', reject);
|
|
104
|
+
socket.on('end', () => $Mail.log('Connection ended'));
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
throw new Error(`Failed to connect: ${error instanceof Error ? error.message : error}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* send a mail
|
|
114
|
+
* @param destination the destination of the mail
|
|
115
|
+
* @param subject the subject of the mail
|
|
116
|
+
* @param content the content of the mail
|
|
117
|
+
* @param options the options of the mail
|
|
118
|
+
* @returns a promise that resolves to true if the mail was sent successfully
|
|
119
|
+
* @throws an error if the mail could not be sent
|
|
120
|
+
*/
|
|
121
|
+
send(destination, subject, content, options = {}) {
|
|
122
|
+
if (options.files && !Array.isArray(options.files))
|
|
123
|
+
throw new Error('`options.files` must be an array.');
|
|
124
|
+
return new Promise((Resolve, Reject) => {
|
|
125
|
+
this.connect().then((socket) => {
|
|
126
|
+
function listenData(data) {
|
|
127
|
+
const message = data.toString().trim();
|
|
128
|
+
if (/250.+?Ok: queued as./g.test(message)) {
|
|
129
|
+
$Mail.log('Correo enviado.');
|
|
130
|
+
socket.off('data', listenData);
|
|
131
|
+
Resolve(true);
|
|
132
|
+
}
|
|
133
|
+
else if (socket.closed) {
|
|
134
|
+
$Mail.log('Correo no enviado.');
|
|
135
|
+
Reject('No se pudo enviar el correo.');
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
$Mail.log('Enviando correo.');
|
|
139
|
+
socket.on('data', listenData);
|
|
140
|
+
//Información del correo
|
|
141
|
+
socket.write(`MAIL FROM: <${this.email}>\r\n`);
|
|
142
|
+
socket.write(`RCPT TO: <${destination}>\r\n`);
|
|
143
|
+
socket.write('DATA\r\n');
|
|
144
|
+
socket.write(`Subject: ${subject}\r\n`);
|
|
145
|
+
socket.write(`FROM: ${this.email}\r\n`);
|
|
146
|
+
socket.write(`TO: ${destination}\r\n`);
|
|
147
|
+
if (!options.files || options.files.length < 1) {
|
|
148
|
+
//Contenido del correo
|
|
149
|
+
socket.write(`Content-Type: ${options.isHtml ? 'text/html' : 'text/plain'}; charset=utf-8\r\n`);
|
|
150
|
+
socket.write(`${content}\r\n`);
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
let Separador = CRYPTO.randomBytes(32).toString('hex');
|
|
154
|
+
//Definir que se enviara como multipart
|
|
155
|
+
socket.write('MIME-Version: 1.0\r\n');
|
|
156
|
+
socket.write(`Content-Type: multipart/mixed; boundary=${Separador}\n\r`);
|
|
157
|
+
socket.write('\r\n');
|
|
158
|
+
//Contenido del correo
|
|
159
|
+
socket.write(`--${Separador}\r\n`);
|
|
160
|
+
socket.write(`Content-Type: ${options.isHtml ? 'text/html' : 'text/plain'}; charset=utf-8\r\n`);
|
|
161
|
+
socket.write('Content-Disposition: inline\r\n');
|
|
162
|
+
socket.write('\r\n');
|
|
163
|
+
socket.write(`${content}\r\n`);
|
|
164
|
+
socket.write('\r\n');
|
|
165
|
+
/*
|
|
166
|
+
const files = options.Files ? options.Files.map((File) => {
|
|
167
|
+
const Content = FS.readFileSync(File.Path);
|
|
168
|
+
$Mail.log(`Archivo adjunto: ${File.Name}`);
|
|
169
|
+
return { name: File.Name, content: Content.toString('base64') };
|
|
170
|
+
}) : [];
|
|
171
|
+
*/
|
|
172
|
+
//Archivos adjuntos
|
|
173
|
+
options.files.forEach((file, Pos) => {
|
|
174
|
+
socket.write(`--${Separador}\r\n`);
|
|
175
|
+
socket.write(`Content-disposition: attachment; filename=${file.name}\r\n`);
|
|
176
|
+
socket.write('Content-Transfer-Encoding: base64\r\n');
|
|
177
|
+
socket.write('Content-Type: application/octet-stream\r\n');
|
|
178
|
+
socket.write('\r\n');
|
|
179
|
+
socket.write(`${FS.readFileSync(file.path).toString('base64')}\r\n`);
|
|
180
|
+
socket.write('\r\n');
|
|
181
|
+
});
|
|
182
|
+
socket.write(`--${Separador}--\r\n`);
|
|
183
|
+
}
|
|
184
|
+
//Enviar y finalizar conexión
|
|
185
|
+
socket.write('\r\n.\r\n');
|
|
186
|
+
socket.write('QUIT\r\n');
|
|
187
|
+
}).catch(Reject);
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
export default Mail;
|
|
192
|
+
//# sourceMappingURL=Mail.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Mail.js","sourceRoot":"","sources":["../../src/Beta/Mail.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,MAAM,aAAa,CAAC;AAEhC,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,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