ParseHost 解析主机字符串,返回Host结构体
(hostStr string)
| 67 | |
| 68 | // ParseHost 解析主机字符串,返回Host结构体 |
| 69 | func ParseHost(hostStr string) (Host, error) { |
| 70 | hostStr = strings.TrimSpace(hostStr) |
| 71 | |
| 72 | // 尝试解析为IP地址 |
| 73 | if ip := net.ParseIP(hostStr); ip != nil { |
| 74 | return Host{ |
| 75 | IP: ip, |
| 76 | Origin: hostStr, |
| 77 | Type: HostTypeIP, |
| 78 | }, nil |
| 79 | } |
| 80 | |
| 81 | // 尝试解析为CIDR |
| 82 | if _, _, err := net.ParseCIDR(hostStr); err == nil { |
| 83 | return Host{ |
| 84 | Origin: hostStr, |
| 85 | Type: HostTypeCIDR, |
| 86 | }, nil |
| 87 | } |
| 88 | |
| 89 | // 尝试解析为域名 |
| 90 | if ValidateDomainName(hostStr) { |
| 91 | return Host{ |
| 92 | Origin: hostStr, |
| 93 | Type: HostTypeDomain, |
| 94 | }, nil |
| 95 | } |
| 96 | |
| 97 | return Host{}, fmt.Errorf("无法解析主机: %s", hostStr) |
| 98 | } |
| 99 | |
| 100 | // Iterate 从Reader中迭代读取主机信息 |
| 101 | func Iterate(reader io.Reader) <-chan Host { |
no test coverage detected