1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-05 01:38:24 -05:00
v2fly/main/commands/all/tls/ping.go

127 lines
3.0 KiB
Go
Raw Normal View History

package tls
2019-02-19 04:56:06 -05:00
import (
"crypto/tls"
2019-02-19 10:35:05 -05:00
"crypto/x509"
"encoding/base64"
2019-02-19 04:56:06 -05:00
"fmt"
"net"
"github.com/v2fly/v2ray-core/v4/main/commands/base"
v2tls "github.com/v2fly/v2ray-core/v4/transport/internet/tls"
2019-02-19 04:56:06 -05:00
)
// cmdPing is the tls ping command
var cmdPing = &base.Command{
UsageLine: "{{.Exec}} tls ping [-ip <ip>] <domain>",
Short: "ping the domain with TLS handshake",
Long: `
Ping the domain with TLS handshake.
2019-02-19 04:56:06 -05:00
Arguments:
2019-02-19 04:56:06 -05:00
-ip <ip>
The IP address of the domain.
`,
2019-02-19 04:56:06 -05:00
}
func init() {
cmdPing.Run = executePing // break init loop
2019-02-19 10:35:05 -05:00
}
2021-11-27 01:32:07 -05:00
var pingIPStr = cmdPing.Flag.String("ip", "", "")
2019-02-19 04:56:06 -05:00
func executePing(cmd *base.Command, args []string) {
if cmdPing.Flag.NArg() < 1 {
base.Fatalf("domain not specified")
2019-02-19 04:56:06 -05:00
}
domain := cmdPing.Flag.Arg(0)
2019-02-19 04:56:06 -05:00
fmt.Println("Tls ping: ", domain)
var ip net.IP
if len(*pingIPStr) > 0 {
v := net.ParseIP(*pingIPStr)
2019-02-19 04:56:06 -05:00
if v == nil {
base.Fatalf("invalid IP: %s", *pingIPStr)
2019-02-19 04:56:06 -05:00
}
ip = v
} else {
v, err := net.ResolveIPAddr("ip", domain)
if err != nil {
base.Fatalf("Failed to resolve IP: %s", err)
2019-02-19 04:56:06 -05:00
}
ip = v.IP
}
fmt.Println("Using IP: ", ip.String())
2019-02-19 10:35:05 -05:00
fmt.Println("-------------------")
2019-02-19 04:56:06 -05:00
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)
2019-02-19 04:56:06 -05:00
}
tlsConn := tls.Client(tcpConn, &tls.Config{
2021-04-17 05:50:53 -04:00
InsecureSkipVerify: true,
NextProtos: []string{"http/1.1"},
MaxVersion: tls.VersionTLS12,
MinVersion: tls.VersionTLS12,
// Do not release tool before v5's refactor
// VerifyPeerCertificate: showCert(),
2019-02-19 04:56:06 -05:00
})
err = tlsConn.Handshake()
if err != nil {
fmt.Println("Handshake failure: ", err)
} else {
fmt.Println("Handshake succeeded")
2019-02-19 10:35:05 -05:00
printCertificates(tlsConn.ConnectionState().PeerCertificates)
2019-02-19 04:56:06 -05:00
}
tlsConn.Close()
}
2019-02-19 10:35:05 -05:00
fmt.Println("-------------------")
2019-02-19 04:56:06 -05:00
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)
2019-02-19 04:56:06 -05:00
}
tlsConn := tls.Client(tcpConn, &tls.Config{
2021-04-17 05:50:53 -04:00
ServerName: domain,
NextProtos: []string{"http/1.1"},
MaxVersion: tls.VersionTLS12,
MinVersion: tls.VersionTLS12,
// Do not release tool before v5's refactor
// VerifyPeerCertificate: showCert(),
2019-02-19 04:56:06 -05:00
})
err = tlsConn.Handshake()
if err != nil {
fmt.Println("handshake failure: ", err)
} else {
fmt.Println("handshake succeeded")
2019-02-19 10:35:05 -05:00
printCertificates(tlsConn.ConnectionState().PeerCertificates)
2019-02-19 04:56:06 -05:00
}
tlsConn.Close()
}
fmt.Println("Tls ping finished")
}
2019-02-19 04:56:06 -05:00
func printCertificates(certs []*x509.Certificate) {
for _, cert := range certs {
if len(cert.DNSNames) == 0 {
continue
}
fmt.Println("Allowed domains: ", cert.DNSNames)
}
2019-02-19 04:56:06 -05:00
}
func showCert() func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
return func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
hash := v2tls.GenerateCertChainHash(rawCerts)
fmt.Println("Certificate Chain Hash: ", base64.StdEncoding.EncodeToString(hash))
return nil
}
}