Skip to content

Commit 88c898f

Browse files
authored
[add] http proxy (#19)
* [add] http proxy * [fix]socks5 Co-authored-by: sxueck <[email protected]>
1 parent 7867893 commit 88c898f

File tree

18 files changed

+1034
-3
lines changed

18 files changed

+1034
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515

1616
dist
1717
*.tar.gz
18+
.idea/

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ require (
77
github.com/smartystreets/goconvey v0.0.0-20190306220146-200a235640ff
88
github.com/spf13/cobra v0.0.3
99
github.com/spf13/pflag v1.0.3 // indirect
10+
golang.org/x/net v0.0.0-20210224082022-3d97a244fca7
1011
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
1212
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
1313
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
1414
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
15+
golang.org/x/net v0.0.0-20210224082022-3d97a244fca7 h1:OgUuv8lsRpBibGNbSizVwKWlysjaNzmC9gYMhPVfqFM=
16+
golang.org/x/net v0.0.0-20210224082022-3d97a244fca7/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
17+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
18+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
19+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
20+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var (
1818
version string
1919
gitCommit string
2020
counter int
21+
proxy string
2122
timeout string
2223
interval string
2324
sigs chan os.Signal
@@ -123,6 +124,7 @@ var rootCmd = cobra.Command{
123124
Host: parseHost,
124125
Port: port,
125126
Counter: counter,
127+
Proxy: proxy,
126128
Protocol: protocol,
127129
}
128130
var pinger ping.Pinger
@@ -161,6 +163,7 @@ var rootCmd = cobra.Command{
161163
func init() {
162164
rootCmd.Flags().BoolVarP(&showVersion, "version", "v", false, "show the version and exit")
163165
rootCmd.Flags().IntVarP(&counter, "counter", "c", 4, "ping counter")
166+
rootCmd.Flags().StringVar(&proxy, "proxy", "", "Use HTTP proxy")
164167
rootCmd.Flags().StringVarP(&timeout, "timeout", "T", "1s", `connect timeout, units are "ns", "us" (or "µs"), "ms", "s", "m", "h"`)
165168
rootCmd.Flags().StringVarP(&interval, "interval", "I", "1s", `ping interval, units are "ns", "us" (or "µs"), "ms", "s", "m", "h"`)
166169

ping/http.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ package ping
22

33
import (
44
"bytes"
5+
"context"
56
"fmt"
7+
"golang.org/x/net/proxy"
68
"io"
79
"io/ioutil"
10+
"net"
811
"net/http"
912
"net/http/httptrace"
13+
"net/url"
14+
"strings"
1015
"time"
1116
)
1217

@@ -96,10 +101,12 @@ func (ping HTTPing) ping() (time.Duration, *http.Response, string, error) {
96101
body = bytes.NewBufferString("{}")
97102
}
98103
req, err := http.NewRequest(ping.Method, ping.target.String(), body)
99-
req.Header.Set(http.CanonicalHeaderKey("User-Agent"), "tcping")
100104
if err != nil {
101105
return 0, nil, "", err
102106
}
107+
108+
req.Header.Set(http.CanonicalHeaderKey("User-Agent"), "tcping")
109+
103110
var remoteAddr string
104111
trace := &httptrace.ClientTrace{
105112
ConnectStart: func(network, addr string) {
@@ -108,7 +115,45 @@ func (ping HTTPing) ping() (time.Duration, *http.Response, string, error) {
108115
}
109116
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
110117
duration, errIfce := timeIt(func() interface{} {
111-
client := http.Client{Timeout: ping.target.Timeout}
118+
client := http.Client{
119+
Timeout: ping.target.Timeout,
120+
}
121+
122+
proxyProco := ping.target.Proxy
123+
if ping.target.Proxy != "" {
124+
125+
var (
126+
parProxy *url.URL
127+
dialer proxy.Dialer
128+
)
129+
130+
httpTransport := &http.Transport{}
131+
132+
if strings.HasPrefix(proxyProco, "http") {
133+
parProxy, err = url.Parse(ping.target.Proxy)
134+
if err != nil {
135+
return err
136+
}
137+
httpTransport.Proxy = http.ProxyURL(parProxy)
138+
139+
} else if strings.HasPrefix(proxyProco, "sock") {
140+
141+
dialer, err = proxy.SOCKS5("tcp", proxyProco[strings.Index(proxyProco, "//")+2:], nil, proxy.Direct)
142+
if err != nil {
143+
return fmt.Errorf("can't connect to the proxy: %s", err)
144+
}
145+
146+
httpTransport.DialContext = func(ctx context.Context, network string, addr string) (net.Conn, error) {
147+
return dialer.Dial(network, addr)
148+
}
149+
150+
} else {
151+
return fmt.Errorf("%s", "please enter the correct agency procotol")
152+
}
153+
154+
client.Transport = httpTransport
155+
}
156+
112157
resp, err = client.Do(req)
113158
return err
114159
})

ping/ping.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type Target struct {
5252
Protocol Protocol
5353
Host string
5454
Port int
55+
Proxy string
5556

5657
Counter int
5758
Interval time.Duration

vendor/golang.org/x/net/AUTHORS

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/net/CONTRIBUTORS

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/net/LICENSE

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/net/PATENTS

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)