@hackthedev/dsync-ipsec 1.0.5 → 1.0.7

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.
Files changed (2) hide show
  1. package/index.mjs +50 -37
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -104,48 +104,61 @@ export default class dSyncIPSec {
104
104
  return ArrayTools.matches(this.ipWhitelist, ip)
105
105
  }
106
106
 
107
- async filterExpressTraffic(app) {
108
- if (!app) throw new Error("Unable to filter express traffic as no express app was provided.");
107
+ async checkRequest(req) {
108
+ let clientIP = this.getClientIp(req);
109
109
 
110
+ // remove localhost ips
111
+ if(clientIP === "::1" || clientIP === "127.0.0.1") return { allow: true }
112
+
113
+ const ipInfo = await this.lookupIP(clientIP);
114
+ if (!ipInfo) return { allow: true };
115
+ if (ipInfo?.blocked === true) return { allow: false };
116
+
117
+ const reqPath = req.path;
118
+ if (!reqPath) return { allow: true };
119
+
120
+ if (ArrayTools.matches(this.ipBlacklist, ipInfo.ip))
121
+ return { allow: false, code: 403 };
122
+
123
+ if (ArrayTools.matches(this.urlWhitelist, reqPath))
124
+ return { allow: true };
125
+
126
+ if (ArrayTools.matches(this.ipWhitelist, ipInfo.ip))
127
+ return { allow: true };
128
+
129
+ if (ArrayTools.matches(this.companyDomainWhitelist, ipInfo?.company?.domain))
130
+ return { allow: true };
131
+
132
+ if (ipInfo.is_bogon && this.blockBogon) return { allow: false, code: 403 };
133
+ if (ipInfo.is_datacenter && this.blockDatacenter) return { allow: false, code: 403 };
134
+ if (ipInfo.is_satelite && this.blockSatelite) return { allow: false, code: 403 };
135
+ if (ipInfo.is_crawler && this.blockCrawler) return { allow: false, code: 403 };
136
+ if (ipInfo.is_proxy && this.blockProxy) return { allow: false, code: 403 };
137
+ if (ipInfo.is_vpn && this.blockVPN) return { allow: false, code: 403 };
138
+ if (ipInfo.is_tor && this.blockTor) return { allow: false, code: 403 };
139
+ if (ipInfo.is_abuser && this.blockAbuser) return { allow: false, code: 403 };
140
+
141
+ if (
142
+ ipInfo.location?.country_code &&
143
+ ArrayTools.matches(
144
+ this.blockedCountriesByCode,
145
+ ipInfo.location.country_code.toLowerCase()
146
+ )
147
+ ) return { allow: false, code: 403 };
148
+
149
+ return { allow: true };
150
+ }
151
+
152
+
153
+ filterExpressTraffic(app) {
110
154
  app.use(async (req, res, next) => {
111
- const ipInfo = await this.lookupIP(this.getClientIp(req));
112
- if (!ipInfo) return next();
113
-
114
- // whitelist some urls for functionality
115
- let reqPath = req.path;
116
- if (!reqPath) throw new Error("Unable to get request path from req parameter as it wasnt specified or null");
117
-
118
- // first check for ip blacklist
119
- if (ArrayTools.matches(this.ipBlacklist, ipInfo?.ip)) return res.sendStatus(403);
120
-
121
- // then we can check for whitelisted urls as these bypass normal checks
122
- // url whitelist
123
- if (ArrayTools.matches(this.urlWhitelist, reqPath)) return next();
124
- // let whitelisted ips pass
125
- if (ArrayTools.matches(this.ipWhitelist, ipInfo?.ip)) return next();
126
- // company domain whitelist
127
- if (ArrayTools.matches(this.companyDomainWhitelist, ipInfo?.company?.domain)) return next();
128
-
129
- // looking kinda beautiful
130
- if (ipInfo?.is_bogon && this.blockBogon) return res.sendStatus(403);
131
- if (ipInfo?.is_datacenter && this.blockDatacenter) return res.sendStatus(403);
132
- if (ipInfo?.is_satelite && this.blockSatelite) return res.sendStatus(403);
133
- if (ipInfo?.is_crawler && this.blockCrawler) return res.sendStatus(403);
134
- if (ipInfo?.is_proxy && this.blockProxy) return res.sendStatus(403);
135
- if (ipInfo?.is_vpn && this.blockVPN) return res.sendStatus(403);
136
- if (ipInfo?.is_tor && this.blockTor) return res.sendStatus(403);
137
- if (ipInfo?.is_abuser && this.blockAbuser) return res.sendStatus(403);
138
-
139
- if (
140
- ipInfo.location?.country_code &&
141
- ArrayTools.matches(this.blockedCountriesByCode, ipInfo?.location?.country_code?.toLowerCase())
142
- ) return res.sendStatus(403);
143
-
144
- // continue
155
+ const r = await this.checkRequest(req);
156
+ if (!r.allow) return res.sendStatus(r.code || 403);
145
157
  next();
146
158
  });
147
159
  }
148
160
 
161
+
149
162
  getClientIp(req) {
150
163
  if (!req) throw new Error("Unable to get client ip from req parameter as it wasnt specified or null");
151
164
  const xf = req.headers["x-forwarded-for"];
@@ -157,7 +170,7 @@ export default class dSyncIPSec {
157
170
  if (!ip) throw new Error("Unable to lookup ip as it wasnt provided.")
158
171
 
159
172
  // if an ip is blacklisted we return with an error "reponse"
160
- if (this.isBlacklistedIp(ip)) return {error: `IP ${ip} was blacklisted.`};
173
+ if (this.isBlacklistedIp(ip)) return {error: `IP ${ip} was blacklisted.`, blocked: true};
161
174
 
162
175
  // if we use cache we can skip the fetch
163
176
  if (this.checkCache && typeof this.checkCache === "function") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hackthedev/dsync-ipsec",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "IP Based Security Library against Abuse",
5
5
  "license": "ISC",
6
6
  "author": "",