From 5d154ce6a147f58540ae97ea76ca49d90aba0771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=B1=E8=88=9E=E8=80=85?= Date: Mon, 13 Nov 2023 11:37:53 +0800 Subject: [PATCH] Update ParseIP.go --- common/ParseIP.go | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/common/ParseIP.go b/common/ParseIP.go index 17ddbc2..96f2317 100644 --- a/common/ParseIP.go +++ b/common/ParseIP.go @@ -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-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) if filename != "" { var filehost []string - filehost, _ = readipfile(hostPort, filename) + filehost, _ = Readipfile(filename) hosts = append(hosts, filehost...) } @@ -55,7 +55,7 @@ func ParseIP(hostPort *[]string, host string, filename string, nohosts ...string } } hosts = RemoveDuplicate(hosts) - if len(hosts) == 0 && len(*hostPort) == 0 && host != "" && filename != "" { + if len(hosts) == 0 && len(HostPort) == 0 && host != "" && filename != "" { err = ParseIPErr } return @@ -114,8 +114,9 @@ func parseIP2(host string) (hosts []string) { return } -// 解析ip段: 192.168.111.1-255 +// 解析ip段: // +// 192.168.111.1-255 // 192.168.111.1-192.168.112.255 func parseIP1(ip string) []string { IPRange := strings.Split(ip, "-") @@ -176,22 +177,19 @@ func IPRange(c *net.IPNet) string { } // 按行读ip -func readipfile(hostPort *[]string, filename string) ([]string, error) { +func Readipfile(filename string) ([]string, error) { file, err := os.Open(filename) if err != nil { fmt.Printf("Open %s error, %v", filename, err) os.Exit(0) } defer file.Close() - + var content []string scanner := bufio.NewScanner(file) scanner.Split(bufio.ScanLines) - - var content []string for scanner.Scan() { line := strings.TrimSpace(scanner.Text()) if line != "" { - var hosts []string text := strings.Split(line, ":") if len(text) == 2 { 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) { continue } - hosts = ParseIPs(text[0]) + hosts := ParseIPs(text[0]) for _, host := range hosts { - *hostPort = append(*hostPort, fmt.Sprintf("%s:%s", host, port)) + HostPort = append(HostPort, fmt.Sprintf("%s:%s", host, port)) } } else { - hosts = ParseIPs(line) + host := ParseIPs(line) + content = append(content, host...) } - content = append(content, hosts...) } } return content, nil