Xray-core/main/commands/all/tls/ping.go

127 lines
3.0 KiB
Go
Raw Permalink Normal View History

2020-12-04 01:36:16 +00:00
package tls
2020-11-25 11:01:53 +00:00
import (
gotls "crypto/tls"
2020-11-25 11:01:53 +00:00
"crypto/x509"
"encoding/base64"
2020-11-25 11:01:53 +00:00
"fmt"
"net"
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/main/commands/base"
. "github.com/xtls/xray-core/transport/internet/tls"
2020-11-25 11:01:53 +00:00
)
2020-12-04 01:36:16 +00:00
// cmdPing is the tls ping command
var cmdPing = &base.Command{
2020-11-25 11:01:53 +00:00
UsageLine: "{{.Exec}} tls ping [-ip <ip>] <domain>",
Short: "Ping the domain with TLS handshake",
Long: `
Ping the domain with TLS handshake.
2020-12-04 01:36:16 +00:00
Arguments:
-ip
The IP address of the domain.
`,
2020-11-25 11:01:53 +00:00
}
func init() {
2020-12-04 01:36:16 +00:00
cmdPing.Run = executePing // break init loop
2020-11-25 11:01:53 +00:00
}
2021-10-19 16:57:14 +00:00
var pingIPStr = cmdPing.Flag.String("ip", "", "")
2020-11-25 11:01:53 +00:00
func executePing(cmd *base.Command, args []string) {
2020-12-04 01:36:16 +00:00
if cmdPing.Flag.NArg() < 1 {
2020-11-25 11:01:53 +00:00
base.Fatalf("domain not specified")
}
2020-12-04 01:36:16 +00:00
domain := cmdPing.Flag.Arg(0)
2020-11-25 11:01:53 +00:00
fmt.Println("Tls ping: ", domain)
var ip net.IP
if len(*pingIPStr) > 0 {
v := net.ParseIP(*pingIPStr)
if v == nil {
base.Fatalf("invalid IP: %s", *pingIPStr)
}
ip = v
} else {
v, err := net.ResolveIPAddr("ip", domain)
if err != nil {
base.Fatalf("Failed to resolve IP: %s", err)
}
ip = v.IP
}
fmt.Println("Using IP: ", ip.String())
fmt.Println("-------------------")
fmt.Println("Pinging without SNI")
{
tcpConn, err := net.DialTCP("tcp", nil, &net.TCPAddr{IP: ip, Port: 443})
if err != nil {
base.Fatalf("Failed to dial tcp: %s", err)
}
tlsConn := gotls.Client(tcpConn, &gotls.Config{
2020-11-25 11:01:53 +00:00
InsecureSkipVerify: true,
NextProtos: []string{"http/1.1"},
MaxVersion: gotls.VersionTLS12,
MinVersion: gotls.VersionTLS12,
// Do not release tool before v5's refactor
// VerifyPeerCertificate: showCert(),
2020-11-25 11:01:53 +00:00
})
err = tlsConn.Handshake()
if err != nil {
fmt.Println("Handshake failure: ", err)
} else {
fmt.Println("Handshake succeeded")
printCertificates(tlsConn.ConnectionState().PeerCertificates)
}
tlsConn.Close()
}
fmt.Println("-------------------")
fmt.Println("Pinging with SNI")
{
tcpConn, err := net.DialTCP("tcp", nil, &net.TCPAddr{IP: ip, Port: 443})
if err != nil {
base.Fatalf("Failed to dial tcp: %s", err)
}
tlsConn := gotls.Client(tcpConn, &gotls.Config{
2020-11-25 11:01:53 +00:00
ServerName: domain,
NextProtos: []string{"http/1.1"},
MaxVersion: gotls.VersionTLS12,
MinVersion: gotls.VersionTLS12,
// Do not release tool before v5's refactor
// VerifyPeerCertificate: showCert(),
2020-11-25 11:01:53 +00:00
})
err = tlsConn.Handshake()
if err != nil {
fmt.Println("handshake failure: ", err)
} else {
fmt.Println("handshake succeeded")
printCertificates(tlsConn.ConnectionState().PeerCertificates)
}
tlsConn.Close()
}
fmt.Println("Tls ping finished")
}
func printCertificates(certs []*x509.Certificate) {
for _, cert := range certs {
if len(cert.DNSNames) == 0 {
continue
}
fmt.Println("Allowed domains: ", cert.DNSNames)
}
}
func showCert() func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
return func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
hash := GenerateCertChainHash(rawCerts)
fmt.Println("Certificate Chain Hash: ", base64.StdEncoding.EncodeToString(hash))
return nil
}
}