ssl.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package setup
  2. import (
  3. "crypto"
  4. "crypto/ecdsa"
  5. "crypto/elliptic"
  6. "crypto/rand"
  7. "fmt"
  8. log "github.com/sirupsen/logrus"
  9. "pmail/utils/errors"
  10. "github.com/go-acme/lego/v4/certcrypto"
  11. "github.com/go-acme/lego/v4/certificate"
  12. "github.com/go-acme/lego/v4/challenge/http01"
  13. "github.com/go-acme/lego/v4/challenge/tlsalpn01"
  14. "github.com/go-acme/lego/v4/lego"
  15. "github.com/go-acme/lego/v4/registration"
  16. )
  17. type MyUser struct {
  18. Email string
  19. Registration *registration.Resource
  20. key crypto.PrivateKey
  21. }
  22. func (u *MyUser) GetEmail() string {
  23. return u.Email
  24. }
  25. func (u MyUser) GetRegistration() *registration.Resource {
  26. return u.Registration
  27. }
  28. func (u *MyUser) GetPrivateKey() crypto.PrivateKey {
  29. return u.key
  30. }
  31. func GenSSL() error {
  32. configData, err := readConfig()
  33. if err != nil {
  34. return errors.Wrap(err)
  35. }
  36. // Create a user. New accounts need an email and private key to start.
  37. privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  38. if err != nil {
  39. log.Fatal(err)
  40. }
  41. myUser := MyUser{
  42. Email: "i@" + configData.Domain,
  43. key: privateKey,
  44. }
  45. config := lego.NewConfig(&myUser)
  46. config.Certificate.KeyType = certcrypto.RSA2048
  47. // A client facilitates communication with the CA server.
  48. client, err := lego.NewClient(config)
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. // We specify an HTTP port of 5002 and an TLS port of 5001 on all interfaces
  53. // because we aren't running as root and can't bind a listener to port 80 and 443
  54. // (used later when we attempt to pass challenges). Keep in mind that you still
  55. // need to proxy challenge traffic to port 5002 and 5001.
  56. err = client.Challenge.SetHTTP01Provider(http01.NewProviderServer("", "5001"))
  57. if err != nil {
  58. log.Fatal(err)
  59. }
  60. err = client.Challenge.SetTLSALPN01Provider(tlsalpn01.NewProviderServer("", "443"))
  61. if err != nil {
  62. log.Fatal(err)
  63. }
  64. // New users will need to register
  65. reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. myUser.Registration = reg
  70. request := certificate.ObtainRequest{
  71. Domains: []string{
  72. fmt.Sprintf("smtp.%s", configData.Domain),
  73. configData.WebDomain,
  74. },
  75. Bundle: true,
  76. }
  77. certificates, err := client.Certificate.Obtain(request)
  78. if err != nil {
  79. log.Fatal(err)
  80. }
  81. // Each certificate comes back with the cert bytes, the bytes of the client's
  82. // private key, and a certificate URL. SAVE THESE TO DISK.
  83. fmt.Printf("%#v\n", certificates)
  84. // ... all done.
  85. return nil
  86. }