mirror of
https://github.com/Jinnrry/PMail.git
synced 2025-11-03 02:43:31 +08:00
104 lines
2.5 KiB
Go
104 lines
2.5 KiB
Go
package setup
|
|
|
|
import (
|
|
"crypto"
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"fmt"
|
|
log "github.com/sirupsen/logrus"
|
|
"pmail/utils/errors"
|
|
|
|
"github.com/go-acme/lego/v4/certcrypto"
|
|
"github.com/go-acme/lego/v4/certificate"
|
|
"github.com/go-acme/lego/v4/challenge/http01"
|
|
"github.com/go-acme/lego/v4/challenge/tlsalpn01"
|
|
"github.com/go-acme/lego/v4/lego"
|
|
"github.com/go-acme/lego/v4/registration"
|
|
)
|
|
|
|
type MyUser struct {
|
|
Email string
|
|
Registration *registration.Resource
|
|
key crypto.PrivateKey
|
|
}
|
|
|
|
func (u *MyUser) GetEmail() string {
|
|
return u.Email
|
|
}
|
|
func (u MyUser) GetRegistration() *registration.Resource {
|
|
return u.Registration
|
|
}
|
|
func (u *MyUser) GetPrivateKey() crypto.PrivateKey {
|
|
return u.key
|
|
}
|
|
|
|
func GenSSL() error {
|
|
|
|
configData, err := readConfig()
|
|
if err != nil {
|
|
return errors.Wrap(err)
|
|
}
|
|
|
|
// Create a user. New accounts need an email and private key to start.
|
|
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
myUser := MyUser{
|
|
Email: "i@" + configData.Domain,
|
|
key: privateKey,
|
|
}
|
|
|
|
config := lego.NewConfig(&myUser)
|
|
|
|
config.Certificate.KeyType = certcrypto.RSA2048
|
|
|
|
// A client facilitates communication with the CA server.
|
|
client, err := lego.NewClient(config)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// We specify an HTTP port of 5002 and an TLS port of 5001 on all interfaces
|
|
// because we aren't running as root and can't bind a listener to port 80 and 443
|
|
// (used later when we attempt to pass challenges). Keep in mind that you still
|
|
// need to proxy challenge traffic to port 5002 and 5001.
|
|
err = client.Challenge.SetHTTP01Provider(http01.NewProviderServer("", "5001"))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
err = client.Challenge.SetTLSALPN01Provider(tlsalpn01.NewProviderServer("", "443"))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// New users will need to register
|
|
reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
myUser.Registration = reg
|
|
|
|
request := certificate.ObtainRequest{
|
|
Domains: []string{
|
|
fmt.Sprintf("smtp.%s", configData.Domain),
|
|
configData.WebDomain,
|
|
},
|
|
Bundle: true,
|
|
}
|
|
certificates, err := client.Certificate.Obtain(request)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Each certificate comes back with the cert bytes, the bytes of the client's
|
|
// private key, and a certificate URL. SAVE THESE TO DISK.
|
|
fmt.Printf("%#v\n", certificates)
|
|
|
|
// ... all done.
|
|
|
|
return nil
|
|
}
|