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

🔥 feat: Add support for AutoTLS / ACME #3201

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/tinylib/msgp v1.2.4
github.com/valyala/bytebufferpool v1.0.0
github.com/valyala/fasthttp v1.57.0
golang.org/x/crypto v0.28.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
15 changes: 15 additions & 0 deletions listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/gofiber/fiber/v3/log"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"golang.org/x/crypto/acme/autocert"
)

// Figlet text to show Fiber ASCII art on startup message
Expand Down Expand Up @@ -70,6 +71,14 @@ type ListenConfig struct {
//
// Default: nil
OnShutdownSuccess func()

// AutoCertManager manages TLS certificates automatically using the ACME protocol,
// enabling integration with Let's Encrypt or other ACME-compatible providers.
// Provide an *autocert.Manager instance to enable automatic certificate management.
//
// Default: nil
AutoCertManager *autocert.Manager

// Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only)
// WARNING: When prefork is set to true, only "tcp4" and "tcp6" can be chosen.
//
Expand Down Expand Up @@ -176,6 +185,12 @@ func (app *App) Listen(addr string, config ...ListenConfig) error {

// Attach the tlsHandler to the config
app.SetTLSHandler(tlsHandler)
} else if cfg.AutoCertManager != nil {
wangjq4214 marked this conversation as resolved.
Show resolved Hide resolved
tlsConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
GetCertificate: cfg.AutoCertManager.GetCertificate,
NextProtos: []string{"http/1.1", "acme-tls/1"},
}
}

if cfg.TLSConfigFunc != nil {
Expand Down
80 changes: 80 additions & 0 deletions listen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"github.com/stretchr/testify/require"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
"golang.org/x/crypto/acme"
"golang.org/x/crypto/acme/autocert"
)

// go test -run Test_Listen
Expand Down Expand Up @@ -145,6 +147,45 @@
}))
}

// go test -run Test_Listen_Acme_TLS
func Test_Listen_Acme_TLS(t *testing.T) {
dir, _ := os.Getwd()

Check failure on line 152 in listen_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `os.Getwd` is not checked (errcheck)
dir, err := os.MkdirTemp(dir, "certs")
require.NoError(t, err)
defer os.RemoveAll(dir)

Check failure on line 155 in listen_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `os.RemoveAll` is not checked (errcheck)

// Certificate manager
m := &autocert.Manager{
Prompt: autocert.AcceptTOS,
// Replace with your domain
HostPolicy: autocert.HostWhitelist("example.com"),
// Folder to store the certificates
Cache: autocert.DirCache(dir),
// Define the Client for test
Client: &acme.Client{
DirectoryURL: "https://acme-staging-v02.api.letsencrypt.org/directory",
},
}

app := New()

// invalid port
require.Error(t, app.Listen(":99999", ListenConfig{
DisableStartupMessage: true,
AutoCertManager: m,
}))

go func() {
time.Sleep(1000 * time.Millisecond)
assert.NoError(t, app.Shutdown())
}()

require.NoError(t, app.Listen(":0", ListenConfig{
DisableStartupMessage: true,
AutoCertManager: m,
}))
}

// go test -run Test_Listen_TLS_Prefork
func Test_Listen_TLS_Prefork(t *testing.T) {
testPreforkMaster = true
Expand Down Expand Up @@ -172,6 +213,45 @@
}))
}

// go test -run Test_Listen_Acme_TLS_Prefork
func Test_Listen_Acme_TLS_Prefork(t *testing.T) {
dir, _ := os.MkdirTemp(".", "./certs")

Check failure on line 218 in listen_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `os.MkdirTemp` is not checked (errcheck)
defer os.RemoveAll(dir)

Check failure on line 219 in listen_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `os.RemoveAll` is not checked (errcheck)

// Certificate manager
m := &autocert.Manager{
Prompt: autocert.AcceptTOS,
// Replace with your domain
HostPolicy: autocert.HostWhitelist("example.com"),
// Folder to store the certificates
Cache: autocert.DirCache(dir),
// Define the Client for test
Client: &acme.Client{
DirectoryURL: "https://acme-staging-v02.api.letsencrypt.org/directory",
},
}

app := New()

// invalid port
require.Error(t, app.Listen(":0", ListenConfig{
DisableStartupMessage: true,
EnablePrefork: true,
AutoCertManager: m,
}))

go func() {
time.Sleep(1000 * time.Millisecond)
assert.NoError(t, app.Shutdown())
}()

require.NoError(t, app.Listen(":99999", ListenConfig{
DisableStartupMessage: true,
EnablePrefork: true,
AutoCertManager: m,
}))
}

// go test -run Test_Listen_MutualTLS
func Test_Listen_MutualTLS(t *testing.T) {
app := New()
Expand Down
Loading