Преглед изворни кода

Set ServerName using tls.DialWithDialer approach

Diego Alvarez пре 5 година
родитељ
комит
ab525ed5db
2 измењених фајлова са 12 додато и 2 уклоњено
  1. 11 0
      broker.go
  2. 1 2
      client_tls_test.go

+ 11 - 0
broker.go

@@ -165,6 +165,17 @@ func (b *Broker) Open(conf *Config) error {
 
 		if conf.Net.TLS.Enable {
 			Logger.Printf("Using tls")
+
+			// If no ServerName is set, infer the ServerName
+			// from the hostname we're connecting to.
+			if conf.Net.TLS.Config.ServerName == "" {
+				colonPos := strings.LastIndex(b.addr, ":")
+				if colonPos == -1 {
+					colonPos = len(b.addr)
+				}
+				hostname := b.addr[:colonPos]
+				conf.Net.TLS.Config.ServerName = hostname
+			}
 			b.conn = tls.Client(b.conn, conf.Net.TLS.Config)
 		}
 

+ 1 - 2
client_tls_test.go

@@ -158,8 +158,7 @@ func TestTLS(t *testing.T) {
 			Succeed: true,
 			Server:  serverTLSConfig,
 			Client: &tls.Config{
-				RootCAs:    pool,
-				ServerName: "127.0.0.1",
+				RootCAs: pool,
 				Certificates: []tls.Certificate{{
 					Certificate: [][]byte{clientDer},
 					PrivateKey:  clientkey,