Skip to content

Commit 735ddbd

Browse files
committed
fix: only get ipv4
1 parent c5bc3b5 commit 735ddbd

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

fetch_hosts.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,17 +272,23 @@ func FetchHosts(domains []string) (hostsJson, hostsFile []byte, now string, err
272272
hosts := make([][]string, 0, len(domains))
273273
hostsFileData := bytes.NewBufferString("# fetch-github-hosts begin\n")
274274
for _, domain := range domains {
275-
host, err := net.LookupHost(domain)
276-
if err != nil {
275+
ipList, sErr := net.LookupIP(domain)
276+
if sErr != nil {
277277
fmt.Printf("%s: %s\b", t(&i18n.Message{
278278
ID: "GetHostRecordErr",
279279
Other: "获取主机记录失败",
280-
}), err.Error())
280+
}), sErr.Error())
281281
continue
282282
}
283-
item := []string{host[0], domain}
284-
hosts = append(hosts, item)
285-
hostsFileData.WriteString(fmt.Sprintf("%-28s%s\n", item[0], item[1]))
283+
for _, ip := range ipList {
284+
ipv4 := ip.To4()
285+
if ipv4 != nil {
286+
item := []string{ipv4.String(), domain}
287+
hosts = append(hosts, item)
288+
hostsFileData.WriteString(fmt.Sprintf("%-28s%s\n", item[0], item[1]))
289+
break
290+
}
291+
}
286292
}
287293
hostsFileData.WriteString("# last fetch time: ")
288294
hostsFileData.WriteString(now)

0 commit comments

Comments
 (0)