server: replace domain from URL RegEx with plain split, slice and join

This commit is contained in:
Jesse Chan
2021-02-04 08:09:27 +08:00
parent 1aa071dac7
commit 04a8573511
2 changed files with 11 additions and 25 deletions

View File

@@ -1,5 +1,3 @@
import {domainName as matchDomainName} from '../../shared/util/regEx';
import type {TorrentProperties} from '../../shared/types/Torrent';
export const hasTorrentFinished = (
@@ -22,31 +20,20 @@ export const hasTorrentFinished = (
};
export const getDomainsFromURLs = (urls: Array<string>): Array<string> => {
const domains: Array<string> = [];
return [
...urls.reduce((memo: Set<string>, url: string) => {
const fqdn = url.split('/')[2]?.split(':')[0];
urls.forEach((url) => {
const regexMatched = matchDomainName.exec(url);
if (fqdn?.length > 0) {
const domainLastParts = fqdn.split('.').slice(-2);
if (regexMatched != null && regexMatched[1]) {
let domain = regexMatched[1];
const minSubsetLength = 3;
const domainSubsets = domain.split('.');
let desiredSubsets = 2;
if (domainSubsets.length > desiredSubsets) {
const lastDesiredSubset = domainSubsets[domainSubsets.length - desiredSubsets];
if (lastDesiredSubset.length <= minSubsetLength) {
desiredSubsets += 1;
// ignore IP addresses. RFC 952: first char of a valid TLD must be an alpha char
if (!'0123456789'.includes(domainLastParts[domainLastParts.length - 1]?.[0])) {
memo.add(domainLastParts.join('.'));
}
}
domain = domainSubsets.slice(desiredSubsets * -1).join('.');
domains.push(domain);
}
});
// Deduplicate
return [...new Set(domains)];
return memo;
}, new Set()),
];
};

View File

@@ -1,4 +1,3 @@
export const url = /^(?:https?|ftp):\/\/.{1,}\.{1}.{1,}/;
export const domainName = /(?:https?|udp):\/\/(?:www\.)?([-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,18}\b)*(\/[/\d\w.-]*)*(?:[?])*(.+)*/i;
export const cdata = /<!\[CDATA\[(.*?)\]\]>/;
export const noComma = /^[^,]+$/;