Skip to content

Commit 15247c7

Browse files
author
feimyy
committed
并发数修正
1 parent 3b027a9 commit 15247c7

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
goSynPortScanner
22
================
3+
####目前只支持在linux运行,可能是最快的tcp syn端口扫描器了
34

4-
USAGE : goSynPortScanner [SourceIP] [SourcePort] [DestStartIP-DestEndIP] [DestStartPort-DestEndPort] [goroutineNum]
55

6-
Example : goSynPortScanner 192.168.1.1 1234 8.8.8.8-8.8.8.9 50-100 10
6+
USAGE : goSynPortScanner [SourceIP] [SourcePort] [DestStartIP]-[DestEndIP] [DestStartPort]-[DestEndPort] [RoutineNum] [IsRandomSrcPort]
7+
8+
Example : goSynPortScanner 192.168.1.1 1234 8.8.8.8-8.8.8.9 53-1024 10 true
79

8-
The Source Address of the packet is : 192.168.1.1
9-
The Source Port of the packet is : 1234
10-
The Destinations Start Address of the packet is : 8.8.8.8
11-
The Destinations End Address of the packet is : 8.8.8.9
12-
The Destinations Start Port of the packet is : 53
13-
The Destinations End Port of the packet is : 100
14-
The numbers of goroutine is : 10
10+
The Source Address of the packet : 192.168.1.1
11+
The Source Port of the packet : 1234
12+
The Destination Start Address of the packet : 8.8.8.8
13+
The Destination End Address of the packet : 8.8.8.8
14+
The Destination Start Port of the packet : 53
15+
The Destination End Port of the packet : 1024
16+
The Routine Number of Will Created : 10
17+
Whether the source port be random generated : false
1518

16-
Note : You must check that The Destinations Address is legal
17-
And The Source Address should be The Address of Network card
19+
Note :
20+
You must check that The Destinations Address is legal
21+
And The Source Address should be The Address of Network card

src/main/goSynPortScanner.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"strconv"
1616
"strings"
1717
"sync"
18+
"time"
1819
)
1920

