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 4 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
13 changes: 13 additions & 0 deletions listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"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,13 @@
//
// Default: nil
OnShutdownSuccess func()

// AutoCertManager is a acme manager for go crypto package.
wangjq4214 marked this conversation as resolved.
Show resolved Hide resolved
// If you want to use acme, you have to provide it.
//
// 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 +184,11 @@

// 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{

Check failure on line 188 in listen.go

View workflow job for this annotation

GitHub Actions / lint

G402: TLS MinVersion too low. (gosec)
GetCertificate: cfg.AutoCertManager.GetCertificate,
NextProtos: []string{"http/1.1", "acme-tls/1"},
}
}

if cfg.TLSConfigFunc != nil {
Expand Down
70 changes: 70 additions & 0 deletions listen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"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,38 @@ func Test_Listen_TLS(t *testing.T) {
}))
}

// go test -run Test_Listen_Acme_TLS
func Test_Listen_Acme_TLS(t *testing.T) {
// 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("./certs"),
wangjq4214 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Can we check if the cert has been downloaded to the 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{
AutoCertManager: m,
}))

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

require.NoError(t, app.Listen(":0", ListenConfig{
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 +206,42 @@ func Test_Listen_TLS_Prefork(t *testing.T) {
}))
}

// go test -run Test_Listen_Acme_TLS_Prefork
func Test_Listen_Acme_TLS_Prefork(t *testing.T) {
// 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("./certs"),
wangjq4214 marked this conversation as resolved.
Show resolved Hide resolved
// 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