@hackthedev/dsync-ipsec 1.0.6 → 1.0.8

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 +21 -13
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -105,8 +105,14 @@ export default class dSyncIPSec {
105
105
  }
106
106
 
107
107
  async checkRequest(req) {
108
- const ipInfo = await this.lookupIP(this.getClientIp(req));
108
+ let clientIP = this.getClientIp(req);
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);
109
114
  if (!ipInfo) return { allow: true };
115
+ if (ipInfo?.blocked === true) return { allow: false };
110
116
 
111
117
  const reqPath = req.path;
112
118
  if (!reqPath) return { allow: true };
@@ -164,7 +170,7 @@ export default class dSyncIPSec {
164
170
  if (!ip) throw new Error("Unable to lookup ip as it wasnt provided.")
165
171
 
166
172
  // if an ip is blacklisted we return with an error "reponse"
167
- if (this.isBlacklistedIp(ip)) return {error: `IP ${ip} was blacklisted.`};
173
+ if (this.isBlacklistedIp(ip)) return {error: `IP ${ip} was blacklisted.`, blocked: true};
168
174
 
169
175
  // if we use cache we can skip the fetch
170
176
  if (this.checkCache && typeof this.checkCache === "function") {
@@ -173,16 +179,18 @@ export default class dSyncIPSec {
173
179
  }
174
180
 
175
181
  // make request to get ip info
176
- let ipRequest = await fetch(`https://api.ipapi.is/?q=${ip}`);
177
- if (ipRequest.status === 200) {
178
- let ipData = await ipRequest.json();
179
-
180
- // possibility to set cache
181
- if(this.setCache && typeof this.setCache === "function") await this.setCache(ip, ipData);
182
-
183
- return ipData;
184
- } else {
185
- return {error: "Failed to fetch IP data"};
186
- }
182
+ try{
183
+ let ipRequest = await fetch(`https://api.ipapi.is/?q=${ip}`);
184
+ if (ipRequest.status === 200) {
185
+ let ipData = await ipRequest.json();
186
+
187
+ // possibility to set cache
188
+ if(this.setCache && typeof this.setCache === "function") await this.setCache(ip, ipData);
189
+
190
+ return ipData;
191
+ } else {
192
+ return {error: "Failed to fetch IP data"};
193
+ }
194
+ }catch{}
187
195
  }
188
196
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hackthedev/dsync-ipsec",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "IP Based Security Library against Abuse",
5
5
  "license": "ISC",
6
6
  "author": "",