1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-09 17:30:44 +00:00
v2fly/transport/internet/tls/config.go

50 lines
1.0 KiB
Go
Raw Normal View History

2016-10-02 21:43:58 +00:00
package tls
import (
"crypto/tls"
2017-02-01 20:35:40 +00:00
"v2ray.com/core/app/log"
2016-10-02 21:43:58 +00:00
)
var (
globalSessionCache = tls.NewLRUClientSessionCache(128)
)
2017-10-26 09:43:02 +00:00
func (c *Config) BuildCertificates() []tls.Certificate {
certs := make([]tls.Certificate, 0, len(c.Certificate))
for _, entry := range c.Certificate {
2016-10-02 21:43:58 +00:00
keyPair, err := tls.X509KeyPair(entry.Certificate, entry.Key)
if err != nil {
2017-04-09 13:04:04 +00:00
log.Trace(newError("ignoring invalid X509 key pair").Base(err).AtWarning())
2016-10-02 21:43:58 +00:00
continue
}
certs = append(certs, keyPair)
}
return certs
}
2017-10-26 09:43:02 +00:00
func (c *Config) GetTLSConfig() *tls.Config {
2016-10-02 21:43:58 +00:00
config := &tls.Config{
ClientSessionCache: globalSessionCache,
2016-12-31 22:22:26 +00:00
NextProtos: []string{"http/1.1"},
2016-10-02 21:43:58 +00:00
}
2017-10-26 09:43:02 +00:00
if c == nil {
2016-10-02 21:43:58 +00:00
return config
}
2017-10-26 09:43:02 +00:00
config.InsecureSkipVerify = c.AllowInsecure
config.Certificates = c.BuildCertificates()
2016-10-02 21:43:58 +00:00
config.BuildNameToCertificate()
2017-10-26 09:43:02 +00:00
if len(c.ServerName) > 0 {
config.ServerName = c.ServerName
2016-12-11 22:58:37 +00:00
}
2016-10-02 21:43:58 +00:00
return config
}
2017-10-26 09:43:02 +00:00
func (c *Config) OverrideServerNameIfEmpty(serverName string) {
if len(c.ServerName) == 0 {
c.ServerName = serverName
}
}