mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-16 17:40:46 -04:00
local spectators get real video maybe if it works
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
const net = require('net');
|
||||
|
||||
function extractForwardedIp(value) {
|
||||
if (typeof value === 'string' && value.trim()) {
|
||||
return value.split(',')[0].trim();
|
||||
@@ -8,6 +10,56 @@ function extractForwardedIp(value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function normalizeIp(value) {
|
||||
if (!value) return null;
|
||||
let ip = String(value).trim();
|
||||
if (!ip) return null;
|
||||
if (ip.startsWith('::ffff:')) {
|
||||
ip = ip.slice(7);
|
||||
}
|
||||
if (ip.includes('%')) {
|
||||
ip = ip.split('%')[0];
|
||||
}
|
||||
return ip.trim() || null;
|
||||
}
|
||||
|
||||
function isPrivateIpv4(ip) {
|
||||
const parts = ip.split('.').map((part) => Number(part));
|
||||
if (parts.length !== 4 || parts.some((part) => Number.isNaN(part))) {
|
||||
return false;
|
||||
}
|
||||
const [a, b] = parts;
|
||||
if (a === 10) return true;
|
||||
if (a === 127) return true;
|
||||
if (a === 192 && b === 168) return true;
|
||||
return a === 172 && b >= 16 && b <= 31;
|
||||
}
|
||||
|
||||
function isPrivateIpv6(ip) {
|
||||
const lower = ip.toLowerCase();
|
||||
if (lower === '::1') return true;
|
||||
if (lower.startsWith('fc') || lower.startsWith('fd')) return true; // fc00::/7
|
||||
return (
|
||||
lower.startsWith('fe8') ||
|
||||
lower.startsWith('fe9') ||
|
||||
lower.startsWith('fea') ||
|
||||
lower.startsWith('feb')
|
||||
); // fe80::/10
|
||||
}
|
||||
|
||||
function isLocalNetwork(ip) {
|
||||
const normalized = normalizeIp(ip);
|
||||
if (!normalized) return false;
|
||||
const version = net.isIP(normalized);
|
||||
if (version === 4) {
|
||||
return isPrivateIpv4(normalized);
|
||||
}
|
||||
if (version === 6) {
|
||||
return isPrivateIpv6(normalized);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function getSocketIp(socket) {
|
||||
if (!socket) return null;
|
||||
const headers = socket.handshake?.headers || {};
|
||||
@@ -42,4 +94,6 @@ function getRequestIp(req, override) {
|
||||
module.exports = {
|
||||
getSocketIp,
|
||||
getRequestIp,
|
||||
isLocalNetwork,
|
||||
normalizeIp,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user