config.go 811 B

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