@hackthedev/dsync-ipsec 1.0.2 → 1.0.3

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,9 @@ export default class dSyncIPSec {
19
19
  "::1",
20
20
  "127.0.0.1",
21
21
  "localhost"
22
- ]
22
+ ],
23
+ //
24
+ checkCache = null
23
25
  } = {}) {
24
26
 
25
27
  this.blockBogon = blockBogon;
@@ -31,38 +33,40 @@ export default class dSyncIPSec {
31
33
  this.blockTor = blockTor;
32
34
  this.blockAbuser = blockAbuser;
33
35
 
34
- this.urlWhitelist = whitelistedUrls
35
- this.ipWhitelist = whitelistedIps
36
- this.ipBlacklist = blacklistedIps
37
- this.companyDomainWhitelist = whitelistedCompanyDomains
38
- this.blockedCountriesByCode = blockedCountryCodes
36
+ this.urlWhitelist = whitelistedUrls;
37
+ this.ipWhitelist = whitelistedIps;
38
+ this.ipBlacklist = blacklistedIps;
39
+ this.companyDomainWhitelist = whitelistedCompanyDomains;
40
+ this.blockedCountriesByCode = blockedCountryCodes;
41
+
42
+ this.checkCache = checkCache;
39
43
  }
40
44
 
41
45
  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,
46
+ blockBogon = null,
47
+ blockDatacenter = null,
48
+ blockSatelite = null,
49
+ blockCrawler = null,
50
+ blockProxy = null,
51
+ blockVPN = null,
52
+ blockTor = null,
53
+ blockAbuser = null,
50
54
 
51
55
  whitelistedUrls = null,
52
56
  whitelistedIps = null,
53
57
  blockedCountryCodes = null,
54
58
  whitelistedCompanyDomains = null,
55
59
  blacklistedIps = null,
56
- }){
60
+ }) {
57
61
 
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
62
+ if (blockBogon !== null) this.blockBogon = blockBogon
63
+ if (blockDatacenter !== null) this.blockDatacenter = blockDatacenter
64
+ if (blockSatelite !== null) this.blockSatelite = blockSatelite
65
+ if (blockCrawler !== null) this.blockCrawler = blockCrawler
66
+ if (blockProxy !== null) this.blockProxy = blockProxy
67
+ if (blockVPN !== null) this.blockVPN = blockVPN
68
+ if (blockTor !== null) this.blockTor = blockTor
69
+ if (blockAbuser !== null) this.blockAbuser = blockAbuser
66
70
 
67
71
  if (whitelistedUrls !== null) this.urlWhitelist = whitelistedUrls
68
72
  if (whitelistedIps !== null) this.ipWhitelist = whitelistedIps
@@ -71,35 +75,35 @@ export default class dSyncIPSec {
71
75
  }
72
76
 
73
77
 
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)
78
+ whitelistIP(ip, allowDuplicates = false) {
79
+ if (!ip) throw new Error("Unable to whitelist ip as no ip was provided.");
80
+ if (!ArrayTools.matches(this.ipWhitelist, ip) && !allowDuplicates)
77
81
  ArrayTools.addEntry(this.ipWhitelist, ip);
78
- if(ArrayTools.matches(this.ipBlacklist, ip))
82
+ if (ArrayTools.matches(this.ipBlacklist, ip))
79
83
  this.ipBlacklist = ArrayTools.removeEntry(this.ipBlacklist, ip);
80
84
  }
81
85
 
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)
86
+ blacklistIp(ip, allowDuplicates = false) {
87
+ if (!ip) throw new Error("Unable to blacklist ip as no ip was provided.");
88
+ if (!ArrayTools.matches(this.ipBlacklist, ip) && !allowDuplicates)
85
89
  ArrayTools.addEntry(this.ipBlacklist, ip);
86
- if(ArrayTools.matches(this.ipWhitelist, ip))
90
+ if (ArrayTools.matches(this.ipWhitelist, ip))
87
91
  this.ipWhitelist = ArrayTools.removeEntry(this.ipWhitelist, ip);
88
92
  }
89
93
 
90
94
 
