certchainhash.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package control
  2. import (
  3. "encoding/base64"
  4. "encoding/pem"
  5. "flag"
  6. "fmt"
  7. "github.com/v2fly/v2ray-core/v4/common"
  8. v2tls "github.com/v2fly/v2ray-core/v4/transport/internet/tls"
  9. "io/ioutil"
  10. )
  11. type CertificateChainHashCommand struct {
  12. }
  13. func (c CertificateChainHashCommand) Name() string {
  14. return "certChainHash"
  15. }
  16. func (c CertificateChainHashCommand) Description() Description {
  17. return Description{
  18. Short: "Calculate TLS certificates hash.",
  19. Usage: []string{
  20. "v2ctl certChainHash --cert <cert.pem>",
  21. "Calculate TLS certificate chain hash.",
  22. },
  23. }
  24. }
  25. func (c CertificateChainHashCommand) Execute(args []string) error {
  26. fs := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  27. cert := fs.String("cert", "fullchain.pem", "The file path of the certificates chain")
  28. if err := fs.Parse(args); err != nil {
  29. return err
  30. }
  31. certContent, err := ioutil.ReadFile(*cert)
  32. if err != nil {
  33. return err
  34. }
  35. var certChain [][]byte
  36. for {
  37. block, remain := pem.Decode(certContent)
  38. if block == nil {
  39. break
  40. }
  41. certChain = append(certChain, block.Bytes)
  42. certContent = remain
  43. }
  44. certChainHash := v2tls.GenerateCertChainHash(certChain)
  45. certChainHashB64 := base64.StdEncoding.EncodeToString(certChainHash)
  46. fmt.Println(certChainHashB64)
  47. return nil
  48. }
  49. func init() {
  50. common.Must(RegisterCommand(&CertificateChainHashCommand{}))
  51. }