Skip to content

Commit

Permalink
fix: only get ipv4
Browse files Browse the repository at this point in the history
  • Loading branch information
Licoy committed Jan 29, 2024
1 parent c5bc3b5 commit 735ddbd
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions fetch_hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,23 @@ func FetchHosts(domains []string) (hostsJson, hostsFile []byte, now string, err
hosts := make([][]string, 0, len(domains))
hostsFileData := bytes.NewBufferString("# fetch-github-hosts begin\n")
for _, domain := range domains {
host, err := net.LookupHost(domain)
if err != nil {
ipList, sErr := net.LookupIP(domain)
if sErr != nil {
fmt.Printf("%s: %s\b", t(&i18n.Message{
ID: "GetHostRecordErr",
Other: "获取主机记录失败",
}), err.Error())
}), sErr.Error())
continue
}
item := []string{host[0], domain}
hosts = append(hosts, item)
hostsFileData.WriteString(fmt.Sprintf("%-28s%s\n", item[0], item[1]))
for _, ip := range ipList {
ipv4 := ip.To4()
if ipv4 != nil {
item := []string{ipv4.String(), domain}
hosts = append(hosts, item)
hostsFileData.WriteString(fmt.Sprintf("%-28s%s\n", item[0], item[1]))
break
}
}
}
hostsFileData.WriteString("# last fetch time: ")
hostsFileData.WriteString(now)
Expand Down

0 comments on commit 735ddbd

Please sign in to comment.