letsencryptserver.go 828 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package main
  2. import (
  3. "crypto/tls"
  4. "net"
  5. "github.com/valyala/fasthttp"
  6. "golang.org/x/crypto/acme"
  7. "golang.org/x/crypto/acme/autocert"
  8. )
  9. func requestHandler(ctx *fasthttp.RequestCtx) {
  10. ctx.SetBodyString("hello from https!")
  11. }
  12. func main() {
  13. m := &autocert.Manager{
  14. Prompt: autocert.AcceptTOS,
  15. HostPolicy: autocert.HostWhitelist("example.com"), // Replace with your domain.
  16. Cache: autocert.DirCache("./certs"),
  17. }
  18. cfg := &tls.Config{
  19. GetCertificate: m.GetCertificate,
  20. NextProtos: []string{
  21. "http/1.1", acme.ALPNProto,
  22. },
  23. }
  24. // Let's Encrypt tls-alpn-01 only works on port 443.
  25. ln, err := net.Listen("tcp4", "0.0.0.0:443") /* #nosec G102 */
  26. if err != nil {
  27. panic(err)
  28. }
  29. lnTls := tls.NewListener(ln, cfg)
  30. if err := fasthttp.Serve(lnTls, requestHandler); err != nil {
  31. panic(err)
  32. }
  33. }