Update ParseIP.go

This commit is contained in:
影舞者 2023-11-13 11:37:53 +08:00 committed by GitHub
parent 59983affb7
commit 5d154ce6a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,11 +23,11 @@ var ParseIPErr = errors.New(" host parsing error\n" +
"192.168.1.1-192.168.255.255\n" + "192.168.1.1-192.168.255.255\n" +
"192.168.1.1-255") "192.168.1.1-255")
func ParseIP(hostPort *[]string, host string, filename string, nohosts ...string) (hosts []string, err error) { func ParseIP(host string, filename string, nohosts ...string) (hosts []string, err error) {
hosts = ParseIPs(host) hosts = ParseIPs(host)
if filename != "" { if filename != "" {
var filehost []string var filehost []string
filehost, _ = readipfile(hostPort, filename) filehost, _ = Readipfile(filename)
hosts = append(hosts, filehost...) hosts = append(hosts, filehost...)
} }
@ -55,7 +55,7 @@ func ParseIP(hostPort *[]string, host string, filename string, nohosts ...string
} }
} }
hosts = RemoveDuplicate(hosts) hosts = RemoveDuplicate(hosts)
if len(hosts) == 0 && len(*hostPort) == 0 && host != "" && filename != "" { if len(hosts) == 0 && len(HostPort) == 0 && host != "" && filename != "" {
err = ParseIPErr err = ParseIPErr
} }
return return
@ -114,8 +114,9 @@ func parseIP2(host string) (hosts []string) {
return return
} }
// 解析ip段: 192.168.111.1-255 // 解析ip段:
// //
// 192.168.111.1-255
// 192.168.111.1-192.168.112.255 // 192.168.111.1-192.168.112.255
func parseIP1(ip string) []string { func parseIP1(ip string) []string {
IPRange := strings.Split(ip, "-") IPRange := strings.Split(ip, "-")
@ -176,22 +177,19 @@ func IPRange(c *net.IPNet) string {
} }
// 按行读ip // 按行读ip
func readipfile(hostPort *[]string, filename string) ([]string, error) { func Readipfile(filename string) ([]string, error) {
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {
fmt.Printf("Open %s error, %v", filename, err) fmt.Printf("Open %s error, %v", filename, err)
os.Exit(0) os.Exit(0)
} }
defer file.Close() defer file.Close()
var content []string
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines) scanner.Split(bufio.ScanLines)
var content []string
for scanner.Scan() { for scanner.Scan() {
line := strings.TrimSpace(scanner.Text()) line := strings.TrimSpace(scanner.Text())
if line != "" { if line != "" {
var hosts []string
text := strings.Split(line, ":") text := strings.Split(line, ":")
if len(text) == 2 { if len(text) == 2 {
port := strings.Split(text[1], " ")[0] port := strings.Split(text[1], " ")[0]
@ -199,14 +197,14 @@ func readipfile(hostPort *[]string, filename string) ([]string, error) {
if err != nil || (num < 1 || num > 65535) { if err != nil || (num < 1 || num > 65535) {
continue continue
} }
hosts = ParseIPs(text[0]) hosts := ParseIPs(text[0])
for _, host := range hosts { for _, host := range hosts {
*hostPort = append(*hostPort, fmt.Sprintf("%s:%s", host, port)) HostPort = append(HostPort, fmt.Sprintf("%s:%s", host, port))
} }
} else { } else {
hosts = ParseIPs(line) host := ParseIPs(line)
content = append(content, host...)
} }
content = append(content, hosts...)
} }
} }
return content, nil return content, nil