From b46185e1b028f77edbf88876fd9c4e6e5f4e2833 Mon Sep 17 00:00:00 2001 From: Kiyon Date: Tue, 23 Feb 2021 16:40:17 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20Fix=20hardcode=20tls=20to=20star?= =?UTF-8?q?tupMessage=20in=20app.Listener?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.go | 2 +- app_test.go | 44 +++++++++++++++++++++----------------------- helpers.go | 13 +++++++++++-- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/app.go b/app.go index c256b465..46fc3e2c 100644 --- a/app.go +++ b/app.go @@ -568,7 +568,7 @@ func (app *App) Listener(ln net.Listener) error { app.startupProcess() // Print startup message if !app.config.DisableStartupMessage { - app.startupMessage(ln.Addr().String(), false, "") + app.startupMessage(ln.Addr().String(), getTlsConfig(ln) != nil, "") } // Start listening return app.server.Serve(ln) diff --git a/app_test.go b/app_test.go index c6e29f1b..c1b07355 100644 --- a/app_test.go +++ b/app_test.go @@ -382,29 +382,6 @@ func Test_App_Add_Method_Test(t *testing.T) { app.Add("JOHN", "/doe", testEmptyHandler) } -func Test_App_Listener_TLS(t *testing.T) { - app := New() - - // Create tls certificate - cer, err := tls.LoadX509KeyPair("./.github/testdata/ssl.pem", "./.github/testdata/ssl.key") - if err != nil { - utils.AssertEqual(t, nil, err) - } - config := &tls.Config{Certificates: []tls.Certificate{cer}} - - ln, err := net.Listen(NetworkTCP4, ":3078") - utils.AssertEqual(t, nil, err) - - ln = tls.NewListener(ln, config) - - go func() { - time.Sleep(1000 * time.Millisecond) - utils.AssertEqual(t, nil, app.Shutdown()) - }() - - utils.AssertEqual(t, nil, app.Listener(ln)) -} - // go test -run Test_App_GETOnly func Test_App_GETOnly(t *testing.T) { app := New(Config{ @@ -1011,6 +988,27 @@ func Test_App_Listener_Prefork(t *testing.T) { utils.AssertEqual(t, nil, app.Listener(ln)) } +func Test_App_Listener_TLS(t *testing.T) { + // Create tls certificate + cer, err := tls.LoadX509KeyPair("./.github/testdata/ssl.pem", "./.github/testdata/ssl.key") + if err != nil { + utils.AssertEqual(t, nil, err) + } + config := &tls.Config{Certificates: []tls.Certificate{cer}} + + ln, err := tls.Listen(NetworkTCP4, ":0", config) + utils.AssertEqual(t, nil, err) + + app := New() + + go func() { + time.Sleep(time.Millisecond * 500) + utils.AssertEqual(t, nil, app.Shutdown()) + }() + + utils.AssertEqual(t, nil, app.Listener(ln)) +} + // go test -v -run=^$ -bench=Benchmark_AcquireCtx -benchmem -count=4 func Benchmark_AcquireCtx(b *testing.B) { app := New() diff --git a/helpers.go b/helpers.go index 4439a5c2..a0f8d161 100644 --- a/helpers.go +++ b/helpers.go @@ -49,6 +49,14 @@ func lnMetadata(network string, ln net.Listener) (addr string, cfg *tls.Config) panic("listener: " + addr + ": Only one usage of each socket address (protocol/network address/port) is normally permitted.") } + cfg = getTlsConfig(ln) + + return +} + +/* #nosec */ +// getTlsConfig returns a net listener's tls config +func getTlsConfig(ln net.Listener) *tls.Config { // Get listener type pointer := reflect.ValueOf(ln) @@ -63,13 +71,14 @@ func lnMetadata(network string, ln net.Listener) (addr string, cfg *tls.Config) // Get element from pointer if elem := newval.Elem(); elem.Type() != nil { // Cast value to *tls.Config - cfg = elem.Interface().(*tls.Config) + return elem.Interface().(*tls.Config) } } } } } - return + + return nil } // readContent opens a named file and read content from it