config.go 954 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package tls
  2. import (
  3. "crypto/tls"
  4. "v2ray.com/core/app/log"
  5. "v2ray.com/core/common/errors"
  6. )
  7. var (
  8. globalSessionCache = tls.NewLRUClientSessionCache(128)
  9. )
  10. func (v *Config) BuildCertificates() []tls.Certificate {
  11. certs := make([]tls.Certificate, 0, len(v.Certificate))
  12. for _, entry := range v.Certificate {
  13. keyPair, err := tls.X509KeyPair(entry.Certificate, entry.Key)
  14. if err != nil {
  15. log.Trace(errors.New("TLS: ignoring invalid X509 key pair").Base(err).AtWarning())
  16. continue
  17. }
  18. certs = append(certs, keyPair)
  19. }
  20. return certs
  21. }
  22. func (v *Config) GetTLSConfig() *tls.Config {
  23. config := &tls.Config{
  24. ClientSessionCache: globalSessionCache,
  25. NextProtos: []string{"http/1.1"},
  26. }
  27. if v == nil {
  28. return config
  29. }
  30. config.InsecureSkipVerify = v.AllowInsecure
  31. config.Certificates = v.BuildCertificates()
  32. config.BuildNameToCertificate()
  33. if len(v.ServerName) > 0 {
  34. config.ServerName = v.ServerName
  35. }
  36. return config
  37. }