Skip to content

Commit

Permalink
fix: 协程运行完成等待逻辑
Browse files Browse the repository at this point in the history
add: 添加编译tags支持,nosyn 可以禁止引入syn,pcap模块
  • Loading branch information
XinRoom committed Dec 26, 2022
1 parent 96c32dd commit 36747a6
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 86 deletions.
74 changes: 31 additions & 43 deletions cmd/go-portScan.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package main

import (
"bufio"
"fmt"
"github.com/XinRoom/go-portScan/core/host"
"github.com/XinRoom/go-portScan/core/port"
"github.com/XinRoom/go-portScan/core/port/fingerprint"
"github.com/XinRoom/go-portScan/core/port/syn"
"github.com/XinRoom/go-portScan/util"
"github.com/XinRoom/iprange"
"github.com/google/gopacket/pcap"
"github.com/panjf2000/ants/v2"
"github.com/urfave/cli/v2"
"log"
Expand All @@ -22,19 +20,24 @@ import (
)

var (
ipStr string
portStr string
pn bool
sT bool
rate int
sV bool
timeout int
rateP int
iL string
devices bool
dev string
httpx bool
netLive bool
ipStr string
portStr string
pn bool
sT bool
rate int
sV bool
timeout int
rateP int
iL string
devices bool
dev string
httpx bool
netLive bool
bp bool
user string
passwd string
service string
threadNum int
)

func parseFlag(c *cli.Context) {
Expand All @@ -59,12 +62,10 @@ func run(c *cli.Context) error {
}
parseFlag(c)
if devices {
pcapDevices, err := pcap.FindAllDevs()
if err != nil {
log.Fatalf("list pcapDevices failed: %s", err.Error())
}
for _, dev := range pcapDevices {
fmt.Println("Dev:", dev.Name, "\tDes:", dev.Description)
if r, err := syn.GetAllDevs(); err != nil {
log.Fatal(err.Error())
} else {
fmt.Print(r)
}
os.Exit(0)
}
Expand All @@ -82,19 +83,11 @@ func run(c *cli.Context) error {
ips = strings.Split(ipStr, ",")
}
if iL != "" {
file, err := os.Open(iL)
var err error
ips, err = util.GetLines(iL)
if err != nil {
log.Fatalf("open file failed: %s", err.Error())
}
scanner := bufio.NewScanner(file)
var line string
for scanner.Scan() {
line = strings.TrimSpace(scanner.Text())
if line != "" {
ips = append(ips, line)
}
}
file.Close()
}
for _, _ip := range ips {
it, startIp, err := iprange.NewIter(_ip)
Expand Down Expand Up @@ -152,8 +145,7 @@ func run(c *cli.Context) error {
}

// recv
single1 := make(chan struct{})
single2 := make(chan struct{})
single := make(chan struct{})
retChan := make(chan port.OpenIpPort, 65535)
// port fingerprint
var httpxFile *os.File
Expand Down Expand Up @@ -188,12 +180,13 @@ func run(c *cli.Context) error {
})
defer poolPortIdentify.Release()
go func() {
sendOverFlag := false
for {
select {
case <-single1:
sendOverFlag = true
case ret := <-retChan:
if ret.Port == 0 {
single <- struct{}{}
return
}
if sV || httpx {
// port fingerprint
wgPortIdentify.Add(1)
Expand All @@ -203,10 +196,6 @@ func run(c *cli.Context) error {
fmt.Printf("%v:%d\n", ret.Ip, ret.Port)
}
default:
if sendOverFlag {
single2 <- struct{}{}
return
}
time.Sleep(time.Millisecond * 10)
}
}
Expand Down Expand Up @@ -297,12 +286,11 @@ func run(c *cli.Context) error {
}
}
}
wgScan.Wait() // 扫描器-发
wgPing.Wait() // PING组
wgScan.Wait() // 扫描器-发
s.Wait() // 扫描器-等
s.Close() // 扫描器-收
single1 <- struct{}{} // 告知接收器没有新端口输出了
<-single2 // 接收器-收
<-single // 接收器-收
wgPortIdentify.Wait() // 识别器-收
fmt.Printf("[*] elapsed time: %s\n", time.Since(start))
return nil
Expand Down
2 changes: 2 additions & 0 deletions core/port/syn/device.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nosyn

package syn

import (
Expand Down
19 changes: 14 additions & 5 deletions core/port/syn/syn.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ import (
limiter "golang.org/x/time/rate"
"math/rand"
"net"
"strings"
"sync"
"time"
)

var DefaultSynOption = port.Option{
Rate: 2000,
Timeout: 800,
}

type synScanner struct {
srcMac, gwMac net.HardwareAddr // macAddr
devName string // eth dev(pcap)
Expand Down Expand Up @@ -214,6 +210,7 @@ func (ss *synScanner) Wait() {
// Close cleans up the handle and chan.
func (ss *synScanner) Close() {
ss.isDone = true
ss.retChan <- port.OpenIpPort{}
if ss.handle != nil {
ss.handle.Close()
}
Expand Down Expand Up @@ -421,3 +418,15 @@ func (ss *synScanner) recv() {
}
}
}

func GetAllDevs() (string, error) {
pcapDevices, err := pcap.FindAllDevs()
if err != nil {
return "", errors.New(fmt.Sprintf("list pcapDevices failed: %s", err.Error()))
}
var buf strings.Builder
for _, dev := range pcapDevices {
buf.WriteString(fmt.Sprintln("Dev:", dev.Name, "\tDes:", dev.Description))
}
return buf.String(), nil
}
2 changes: 2 additions & 0 deletions core/port/syn/watchIpStatus.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nosyn

package syn

import (
Expand Down
2 changes: 2 additions & 0 deletions core/port/syn/watchMacCache.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nosyn

package syn

import (
Expand Down
1 change: 1 addition & 0 deletions core/port/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (ts *tcpScanner) Wait() {
// Close chan
func (ts *tcpScanner) Close() {
ts.isDone = true
ts.retChan <- OpenIpPort{}
}

// WaitLimiter Waiting for the speed limit
Expand Down
47 changes: 35 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,50 @@ module github.com/XinRoom/go-portScan
go 1.17

require (
github.com/XinRoom/iprange v1.1.1
github.com/go-ping/ping v0.0.0-20211130115550-779d1e919534
github.com/XinRoom/iprange v1.1.3
github.com/go-ping/ping v1.1.0
github.com/go-redis/redis v6.15.9+incompatible
github.com/go-sql-driver/mysql v1.6.0
github.com/google/gopacket v1.1.19
github.com/icodeface/grdp v0.0.0-20200414055757-e0008b0b5cb2
github.com/jackpal/gateway v1.0.7
github.com/libp2p/go-netroute v0.2.0
github.com/panjf2000/ants/v2 v2.5.0
github.com/projectdiscovery/stringsutil v0.0.0-20220712170325-48c50c332cb4
github.com/urfave/cli/v2 v2.6.0
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4
golang.org/x/text v0.3.7
golang.org/x/time v0.0.0-20220411224347-583f2d630306
github.com/lib/pq v1.10.7
github.com/libp2p/go-netroute v0.2.1
github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed
github.com/panjf2000/ants/v2 v2.6.0
github.com/projectdiscovery/stringsutil v0.0.2
github.com/sijms/go-ora/v2 v2.5.7
github.com/smallfish/ftp v0.0.0-20160801035311-6d094f003ac5
github.com/urfave/cli/v2 v2.23.5
go.mongodb.org/mongo-driver v1.11.0
golang.org/x/crypto v0.3.0
golang.org/x/net v0.2.0
golang.org/x/text v0.4.0
golang.org/x/time v0.2.0
)

require (
github.com/aymerick/douceur v0.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/microcosm-cc/bluemonday v1.0.19 // indirect
github.com/icodeface/tls v0.0.0-20190904082144-a3e1fe30543e // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/lunixbochs/struc v0.0.0-20190326164542-a9e4041416c2 // indirect
github.com/microcosm-cc/bluemonday v1.0.21 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.22.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.2.0 // indirect
)
Loading

0 comments on commit 36747a6

Please sign in to comment.