From ff9bb2d8df194761ab39d6409ed9c68c86ed799e Mon Sep 17 00:00:00 2001 From: RPRX <63339210+rprx@users.noreply.github.com> Date: Thu, 17 Dec 2020 09:25:30 +0000 Subject: [PATCH] Optimize cipherSuites setting loader --- transport/internet/tls/config.go | 20 ++++++++------------ transport/internet/xtls/config.go | 20 ++++++++------------ 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/transport/internet/tls/config.go b/transport/internet/tls/config.go index 541a1c61..1238fe8c 100644 --- a/transport/internet/tls/config.go +++ b/transport/internet/tls/config.go @@ -234,22 +234,18 @@ func (c *Config) GetTLSConfig(opts ...Option) *tls.Config { config.MaxVersion = tls.VersionTLS13 } - var cipherSuites []uint16 if len(c.CipherSuites) > 0 { - cipherSuitesArray := strings.Split(c.CipherSuites, ":") - if len(cipherSuitesArray) > 0 { - all := tls.CipherSuites() - for _, suite := range cipherSuitesArray { - for _, s := range all { - if s.Name == suite { - cipherSuites = append(cipherSuites, s.ID) - break - } - } + id := make(map[string]uint16) + for _, s := range tls.CipherSuites() { + id[s.Name] = s.ID + } + for _, n := range strings.Split(c.CipherSuites, ":") { + if id[n] != 0 { + config.CipherSuites = append(config.CipherSuites, id[n]) } } } - config.CipherSuites = cipherSuites + config.PreferServerCipherSuites = c.PreferServerCipherSuites return config diff --git a/transport/internet/xtls/config.go b/transport/internet/xtls/config.go index 2989336f..926657ce 100644 --- a/transport/internet/xtls/config.go +++ b/transport/internet/xtls/config.go @@ -225,22 +225,18 @@ func (c *Config) GetXTLSConfig(opts ...Option) *xtls.Config { config.MaxVersion = xtls.VersionTLS13 } - var cipherSuites []uint16 if len(c.CipherSuites) > 0 { - cipherSuitesArray := strings.Split(c.CipherSuites, ":") - if len(cipherSuitesArray) > 0 { - all := xtls.CipherSuites() - for _, suite := range cipherSuitesArray { - for _, s := range all { - if s.Name == suite { - cipherSuites = append(cipherSuites, s.ID) - break - } - } + id := make(map[string]uint16) + for _, s := range xtls.CipherSuites() { + id[s.Name] = s.ID + } + for _, n := range strings.Split(c.CipherSuites, ":") { + if id[n] != 0 { + config.CipherSuites = append(config.CipherSuites, id[n]) } } } - config.CipherSuites = cipherSuites + config.PreferServerCipherSuites = c.PreferServerCipherSuites return config