UNPKG

@suzakuteam/scraper-node

Version:

Sebuah Module Scraper yang dibuat oleh Sxyz dan SuzakuTeam untuk memudahkan penggunaan scraper di project ESM maupun CJS.

33 lines (30 loc) 891 B
const getValidIPv4 = (ip) => { const match = !ip || ip.match( /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\/([0-9]|[1-2][0-9]|3[0-2]))?$/, ); if (match) { if (match[5]) { const mask = parseInt(match[5], 10); let [a, b, c, d] = ip.split(".").map((x) => parseInt(x, 10)); const max = (1 << (32 - mask)) - 1; const rand = Math.floor(Math.random() * max); d += rand; c += Math.floor(d / 256); d %= 256; b += Math.floor(c / 256); c %= 256; a += Math.floor(b / 256); b %= 256; return `${a}.${b}.${c}.${d}`; } return ip; } return undefined; }; const generateRandomIP = () => { const octet = () => Math.floor(Math.random() * 256); return `${octet()}.${octet()}.${octet()}.${octet()}`; }; module.exports = { generateRandomIP, getValidIPv4 };