-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (143 loc) · 3.85 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
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"strings"
"github.com/koding/multiconfig"
)
const certPrefix = "certificates"
// note: according to the chosen dns, corresponding envs need to be set
// run `lego dnshelp` for help on usage
type confs struct {
// lets encrypt configs
Email string
Domains string
DNS string
Renew bool
Path string `default:"~/.lego"`
SkipNewCert bool // if certs have been fetched manually, set it to true
// qiniu configs
CertName string `required:"true"`
QiniuDomain string `required:"true"` // domain on qiniu
ExistCertID string // if qiniu cert has been created, set it to the id to use. this means only updating domain with the cert
}
func main() {
m := multiconfig.New()
conf := new(confs)
m.MustLoad(conf)
domains := getDomains(conf.Domains)
if strings.Contains(conf.Path, "~") {
usr, err := user.Current()
if err != nil {
fmt.Println("fail to normalize path, err:", err)
return
}
conf.Path = strings.Replace(conf.Path, "~", usr.HomeDir, 1)
}
if !conf.SkipNewCert {
if err := generateCerts(conf.Email, conf.Path,
conf.DNS, conf.Renew, domains); err != nil {
fmt.Println("fail to generate certificates, err: ", err)
return
}
fmt.Println("Certificates have been generated successfully!")
}
accessKey := os.Getenv("QINIU_ACCESS_KEY")
secretKey := os.Getenv("QINIU_SECRET_KEY")
client := newQiniuClient(accessKey, secretKey)
certID := conf.ExistCertID
// if certID is empty, then do the upload
if certID == "" {
var err error
certID, err = uploadCert(client, conf.CertName, conf.Path, domains)
if err != nil {
fmt.Println("fail to upload certificates, err: ", err)
return
}
fmt.Println("Certificates have been uploaded successfully!")
}
if err := updateCert(client, conf.QiniuDomain, certID); err != nil {
fmt.Println("fail to update certificates, err: ", err)
return
}
fmt.Println("Certificates have been updated successfully!")
fmt.Println("All done!")
}
func getDomains(confDomains string) []string {
rawDomains := strings.Split(confDomains, ",")
domains := make([]string, 0)
for _, d := range rawDomains {
d = strings.Trim(d, " ")
if d != "" {
domains = append(domains, d)
}
}
return domains
}
func generateCerts(email, path, dns string, renew bool, domains []string) error {
path, err := filepath.Abs(path)
if err != nil {
return err
}
args := []string{
"--email=" + email,
"--dns=" + dns,
"--path=" + path,
"--accept-tos",
"--dns.disable-cp",
}
for _, d := range domains {
args = append(args, "--domains="+d)
}
if renew {
args = append(args, "renew")
} else {
args = append(args, "run")
}
cmd := exec.Command("lego", args...)
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func uploadCert(client *qiniuClient, certName, certPath string,
domains []string) (string, error) {
if len(domains) == 0 {
return "", errors.New("no Domains provided. Don't know which is the cert file")
}
commonName := domains[0]
// change *.xxx.com to _.xxx.com
filePrefix := strings.Replace(commonName, "*", "_", -1)
priKeyFile := path.Join(certPath, certPrefix, filePrefix+".key")
certFile := path.Join(certPath, certPrefix, filePrefix+".crt")
pri, err := ioutil.ReadFile(priKeyFile)
if err != nil {
return "", err
}
ca, err := ioutil.ReadFile(certFile)
if err != nil {
return "", err
}
resp, err := client.uploadCert(certName, commonName, string(pri),
string(ca))
if err != nil {
return "", err
}
return resp.CertID, nil
}
func updateCert(client *qiniuClient, qiniuDomain, certID string) error {
resp, err := client.getDomainInfo(qiniuDomain)
if err != nil {
return err
}
if resp.Protocol == "http" {
return client.domainToHTTPS(qiniuDomain, certID)
}
return client.domainUpdateCert(qiniuDomain, certID)
}