-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
195 lines (159 loc) · 4.68 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
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/spf13/pflag"
"github.com/yarlson/zero/certificates"
"github.com/yarlson/zero/zerossl"
)
const (
defaultCertDir = "./certs"
)
type Config struct {
Domain string
Email string
CertDir string
Issue bool
Renew bool
Cron bool
Time string
}
func parseFlags() (*Config, error) {
cfg := &Config{}
pflag.StringVarP(&cfg.Domain, "domain", "d", "", "Domain name for the certificate")
pflag.StringVarP(&cfg.Email, "email", "e", "", "Email address for account registration")
pflag.StringVarP(&cfg.CertDir, "cert-dir", "c", defaultCertDir, "Directory to store certificates")
pflag.BoolVarP(&cfg.Issue, "issue", "i", false, "Issue a new certificate")
pflag.BoolVarP(&cfg.Renew, "renew", "r", false, "Renew the existing certificate")
pflag.BoolVar(&cfg.Cron, "cron", false, "Run in cron mode for daily renewals")
pflag.StringVar(&cfg.Time, "time", "02:00", "Time for daily renewal in HH:mm format (24-hour or 12-hour with AM/PM)")
pflag.Usage = func() {
_, _ = fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
_, _ = fmt.Fprintf(os.Stderr, " %s -d example.com -e [email protected] [-c /path/to/certs] [-i] [-r] [--cron] [--time HH:mm]\n\n", os.Args[0])
_, _ = fmt.Fprintf(os.Stderr, "Options:\n")
pflag.PrintDefaults()
}
pflag.Parse()
if cfg.Domain == "" || cfg.Email == "" {
return nil, errors.New("domain and email are required")
}
if cfg.Issue && cfg.Renew {
return nil, errors.New("cannot specify both --issue and --renew")
}
if cfg.Cron {
if _, err := parseTime(cfg.Time); err != nil {
return nil, fmt.Errorf("invalid time format: %w", err)
}
}
return cfg, nil
}
func parseTime(timeStr string) (time.Time, error) {
formats := []string{
"15:04",
"3:04PM",
"3:04 PM",
}
for _, format := range formats {
t, err := time.Parse(format, timeStr)
if err == nil {
return t, nil
}
}
return time.Time{}, fmt.Errorf("unable to parse time: %s", timeStr)
}
func run(cfg *Config) error {
if err := os.MkdirAll(cfg.CertDir, 0700); err != nil {
return fmt.Errorf("create cert directory: %w", err)
}
certFile := filepath.Join(cfg.CertDir, cfg.Domain+".crt")
keyFile := filepath.Join(cfg.CertDir, cfg.Domain+".key")
action := "auto"
if cfg.Issue {
action = "issue"
} else if cfg.Renew {
action = "renew"
}
zeroSSLService := zerossl.New()
certService := certificates.New(zeroSSLService)
cert, err := certService.LoadCertificate(certFile)
if err != nil && action != "issue" {
log.Printf("Load existing certificate: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigChan
log.Println("Received interrupt signal. Shutting down...")
cancel()
}()
if certService.ShouldObtainCertificate(action, cert) {
log.Printf("Obtaining certificate for %s", cfg.Domain)
if err := certService.ObtainOrRenewCertificate(ctx, cfg.Domain, cfg.Email, certFile, keyFile); err != nil {
if errors.Is(err, context.Canceled) {
return errors.New("operation canceled")
}
return fmt.Errorf("failed to obtain/renew certificate: %w", err)
}
} else if cert == nil {
log.Println("No existing certificate found.")
} else {
log.Printf("Certificate is valid until %s. No action needed.", cert.NotAfter.Format(time.RFC3339))
}
return nil
}
func runCron(cfg *Config) error {
renewalTime, err := parseTime(cfg.Time)
if err != nil {
return fmt.Errorf("parse renewal time: %w", err)
}
log.Printf("Starting cron mode. Daily renewal scheduled at %s", renewalTime.Format("15:04"))
ticker := time.NewTicker(getNextTickDuration(renewalTime))
defer ticker.Stop()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case <-ticker.C:
log.Println("Running scheduled renewal")
if err := run(cfg); err != nil {
log.Printf("Error during scheduled renewal: %v", err)
}
ticker.Reset(24 * time.Hour)
case <-sigChan:
log.Println("Received interrupt signal. Shutting down...")
return nil
}
}
}
func getNextTickDuration(t time.Time) time.Duration {
now := time.Now()
next := time.Date(now.Year(), now.Month(), now.Day(), t.Hour(), t.Minute(), 0, 0, now.Location())
if next.Before(now) {
next = next.Add(24 * time.Hour)
}
return next.Sub(now)
}
func main() {
cfg, err := parseFlags()
if err != nil {
log.Fatalf("Error parsing flags: %v", err)
}
if cfg.Cron {
if err := runCron(cfg); err != nil {
log.Fatalf("Error in cron mode: %v", err)
}
return
}
if err := run(cfg); err != nil {
log.Fatalf("Error: %v", err)
}
}