package main
import (
"fmt"
"net/http"
"time"
"crypto/tls"
)
func main() {
sites := []string{
"https://www.github.com",
"https://www.cloudflare.com",
"https://www.youtube.com",
}
for _, site := range sites {
start := time.Now()
// 创建一个自定义的TLS配置,启用了TCP连接时的RTT测量
tlsConfig := &tls.Config{
InsecureSkipVerify: true, // 跳过证书验证,仅用于测量目的
}
transport := &http.Transport{
TLSClientConfig: tlsConfig,
}
client := &http.Client{
Transport: transport,
}
resp, err := client.Get(site)
if err != nil {
fmt.Printf("无法连接到 %s: %v\n", site, err)
continue
}
defer resp.Body.Close()
elapsed := time.Since(start).Milliseconds()
fmt.Printf("网站: %s\n", site)
fmt.Printf("HTTPS延迟: %d ms\n", elapsed)
tlsConnState, ok := resp.TLS.HandshakeComplete()
if ok {
rtt := tlsConnState.RTT.Milliseconds()
fmt.Printf("TLS RTT: %d ms\n", rtt)
} else {
fmt.Println("未能测量TLS RTT")
}
fmt.Println()
}
}