/
main.go
198 lines (166 loc) · 4.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"regexp"
"strings"
"time"
"github.com/xenolf/lego/acme"
"github.com/xenolf/lego/providers/dns/dnsimple"
)
// Create a user. New accounts need an email and private key to start.
const rsaKeySize = 2048
// You'll need a user or account type that implements acme.User
type User struct {
Email string
Registration *acme.RegistrationResource
key crypto.PrivateKey
}
func (u User) GetEmail() string {
return u.Email
}
func (u User) GetRegistration() *acme.RegistrationResource {
return u.Registration
}
func (u User) GetPrivateKey() crypto.PrivateKey {
return u.key
}
var (
certOut string
dnsimpleApiKey string
acmeUrl string
email string
dataPath string
)
func usage() {
bin := path.Base(os.Args[0])
fmt.Fprintf(os.Stderr, "Usage of %s:\n", bin)
fmt.Fprintf(os.Stderr, "\t%s [flags] domain\n", bin)
fmt.Fprintf(os.Stderr, "\t%s [flags] domain1,domainN\n", bin)
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
}
func init() {
flag.StringVar(&certOut, "out", "", "Certificates output path")
flag.StringVar(&dnsimpleApiKey, "api-key", "", "DNSimple APIv2 account token")
flag.StringVar(&acmeUrl, "url", "https://acme-staging.api.letsencrypt.org/", "The CA URL")
flag.StringVar(&email, "email", "", "Email used for registration and recovery contact")
flag.StringVar(&dataPath, "path", ".data", "Directory to use for storing the data")
flag.Usage = usage
flag.Parse()
}
func main() {
if flag.NArg() != 1 {
flag.Usage()
os.Exit(2)
}
if email == "" {
fmt.Println("--email is required")
os.Exit(2)
}
now := time.Now().Unix()
// ?
if r, _ := regexp.Compile("%v"); r.MatchString(email) {
// ?
email = fmt.Sprintf(email, now)
}
domains := strings.Split(flag.Args()[0], ",")
privateKey, err := rsa.GenerateKey(rand.Reader, rsaKeySize)
if err != nil {
log.Fatal(err)
}
user := User{
Email: email,
key: privateKey,
}
usersPath := fmt.Sprintf("%v/users/%v", dataPath, user.GetEmail())
log.Println(usersPath)
fileWrite(usersPath, "privkey.pem", pemEncode(privateKey))
fileWrite(usersPath, "pubkey.pem", pemEncode(privateKey.Public()))
// log: user
log.Println(user)
// A client facilitates communication with the CA server.
client, err := acme.NewClient(strings.Join([]string{acmeUrl, "directory"}, "/"), &user, acme.RSA2048)
if err != nil {
log.Fatal(err)
}
// Force to use DNSimple
provider, err := dnsimple.NewDNSProviderCredentials(dnsimpleApiKey, "")
if err != nil {
log.Fatal(err)
}
client.ExcludeChallenges([]acme.Challenge{acme.HTTP01, acme.TLSSNI01})
err = client.SetChallengeProvider(acme.DNS01, provider)
if err != nil {
log.Fatal(err)
}
// New users will need to register; be sure to save it
reg, err := client.Register()
if err != nil {
log.Fatal(err)
}
user.Registration = reg
// log: registration
log.Println(reg)
// The client has a URL to the current Let's Encrypt Subscriber Agreement.
// The user will need to agree to it.
err = client.AgreeToTOS()
if err != nil {
log.Fatal(err)
}
// The acme library takes care of completing the challenges to obtain the certificate(s).
bundle := true
certificates, failures := client.ObtainCertificate(domains, bundle, nil, false)
if len(failures) > 0 {
log.Fatal(failures)
}
// log: certificate
log.Println(fmt.Printf("[INFO][%s] Certificate %s", certificates.CertURL, strings.Join(domains, ", ")))
// Each certificate comes back with the cert bytes, the bytes of the client's
// private key, and a certificate URL. This is where you should save them to files!
//fmt.Printf("%#v\n", certificates)
var certsPath string
if certOut == "" {
certsPath = fmt.Sprintf("%v/certs/%v", dataPath, now)
} else {
certsPath = certOut
}
log.Println(certsPath)
fileWrite(certsPath, "privkey.pem", certificates.PrivateKey)
fileWrite(certsPath, "fullchain.pem", certificates.Certificate)
log.Println("completed!")
}
func mkPath(path string) {
if err := os.MkdirAll(path, 0755); err != nil {
log.Fatal(err)
}
}
func fileWrite(path, filename string, data []byte) {
filepath := fmt.Sprintf("%s/%s", path, filename)
mkPath(path)
if err := ioutil.WriteFile(filepath, data, 0644); err != nil {
log.Fatal(err)
}
}
func pemEncode(data interface{}) []byte {
var pemBlock *pem.Block
switch key := data.(type) {
case *rsa.PrivateKey:
pemBlock = &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}
break
case *rsa.PublicKey:
bytes, _ := x509.MarshalPKIXPublicKey(key)
pemBlock = &pem.Block{Type: "RSA PUBLIC KEY", Bytes: bytes}
break
}
return pem.EncodeToMemory(pemBlock)
}