-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (80 loc) · 2.12 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
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"fmt"
"log"
"time"
"github.com/go-acme/lego/v4/certificate"
"github.com/go-acme/lego/v4/lego"
"github.com/paraparty/acme-task/acme"
"github.com/paraparty/acme-task/acme/challenge"
"github.com/paraparty/acme-task/configuration"
"github.com/paraparty/acme-task/handler"
"github.com/paraparty/acme-task/model"
)
func main() {
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
log.Fatal(err)
}
config, err := configuration.ReadConfig()
if err != nil {
log.Fatal(err)
}
user := &model.User{
Email: config.Acme.Email,
Key: privateKey,
}
acmeClient, err := acme.NewClient(config, user)
if err != nil {
log.Fatal(err)
}
for _, task := range config.Tasks {
err = doTask(config, acmeClient, &task)
if err != nil {
log.Printf("%v", err)
}
}
}
func doTask(config *model.Config, client *lego.Client, task *model.Task) error {
err := resolveChallenge(client, task)
if err != nil {
return err
}
request := certificate.ObtainRequest{
Domains: task.Domains,
Bundle: true,
}
if config.Acme.Type == "google" {
if config.Acme.ValidityPeriod != "" {
duration, err := time.ParseDuration(config.Acme.ValidityPeriod)
if err == nil {
request.NotAfter = time.Now().Add(duration)
} else {
log.Printf("ValidityPeriod is not acceptable, %v", err)
}
}
}
certificates, err := client.Certificate.Obtain(request)
if err != nil {
return err
}
if task.TaskDetails.Type == "imagex" {
return handler.ImageXHandler(task, certificates)
} else if task.TaskDetails.Type == "file" {
return handler.CertFileHandler(task, certificates)
}
return fmt.Errorf("task type not support")
}
func resolveChallenge(client *lego.Client, task *model.Task) error {
if task.Challenge.Type == "cloudflare" {
return challenge.CloudflareChallenge(client, task)
} else if task.Challenge.Type == "tencent-cloud" {
return challenge.TencentCloudChallenge(client, task)
} else if task.Challenge.Type == "imagex" {
return challenge.ImageXChallenge(client, task)
}
return fmt.Errorf("credential type not support")
}