Skip to content

Commit

Permalink
make tlshandler public
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Aug 20, 2022
1 parent 1830fb6 commit 1db8c14
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 18 deletions.
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)
}
7 changes: 4 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 Expand Up @@ -811,6 +811,7 @@ func (c *Ctx) MultipartForm() (*multipart.Form, error) {

// ClientHelloInfo return CHI from context
func (c *Ctx) ClientHelloInfo() *tls.ClientHelloInfo {
fmt.Print(c.app.tlsHandler)
if c.app.tlsHandler != nil {
return c.app.tlsHandler.clientHelloInfo
}
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
30 changes: 17 additions & 13 deletions listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,9 @@ import (

// Listener can be used to pass a custom listener.
func (app *App) Listener(ln net.Listener) error {
// ClientHelloInfo support
tlsHandler := &tlsHandler{}
addr, tlsConfig := lnMetadata(app.config.Network, ln)
if tlsConfig != nil {
tlsConfig.GetCertificate = tlsHandler.GetClientInfo
}

// Prefork is supported for custom listeners
if app.config.Prefork {
addr, tlsConfig := lnMetadata(app.config.Network, ln)
return app.prefork(app.config.Network, addr, tlsConfig)
}

Expand All @@ -51,9 +45,6 @@ func (app *App) Listener(ln net.Listener) error {
app.printRoutesMessage()
}

// Attach the tlsHandler to the config
app.tlsHandler = tlsHandler

// Start listening
return app.server.Serve(ln)
}
Expand All @@ -67,17 +58,21 @@ 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()
Expand All @@ -96,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 @@ -117,23 +115,27 @@ 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.mutex.Lock()
app.tlsHandler = tlsHandler
app.mutex.Unlock()

// Start listening
return app.server.Serve(ln)
Expand Down Expand Up @@ -161,7 +163,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 @@ -197,7 +199,9 @@ func (app *App) ListenMutualTLS(addr, certFile, keyFile, clientCertFile string)
}

// Attach the tlsHandler to the config
app.mutex.Lock()
app.tlsHandler = tlsHandler
app.mutex.Unlock()

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

1 comment on commit 1db8c14

@ReneWerner87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 1db8c14 Previous: b161f80 Ratio
Benchmark_Ctx_Protocol 15.91 ns/op 0 B/op 0 allocs/op 2.426 ns/op 0 B/op 0 allocs/op 6.56
Benchmark_StatusMessage/default 9.254 ns/op 0 B/op 0 allocs/op 4.017 ns/op 0 B/op 0 allocs/op 2.30

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.