config.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 (c *Config) BuildCertificates() []tls.Certificate {
  10. certs := make([]tls.Certificate, 0, len(c.Certificate))
  11. for _, entry := range c.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 (c *Config) GetTLSConfig() *tls.Config {
  22. config := &tls.Config{
  23. ClientSessionCache: globalSessionCache,
  24. NextProtos: []string{"http/1.1"},
  25. }
  26. if c == nil {
  27. return config
  28. }
  29. config.InsecureSkipVerify = c.AllowInsecure
  30. config.Certificates = c.BuildCertificates()
  31. config.BuildNameToCertificate()
  32. if len(c.ServerName) > 0 {
  33. config.ServerName = c.ServerName
  34. }
  35. return config
  36. }
  37. func (c *Config) OverrideServerNameIfEmpty(serverName string) {
  38. if len(c.ServerName) == 0 {
  39. c.ServerName = serverName
  40. }
  41. }