forked from cloverstd/tcping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
195 lines (176 loc) · 4.62 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
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
package main
import (
"context"
"fmt"
"github.com/cloverstd/tcping/ping"
"github.com/cloverstd/tcping/ping/http"
"github.com/cloverstd/tcping/ping/tcp"
"github.com/spf13/cobra"
"net"
"net/url"
"os"
"os/signal"
"strconv"
"syscall"
)
var (
showVersion bool
version string
gitCommit string
counter int
timeout string
interval string
sigs chan os.Signal
httpMethod string
httpUA string
dnsServer []string
)
var rootCmd = cobra.Command{
Use: "tcping host port",
Short: "tcping is a tcp ping",
Long: "tcping is a ping over tcp connection",
Example: `
1. ping over tcp
> tcping google.com
2. ping over tcp with custom port
> tcping google.com 443
3. ping over http
> tcping http://google.com
4. ping with URI schema
> tcping https://hui.lu
`,
Run: func(cmd *cobra.Command, args []string) {
if showVersion {
fmt.Printf("version: %s\n", version)
fmt.Printf("git: %s\n", gitCommit)
return
}
if len(args) == 0 {
cmd.Usage()
return
}
if len(args) > 2 {
cmd.Println("invalid command arguments")
return
}
url, err := ping.ParseAddress(args[0])
if err != nil {
fmt.Printf("%s is an invalid target.\n", args[0])
return
}
defaultPort := "80"
if port := url.Port(); port != "" {
defaultPort = port
} else if url.Scheme == "https" {
defaultPort = "443"
}
if len(args) > 1 {
defaultPort = args[1]
}
port, err := strconv.Atoi(defaultPort)
if err != nil {
cmd.Printf("%s is invalid port.\n", defaultPort)
return
}
url.Host = fmt.Sprintf("%s:%d", url.Hostname(), port)
timeoutDuration, err := ping.ParseDuration(timeout)
if err != nil {
cmd.Println("parse timeout failed", err)
cmd.Usage()
return
}
intervalDuration, err := ping.ParseDuration(interval)
if err != nil {
cmd.Println("parse interval failed", err)
cmd.Usage()
return
}
protocol, err := ping.NewProtocol(url.Scheme)
if err != nil {
cmd.Println("invalid protocol", err)
cmd.Usage()
return
}
option := ping.Option{
Timeout: timeoutDuration,
}
if len(dnsServer) != 0 {
option.Resolver = &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (conn net.Conn, err error) {
for _, addr := range dnsServer {
if conn, err = net.Dial("udp", addr+":53"); err != nil {
continue
} else {
return conn, nil
}
}
return
},
}
}
pingFactory := ping.Load(protocol)
p, err := pingFactory(url, &option)
if err != nil {
cmd.Println("load pinger failed", err)
cmd.Usage()
return
}
pinger := ping.NewPinger(os.Stdout, url, p, intervalDuration, counter)
sigs = make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go pinger.Ping()
select {
case <-sigs:
case <-pinger.Done():
}
pinger.Stop()
pinger.Summarize()
},
}
func fixProxy(proxy string, op *ping.Option) error {
if proxy == "" {
return nil
}
u, err := url.Parse(proxy)
op.Proxy = u
return err
}
func init() {
rootCmd.Flags().StringVar(&httpMethod, "http-method", "GET", `Use custom HTTP method instead of GET in http mode.`)
ua := rootCmd.Flags().String("user-agent", "tcping", `Use custom UA in http mode.`)
meta := rootCmd.Flags().Bool("meta", false, `With meta info`)
proxy := rootCmd.Flags().String("proxy", "", "Use HTTP proxy")
ping.Register(ping.HTTP, func(url *url.URL, op *ping.Option) (ping.Ping, error) {
if err := fixProxy(*proxy, op); err != nil {
return nil, err
}
op.UA = *ua
return http.New(httpMethod, url.String(), op, *meta)
})
ping.Register(ping.HTTPS, func(url *url.URL, op *ping.Option) (ping.Ping, error) {
if err := fixProxy(*proxy, op); err != nil {
return nil, err
}
op.UA = *ua
return http.New(httpMethod, url.String(), op, *meta)
})
ping.Register(ping.TCP, func(url *url.URL, op *ping.Option) (ping.Ping, error) {
port, err := strconv.Atoi(url.Port())
if err != nil {
return nil, err
}
return tcp.New(url.Hostname(), port, op, *meta), nil
})
rootCmd.Flags().BoolVarP(&showVersion, "version", "v", false, "show the version and exit.")
rootCmd.Flags().IntVarP(&counter, "counter", "c", ping.DefaultCounter, "ping counter")
rootCmd.Flags().StringVarP(&timeout, "timeout", "T", "1s", `connect timeout, units are "ns", "us" (or "µs"), "ms", "s", "m", "h"`)
rootCmd.Flags().StringVarP(&interval, "interval", "I", "1s", `ping interval, units are "ns", "us" (or "µs"), "ms", "s", "m", "h"`)
rootCmd.Flags().StringArrayVarP(&dnsServer, "dns-server", "D", nil, `Use the specified dns resolve server.`)
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}