@open-wa/wa-automate 4.43.5 → 4.44.2

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.
@@ -240,6 +240,31 @@ exports.optionList = [{
240
240
  typeLabel: '{yellow {underline "--max-memory-restart 300M"}}',
241
241
  description: "Offload the EASY API to local instance of pm2. You can add pm2 specific arguments also if you want."
242
242
  },
243
+ {
244
+ name: 'privkey',
245
+ type: String,
246
+ typeLabel: '{yellow {underline "./privatekey.pem"}}',
247
+ description: "The private key to use for the TLS connection. --cert is also required"
248
+ },
249
+ {
250
+ name: 'cert',
251
+ type: String,
252
+ typeLabel: '{yellow {underline "./certificate.pem"}}',
253
+ description: "The certificate to use for the TLS connection. --privkey is also required"
254
+ },
255
+ {
256
+ name: 'helmet',
257
+ type: Boolean,
258
+ description: "Enable helmet middleware for security.",
259
+ },
260
+ {
261
+ name: 'allow-ips',
262
+ type: String,
263
+ //@ts-ignore
264
+ isMultiple: true,
265
+ typeLabel: '{blueBright {underline 192.168.0.1,192.168.0.2}}',
266
+ description: "Allow only these IPs to connect to the EASY API. By default, all IPs are allowed."
267
+ },
243
268
  {
244
269
  name: 'help',
245
270
  description: 'Print this usage guide.'
package/dist/cli/index.js CHANGED
@@ -102,6 +102,7 @@ function start() {
102
102
  }
103
103
  try {
104
104
  const client = yield (0, index_1.create)(Object.assign({}, createConfig));
105
+ (0, server_1.setupHttpServer)(cliConfig);
105
106
  if (cliConfig.autoReject) {
106
107
  yield client.autoReject(cliConfig.onCall);
107
108
  }
@@ -1,11 +1,12 @@
1
1
  /// <reference types="node" />
2
- import http from 'http';
2
+ import http from 'node:http';
3
3
  import { Client } from '..';
4
4
  export declare const app: import("express-serve-static-core").Express;
5
- export declare const server: http.Server;
5
+ export declare let server: http.Server;
6
6
  export declare type cliFlags = {
7
7
  [k: string]: number | string | boolean;
8
8
  };
9
+ export declare const setupHttpServer: (cliConfig: cliFlags) => void;
9
10
  export declare const setUpExpressApp: () => void;
10
11
  export declare const enableCORSRequests: () => void;
11
12
  export declare const setupAuthenticationLayer: (cliConfig: cliFlags) => void;
@@ -31,10 +31,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
31
31
  return (mod && mod.__esModule) ? mod : { "default": mod };
32
32
  };
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
- exports.setupSocketServer = exports.setupBotPressHandler = exports.setupChatwoot = exports.setupTwilioCompatibleWebhook = exports.setupMediaMiddleware = exports.listListeners = exports.getCommands = exports.setupRefocusDisengageMiddleware = exports.setupSwaggerStatsMiddleware = exports.setupApiDocs = exports.setupAuthenticationLayer = exports.enableCORSRequests = exports.setUpExpressApp = exports.server = exports.app = void 0;
34
+ exports.setupSocketServer = exports.setupBotPressHandler = exports.setupChatwoot = exports.setupTwilioCompatibleWebhook = exports.setupMediaMiddleware = exports.listListeners = exports.getCommands = exports.setupRefocusDisengageMiddleware = exports.setupSwaggerStatsMiddleware = exports.setupApiDocs = exports.setupAuthenticationLayer = exports.enableCORSRequests = exports.setUpExpressApp = exports.setupHttpServer = exports.server = exports.app = void 0;
35
35
  //@ts-ignore
36
36
  const express_1 = __importDefault(require("express"));
37
- const http_1 = __importDefault(require("http"));
37
+ const node_https_1 = __importDefault(require("node:https"));
38
+ const node_http_1 = __importDefault(require("node:http"));
38
39
  const collections_1 = require("./collections");
39
40
  const express_robots_txt_1 = __importDefault(require("express-robots-txt"));
40
41
  const swagger_ui_express_1 = __importDefault(require("swagger-ui-express"));
@@ -42,15 +43,69 @@ const axios_1 = __importDefault(require("axios"));
42
43
  const parse_function_1 = __importDefault(require("parse-function"));
43
44
  const __1 = require("..");
44
45
  const qs_1 = __importDefault(require("qs"));
46
+ const fs = __importStar(require("fs"));
45
47
  const xmlbuilder2_1 = require("xmlbuilder2");
46
48
  const chatwoot_1 = require("./integrations/chatwoot");
49
+ const express_ipfilter_1 = require("express-ipfilter");
50
+ const helmet_1 = __importDefault(require("helmet"));
47
51
  exports.app = (0, express_1.default)();
48
- exports.server = http_1.default.createServer(exports.app);
52
+ exports.server = node_http_1.default.createServer(exports.app);
49
53
  const trimChatId = (chatId) => chatId.replace("@c.us", "").replace("@g.us", "");
50
54
  const socketListenerCallbacks = {};
51
55
  // const existingListeners = () => Object.keys(Object.keys(socketListenerCallbacks).flatMap(id=>Object.keys(socketListenerCallbacks[id])).reduce((acc,curr)=>{acc[curr]=true;return acc},{}))
52
56
  const existingListeners = [];
53
57
  const getCallbacks = (listener) => Object.keys(socketListenerCallbacks).flatMap(k => socketListenerCallbacks[k]).map(o => o[listener]).filter(x => x);
58
+ const setupHttpServer = (cliConfig) => {
59
+ //check if there is an allow IP list:
60
+ if (cliConfig.allowIps) {
61
+ let allowIps = cliConfig.allowIps;
62
+ if (!Array.isArray(cliConfig.allowIps))
63
+ allowIps = [cliConfig.allowIps];
64
+ if (Array.isArray(allowIps) && allowIps.length > 0 && allowIps[0]) {
65
+ console.log("Allowed IPs", allowIps);
66
+ let allowIpsOptions = {
67
+ mode: 'allow',
68
+ forbidden: 'You are not authorized to access this page.',
69
+ log: false
70
+ };
71
+ if (cliConfig.verbose)
72
+ allowIpsOptions = Object.assign(Object.assign({}, allowIpsOptions), { logLevel: 'deny', log: true });
73
+ exports.app.use((0, express_ipfilter_1.IpFilter)(allowIps, allowIpsOptions));
74
+ exports.app.use((err, req, res, next) => {
75
+ if (err instanceof express_ipfilter_1.IpDeniedError) {
76
+ res.status(401);
77
+ res.send("Access Denied");
78
+ return;
79
+ }
80
+ next();
81
+ });
82
+ }
83
+ }
84
+ if (cliConfig.helmet) {
85
+ //@ts-ignore
86
+ exports.app.use((0, helmet_1.default)());
87
+ }
88
+ const privkey = `${process.env.PRIV || cliConfig.privkey || ""}`;
89
+ const cert = `${process.env.CERT || cliConfig.cert || ""}`;
90
+ if (privkey && cert) {
91
+ console.log("HTTPS Mode:", privkey, cert);
92
+ const privContents = fs.readFileSync(privkey);
93
+ const certContents = fs.readFileSync(cert);
94
+ exports.app.use((req, res, next) => {
95
+ if (!req.secure && req.get('x-forwarded-proto') !== 'https' && process.env.NODE_ENV !== "development") {
96
+ return res.redirect('https://' + req.get('host') + req.url);
97
+ }
98
+ next();
99
+ });
100
+ if (privContents && certContents) {
101
+ const options = { key: privContents, cert: certContents };
102
+ exports.server = node_https_1.default.createServer(options, exports.app);
103
+ return;
104
+ }
105
+ }
106
+ exports.server = node_http_1.default.createServer(exports.app);
107
+ };
108
+ exports.setupHttpServer = setupHttpServer;
54
109
  const setUpExpressApp = () => {
55
110
  exports.app.use((0, express_robots_txt_1.default)({ UserAgent: '*', Disallow: '/' }));
56
111
  //@ts-ignore
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-wa/wa-automate",
3
- "version": "4.43.5",
3
+ "version": "4.44.2",
4
4
  "licenseCheckUrl": "https://funcs.openwa.dev/license-check",
5
5
  "brokenMethodReportUrl": "https://funcs.openwa.dev/report-bm",
6
6
  "patches": "https://cdn.openwa.dev/patches.json",
@@ -75,7 +75,7 @@
75
75
  "@types/localtunnel": "^2.0.1",
76
76
  "@types/marked": "^4.0.2",
77
77
  "@types/mime-types": "^2.1.0",
78
- "@types/node": "^17.0.13",
78
+ "@types/node": "^18.7.6",
79
79
  "@types/puppeteer": "^5.4.0",
80
80
  "@types/shelljs": "^0.8.5",
81
81
  "@types/winston-syslog": "^2.4.0",
@@ -123,12 +123,14 @@
123
123
  "death": "^1.1.0",
124
124
  "eventemitter2": "^6.4.4",
125
125
  "express": "^4.17.1",
126
+ "express-ipfilter": "^1.3.1",
126
127
  "express-robots-txt": "^1.0.0",
127
128
  "find-up": "^5.0.0",
128
129
  "form-data": "^4.0.0",
129
130
  "fs-extra": "^10.0.0",
130
131
  "get-port": "^5.1.1",
131
132
  "hasha": "^5.2.0",
133
+ "helmet": "^5.1.1",
132
134
  "image-type": "^4.1.0",
133
135
  "is-url-superb": "^5.0.0",
134
136
  "json5": "^2.2.0",