@@ -3,8 +3,11 @@ package main
33import (
44 "bufio"
55 "errors"
6+ "fmt"
67 "io"
78 "log/slog"
9+ "math"
10+ "math/big"
811 "net"
912 "net/netip"
1013 "regexp"
@@ -108,6 +111,67 @@ func ExistOnlyOne(arr []string) bool {
108111 }
109112 return exist
110113}
114+ func IterateAddr (addr string ) <- chan Host {
115+ hostChan := make (chan Host )
116+ _ , _ , err := net .ParseCIDR (addr )
117+ if err == nil {
118+ // is CIDR
119+ return Iterate (strings .NewReader (addr ))
120+ }
121+ ip := net .ParseIP (addr )
122+ if ip == nil {
123+ ip , err = LookupIP (addr )
124+ if err != nil {
125+ close (hostChan )
126+ slog .Error ("Not a valid IP, IP CIDR or domain" , "addr" , addr )
127+ return hostChan
128+ }
129+ }
130+ go func () {
131+ slog .Info ("Enable infinite mode" , "init" , ip .String ())
132+ lowIP := ip
133+ highIP := ip
134+ hostChan <- Host {
135+ IP : ip ,
136+ Origin : addr ,
137+ Type : HostTypeIP ,
138+ }
139+ for i := 0 ; i < math .MaxInt ; i ++ {
140+ if i % 2 == 0 {
141+ lowIP = NextIP (lowIP , false )
142+ hostChan <- Host {
143+ IP : lowIP ,
144+ Origin : lowIP .String (),
145+ Type : HostTypeIP ,
146+ }
147+ } else {
148+ highIP = NextIP (highIP , true )
149+ hostChan <- Host {
150+ IP : highIP ,
151+ Origin : highIP .String (),
152+ Type : HostTypeIP ,
153+ }
154+ }
155+ }
156+ }()
157+ return hostChan
158+ }
159+ func LookupIP (addr string ) (net.IP , error ) {
160+ ips , err := net .LookupIP (addr )
161+ if err != nil {
162+ return nil , fmt .Errorf ("failed to lookup: %w" , err )
163+ }
164+ var arr []net.IP
165+ for _ , ip := range ips {
166+ if ip .To4 () != nil || enableIPv6 {
167+ arr = append (arr , ip )
168+ }
169+ }
170+ if len (arr ) == 0 {
171+ return nil , errors .New ("no IP found" )
172+ }
173+ return arr [0 ], nil
174+ }
111175func RemoveDuplicateStr (strSlice []string ) []string {
112176 allKeys := make (map [string ]bool )
113177 var list []string
@@ -128,3 +192,17 @@ func OutWriter(writer io.Writer) chan<- string {
128192 }()
129193 return ch
130194}
195+ func NextIP (ip net.IP , increment bool ) net.IP {
196+ // Convert to big.Int and increment
197+ ipb := big .NewInt (0 ).SetBytes (ip )
198+ if increment {
199+ ipb .Add (ipb , big .NewInt (1 ))
200+ } else {
201+ ipb .Sub (ipb , big .NewInt (1 ))
202+ }
203+
204+ // Add leading zeros
205+ b := ipb .Bytes ()
206+ b = append (make ([]byte , len (ip )- len (b )), b ... )
207+ return b
208+ }
0 commit comments