mirror of
https://github.com/bdnugget/sse-vue-test.git
synced 2026-11-07 07:07:27 +00:00
Make shitty tool to deal with CIDR notation
This commit is contained in:
1 parent
82da11cd37
commit
fa784058ab
5 files changed
+3707
-9
No files matched your search
+40
@@ -0,0 +1,40 @@
|
||||
// ipTools.js
|
||||
// Utils to deal with CIDR notations n shiet
|
||||
// @bdnugget
|
||||
|
||||
function cidrToSubnetMask(prefixLength) {
|
||||
const subnetMask = [];
|
||||
for (let i = 0; i < 32; i++) {
|
||||
subnetMask.push(i < prefixLength ? '1' : '0');
|
||||
}
|
||||
return [
|
||||
parseInt(subnetMask.slice(0, 8).join(''), 2),
|
||||
parseInt(subnetMask.slice(8, 16).join(''), 2),
|
||||
parseInt(subnetMask.slice(16, 24).join(''), 2),
|
||||
parseInt(subnetMask.slice(24, 32).join(''), 2)
|
||||
].join('.');
|
||||
}
|
||||
|
||||
function isIPInRange(ip, cidr) {
|
||||
const [subnet, prefixLength] = cidr.split('/');
|
||||
const subnetMask = cidrToSubnetMask(parseInt(prefixLength, 10));
|
||||
|
||||
const ipParts = ip.split('.');
|
||||
const subnetParts = subnet.split('.');
|
||||
const subnetMaskParts = subnetMask.split('.');
|
||||
|
||||
for (let i = 0; i < 4; i++) {
|
||||
const ipPart = parseInt(ipParts[i], 10);
|
||||
const subnetPart = parseInt(subnetParts[i], 10);
|
||||
const subnetMaskPart = parseInt(subnetMaskParts[i], 10);
|
||||
|
||||
if ((ipPart & subnetMaskPart) !== (subnetPart & subnetMaskPart)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
module.exports = { cidrToSubnetMask, isIPInRange };
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// @bdnugget test for ipTools
|
||||
|
||||
const { cidrToSubnetMask, isIPInRange } = require('./ipTools');
|
||||
|
||||
// Test cidrToSubnetMask function
|
||||
test('CIDR to subnet mask conversion', () => {
|
||||
expect(cidrToSubnetMask(24)).toBe('255.255.255.0');
|
||||
expect(cidrToSubnetMask(16)).toBe('255.255.0.0');
|
||||
expect(cidrToSubnetMask(30)).toBe('255.255.255.252');
|
||||
});
|
||||
|
||||
// Test isIPInRange function
|
||||
test('Check if IP is in range', () => {
|
||||
expect(isIPInRange('192.168.178.115', '192.168.178.0/24')).toBe(true);
|
||||
expect(isIPInRange('192.168.1.100', '192.168.1.0/24')).toBe(true);
|
||||
expect(isIPInRange('192.168.2.100', '192.168.1.0/24')).toBe(false);
|
||||
});
|
||||
|
||||
Generated
+3622
-1
File diff suppressed because it is too large.
Load diff
+4
-2
@@ -8,7 +8,8 @@
|
||||
"lint": "vue-cli-service lint",
|
||||
"start": "concurrently \"npm run serve\" \"npm run server\" \"npm run sse-server\"",
|
||||
"server": "node server.js",
|
||||
"sse-server": "node sse-server.js"
|
||||
"sse-server": "node sse-server.js",
|
||||
"test": "jest"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^1.6.7",
|
||||
@@ -24,7 +25,8 @@
|
||||
"@vue/cli-service": "~5.0.0",
|
||||
"concurrently": "^8.2.2",
|
||||
"eslint": "^7.32.0",
|
||||
"eslint-plugin-vue": "^8.0.3"
|
||||
"eslint-plugin-vue": "^8.0.3",
|
||||
"jest": "^29.7.0"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"root": true,
|
||||
|
||||
+23
-6
@@ -1,12 +1,29 @@
|
||||
const http = require('http');
|
||||
|
||||
const server = http.createServer((req, res) => {
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Connection': 'keep-alive',
|
||||
'Access-Control-Allow-Origin': 'http://localhost:8080'
|
||||
});
|
||||
const allowedOrigins = [
|
||||
'http://localhost:8080',
|
||||
'http://192.168.0.0/16:8080', // Common LAN range 192.168.x.x
|
||||
'http://10.0.0.0/8:8080', // Common LAN range 10.x.x.x
|
||||
'http://172.16.0.0/12:8080', // Common LAN range 172.16.x.x - 172.31.x.x
|
||||
// 'http://192.168.178.115:8080',
|
||||
];
|
||||
|
||||
const origin = req.headers.origin;
|
||||
console.log('orig: ' + origin);
|
||||
if (allowedOrigins.includes(origin)) {
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Connection': 'keep-alive',
|
||||
'Access-Control-Allow-Origin': origin
|
||||
});
|
||||
} else {
|
||||
// Origin not allowed
|
||||
res.writeHead(403);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
function sendRandomMessage() {
|
||||
const randomInterval = Math.round((Math.random() * 9000) + 1000); // Random interval between 1 and 10 seconds
|
||||
|
||||
Reference in new issue
Block a user