Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 bug: make tlsHandler public to use it with Listener #2034

Merged
merged 6 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type App struct {
latestRoute *Route
latestGroup *Group
// TLS handler
tlsHandler *tlsHandler
tlsHandler *TLSHandler
}

// Config is a struct holding the server settings.
Expand Down Expand Up @@ -563,6 +563,14 @@ func (app *App) handleTrustedProxy(ipAddress string) {
}
}

// You can use SetTLSHandler to use ClientHelloInfo when using TLS with Listener.
func (app *App) SetTLSHandler(tlsHandler *TLSHandler) {
// Attach the tlsHandler to the config
app.mutex.Lock()
app.tlsHandler = tlsHandler
app.mutex.Unlock()
}

// Mount attaches another app instance as a sub-router along a routing path.
// It's very useful to split up a large API as many independent routers and
// compose them as a single service using Mount. The fiber's error handler and
Expand Down
15 changes: 15 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package fiber

import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -1560,3 +1561,17 @@ func Test_App_Test_no_timeout_infinitely(t *testing.T) {
t.FailNow()
}
}

func Test_App_SetTLSHandler(t *testing.T) {
tlsHandler := &TLSHandler{clientHelloInfo: &tls.ClientHelloInfo{
ServerName: "example.golang",
}}

app := New()
app.SetTLSHandler(tlsHandler)

c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)

utils.AssertEqual(t, "example.golang", c.ClientHelloInfo().ServerName)
}
6 changes: 3 additions & 3 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ type Ctx struct {
viewBindMap *dictpool.Dict // Default view map to bind template engine
}

// tlsHandle object
type tlsHandler struct {
// TLSHandler object
type TLSHandler struct {
clientHelloInfo *tls.ClientHelloInfo
}

// GetClientInfo Callback function to set CHI
func (t *tlsHandler) GetClientInfo(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
func (t *TLSHandler) GetClientInfo(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
t.clientHelloInfo = info
return nil, nil
}
Expand Down
2 changes: 1 addition & 1 deletion ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ func Test_Ctx_ClientHelloInfo(t *testing.T) {
PSSWithSHA256 = 0x0804
VersionTLS13 = 0x0304
)
app.tlsHandler = &tlsHandler{clientHelloInfo: &tls.ClientHelloInfo{
app.tlsHandler = &TLSHandler{clientHelloInfo: &tls.ClientHelloInfo{
ServerName: "example.golang",
SignatureSchemes: []tls.SignatureScheme{PSSWithSHA256},
SupportedVersions: []uint16{VersionTLS13},
Expand Down
24 changes: 19 additions & 5 deletions listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ func (app *App) Listener(ln net.Listener) error {
addr, tlsConfig := lnMetadata(app.config.Network, ln)
return app.prefork(app.config.Network, addr, tlsConfig)
}

// prepare the server for the start
app.startupProcess()

// Print startup message
if !app.config.DisableStartupMessage {
app.startupMessage(ln.Addr().String(), getTlsConfig(ln) != nil, "")
}

// Print routes
if app.config.EnablePrintRoutes {
app.printRoutesMessage()
}

// Start listening
return app.server.Serve(ln)
}
Expand All @@ -54,21 +58,26 @@ func (app *App) Listen(addr string) error {
if app.config.Prefork {
return app.prefork(app.config.Network, addr, nil)
}

// Setup listener
ln, err := net.Listen(app.config.Network, addr)
if err != nil {
return err
}

// prepare the server for the start
app.startupProcess()

// Print startup message
if !app.config.DisableStartupMessage {
app.startupMessage(ln.Addr().String(), false, "")
}

// Print routes
if app.config.EnablePrintRoutes {
app.printRoutesMessage()
}

// Start listening
return app.server.Serve(ln)
}
Expand All @@ -82,19 +91,22 @@ func (app *App) ListenTLS(addr, certFile, keyFile string) error {
if len(certFile) == 0 || len(keyFile) == 0 {
return errors.New("tls: provide a valid cert or key path")
}

// Set TLS config with handler
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return fmt.Errorf("tls: cannot load TLS key pair from certFile=%q and keyFile=%q: %s", certFile, keyFile, err)
}
tlsHandler := &tlsHandler{}

tlsHandler := &TLSHandler{}
config := &tls.Config{
MinVersion: tls.VersionTLS12,
Certificates: []tls.Certificate{
cert,
},
GetCertificate: tlsHandler.GetClientInfo,
}

// Prefork is supported
if app.config.Prefork {
return app.prefork(app.config.Network, addr, config)
Expand All @@ -103,23 +115,25 @@ func (app *App) ListenTLS(addr, certFile, keyFile string) error {
// Setup listener
ln, err := net.Listen(app.config.Network, addr)
ln = tls.NewListener(ln, config)

if err != nil {
return err
}

// prepare the server for the start
app.startupProcess()

// Print startup message
if !app.config.DisableStartupMessage {
app.startupMessage(ln.Addr().String(), true, "")
}

// Print routes
if app.config.EnablePrintRoutes {
app.printRoutesMessage()
}

// Attach the tlsHandler to the config
app.tlsHandler = tlsHandler
app.SetTLSHandler(tlsHandler)

// Start listening
return app.server.Serve(ln)
Expand Down Expand Up @@ -147,7 +161,7 @@ func (app *App) ListenMutualTLS(addr, certFile, keyFile, clientCertFile string)
clientCertPool := x509.NewCertPool()
clientCertPool.AppendCertsFromPEM(clientCACert)

tlsHandler := &tlsHandler{}
tlsHandler := &TLSHandler{}
config := &tls.Config{
MinVersion: tls.VersionTLS12,
ClientAuth: tls.RequireAndVerifyClientCert,
Expand Down Expand Up @@ -183,7 +197,7 @@ func (app *App) ListenMutualTLS(addr, certFile, keyFile, clientCertFile string)
}

// Attach the tlsHandler to the config
app.tlsHandler = tlsHandler
app.SetTLSHandler(tlsHandler)

// Start listening
return app.server.Serve(ln)
Expand Down