Skip to content

Commit

Permalink
♻️ 优化 IPv4 / IPv6 地址判断
Browse files Browse the repository at this point in the history
  • Loading branch information
naiba committed Jun 22, 2021
1 parent 365148e commit d20f33a
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pkg/utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ func resolveIP(addr string, ipv6 bool) (string, error) {
var ipv4Resolved, ipv6Resolved bool

for i := 0; i < len(res); i++ {
if ip4 := res[i].To4(); ip4 != nil && !ipv6 {
ip := res[i].String()
if strings.Contains(ip, ".") && !ipv6 {
ipv4Resolved = true
url[0] = ip4.String()
url[0] = ip
break
}
if ip6 := res[i].To16(); ip6 != nil && ipv6 {
if strings.Contains(ip, ":") && ipv6 {
ipv6Resolved = true
url[0] = "[" + ip6.String() + "]"
url[0] = "[" + ip + "]"
break
}
}
Expand Down

0 comments on commit d20f33a

Please sign in to comment.