mirror of https://github.com/XTLS/Xray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
2.5 KiB
115 lines
2.5 KiB
4 years ago
|
package tls
|
||
4 years ago
|
|
||
|
import (
|
||
|
"crypto/tls"
|
||
|
"crypto/x509"
|
||
|
"fmt"
|
||
|
"net"
|
||
|
|
||
4 years ago
|
"github.com/xtls/xray-core/main/commands/base"
|
||
4 years ago
|
)
|
||
|
|
||
4 years ago
|
// cmdPing is the tls ping command
|
||
|
var cmdPing = &base.Command{
|
||
4 years ago
|
UsageLine: "{{.Exec}} tls ping [-ip <ip>] <domain>",
|
||
|
Short: "Ping the domain with TLS handshake",
|
||
|
Long: `
|
||
|
Ping the domain with TLS handshake.
|
||
|
|
||
4 years ago
|
Arguments:
|
||
|
|
||
|
-ip
|
||
|
The IP address of the domain.
|
||
|
`,
|
||
4 years ago
|
}
|
||
|
|
||
|
func init() {
|
||
4 years ago
|
cmdPing.Run = executePing // break init loop
|
||
4 years ago
|
}
|
||
|
|
||
|
var (
|
||
4 years ago
|
pingIPStr = cmdPing.Flag.String("ip", "", "")
|
||
4 years ago
|
)
|
||
|
|
||
|
func executePing(cmd *base.Command, args []string) {
|
||
4 years ago
|
if cmdPing.Flag.NArg() < 1 {
|
||
4 years ago
|
base.Fatalf("domain not specified")
|
||
|
}
|
||
|
|
||
4 years ago
|
domain := cmdPing.Flag.Arg(0)
|
||
4 years ago
|
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 := tls.Client(tcpConn, &tls.Config{
|
||
|
InsecureSkipVerify: true,
|
||
|
NextProtos: []string{"http/1.1"},
|
||
|
MaxVersion: tls.VersionTLS12,
|
||
|
MinVersion: tls.VersionTLS12,
|
||
|
})
|
||
|
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 := tls.Client(tcpConn, &tls.Config{
|
||
|
ServerName: domain,
|
||
|
NextProtos: []string{"http/1.1"},
|
||
|
MaxVersion: tls.VersionTLS12,
|
||
|
MinVersion: tls.VersionTLS12,
|
||
|
})
|
||
|
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)
|
||
|
}
|
||
|
}
|