Skip to content

Commit b0c24d2

Browse files
committed
http3 ping
1 parent 3c8b3d2 commit b0c24d2

File tree

3 files changed

+44
-34
lines changed

3 files changed

+44
-34
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pping
22

3-
[![GoDoc](https://godoc.org/github.com/wzv5/pping?status.svg)](https://godoc.org/github.com/wzv5/pping)
3+
[![Go Reference](https://pkg.go.dev/badge/github.com/wzv5/pping.svg)](https://pkg.go.dev/github.com/wzv5/pping)
44

55
tcp ping, tls ping, http ping, icmp ping, dns ping, quic ping.
66

cmd/pping/cmd/http.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type httpFlags struct {
2020
timeout time.Duration
2121
refer string
2222
ua string
23+
http3 bool
2324
}
2425

2526
var httpflag httpFlags
@@ -40,6 +41,7 @@ func addHttpCommand() {
4041
cmd.Flags().BoolVarP(&httpflag.insecure, "insecure", "k", false, "allow insecure server connections when using SSL")
4142
cmd.Flags().StringVarP(&httpflag.refer, "referrer", "r", "", "Referer header")
4243
cmd.Flags().StringVarP(&httpflag.ua, "useragent", "u", "", "User-Agent header")
44+
cmd.Flags().BoolVarP(&httpflag.http3, "http3", "3", false, "use HTTP/3")
4345
rootCmd.AddCommand(cmd)
4446
}
4547

@@ -63,5 +65,6 @@ func runhttp(cmd *cobra.Command, args []string) error {
6365
p.Referrer = httpflag.refer
6466
p.UserAgent = httpflag.ua
6567
p.IP = ip
68+
p.Http3 = httpflag.http3
6669
return RunPing(p)
6770
}

pkg/ping/http.go

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"net"
99
"net/http"
1010
"net/url"
11-
"strings"
1211
"time"
12+
13+
"github.com/quic-go/quic-go"
14+
"github.com/quic-go/quic-go/http3"
1315
)
1416

1517
type HttpPingResult struct {
@@ -48,6 +50,7 @@ type HttpPing struct {
4850
Insecure bool
4951
Referrer string
5052
UserAgent string
53+
Http3 bool
5154
IP net.IP
5255
}
5356

@@ -60,7 +63,9 @@ func (this *HttpPing) PingContext(ctx context.Context) IPingResult {
6063
if err != nil {
6164
return this.errorResult(err)
6265
}
66+
orighost := u.Host
6367
host := u.Hostname()
68+
port := u.Port()
6469
ip := cloneIP(this.IP)
6570
if ip == nil {
6671
var err error
@@ -69,51 +74,53 @@ func (this *HttpPing) PingContext(ctx context.Context) IPingResult {
6974
return this.errorResult(err)
7075
}
7176
}
72-
73-
dialer := &net.Dialer{
74-
Timeout: this.Timeout,
75-
KeepAlive: -1,
77+
if port != "" {
78+
u.Host = net.JoinHostPort(ip.String(), port)
79+
} else {
80+
u.Host = ip.String()
7681
}
77-
78-
dialfunc := func(ctx context.Context, network, address string) (net.Conn, error) {
79-
h, p, err := net.SplitHostPort(address)
80-
if err != nil {
81-
return nil, err
82+
url2 := u.String()
83+
84+
var transport http.RoundTripper
85+
if this.Http3 {
86+
transport = &http3.RoundTripper{
87+
DisableCompression: this.DisableCompression,
88+
QuicConfig: &quic.Config{
89+
KeepAlivePeriod: 0,
90+
},
91+
TLSClientConfig: &tls.Config{
92+
InsecureSkipVerify: this.Insecure,
93+
ServerName: host,
94+
},
8295
}
83-
if ip == nil || !strings.EqualFold(h, host) {
84-
var err error
85-
ip, err = LookupFunc(h)
86-
if err != nil {
87-
return nil, err
88-
}
96+
} else {
97+
trans := http.DefaultTransport.(*http.Transport).Clone()
98+
trans.DisableKeepAlives = true
99+
trans.MaxIdleConnsPerHost = -1
100+
trans.DisableCompression = this.DisableCompression
101+
trans.ForceAttemptHTTP2 = !this.DisableHttp2
102+
trans.TLSClientConfig = &tls.Config{
103+
InsecureSkipVerify: this.Insecure,
104+
ServerName: host,
89105
}
90-
addr := net.JoinHostPort(ip.String(), p)
91-
return dialer.DialContext(ctx, network, addr)
92-
}
93-
94-
trans := http.DefaultTransport.(*http.Transport).Clone()
95-
trans.DialContext = dialfunc
96-
trans.DisableKeepAlives = true
97-
trans.MaxIdleConnsPerHost = -1
98-
trans.DisableCompression = this.DisableCompression
99-
trans.ForceAttemptHTTP2 = !this.DisableHttp2
100-
trans.TLSClientConfig = &tls.Config{
101-
InsecureSkipVerify: this.Insecure,
106+
transport = trans
102107
}
103108

104-
req, err := http.NewRequestWithContext(ctx, this.Method, this.URL, nil)
109+
req, err := http.NewRequestWithContext(ctx, this.Method, url2, nil)
105110
if err != nil {
106111
return this.errorResult(err)
107112
}
108-
if this.UserAgent == "" {
109-
this.UserAgent = "httping"
113+
ua := "httping"
114+
if this.UserAgent != "" {
115+
ua = this.UserAgent
110116
}
111-
req.Header.Set("User-Agent", this.UserAgent)
117+
req.Header.Set("User-Agent", ua)
112118
if this.Referrer != "" {
113119
req.Header.Set("Referer", this.Referrer)
114120
}
121+
req.Host = orighost
115122
client := &http.Client{}
116-
client.Transport = trans
123+
client.Transport = transport
117124
client.Timeout = this.Timeout
118125
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
119126
return http.ErrUseLastResponse

0 commit comments

Comments
 (0)