@nka212bg/backend-utils 0.1.59 → 0.1.61
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/location/index.js +8 -5
- package/mailSmsTools/index.js +24 -8
- package/package.json +1 -1
package/location/index.js
CHANGED
|
@@ -5,21 +5,24 @@ const { locales, timeZones } = require(`./localelib`);
|
|
|
5
5
|
const Reader = require("@maxmind/geoip2-node").Reader;
|
|
6
6
|
const GeoDB = Reader.openBuffer(readFileSync(`${__dirname}/GeoLite2-City.mmdb`));
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
// x-forwarded-for is client-controlled, trust it only behind a reverse proxy (nginx, Cloudflare...)
|
|
9
|
+
exports.userIp = (req, { trustProxy = false } = {}) => {
|
|
9
10
|
if (!req) throw new Error("Request object is missing, can't work like that! That's absurd!!!");
|
|
10
11
|
|
|
11
|
-
let ip = (req.headers["x-forwarded-for"] || req.socket?.remoteAddress || req.connection?.remoteAddress || req.ip)
|
|
12
|
+
let ip = ((trustProxy && req.headers["x-forwarded-for"]) || req.socket?.remoteAddress || req.connection?.remoteAddress || req.ip)
|
|
13
|
+
?.split(",")[0]
|
|
14
|
+
.trim()
|
|
15
|
+
.replace(/::ffff:/gi, "");
|
|
12
16
|
|
|
13
|
-
// if (["127.0.0.1", "::1"].includes(ip)) return "146.70.185.8";
|
|
14
17
|
return ip;
|
|
15
18
|
};
|
|
16
19
|
|
|
17
20
|
// user geo
|
|
18
|
-
exports.location = ({ req, ip }) => {
|
|
21
|
+
exports.location = ({ req, ip, trustProxy }) => {
|
|
19
22
|
let userIP;
|
|
20
23
|
try {
|
|
21
24
|
if (!ip && !req) throw "missing_required";
|
|
22
|
-
userIP = ip || this.userIp(req);
|
|
25
|
+
userIP = ip || this.userIp(req, { trustProxy });
|
|
23
26
|
const geo = GeoDB.city(userIP);
|
|
24
27
|
|
|
25
28
|
const country = locales[geo.country.isoCode];
|
package/mailSmsTools/index.js
CHANGED
|
@@ -50,6 +50,28 @@ const subscriptionState = {
|
|
|
50
50
|
},
|
|
51
51
|
};
|
|
52
52
|
|
|
53
|
+
// SMTP_URL=smtps://user%40mail.com:pass@smtp.host.com:465 (smtps = TLS on 465, smtp = STARTTLS on 587)
|
|
54
|
+
// falls back to SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASS
|
|
55
|
+
const smtpEnv = () => {
|
|
56
|
+
if (!process.env.SMTP_URL) {
|
|
57
|
+
return {
|
|
58
|
+
host: process.env.SMTP_HOST,
|
|
59
|
+
port: parseInt(process.env.SMTP_PORT),
|
|
60
|
+
secure: true,
|
|
61
|
+
auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS },
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const u = new URL(process.env.SMTP_URL);
|
|
66
|
+
const secure = u.protocol == "smtps:";
|
|
67
|
+
return {
|
|
68
|
+
host: u.hostname,
|
|
69
|
+
port: parseInt(u.port) || (secure ? 465 : 587),
|
|
70
|
+
secure,
|
|
71
|
+
auth: { user: decodeURIComponent(u.username), pass: decodeURIComponent(u.password) },
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
|
|
53
75
|
// every call creates a separate instance with its own config and transports:
|
|
54
76
|
// const { sendEmail } = require(".../mailSmsTools")({ name, logoUrl, baseUrl, smtp, twilio });
|
|
55
77
|
// const { sendEmail } = require(".../mailSmsTools"); // default instance, env config only
|
|
@@ -81,13 +103,7 @@ const createInstance = (options = {}) => {
|
|
|
81
103
|
maxMessages: Infinity,
|
|
82
104
|
maxConnections: 20,
|
|
83
105
|
|
|
84
|
-
|
|
85
|
-
port: parseInt(process.env.SMTP_PORT),
|
|
86
|
-
secure: true, // true for 465, false for other ports
|
|
87
|
-
auth: {
|
|
88
|
-
user: process.env.SMTP_USER,
|
|
89
|
-
pass: process.env.SMTP_PASS,
|
|
90
|
-
},
|
|
106
|
+
...smtpEnv(),
|
|
91
107
|
...smtp,
|
|
92
108
|
}));
|
|
93
109
|
};
|
|
@@ -115,7 +131,7 @@ const createInstance = (options = {}) => {
|
|
|
115
131
|
const name = val(config.name);
|
|
116
132
|
const baseUrl = val(config.baseUrl) || process.env.BASE_URL;
|
|
117
133
|
const smtp = val(config.smtp) || {};
|
|
118
|
-
const smtpUser = smtp.auth?.user ||
|
|
134
|
+
const smtpUser = smtp.auth?.user || smtpEnv().auth.user;
|
|
119
135
|
logoUrl ||= val(config.logoUrl);
|
|
120
136
|
|
|
121
137
|
const emailArray = strToEmailsArray(email || smtpUser);
|