1
0
Эх сурвалжийг харах

allow both ipv4 & ipv6 hosts for same domain. #536

oldj 4 жил өмнө
parent
commit
565de9ef6a

+ 8 - 5
src/common/normalize.ts

@@ -23,10 +23,10 @@ interface IDomainsIPMap {
 }
 
 export const parseLine = (line: string): IHostsLineObj => {
-  let [ cnt, ...cmt ] = line.split('#')
+  let [cnt, ...cmt] = line.split('#')
   let comment = cmt.join('#').trim()
 
-  let [ ip, ...domains ] = cnt.trim().replace(/\s+/g, ' ').split(' ')
+  let [ip, ...domains] = cnt.trim().replace(/\s+/g, ' ').split(' ')
 
   return { ip, domains, comment }
 }
@@ -36,7 +36,7 @@ export const formatLine = (o: Partial<IHostsLineObj>): string => {
   if (comment) {
     comment = '# ' + comment
   }
-  return [ o.ip || '', (o.domains || []).join(' '), comment ].join(' ').trim()
+  return [o.ip || '', (o.domains || []).join(' '), comment].join(' ').trim()
 }
 
 const removeDuplicateRecords = (content: string): string => {
@@ -52,14 +52,17 @@ const removeDuplicateRecords = (content: string): string => {
       return
     }
 
+    const ipv = /:/.test(ip) ? 6 : 4
+
     let new_domains: string[] = []
     let duplicate_domains: string[] = []
     domains.map(domain => {
-      if (domain in domain_ip_map) {
+      const domain_v = `${domain}_${ipv}`
+      if (domain_v in domain_ip_map) {
         duplicate_domains.push(domain)
       } else {
         new_domains.push(domain)
-        domain_ip_map[domain] = ip
+        domain_ip_map[domain_v] = ip
       }
     })
 

+ 1 - 1
test/_base.ts

@@ -10,7 +10,7 @@ import * as path from 'path'
 global.db_dir = path.join(__dirname, 'tmp', 'db')
 
 import { swhdb } from '@root/main/data'
-import Db from '@root/main/utils/db'
+import Db from 'potdb'
 
 declare global {
   namespace NodeJS {

+ 2 - 0
test/common/mock/normalize.001.input.hosts

@@ -13,3 +13,5 @@
 127.0.0.1 d.com # some e.com note
 
 127.0.0.2 b.com     c.com   e.com  d.com   # comment for e.com
+
+::1		    localhost

+ 2 - 0
test/common/mock/normalize.001.output.hosts

@@ -14,3 +14,5 @@
 
 127.0.0.2 e.com # comment for e.com
 # invalid hosts (repeated): 127.0.0.2 b.com c.com d.com
+
+::1 localhost