Update icmp.go

This commit is contained in:
影舞者 2023-11-13 10:45:48 +08:00 committed by GitHub
parent dc0dd7a469
commit 9d02632dcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,16 +3,14 @@ package Plugins
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/shadow1ng/fscan/common"
"golang.org/x/net/icmp"
"net" "net"
"os/exec" "os/exec"
"runtime" "runtime"
"strings" "strings"
"sync" "sync"
"time" "time"
"github.com/fatih/color"
"github.com/shadow1ng/fscan/common"
"golang.org/x/net/icmp"
) )
var ( var (
@ -22,17 +20,17 @@ var (
livewg sync.WaitGroup livewg sync.WaitGroup
) )
func CheckLive(hostslist []string, ping bool, liveTop int) []string { func CheckLive(hostslist []string, Ping bool) []string {
chanHosts := make(chan string, len(hostslist)) chanHosts := make(chan string, len(hostslist))
go func() { go func() {
for ip := range chanHosts { for ip := range chanHosts {
if _, ok := ExistHosts[ip]; !ok && IsContain(hostslist, ip) { if _, ok := ExistHosts[ip]; !ok && IsContain(hostslist, ip) {
ExistHosts[ip] = struct{}{} ExistHosts[ip] = struct{}{}
if !common.Silent { if common.Silent == false {
if !ping { if Ping == false {
color.Green("(icmp) Target %-15s is alive\n", ip) fmt.Printf("(icmp) Target %-15s is alive\n", ip)
} else { } else {
color.Green("(ping) Target %-15s is alive\n", ip) fmt.Printf("(ping) Target %-15s is alive\n", ip)
} }
} }
AliveHosts = append(AliveHosts, ip) AliveHosts = append(AliveHosts, ip)
@ -41,17 +39,17 @@ func CheckLive(hostslist []string, ping bool, liveTop int) []string {
} }
}() }()
if ping { if Ping == true {
// use ping detection //使用ping探测
RunPing(hostslist, chanHosts) RunPing(hostslist, chanHosts)
} else { } else {
// try to listen to local icmp first, batch detection //优先尝试监听本地icmp,批量探测
conn, err := icmp.ListenPacket("ip4:icmp", "0.0.0.0") conn, err := icmp.ListenPacket("ip4:icmp", "0.0.0.0")
if err == nil { if err == nil {
RunIcmp1(hostslist, conn, chanHosts) RunIcmp1(hostslist, conn, chanHosts)
} else { } else {
common.LogError(err) common.LogError(err)
// Try no listening icmp probe //尝试无监听icmp探测
fmt.Println("trying RunIcmp2") fmt.Println("trying RunIcmp2")
conn, err := net.DialTimeout("ip4:icmp", "127.0.0.1", 3*time.Second) conn, err := net.DialTimeout("ip4:icmp", "127.0.0.1", 3*time.Second)
defer func() { defer func() {
@ -63,7 +61,7 @@ func CheckLive(hostslist []string, ping bool, liveTop int) []string {
RunIcmp2(hostslist, chanHosts) RunIcmp2(hostslist, chanHosts)
} else { } else {
common.LogError(err) common.LogError(err)
// use ping detection //使用ping探测
fmt.Println("The current user permissions unable to send icmp packets") fmt.Println("The current user permissions unable to send icmp packets")
fmt.Println("start ping") fmt.Println("start ping")
RunPing(hostslist, chanHosts) RunPing(hostslist, chanHosts)
@ -75,14 +73,14 @@ func CheckLive(hostslist []string, ping bool, liveTop int) []string {
close(chanHosts) close(chanHosts)
if len(hostslist) > 1000 { if len(hostslist) > 1000 {
arrTop, arrLen := ArrayCountValueTop(AliveHosts, liveTop, true) arrTop, arrLen := ArrayCountValueTop(AliveHosts, common.LiveTop, true)
for i := 0; i < len(arrTop); i++ { for i := 0; i < len(arrTop); i++ {
output := fmt.Sprintf("[*] LiveTop %-16s 段存活数量为: %d", arrTop[i]+".0.0/16", arrLen[i]) output := fmt.Sprintf("[*] LiveTop %-16s 段存活数量为: %d", arrTop[i]+".0.0/16", arrLen[i])
common.LogSuccess(output) common.LogSuccess(output)
} }
} }
if len(hostslist) > 256 { if len(hostslist) > 256 {
arrTop, arrLen := ArrayCountValueTop(AliveHosts, liveTop, false) arrTop, arrLen := ArrayCountValueTop(AliveHosts, common.LiveTop, false)
for i := 0; i < len(arrTop); i++ { for i := 0; i < len(arrTop); i++ {
output := fmt.Sprintf("[*] LiveTop %-16s 段存活数量为: %d", arrTop[i]+".0/24", arrLen[i]) output := fmt.Sprintf("[*] LiveTop %-16s 段存活数量为: %d", arrTop[i]+".0/24", arrLen[i])
common.LogSuccess(output) common.LogSuccess(output)
@ -96,7 +94,7 @@ func RunIcmp1(hostslist []string, conn *icmp.PacketConn, chanHosts chan string)
endflag := false endflag := false
go func() { go func() {
for { for {
if endflag { if endflag == true {
return return
} }
msg := make([]byte, 100) msg := make([]byte, 100)