-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkbang.go
108 lines (93 loc) · 2.05 KB
/
kbang.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"runtime"
"flag"
"os"
"github.com/kaimixu/kbang/conf"
"github.com/kaimixu/kbang/robot"
//"./conf"
//"./robot"
)
var (
n,c,t int
keepalive bool
url string
requestBody string
contentType string
header string
cfgFile string
)
var usage =
`Usage: kbang [options...] <url> (1st form)
or: kbang [options...] -f configfile (2st form)
options:
-n Number of requests to run (default: 10)
-c Number of requests to run concurrency (default: 1)
-t Request connection timeout in second (default: 5s)
-H Http header, eg. -H "Host: www.example.com"
-k[=true|false] Http keep-alive (default: false)
-d Http request body to POST
-T Content-type header to POST, eg. 'application/x-www-form-urlencoded'
(Default:text/plain)
`
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Usage = func() {
fmt.Fprint(os.Stderr, usage)
}
flag.IntVar(&n, "n", 10, "")
flag.IntVar(&c, "c", 1, "")
flag.IntVar(&t, "t", 5, "")
flag.BoolVar(&keepalive, "k", false, "")
flag.StringVar(&cfgFile, "f", "", "")
flag.StringVar(&requestBody, "d", "", "")
flag.StringVar(&contentType, "T", "text/plain", "")
flag.StringVar(&header, "H", "", "")
flag.Parse()
if flag.NArg() < 1 && cfgFile == "" {
abort("")
}
method := "GET"
if requestBody != "" {
method = "POST"
}
var httpConf = robot.HttpConf{
KeepAlive: keepalive,
Header: header,
Timeout: t,
}
if flag.NArg() > 0 {
url = flag.Args()[0]
httpConf.Request[0] = robot.RequestConf{
Weight: 1,
Method: method,
Url: url,
ContentType: contentType,
PostData: requestBody,
}
}else {
cfg := conf.NewConf()
err := cfg.LoadFile(cfgFile)
if err != nil {
abort(err.Error())
}
err = cfg.Parse(&httpConf)
if err != nil {
abort(err.Error())
}
}
robot := robot.NewRoboter(n, c, &httpConf)
err := robot.CreateRequest()
if err != nil {
abort(err.Error())
}
robot.Run()
}
func abort(errmsg string) {
if errmsg != "" {
fmt.Fprintf(os.Stderr, "%s\n\n", errmsg)
}
flag.Usage()
os.Exit(1)
}