91
- isBlacklistedIp(ip){
92
- if(!ip) throw new Error("Coudlnt check ip blacklist as no ip was provided.")
95
+ isBlacklistedIp(ip) {
96
+ if (!ip) throw new Error("Coudlnt check ip blacklist as no ip was provided.")
93
97
  return ArrayTools.matches(this.ipBlacklist, ip)
94
98
  }
95
99
 
96
- isWhitelistedIp(ip){
97
- if(!ip) throw new Error("Coudlnt check ip blacklist as no ip was provided.")
100
+ isWhitelistedIp(ip) {
101
+ if (!ip) throw new Error("Coudlnt check ip blacklist as no ip was provided.")
98
102
  return ArrayTools.matches(this.ipWhitelist, ip)
99
103
  }
100
104
 
101
- async filterExpressTraffic(app){
102
- if(!app) throw new Error("Unable to filter express traffic as no express app was provided.");
105
+ async filterExpressTraffic(app) {
106
+ if (!app) throw new Error("Unable to filter express traffic as no express app was provided.");
103
107
 
104
108
  app.use(async (req, res, next) => {
105
109
  const ipInfo = await this.lookupIP(this.getClientIp(req));
@@ -107,18 +111,18 @@ export default class dSyncIPSec {
107
111
 
108
112
  // whitelist some urls for functionality
109
113
  let reqPath = req.path;
110
- if(!reqPath) throw new Error("Unable to get request path from req parameter as it wasnt specified or null");
114
+ if (!reqPath) throw new Error("Unable to get request path from req parameter as it wasnt specified or null");
111
115
 
112
116
  // first check for ip blacklist
113
- if(ArrayTools.matches(this.ipBlacklist, ipInfo?.ip)) return res.sendStatus(403);
117
+ if (ArrayTools.matches(this.ipBlacklist, ipInfo?.ip)) return res.sendStatus(403);
114
118
 
115
119
  // then we can check for whitelisted urls as these bypass normal checks
116
120
  // url whitelist
117
- if(ArrayTools.matches(this.urlWhitelist, reqPath)) return next();
121
+ if (ArrayTools.matches(this.urlWhitelist, reqPath)) return next();
118
122
  // let whitelisted ips pass
119
- if(ArrayTools.matches(this.ipWhitelist, ipInfo?.ip)) return next();
123
+ if (ArrayTools.matches(this.ipWhitelist, ipInfo?.ip)) return next();
120
124
  // company domain whitelist
121
- if(ArrayTools.matches(this.companyDomainWhitelist, ipInfo?.company?.domain)) return next();
125
+ if (ArrayTools.matches(this.companyDomainWhitelist, ipInfo?.company?.domain)) return next();
122
126
 
123
127
  // looking kinda beautiful
124
128
  if (ipInfo?.is_bogon && this.blockBogon) return res.sendStatus(403);
@@ -141,25 +145,30 @@ export default class dSyncIPSec {
141
145
  }
142
146
 
143
147
  getClientIp(req) {
144
- if(!req) throw new Error("Unable to get client ip from req parameter as it wasnt specified or null");
148
+ if (!req) throw new Error("Unable to get client ip from req parameter as it wasnt specified or null");
145
149
  const xf = req.headers["x-forwarded-for"];
146
150
  if (xf) return xf.split(",")[0].trim();
147
151
  return req.socket?.remoteAddress || req.connection?.remoteAddress;
148
152
  }
149
153
 
150
- async lookupIP(ip){
151
- if(!ip) throw new Error("Unable to lookup ip as it wasnt provided.")
154
+ async lookupIP(ip) {
155
+ if (!ip) throw new Error("Unable to lookup ip as it wasnt provided.")
152
156
 
153
157
  // if an ip is blacklisted we return with an error "reponse"
154
- if(this.isBlacklistedIp(ip)) return {error: `IP ${ip} was local.`};
158
+ if (this.isBlacklistedIp(ip)) return {error: `IP ${ip} was blacklisted.`};
159
+
160
+ // if we use cache we can skip the fetch
161
+ if (this.checkCache && typeof this.checkCache === "function") {
162
+ let ipInfo = await this.checkCache(ip);
163
+ if (ipInfo) return ipInfo;
164
+ }
155
165
 
156
166
  // make request to get ip info
157
167
  let ipRequest = await fetch(`https://api.ipapi.is/?q=${ip}`);
158
- if(ipRequest.status === 200){
168
+ if (ipRequest.status === 200) {
159
169
  let ipData = await ipRequest.json();
160
170
  return ipData;
161
- }
162
- else{
171
+ } else {
163
172
  return {error: "Failed to fetch IP data"};
164
173
  }
165
174
  }
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.3",
4
4
  "description": "IP Based Security Library against Abuse",
5
5
  "license": "ISC",
6
6
  "author": "",