@@ -3,7 +3,6 @@ package parsers
33import (
44 "bufio"
55 "fmt"
6- "net"
76 "os"
87 "sort"
98 "strconv"
@@ -149,14 +148,14 @@ func parseHostString(host string) ([]string, error) {
149148
150149 // 检查是否为CIDR格式
151150 if strings .Contains (h , "/" ) {
152- cidrHosts , err := parseCIDR ( h )
151+ cidrHosts , err := parseIPCIDR ( h , SimpleMaxHosts )
153152 if err != nil {
154153 return nil , fmt .Errorf ("解析CIDR %s 失败: %v" , h , err )
155154 }
156155 hosts = append (hosts , cidrHosts ... )
157156 } else if strings .Contains (h , "-" ) && ! strings .Contains (h , ":" ) {
158157 // IP范围格式 (如 192.168.1.1-10)
159- rangeHosts , err := parseIPRange ( h )
158+ rangeHosts , err := parseIPRangeString ( h , SimpleMaxHosts )
160159 if err != nil {
161160 return nil , fmt .Errorf ("解析IP范围 %s 失败: %v" , h , err )
162161 }
@@ -170,106 +169,6 @@ func parseHostString(host string) ([]string, error) {
170169 return hosts , nil
171170}
172171
173- // parseCIDR 解析CIDR格式的网段
174- func parseCIDR (cidr string ) ([]string , error ) {
175- _ , ipNet , err := net .ParseCIDR (cidr )
176- if err != nil {
177- return nil , err
178- }
179-
180- var hosts []string
181- for ip := ipNet .IP .Mask (ipNet .Mask ); ipNet .Contains (ip ); nextIP (ip ) {
182- hosts = append (hosts , ip .String ())
183-
184- // 限制最大主机数量,防止内存溢出
185- if len (hosts ) > SimpleMaxHosts {
186- break
187- }
188- }
189-
190- // 移除网络地址和广播地址
191- if len (hosts ) > 2 {
192- hosts = hosts [1 : len (hosts )- 1 ]
193- }
194-
195- return hosts , nil
196- }
197-
198- // parseIPRange 解析IP范围
199- func parseIPRange (rangeStr string ) ([]string , error ) {
200- parts := strings .Split (rangeStr , "-" )
201- if len (parts ) != 2 {
202- return nil , fmt .Errorf ("无效的IP范围格式: %s" , rangeStr )
203- }
204-
205- startIP := strings .TrimSpace (parts [0 ])
206- endPart := strings .TrimSpace (parts [1 ])
207-
208- // 检查是否为短格式 (如 192.168.1.1-10)
209- if ! strings .Contains (endPart , "." ) {
210- return parseShortIPRange (startIP , endPart )
211- }
212-
213- // 完整IP范围
214- return parseFullIPRange (startIP , endPart )
215- }
216-
217- // parseShortIPRange 解析短格式IP范围
218- func parseShortIPRange (startIPStr , endSuffix string ) ([]string , error ) {
219- startIP := net .ParseIP (startIPStr )
220- if startIP == nil {
221- return nil , fmt .Errorf ("无效的起始IP: %s" , startIPStr )
222- }
223-
224- endNum , err := strconv .Atoi (endSuffix )
225- if err != nil {
226- return nil , fmt .Errorf ("无效的结束数字: %s" , endSuffix )
227- }
228-
229- var hosts []string
230- startIPParts := strings .Split (startIPStr , "." )
231- if len (startIPParts ) != IPv4OctetCount {
232- return nil , fmt .Errorf ("无效的IP格式: %s" , startIPStr )
233- }
234-
235- baseIP := strings .Join (startIPParts [:3 ], "." )
236- startNum , _ := strconv .Atoi (startIPParts [3 ])
237-
238- for i := startNum ; i <= endNum && i <= RouterSwitchLastOctet ; i ++ {
239- hosts = append (hosts , fmt .Sprintf ("%s.%d" , baseIP , i ))
240- }
241-
242- return hosts , nil
243- }
244-
245- // parseFullIPRange 解析完整IP范围
246- func parseFullIPRange (startIPStr , endIPStr string ) ([]string , error ) {
247- startIP := net .ParseIP (startIPStr )
248- endIP := net .ParseIP (endIPStr )
249-
250- if startIP == nil || endIP == nil {
251- return nil , fmt .Errorf ("无效的IP地址" )
252- }
253-
254- var hosts []string
255- for ip := make (net.IP , len (startIP )); ; nextIP (ip ) {
256- copy (ip , startIP )
257- hosts = append (hosts , ip .String ())
258-
259- if ip .Equal (endIP ) {
260- break
261- }
262-
263- // 限制最大主机数量
264- if len (hosts ) > SimpleMaxHosts {
265- break
266- }
267-
268- nextIP (startIP )
269- }
270-
271- return hosts , nil
272- }
273172
274173// parsePortRange 解析端口范围
275174func parsePortRange (rangeStr string ) []int {
@@ -357,12 +256,3 @@ func removeDuplicatePorts(slice []int) []int {
357256 return result
358257}
359258
360- // nextIP 计算下一个IP地址
361- func nextIP (ip net.IP ) {
362- for j := len (ip ) - 1 ; j >= 0 ; j -- {
363- ip [j ]++
364- if ip [j ] > 0 {
365- break
366- }
367- }
368- }
0 commit comments