Skip to content

Commit

Permalink
Fix RPZ #26
Browse files Browse the repository at this point in the history
  • Loading branch information
sefinek committed Oct 7, 2024
1 parent 3799378 commit 882ce99
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions scripts/generate/rpz.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@ const convert = async (folderPath = path.join(__dirname, '../../blocklists/templ
// Content
const fileContent = await fs.readFile(thisFileName, 'utf8');

const seenDomains = new Set();
const [, domain] = fileContent.split(' ');
if (seenDomains.has(domain)) return;

const replacedFile = fileContent
.replaceAll(/^(?:127\.0\.0\.1|0\.0\.0\.0) (\S+)/gmu, (_, data) => {
const rootDomain = data.split('.').slice(-2).join('.');
seenDomains.add(rootDomain);
return `${rootDomain} CNAME .\n*.${rootDomain} CNAME .`;
})
const outputLines = fileContent
.split('\n')
.reduce((acc, line) => {
const match = line.match(/^(?:127\.0\.0\.1|0\.0\.0\.0) (\S+)/);
if (match) {
const domain = match[1];
const domainParts = domain.split('.');

if (domainParts.length > 2 && !acc.seenDomains.has(domain)) {
acc.seenDomains.add(domain);
acc.output.push(`${domain} CNAME .\n*.${domain} CNAME .`);
}
}
return acc;
}, { seenDomains: new Set(), output: [] });

const replacedFile = outputLines.output.join('\n')
.replaceAll(/#(?: ?127\.0\.0\.1| ?0\.0\.0\.0) |:: /gmu, '; ')
.replaceAll(/#/gmu, ';')
.replace(//g, '')
Expand All @@ -36,7 +43,10 @@ const convert = async (folderPath = path.join(__dirname, '../../blocklists/templ
.replace(/<LastUpdate>/gim, `${date.full} | ${date.now}`);

const fullNewFile = path.join(generatedPath, file.name);
await fs.writeFile(fullNewFile, `$TTL 300\n@ SOA localhost. root.localhost. ${date.timestamp} 43200 3600 259200 300\n NS localhost.\n;\n${replacedFile}`);
await fs.writeFile(
fullNewFile,
`$TTL 300\n@ SOA localhost. root.localhost. ${date.timestamp} 43200 3600 259200 300\n NS localhost.\n;\n${replacedFile}`
);

console.log(`✔️ ${cacheHash || file.name} ++ ${fullNewFile}`);
}));
Expand Down

0 comments on commit 882ce99

Please sign in to comment.