@hackthedev/dsync-ipsec 1.0.2 → 1.0.4

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.
@@ -0,0 +1,43 @@
1
+ name: Publish to npm
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ permissions:
9
+ contents: write
10
+
11
+ jobs:
12
+ publish:
13
+ runs-on: ubuntu-latest
14
+
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+ with:
18
+ persist-credentials: true
19
+
20
+ - name: Skip version bump commits
21
+ run: |
22
+ if git log -1 --pretty=%B | grep -q "chore: bump version"; then
23
+ echo "Version bump commit detected, skipping."
24
+ exit 0
25
+ fi
26
+
27
+ - uses: actions/setup-node@v4
28
+ with:
29
+ node-version: 20
30
+ registry-url: https://registry.npmjs.org/
31
+
32
+ - run: npm ci
33
+
34
+ - run: |
35
+ git config user.name "github-actions"
36
+ git config user.email "actions@github.com"
37
+ npm version patch -m "chore: bump version %s"
38
+ git push
39
+
40
+ - run: npm publish --access public
41
+ env:
42
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
43
+
package/index.mjs CHANGED
@@ -19,7 +19,10 @@ export default class dSyncIPSec {
19
19
  "::1",
20
20
  "127.0.0.1",
21
21
  "localhost"
22
- ]
22
+ ],
23
+ //
24
+ checkCache = null
25
+ setCache = null
23
26
  } = {}) {
24
27
 
25
28
  this.blockBogon = blockBogon;
@@ -31,38 +34,41 @@ export default class dSyncIPSec {
31
34
  this.blockTor = blockTor;
32
35
  this.blockAbuser = blockAbuser;
33
36
 
34
- this.urlWhitelist = whitelistedUrls
35
- this.ipWhitelist = whitelistedIps
36
- this.ipBlacklist = blacklistedIps
37
- this.companyDomainWhitelist = whitelistedCompanyDomains
38
- this.blockedCountriesByCode = blockedCountryCodes
37
+ this.urlWhitelist = whitelistedUrls;
38
+ this.ipWhitelist = whitelistedIps;
39
+ this.ipBlacklist = blacklistedIps;
40
+ this.companyDomainWhitelist = whitelistedCompanyDomains;
41
+ this.blockedCountriesByCode = blockedCountryCodes;
42
+
43
+ this.checkCache = checkCache;
44
+ this.setCache = setCache;
39
45
  }
40
46
 
41
47
  updateRule({
42
- blockBogon = null,
43
- blockDatacenter = null,
44
- blockSatelite = null,
45
- blockCrawler = null,
46
- blockProxy = null,
47
- blockVPN = null,
48
- blockTor = null,
49
- blockAbuser = null,
48
+ blockBogon = null,
49
+ blockDatacenter = null,
50
+ blockSatelite = null,
51
+ blockCrawler = null,
52
+ blockProxy = null,
53
+ blockVPN = null,
54
+ blockTor = null,
55
+ blockAbuser = null,
50
56
 
51
57
  whitelistedUrls = null,
52
58
  whitelistedIps = null,
53
59
  blockedCountryCodes = null,
54
60
  whitelistedCompanyDomains = null,
55
61
  blacklistedIps = null,
56
- }){
62
+ }) {
57
63
 
58
- if(blockBogon !== null) this.blockBogon = blockBogon
59
- if(blockDatacenter !== null) this.blockDatacenter = blockDatacenter
60
- if(blockSatelite !== null) this.blockSatelite =blockSatelite
61
- if(blockCrawler !== null) this.blockCrawler = blockCrawler
62
- if(blockProxy !== null) this.blockProxy = blockProxy
63
- if(blockVPN !== null) this.blockVPN = blockVPN
64
- if(blockTor !== null) this.blockTor = blockTor
65
- if(blockAbuser !== null) this.blockAbuser = blockAbuser
64
+ if (blockBogon !== null) this.blockBogon = blockBogon
65
+ if (blockDatacenter !== null) this.blockDatacenter = blockDatacenter
66
+ if (blockSatelite !== null) this.blockSatelite = blockSatelite
67
+ if (blockCrawler !== null) this.blockCrawler = blockCrawler
68
+ if (blockProxy !== null) this.blockProxy = blockProxy
69
+ if (blockVPN !== null) this.blockVPN = blockVPN
70
+ if (blockTor !== null) this.blockTor = blockTor
71
+ if (blockAbuser !== null) this.blockAbuser = blockAbuser
66
72
 
67
73
  if (whitelistedUrls !== null) this.urlWhitelist = whitelistedUrls
68
74
  if (whitelistedIps !== null) this.ipWhitelist = whitelistedIps
@@ -71,35 +77,35 @@ export default class dSyncIPSec {
71
77
  }
72
78
 
73
79
 
74
- whitelistIP(ip, allowDuplicates = false){
75
- if(!ip) throw new Error("Unable to whitelist ip as no ip was provided.");
76
- if(!ArrayTools.matches(this.ipWhitelist, ip) && !allowDuplicates)
80
+ whitelistIP(ip, allowDuplicates = false) {
81
+ if (!ip) throw new Error("Unable to whitelist ip as no ip was provided.");
82
+ if (!ArrayTools.matches(this.ipWhitelist, ip) && !allowDuplicates)
77
83
  ArrayTools.addEntry(this.ipWhitelist, ip);
78
- if(ArrayTools.matches(this.ipBlacklist, ip))
84
+ if (ArrayTools.matches(this.ipBlacklist, ip))
79
85
  this.ipBlacklist = ArrayTools.removeEntry(this.ipBlacklist, ip);
80
86
  }
81
87
 
82
- blacklistIp(ip, allowDuplicates = false){
83
- if(!ip) throw new Error("Unable to blacklist ip as no ip was provided.");
84
- if(!ArrayTools.matches(this.ipBlacklist, ip) && !allowDuplicates)
88
+ blacklistIp(ip, allowDuplicates = false) {
89
+ if (!ip) throw new Error("Unable to blacklist ip as no ip was provided.");
90
+ if (!ArrayTools.matches(this.ipBlacklist, ip) && !allowDuplicates)
85
91
  ArrayTools.addEntry(this.ipBlacklist, ip);
86
- if(ArrayTools.matches(this.ipWhitelist, ip))
92
+ if (ArrayTools.matches(this.ipWhitelist, ip))
87
93
  this.ipWhitelist = ArrayTools.removeEntry(this.ipWhitelist, ip);
88
94
  }
89
95
 
90
96
 
91
- isBlacklistedIp(ip){
92
- if(!ip) throw new Error("Coudlnt check ip blacklist as no ip was provided.")
97
+ isBlacklistedIp(ip) {
98
+ if (!ip) throw new Error("Coudlnt check ip blacklist as no ip was provided.")
93
99
  return ArrayTools.matches(this.ipBlacklist, ip)
94
100
  }
95
101
 
96
- isWhitelistedIp(ip){
97
- if(!ip) throw new Error("Coudlnt check ip blacklist as no ip was provided.")
102
+ isWhitelistedIp(ip) {
103
+ if (!ip) throw new Error("Coudlnt check ip blacklist as no ip was provided.")
98
104
  return ArrayTools.matches(this.ipWhitelist, ip)
99
105
  }
100
106
 
101
- async filterExpressTraffic(app){
102
- if(!app) throw new Error("Unable to filter express traffic as no express app was provided.");
107
+ async filterExpressTraffic(app) {
108
+ if (!app) throw new Error("Unable to filter express traffic as no express app was provided.");
103
109
 
104
110
  app.use(async (req, res, next) => {
105
111
  const ipInfo = await this.lookupIP(this.getClientIp(req));
@@ -107,18 +113,18 @@ export default class dSyncIPSec {
107
113
 
108
114
  // whitelist some urls for functionality
109
115
  let reqPath = req.path;
110
- if(!reqPath) throw new Error("Unable to get request path from req parameter as it wasnt specified or null");
116
+ if (!reqPath) throw new Error("Unable to get request path from req parameter as it wasnt specified or null");
111
117
 
112
118
  // first check for ip blacklist
113
- if(ArrayTools.matches(this.ipBlacklist, ipInfo?.ip)) return res.sendStatus(403);
119
+ if (ArrayTools.matches(this.ipBlacklist, ipInfo?.ip)) return res.sendStatus(403);
114
120
 
115
121
  // then we can check for whitelisted urls as these bypass normal checks
116
122
  // url whitelist
117
- if(ArrayTools.matches(this.urlWhitelist, reqPath)) return next();
123
+ if (ArrayTools.matches(this.urlWhitelist, reqPath)) return next();
118
124
  // let whitelisted ips pass
119
- if(ArrayTools.matches(this.ipWhitelist, ipInfo?.ip)) return next();
125
+ if (ArrayTools.matches(this.ipWhitelist, ipInfo?.ip)) return next();
120
126
  // company domain whitelist
121
- if(ArrayTools.matches(this.companyDomainWhitelist, ipInfo?.company?.domain)) return next();
127
+ if (ArrayTools.matches(this.companyDomainWhitelist, ipInfo?.company?.domain)) return next();
122
128
 
123
129
  // looking kinda beautiful
124
130
  if (ipInfo?.is_bogon && this.blockBogon) return res.sendStatus(403);
@@ -141,25 +147,34 @@ export default class dSyncIPSec {
141
147
  }
142
148
 
143
149
  getClientIp(req) {
144
- if(!req) throw new Error("Unable to get client ip from req parameter as it wasnt specified or null");
150
+ if (!req) throw new Error("Unable to get client ip from req parameter as it wasnt specified or null");
145
151
  const xf = req.headers["x-forwarded-for"];
146
152
  if (xf) return xf.split(",")[0].trim();
147
153
  return req.socket?.remoteAddress || req.connection?.remoteAddress;
148
154
  }
149
155
 
150
- async lookupIP(ip){
151
- if(!ip) throw new Error("Unable to lookup ip as it wasnt provided.")
156
+ async lookupIP(ip) {
157
+ if (!ip) throw new Error("Unable to lookup ip as it wasnt provided.")
152
158
 
153
159
  // if an ip is blacklisted we return with an error "reponse"
154
- if(this.isBlacklistedIp(ip)) return {error: `IP ${ip} was local.`};
160
+ if (this.isBlacklistedIp(ip)) return {error: `IP ${ip} was blacklisted.`};
161
+
162
+ // if we use cache we can skip the fetch
163
+ if (this.checkCache && typeof this.checkCache === "function") {
164
+ let ipInfo = await this.checkCache(ip);
165
+ if (ipInfo) return ipInfo;
166
+ }
155
167
 
156
168
  // make request to get ip info
157
169
  let ipRequest = await fetch(`https://api.ipapi.is/?q=${ip}`);
158
- if(ipRequest.status === 200){
170
+ if (ipRequest.status === 200) {
159
171
  let ipData = await ipRequest.json();
172
+
173
+ // possibility to set cache
174
+ if(this.setCache && typeof this.setCache === "function") await this.setCache(ip, ipData);
175
+
160
176
  return ipData;
161
- }
162
- else{
177
+ } else {
163
178
  return {error: "Failed to fetch IP data"};
164
179
  }
165
180
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hackthedev/dsync-ipsec",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "IP Based Security Library against Abuse",
5
5
  "license": "ISC",
6
6
  "author": "",