Skip to content

Commit

Permalink
更加随机的 IP 扫描
Browse files Browse the repository at this point in the history
以前是随机扫描 IP 段, 但是扫描 IP 时是顺序的, 必须等待这个段扫描完毕才会扫下一个段,
此更改可以在当前 IP 段没有扫描完成的情况下扫描其他 IP 段。
  • Loading branch information
Kisesy committed Jul 7, 2023
1 parent b01ed19 commit 18e04c3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
3 changes: 3 additions & 0 deletions gscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -45,6 +46,8 @@ type GScanConfig struct {
}

func init() {
rand.Seed(time.Now().UnixNano())

log.SetFlags(log.LstdFlags | log.Lshortfile)
}

Expand Down
12 changes: 9 additions & 3 deletions iprange.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,19 @@ func parseIPRangeFile(file string) (chan string, error) {
rand.Shuffle(len(ipranges), func(i, j int) {
ipranges[i], ipranges[j] = ipranges[j], ipranges[i]
})
for i := range ipranges {

// 打乱IP的扫描顺序, 不再等待一个IP段扫描完毕再进行下一个, 提高扫描的随机性
// 有时候可以更快的扫到IP
n := 15
if len(ipranges) < n {
n = len(ipranges)
}
ops(len(ipranges), n, func(i, _ int) {
c := ipaddr.NewCursor([]ipaddr.Prefix{ipranges[i]})
for ip := c.First(); ip != nil; ip = c.Next() {
out <- ip.IP.String()
}
}

})
}
}()
return out, nil
Expand Down
2 changes: 2 additions & 0 deletions scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func testIPWorker(ctx context.Context, ipQueue chan string, gcfg *GScanConfig, s
cfg, testFunc := gcfg.getScanConfig(gcfg.ScanMode)

for ip := range ipQueue {
// log.Printf("Start testing IP: %s", ip)

srs.IncScanCounter()

if gcfg.VerifyPing {
Expand Down
19 changes: 19 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path"
"strings"
"sync"
)

// 代码来自: goproxy
Expand Down Expand Up @@ -137,3 +138,21 @@ func pathExist(name string) bool {
_, err := os.Stat(name)
return !os.IsNotExist(err)
}

func ops(count, threads int, op func(i, thread int)) {
var wg sync.WaitGroup
wg.Add(threads)
for i := 0; i < threads; i++ {
s, e := count/threads*i, count/threads*(i+1)
if i == threads-1 {
e = count
}
go func(i, s, e int) {
for j := s; j < e; j++ {
op(j, i)
}
wg.Done()
}(i, s, e)
}
wg.Wait()
}

0 comments on commit 18e04c3

Please sign in to comment.