Skip to content

Commit 3461e7f

Browse files
committed
并发优化
1 parent 3b93f17 commit 3461e7f

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

cmd/commons/core/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (o Options) toString() interface{} {
5656
func ParseOptions() *Options {
5757
options := &Options{}
5858
flag.IntVar(&options.Mode, "m", 0, "debug mode off (debug mode = 1) default mode = 0")
59-
flag.IntVar(&options.Thread, "t", 1, "threads number ")
59+
flag.IntVar(&options.Thread, "t", 50, "threads number ")
6060
flag.StringVar(&options.File, "f", "", "file to read example: -file=test.txt http(s)://host:port/path/ (notes: The last line must be empty)")
6161
flag.StringVar(&options.Url, "u", "", "url to read example: -url=http://www.baidu.com:80/")
6262
flag.StringVar(&options.Proxy, "proxy", "", "proxy example: -proxy=http(socks5)://127.0.0.1:8080 ")

cmd/commons/core/runner.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/fatih/structs"
88
log "github.com/sirupsen/logrus"
99
"net/url"
10+
"sync"
1011
)
1112

1213
type Runner struct {
@@ -47,6 +48,7 @@ func (r *Runner) Run() {
4748
log.Debugln("URLs: ", urls)
4849
var i = 0
4950
k := r.options.Thread
51+
var wg sync.WaitGroup
5052
hashmap := structs.Map(&r.options)
5153
for i < len(urls) {
5254
for t := 0; t < k; t++ {
@@ -57,9 +59,15 @@ func (r *Runner) Run() {
5759
log.Debugln("Running attack on: ", urls[i])
5860
// 通道通信 发送url 并且 i++
5961
c := make(chan int)
60-
go Start(urls[i], hashmap, i, c) // Start k goroutines
62+
wg.Add(1)
63+
go func() {
64+
log.Debugf("Running go func() %d", t)
65+
Start(urls[i], hashmap, i, c) // Start k goroutines
66+
wg.Done()
67+
}()
6168
i = <-c
6269
} else {
70+
wg.Wait()
6371
i++
6472
break
6573
}

cmd/test/threads.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"sync"
7+
)
8+
9+
func hello(str int) {
10+
i := strconv.Itoa(str)
11+
fmt.Println("Hello, world." + i)
12+
13+
}
14+
15+
func main() {
16+
var wg sync.WaitGroup
17+
18+
for i := 0; i < 100; i++ {
19+
wg.Add(1)
20+
fmt.Println("Starting goroutine", i)
21+
go func() {
22+
hello(i)
23+
wg.Done()
24+
}()
25+
}
26+
wg.Wait()
27+
}

0 commit comments

Comments
 (0)