Skip to content

Commit 565de9e

Browse files
committed
allow both ipv4 & ipv6 hosts for same domain. #536
1 parent 5d3d80a commit 565de9e

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

src/common/normalize.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ interface IDomainsIPMap {
2323
}
2424

2525
export const parseLine = (line: string): IHostsLineObj => {
26-
let [ cnt, ...cmt ] = line.split('#')
26+
let [cnt, ...cmt] = line.split('#')
2727
let comment = cmt.join('#').trim()
2828

29-
let [ ip, ...domains ] = cnt.trim().replace(/\s+/g, ' ').split(' ')
29+
let [ip, ...domains] = cnt.trim().replace(/\s+/g, ' ').split(' ')
3030

3131
return { ip, domains, comment }
3232
}
@@ -36,7 +36,7 @@ export const formatLine = (o: Partial<IHostsLineObj>): string => {
3636
if (comment) {
3737
comment = '# ' + comment
3838
}
39-
return [ o.ip || '', (o.domains || []).join(' '), comment ].join(' ').trim()
39+
return [o.ip || '', (o.domains || []).join(' '), comment].join(' ').trim()
4040
}
4141

4242
const removeDuplicateRecords = (content: string): string => {
@@ -52,14 +52,17 @@ const removeDuplicateRecords = (content: string): string => {
5252
return
5353
}
5454

55+
const ipv = /:/.test(ip) ? 6 : 4
56+
5557
let new_domains: string[] = []
5658
let duplicate_domains: string[] = []
5759
domains.map(domain => {
58-
if (domain in domain_ip_map) {
60+
const domain_v = `${domain}_${ipv}`
61+
if (domain_v in domain_ip_map) {
5962
duplicate_domains.push(domain)
6063
} else {
6164
new_domains.push(domain)
62-
domain_ip_map[domain] = ip
65+
domain_ip_map[domain_v] = ip
6366
}
6467
})
6568

test/_base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as path from 'path'
1010
global.db_dir = path.join(__dirname, 'tmp', 'db')
1111

1212
import { swhdb } from '@root/main/data'
13-
import Db from '@root/main/utils/db'
13+
import Db from 'potdb'
1414

1515
declare global {
1616
namespace NodeJS {

test/common/mock/normalize.001.input.hosts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313
127.0.0.1 d.com # some e.com note
1414

1515
127.0.0.2 b.com c.com e.com d.com # comment for e.com
16+
17+
::1 localhost

test/common/mock/normalize.001.output.hosts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414

1515
127.0.0.2 e.com # comment for e.com
1616
# invalid hosts (repeated): 127.0.0.2 b.com c.com d.com
17+
18+
::1 localhost

0 commit comments

Comments
 (0)