-
Notifications
You must be signed in to change notification settings - Fork 231
/
ping.go
230 lines (210 loc) · 5.32 KB
/
ping.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Package ping tries to ping a HTTP server through different ways
// Connection, Session (Head), Get and Post
package ping
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"github.com/mehrdadrad/mylg/cli"
)
// Ping represents HTTP ping request
type Ping struct {
url string
host string
timeout time.Duration
count int
method string
buf string
rAddr net.Addr
nsTime time.Duration
conn net.Conn
}
// Result holds Ping result
type Result struct {
StatusCode int
ConnTime float64
TotalTime float64
Size int
Proto string
Server string
Status string
}
// NewPing validate and constructs request object
func NewPing(args string) (*Ping, error) {
URL, flag := cli.Flag(args)
// help
if _, ok := flag["help"]; ok || URL == "" {
help()
return nil, fmt.Errorf("")
}
URL = Normalize(URL)
u, err := url.Parse(URL)
if err != nil {
return &Ping{}, fmt.Errorf("cannot parse url")
}
sTime := time.Now()
ipAddr, err := net.ResolveIPAddr("ip", u.Host)
if err != nil {
return &Ping{}, fmt.Errorf("cannot resolve %s: Unknown host", u.Host)
}
p := &Ping{
url: URL,
host: u.Host,
rAddr: ipAddr,
nsTime: time.Since(sTime),
}
// set count
p.count = cli.SetFlag(flag, "c", 4).(int)
// set timeout
timeout := cli.SetFlag(flag, "t", 2).(int)
p.timeout = time.Duration(timeout)
// set method
p.method = cli.SetFlag(flag, "m", "HEAD").(string)
// set buff (post)
buf := cli.SetFlag(flag, "d", "mylg").(string)
p.buf = buf
return p, nil
}
// Normalize fixes scheme
func Normalize(URL string) string {
re := regexp.MustCompile(`(?i)https{0,1}://`)
if !re.MatchString(URL) {
URL = fmt.Sprintf("http://%s", URL)
}
return URL
}
// pingHeadLoop tries number of connection
// with header information
func (p *Ping) pingHeadLoop() {
pStrPrefix := "HTTP Response seq=%d, "
pStrSuffix := "proto=%s, status=%d, time=%.3f ms\n"
fmt.Printf("HPING %s (%s), Method: HEAD, DNSLookup: %.4f ms\n", p.host, p.rAddr, p.nsTime.Seconds()*1000)
for i := 0; i < p.count; i++ {
if r, ok := p.PingHead(); ok {
fmt.Printf(pStrPrefix+pStrSuffix, i, r.Proto, r.StatusCode, r.TotalTime*1000)
} else {
fmt.Printf(pStrPrefix+"timeout\n", i)
}
}
}
// pingGetLoop tries number of connection
// with header information
func (p *Ping) pingGetLoop() {
pStrPrefix := "HTTP Response seq=%d, "
pStrSuffix := "proto=%s, status=%d, size=%d Bytes, time=%.3f ms\n"
fmt.Printf("HPING %s (%s), Method: GET, DNSLookup: %.4f ms\n", p.host, p.rAddr, p.nsTime.Seconds()*1000)
for i := 0; i < p.count; i++ {
if r, ok := p.PingGet(); ok {
fmt.Printf(pStrPrefix+pStrSuffix, i, r.Proto, r.StatusCode, r.Size, r.TotalTime*1000)
} else {
fmt.Printf(pStrPrefix+"timeout\n", i)
}
}
}
// pingGetLoop tries number of connection
// with header information
func (p *Ping) pingPostLoop() {
pStrPrefix := "HTTP Response seq=%d, "
pStrSuffix := "proto=%s, status=%d, size=%d Bytes, time=%.3f ms\n"
fmt.Printf("HPING %s (%s), Method: POST, DNSLookup: %.4f ms\n", p.host, p.rAddr, p.nsTime.Seconds()*1000)
for i := 0; i < p.count; i++ {
if r, ok := p.PingPost(); ok {
fmt.Printf(pStrPrefix+pStrSuffix, i, r.Proto, r.StatusCode, r.Size, r.TotalTime*1000)
} else {
fmt.Printf(pStrPrefix+"timeout\n", i)
}
}
}
// PingGet tries to ping a web server through http
func (p *Ping) PingGet() (Result, bool) {
var (
r Result
sTime time.Time
)
client := &http.Client{Timeout: p.timeout * time.Second}
sTime = time.Now()
resp, err := client.Get(p.url)
if err != nil {
return r, false
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
r.Size = len(body)
r.TotalTime = time.Since(sTime).Seconds()
if err != nil {
return r, false
}
r.StatusCode = resp.StatusCode
r.Proto = resp.Proto
return r, true
}
// PingHead tries to ping a web server through http
func (p *Ping) PingHead() (Result, bool) {
var (
r Result
sTime time.Time
)
client := &http.Client{Timeout: p.timeout * time.Second}
sTime = time.Now()
resp, err := client.Head(p.url)
if err != nil {
return r, false
}
r.TotalTime = time.Since(sTime).Seconds()
if err != nil {
return r, false
}
r.StatusCode = resp.StatusCode
r.Proto = resp.Proto
return r, true
}
// PingPost tries to ping a web server through http
func (p *Ping) PingPost() (Result, bool) {
var (
r Result
sTime time.Time
)
client := &http.Client{Timeout: p.timeout * time.Second}
sTime = time.Now()
r.Size = len(p.buf)
reader := strings.NewReader(p.buf)
resp, err := client.Post(p.url, "text/plain", reader)
if err != nil {
return r, false
}
r.TotalTime = time.Since(sTime).Seconds()
if err != nil {
return r, false
}
r.StatusCode = resp.StatusCode
r.Proto = resp.Proto
return r, true
}
// Run tries to run ping loop based on the method
func (p *Ping) Run() {
switch p.method {
case "HEAD":
p.pingHeadLoop()
case "GET":
p.pingGetLoop()
case "POST":
p.pingPostLoop()
}
}
// help shows ping help
func help() {
fmt.Println(`
usage:
hping [-c count][-t timeout][-m method][-d data] url
options:
-c count Send 'count' requests (default: 4)
-t timeout Specifies a time limit for requests in second (default is 2)
-m method HTTP methods: GET/POST/HEAD (default: HEAD)
-d data Sending the given data (text/json) (default: "mylg")
`)
}