config.go 915 B

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