@hackthedev/dsync-ipsec 1.0.1 → 1.0.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.
- package/README.md +4 -0
- package/index.mjs +25 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,6 +7,10 @@ This library comes with features meant to prevent abuse in form of spam and othe
|
|
|
7
7
|
|
|
8
8
|
The library was designed for usage with `express` and filtering abusive and (potentially) malicious traffic based on the IP address.
|
|
9
9
|
|
|
10
|
+
> [!IMPORTANT]
|
|
11
|
+
>
|
|
12
|
+
> It is highly recommended that you cache or store the API response!!!!
|
|
13
|
+
|
|
10
14
|
------
|
|
11
15
|
|
|
12
16
|
## Setup
|
package/index.mjs
CHANGED
|
@@ -30,13 +30,12 @@ export default class dSyncIPSec {
|
|
|
30
30
|
this.blockVPN = blockVPN;
|
|
31
31
|
this.blockTor = blockTor;
|
|
32
32
|
this.blockAbuser = blockAbuser;
|
|
33
|
-
this.blockedCountryCodes = blockedCountryCodes;
|
|
34
33
|
|
|
35
|
-
this.urlWhitelist =
|
|
36
|
-
this.ipWhitelist =
|
|
37
|
-
this.ipBlacklist =
|
|
38
|
-
this.companyDomainWhitelist =
|
|
39
|
-
this.blockedCountriesByCode =
|
|
34
|
+
this.urlWhitelist = whitelistedUrls
|
|
35
|
+
this.ipWhitelist = whitelistedIps
|
|
36
|
+
this.ipBlacklist = blacklistedIps
|
|
37
|
+
this.companyDomainWhitelist = whitelistedCompanyDomains
|
|
38
|
+
this.blockedCountriesByCode = blockedCountryCodes
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
updateRule({
|
|
@@ -65,33 +64,38 @@ export default class dSyncIPSec {
|
|
|
65
64
|
if(blockTor !== null) this.blockTor = blockTor
|
|
66
65
|
if(blockAbuser !== null) this.blockAbuser = blockAbuser
|
|
67
66
|
|
|
68
|
-
if(whitelistedUrls !== null) this.urlWhitelist =
|
|
69
|
-
if(whitelistedIps !== null) this.ipWhitelist =
|
|
70
|
-
if(blacklistedIps !== null) this.ipBlacklist =
|
|
71
|
-
if(blockedCountryCodes !== null) this.blockedCountriesByCode =
|
|
67
|
+
if (whitelistedUrls !== null) this.urlWhitelist = whitelistedUrls
|
|
68
|
+
if (whitelistedIps !== null) this.ipWhitelist = whitelistedIps
|
|
69
|
+
if (blacklistedIps !== null) this.ipBlacklist = blacklistedIps
|
|
70
|
+
if (blockedCountryCodes !== null) this.blockedCountriesByCode = blockedCountryCodes
|
|
72
71
|
}
|
|
73
72
|
|
|
74
73
|
|
|
75
74
|
whitelistIP(ip, allowDuplicates = false){
|
|
76
75
|
if(!ip) throw new Error("Unable to whitelist ip as no ip was provided.");
|
|
77
|
-
if(!this.ipWhitelist
|
|
78
|
-
|
|
76
|
+
if(!ArrayTools.matches(this.ipWhitelist, ip) && !allowDuplicates)
|
|
77
|
+
ArrayTools.addEntry(this.ipWhitelist, ip);
|
|
78
|
+
if(ArrayTools.matches(this.ipBlacklist, ip))
|
|
79
|
+
this.ipBlacklist = ArrayTools.removeEntry(this.ipBlacklist, ip);
|
|
79
80
|
}
|
|
80
81
|
|
|
81
82
|
blacklistIp(ip, allowDuplicates = false){
|
|
82
83
|
if(!ip) throw new Error("Unable to blacklist ip as no ip was provided.");
|
|
83
|
-
if(!this.ipBlacklist
|
|
84
|
-
|
|
84
|
+
if(!ArrayTools.matches(this.ipBlacklist, ip) && !allowDuplicates)
|
|
85
|
+
ArrayTools.addEntry(this.ipBlacklist, ip);
|
|
86
|
+
if(ArrayTools.matches(this.ipWhitelist, ip))
|
|
87
|
+
this.ipWhitelist = ArrayTools.removeEntry(this.ipWhitelist, ip);
|
|
85
88
|
}
|
|
86
89
|
|
|
90
|
+
|
|
87
91
|
isBlacklistedIp(ip){
|
|
88
92
|
if(!ip) throw new Error("Coudlnt check ip blacklist as no ip was provided.")
|
|
89
|
-
return this.ipBlacklist
|
|
93
|
+
return ArrayTools.matches(this.ipBlacklist, ip)
|
|
90
94
|
}
|
|
91
95
|
|
|
92
96
|
isWhitelistedIp(ip){
|
|
93
97
|
if(!ip) throw new Error("Coudlnt check ip blacklist as no ip was provided.")
|
|
94
|
-
return this.ipWhitelist
|
|
98
|
+
return ArrayTools.matches(this.ipWhitelist, ip)
|
|
95
99
|
}
|
|
96
100
|
|
|
97
101
|
async filterExpressTraffic(app){
|
|
@@ -106,15 +110,15 @@ export default class dSyncIPSec {
|
|
|
106
110
|
if(!reqPath) throw new Error("Unable to get request path from req parameter as it wasnt specified or null");
|
|
107
111
|
|
|
108
112
|
// first check for ip blacklist
|
|
109
|
-
if(this.ipBlacklist
|
|
113
|
+
if(ArrayTools.matches(this.ipBlacklist, ipInfo?.ip)) return res.sendStatus(403);
|
|
110
114
|
|
|
111
115
|
// then we can check for whitelisted urls as these bypass normal checks
|
|
112
116
|
// url whitelist
|
|
113
|
-
if(this.urlWhitelist
|
|
117
|
+
if(ArrayTools.matches(this.urlWhitelist, reqPath)) return next();
|
|
114
118
|
// let whitelisted ips pass
|
|
115
|
-
if(this.ipWhitelist
|
|
119
|
+
if(ArrayTools.matches(this.ipWhitelist, ipInfo?.ip)) return next();
|
|
116
120
|
// company domain whitelist
|
|
117
|
-
if(this.companyDomainWhitelist
|
|
121
|
+
if(ArrayTools.matches(this.companyDomainWhitelist, ipInfo?.company?.domain)) return next();
|
|
118
122
|
|
|
119
123
|
// looking kinda beautiful
|
|
120
124
|
if (ipInfo?.is_bogon && this.blockBogon) return res.sendStatus(403);
|
|
@@ -128,7 +132,7 @@ export default class dSyncIPSec {
|
|
|
128
132
|
|
|
129
133
|
if (
|
|
130
134
|
ipInfo.location?.country_code &&
|
|
131
|
-
this.blockedCountriesByCode
|
|
135
|
+
ArrayTools.matches(this.blockedCountriesByCode, ipInfo?.location?.country_code?.toLowerCase())
|
|
132
136
|
) return res.sendStatus(403);
|
|
133
137
|
|
|
134
138
|
// continue
|