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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悶 Autocert example is not working #168

Closed
misteeka opened this issue Apr 25, 2022 · 3 comments
Closed

馃悶 Autocert example is not working #168

misteeka opened this issue Apr 25, 2022 · 3 comments
Labels
鈽笍 Bug Something isn't working 馃 Question Further information is requested

Comments

@misteeka
Copy link

Fiber version/commit
v2.32.0
Issue description
I copied example "autocert" but it is not working (Can't establish a connection with site. 127.0.0.1 refused to connect.)
Expected behavior

Steps to reproduce

Code snippet

package main

import (
	"crypto/tls"
	"log"

	"github.com/gofiber/fiber/v2"
	"golang.org/x/crypto/acme/autocert"
)

func main() {
	// Fiber instance
	app := fiber.New()

	// Routes
	app.Get("/", func(c *fiber.Ctx) error {
		return c.SendString("This is a secure server 馃懏")
	})

	// Let鈥檚 Encrypt has rate limits: https://letsencrypt.org/docs/rate-limits/
	// It's recommended to use it's staging environment to test the code:
	// https://letsencrypt.org/docs/staging-environment/

	// 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"),
	}

	// TLS Config
	cfg := &tls.Config{
		// Get Certificate from Let's Encrypt
		GetCertificate: m.GetCertificate,
		// By default NextProtos contains the "h2"
		// This has to be removed since Fasthttp does not support HTTP/2
		// Or it will cause a flood of PRI method logs
		// http://webconcepts.info/concepts/http-method/PRI
		NextProtos: []string{
			"http/1.1", "acme-tls/1",
		},
	}
	ln, err := tls.Listen("tcp", ":443", cfg)
	if err != nil {
		panic(err)
	}

	// Start server
	log.Fatal(app.Listener(ln))
}
@misteeka misteeka added the 鈽笍 Bug Something isn't working label Apr 25, 2022
@welcome
Copy link

welcome bot commented Apr 25, 2022

Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template!

@ReneWerner87
Copy link
Member

where did you test the code ? on your local development environment ? or as described in the comments on your staging system which is accessible from outside ?

for localhost/127.0.0.1
https://letsencrypt.org/docs/certificates-for-localhost/

@ReneWerner87 ReneWerner87 added the 馃 Question Further information is requested label Apr 25, 2022
@misteeka
Copy link
Author

misteeka commented Apr 25, 2022

Thank you for your comment, it helped me figure out the problem.
This code actually shoudnt work because autocert cant generate cert for localhost
The solution: generate certificate by yourself, for example with mkcert

(Forgot about it, because I always used autocert with a domain targeting to localhost)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
鈽笍 Bug Something isn't working 馃 Question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants