profile.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package tlsfingerprint
  2. import (
  3. "crypto/sha256"
  4. "encoding/hex"
  5. "encoding/json"
  6. )
  7. // Clone returns an independent profile. A pooled transport must never retain
  8. // mutable slices owned by an account configuration or the profile cache.
  9. func (p *Profile) Clone() *Profile {
  10. if p == nil {
  11. return nil
  12. }
  13. c := *p
  14. c.CipherSuites = append([]uint16(nil), p.CipherSuites...)
  15. c.Curves = append([]uint16(nil), p.Curves...)
  16. c.PointFormats = append([]uint16(nil), p.PointFormats...)
  17. c.SignatureAlgorithms = append([]uint16(nil), p.SignatureAlgorithms...)
  18. c.ALPNProtocols = append([]string(nil), p.ALPNProtocols...)
  19. c.SupportedVersions = append([]uint16(nil), p.SupportedVersions...)
  20. c.KeyShareGroups = append([]uint16(nil), p.KeyShareGroups...)
  21. c.PSKModes = append([]uint16(nil), p.PSKModes...)
  22. c.Extensions = append([]uint16(nil), p.Extensions...)
  23. return &c
  24. }
  25. // CacheKey identifies the complete configured template, including slice order.
  26. // In particular, editing a template without renaming it creates a new pool.
  27. // Account and proxy isolation must be added by the caller.
  28. func (p *Profile) CacheKey() string {
  29. if p == nil {
  30. return "none"
  31. }
  32. b, _ := json.Marshal(p) // Profile only contains JSON-supported scalar/slice fields.
  33. h := sha256.Sum256(b)
  34. return hex.EncodeToString(h[:])
  35. }