2021
const (
@@ -24,13 +25,24 @@ const (
2425
var mutex sync.Mutex
2526

2627
func usage() {
27-
fmt.Printf("\nUSAGE : goSynPortScanner [SourceIP] [SourcePort] [DestIP] [DestPort] \n")
28-
fmt.Printf("\nExample : goSynPortScanner 192.168.1.1 1234 8.8.8.8 53 \n\n")
29-
fmt.Printf("\t The Source Address of the packet is :\t\t192.168.1.1 \n")
30-
fmt.Printf("\t The Source Port of the packet is :\t\t1234 \n")
31-
fmt.Printf("\t The Destinations Address of the packet is :\t8.8.8.8 \n")
32-
fmt.Printf("\t The Destinations Port of the packet is :\t53 \n")
33-
fmt.Printf("\n Note : You must check that The Destinations Address is legal \n \tAnd The Source Address should be The Address of Network card\n")
28+
fmt.Printf(`
29+
USAGE : goSynPortScanner [SourceIP] [SourcePort] [DestStartIP]-[DestEndIP] [DestStartPort]-[DestEndPort] [RoutineNum] [IsRandomSrcPort]
30+
31+
Example : goSynPortScanner 192.168.1.1 1234 8.8.8.8-8.8.8.9 53-1024 10 true
32+
33+
The Source Address of the packet : 192.168.1.1
34+
The Source Port of the packet : 1234
35+
The Destination Start Address of the packet : 8.8.8.8
36+
The Destination End Address of the packet : 8.8.8.8
37+
The Destination Start Port of the packet : 53
38+
The Destination End Port of the packet : 1024
39+
The Routine Number of Will Created : 10
40+
Whether the source port be random generated : false
41+
42+
Note :
43+
You must check that The Destinations Address is legal
44+
And The Source Address should be The Address of Network card
45+
`)
3446
}
3547

3648
func checkIP(ip string) {
@@ -221,6 +233,7 @@ func getTaskNum(DestStartAddr string, DestEndAddr string, DestStartPort uint16,
221233
}
222234
func main() {
223235

236+
runtime.GOMAXPROCS(runtime.NumCPU())
224237
if len(os.Args) < 7 {
225238
usage()
226239
return
@@ -235,7 +248,6 @@ func main() {
235248

236249
RoutinueNum := getRoutinueNum()
237250
IsRandomSrcPort := getSrcPortOption()
238-
runtime.GOMAXPROCS(int(RoutinueNum))
239251
var i uint32
240252
taskNum := getTaskNum(DestStartAddr, DestEndAddr, DestStartPort, DestEndPort)
241253
space := taskNum / RoutinueNum
@@ -247,6 +259,8 @@ func main() {
247259

248260
NowDestStartAddr := DestStartAddr
249261
NowDestStartPort := DestStartPort
262+
263+
startTime := time.Now()
250264
for i = 0; i < InstanceNum; i++ {
251265
worker := new(manager.Worker)
252266
workers[i] = *worker
@@ -257,6 +271,7 @@ func main() {
257271
workers[i].StartPort = DestStartPort
258272
workers[i].EndPort = DestEndPort
259273
workers[i].IsRandomSrcPort = IsRandomSrcPort
274+
workers[i].RoutineId = int(i)
260275
var NowDestEndAddr string
261276
var NowDestEndPort uint16
262277

@@ -271,7 +286,7 @@ func main() {
271286
workers[i].DestEndAddr = NowDestEndAddr
272287
workers[i].DestEndPort = NowDestEndPort
273288
workers[i].Init()
274-
channels[i] = make(chan int, 1)
289+
channels[i] = make(chan int)
275290
go workers[i].Run(channels[i])
276291
if (InstanceNum - 1) != i {
277292
NowDestStartAddr, NowDestStartPort = nextTask(NowDestEndAddr, NowDestEndPort, DestStartPort, DestEndPort, 1)
@@ -281,7 +296,12 @@ func main() {
281296
break
282297
}
283298
}
284-
for _, ch := range channels {
285-
<-ch
299+
300+
for _, v := range channels {
301+
<-v
302+
//fmt.Printf("The %d Routine Finish its task\n", <-v)
286303
}
304+
endTime := time.Now()
305+
elapseTimeSec := float64(endTime.Sub(startTime).Nanoseconds()) / float64(time.Millisecond)
306+
fmt.Printf("All Task is finished,elapse time :%0.3f ms\n", elapseTimeSec)
287307
}

src/manager/worker.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Worker struct {
2828
nowDestAddr string
2929
nowDestPort uint16
3030
IsRandomSrcPort bool
31+
RoutineId int
3132
}
3233

3334
func (w *Worker) createRawSocket() (fd int) {
@@ -273,11 +274,9 @@ func (w *Worker) run() {
273274
RemoteAddr.Addr[2] = byteAddr[2]
274275
RemoteAddr.Addr[3] = byteAddr[3]
275276
RemoteAddr.Port = int(tcpMaker.GetlittleEndianDestPort())
276-
//if !w.IsRandomSrcPort {
277-
//mutex.Lock()
278-
//}
277+
279278
socket := w.createRawSocket()
280-
fmt.Printf("%d sendpacket,port :%d \n", i, w.nowDestPort)
279+
//fmt.Printf("%d sendpacket,port :%d \n", i, w.nowDestPort)
281280
w.sendPacket(Packet, RemoteAddr, socket)
282281
RecvBuf, RecvLen := w.recvPacket(socket)
283282
if RecvLen == 44 && w.check(RecvBuf) {
@@ -296,9 +295,7 @@ func (w *Worker) run() {
296295
//fmt.Printf("IP :%s Port :%d \t is not open \n", w.nowDestAddr, w.nowDestPort)
297296
syscall.Close(socket)
298297
}
299-
//if !w.IsRandomSrcPort {
300-
//mutex.Unlock()
301-
//}
298+
302299
NextIP, NextPort := w.nextTask()
303300

304301
//fmt.Printf("NextIP %s NextPort %d\n", NextIP, NextPort)
@@ -316,8 +313,7 @@ func (w *Worker) run() {
316313
func (w *Worker) Run(channel chan int) {
317314
w.notify = channel
318315
w.run()
319-
fmt.Printf("Exit...")
320-
channel <- 1
316+
channel <- w.RoutineId
321317
}
322318
func sendRSTPacket(Maker *TCPmaker, RemoteAddr *syscall.SockaddrInet4, socket int) {
323319
buf := Maker.MakePacket(RST)

0 commit comments

Comments
 (0)