This repository has been archived by the owner on Jan 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (68 loc) · 1.5 KB
/
main.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/fatih/color"
"github.com/sakajunquality/tcpong/ping"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "tcpong"
app.Usage = "Ping over TCP"
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "i",
Value: 1,
Usage: "Request Interval (Sec)",
},
cli.IntFlag{
Name: "t",
Value: 1,
Usage: "Request Timeout (Sec)",
},
cli.StringFlag{
Name: "p",
Value: "tcp",
Usage: "Protocol",
},
}
app.Action = func(c *cli.Context) error {
if c.NArg() != 2 {
return cli.NewExitError("illegal number of args", 2)
}
ch := make(chan string, 1)
port, err := strconv.Atoi(c.Args().Get(1))
if err != nil {
return cli.NewExitError("Port number should be interger", 2)
}
reqInterval, reqTimeout := time.Duration(c.GlobalInt("i"))*time.Second, time.Duration(c.GlobalInt("t"))*time.Second
reqProtocol := strings.ToLower(c.GlobalString("p"))
t := ping.Target{
Protocol: reqProtocol,
Host: c.Args().Get(0),
Port: port,
Timeout: reqTimeout,
}
if !t.IsValid() {
return cli.NewExitError("Input values are not valid", 2)
}
color.Blue("%s PING %s:%d Interval:%s Timeout:%s", t.Protocol, t.Host, t.Port, reqInterval, reqTimeout)
for {
go func() {
r, err := t.Dial()
if err != nil {
ch <- fmt.Sprintf("Error: %s", err)
return
}
ch <- r.String()
}()
fmt.Printf("%s\n", <-ch)
time.Sleep(reqInterval)
}
}
app.Run(os.Args)
}