95 lines
2.2 KiB
Go
95 lines
2.2 KiB
Go
|
package test
|
||
|
|
||
|
import (
|
||
|
"crypto"
|
||
|
"crypto/ecdsa"
|
||
|
"crypto/elliptic"
|
||
|
"crypto/rand"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"log"
|
||
|
"testing"
|
||
|
|
||
|
"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/lego"
|
||
|
"github.com/go-acme/lego/v4/registration"
|
||
|
)
|
||
|
|
||
|
// You'll need a user or account type that implements acme.User
|
||
|
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 TestLego(t *testing.T) {
|
||
|
// 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: "me@jackyu.cn",
|
||
|
key: privateKey,
|
||
|
}
|
||
|
|
||
|
config := lego.NewConfig(&myUser)
|
||
|
|
||
|
// This CA URL is configured for a local dev instance of Boulder running in Dockerfile in a VM.
|
||
|
config.CADirURL = "https://acme-staging-v02.api.letsencrypt.org/directory"
|
||
|
config.Certificate.KeyType = certcrypto.RSA2048
|
||
|
|
||
|
// A client facilitates communication with the CA server.
|
||
|
client, err := lego.NewClient(config)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
|
err = client.Challenge.SetHTTP01Provider(http01.NewProviderServer("", "9180"))
|
||
|
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{"shanghai2.ojbk.me"},
|
||
|
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)
|
||
|
err = ioutil.WriteFile("fullchain.cer", certificates.Certificate, 0644)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
err = ioutil.WriteFile("private.key", certificates.PrivateKey, 0644)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
|
}
|