2020-11-30 12:08:08 +00:00
|
|
|
package tls
|
2019-02-19 09:56:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2019-02-19 15:35:05 +00:00
|
|
|
"crypto/x509"
|
2019-02-19 09:56:06 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2021-02-17 00:07:12 +00:00
|
|
|
"github.com/v2fly/v2ray-core/v4/main/commands/base"
|
2019-02-19 09:56:06 +00:00
|
|
|
)
|
|
|
|
|
2020-11-30 12:08:08 +00:00
|
|
|
// cmdPing is the tls ping command
|
|
|
|
var cmdPing = &base.Command{
|
2020-11-23 15:38:43 +00:00
|
|
|
UsageLine: "{{.Exec}} tls ping [-ip <ip>] <domain>",
|
improve commands (#648)
* stat show json refactor
> will show {} since scripts should expect it, its the json style of blank value
* combine statistics commands to one
* code optimize
* fix runtime flag
* remove json indent
* change overridden to override
* api bi -json
* convert stdin support
code optimize
* writeRow() code optimize
add stats tittle
revert back to restartlogger
* api log -restart
* follow log
* codeql
* move -json to shared flags
* flags optimize
* update flag descriptions
* change "-v" of "api bo" to duration
* change "-expire" of "tls cert" to days
* cmds short description optimize
* fix multiple log followers
* Format loader refactor
* "infra/conf/merge" refactor
* "LoadConfig" refactor
* add "infra/conf/mergers"
* contribute to it will benifit `v2ray run`,`v2ray test`,`v2ray convert`
* easily add new formats, by just adding a converter like json.FromTOML
* default format auto, to all cmds above
* auto detect input format
* mixed formats support
* better stdin behavior
* don't wait if no content
* don't use 'stdin:' placeholder
* `v2ray test` now behaves exactly the same with `v2ray run`, including stdin reading
* api ado, adi, rmo, rmi refactor
* support folders to files resolving, mixed formats
* remove remaining 'stdin:' placeholders
* fix tests
* os.Stdin.Stat() behaves different in platforms, removed
* code optimize
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
2021-02-21 15:02:42 +00:00
|
|
|
Short: "ping the domain with TLS handshake",
|
2020-11-23 15:38:43 +00:00
|
|
|
Long: `
|
|
|
|
Ping the domain with TLS handshake.
|
2019-02-19 09:56:06 +00:00
|
|
|
|
2020-11-23 15:38:43 +00:00
|
|
|
Arguments:
|
2019-02-19 09:56:06 +00:00
|
|
|
|
improve commands (#648)
* stat show json refactor
> will show {} since scripts should expect it, its the json style of blank value
* combine statistics commands to one
* code optimize
* fix runtime flag
* remove json indent
* change overridden to override
* api bi -json
* convert stdin support
code optimize
* writeRow() code optimize
add stats tittle
revert back to restartlogger
* api log -restart
* follow log
* codeql
* move -json to shared flags
* flags optimize
* update flag descriptions
* change "-v" of "api bo" to duration
* change "-expire" of "tls cert" to days
* cmds short description optimize
* fix multiple log followers
* Format loader refactor
* "infra/conf/merge" refactor
* "LoadConfig" refactor
* add "infra/conf/mergers"
* contribute to it will benifit `v2ray run`,`v2ray test`,`v2ray convert`
* easily add new formats, by just adding a converter like json.FromTOML
* default format auto, to all cmds above
* auto detect input format
* mixed formats support
* better stdin behavior
* don't wait if no content
* don't use 'stdin:' placeholder
* `v2ray test` now behaves exactly the same with `v2ray run`, including stdin reading
* api ado, adi, rmo, rmi refactor
* support folders to files resolving, mixed formats
* remove remaining 'stdin:' placeholders
* fix tests
* os.Stdin.Stat() behaves different in platforms, removed
* code optimize
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
2021-02-21 15:02:42 +00:00
|
|
|
-ip <ip>
|
2020-11-23 15:38:43 +00:00
|
|
|
The IP address of the domain.
|
|
|
|
`,
|
2019-02-19 09:56:06 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 15:38:43 +00:00
|
|
|
func init() {
|
2020-11-30 12:08:08 +00:00
|
|
|
cmdPing.Run = executePing // break init loop
|
2019-02-19 15:35:05 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 15:38:43 +00:00
|
|
|
var (
|
2020-11-30 12:08:08 +00:00
|
|
|
pingIPStr = cmdPing.Flag.String("ip", "", "")
|
2020-11-23 15:38:43 +00:00
|
|
|
)
|
2019-02-19 09:56:06 +00:00
|
|
|
|
2020-11-23 15:38:43 +00:00
|
|
|
func executePing(cmd *base.Command, args []string) {
|
2020-11-30 12:08:08 +00:00
|
|
|
if cmdPing.Flag.NArg() < 1 {
|
2020-11-23 15:38:43 +00:00
|
|
|
base.Fatalf("domain not specified")
|
2019-02-19 09:56:06 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 12:08:08 +00:00
|
|
|
domain := cmdPing.Flag.Arg(0)
|
2019-02-19 09:56:06 +00:00
|
|
|
fmt.Println("Tls ping: ", domain)
|
|
|
|
|
|
|
|
var ip net.IP
|
2020-11-23 15:38:43 +00:00
|
|
|
if len(*pingIPStr) > 0 {
|
|
|
|
v := net.ParseIP(*pingIPStr)
|
2019-02-19 09:56:06 +00:00
|
|
|
if v == nil {
|
2020-11-23 15:38:43 +00:00
|
|
|
base.Fatalf("invalid IP: %s", *pingIPStr)
|
2019-02-19 09:56:06 +00:00
|
|
|
}
|
|
|
|
ip = v
|
|
|
|
} else {
|
|
|
|
v, err := net.ResolveIPAddr("ip", domain)
|
|
|
|
if err != nil {
|
2020-11-23 15:38:43 +00:00
|
|
|
base.Fatalf("Failed to resolve IP: %s", err)
|
2019-02-19 09:56:06 +00:00
|
|
|
}
|
|
|
|
ip = v.IP
|
|
|
|
}
|
|
|
|
fmt.Println("Using IP: ", ip.String())
|
|
|
|
|
2019-02-19 15:35:05 +00:00
|
|
|
fmt.Println("-------------------")
|
2019-02-19 09:56:06 +00:00
|
|
|
fmt.Println("Pinging without SNI")
|
|
|
|
{
|
|
|
|
tcpConn, err := net.DialTCP("tcp", nil, &net.TCPAddr{IP: ip, Port: 443})
|
|
|
|
if err != nil {
|
2020-11-23 15:38:43 +00:00
|
|
|
base.Fatalf("Failed to dial tcp: %s", err)
|
2019-02-19 09:56:06 +00:00
|
|
|
}
|
|
|
|
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")
|
2019-02-19 15:35:05 +00:00
|
|
|
printCertificates(tlsConn.ConnectionState().PeerCertificates)
|
2019-02-19 09:56:06 +00:00
|
|
|
}
|
|
|
|
tlsConn.Close()
|
|
|
|
}
|
|
|
|
|
2019-02-19 15:35:05 +00:00
|
|
|
fmt.Println("-------------------")
|
2019-02-19 09:56:06 +00:00
|
|
|
fmt.Println("Pinging with SNI")
|
|
|
|
{
|
|
|
|
tcpConn, err := net.DialTCP("tcp", nil, &net.TCPAddr{IP: ip, Port: 443})
|
|
|
|
if err != nil {
|
2020-11-23 15:38:43 +00:00
|
|
|
base.Fatalf("Failed to dial tcp: %s", err)
|
2019-02-19 09:56:06 +00:00
|
|
|
}
|
|
|
|
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")
|
2019-02-19 15:35:05 +00:00
|
|
|
printCertificates(tlsConn.ConnectionState().PeerCertificates)
|
2019-02-19 09:56:06 +00:00
|
|
|
}
|
|
|
|
tlsConn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Tls ping finished")
|
|
|
|
}
|
|
|
|
|
2020-11-23 15:38:43 +00: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 09:56:06 +00:00
|
|
|
}
|