@@ -2,20 +2,19 @@ package main
22
33import (
44 "errors"
5- "io/ioutil"
65 "log"
76 "net"
8- _ "net/http/pprof"
97 "os"
8+ "time"
109
1110 "github.com/txthinking/mad"
1211 "github.com/urfave/cli/v2"
1312)
1413
1514func main () {
1615 app := cli .NewApp ()
17- app .Name = "Mad "
18- app .Version = "20240428 "
16+ app .Name = "mad "
17+ app .Version = "20240923 "
1918 app .Usage = "Generate root CA and derivative certificate for any domains and any IPs"
2019 app .Authors = []* cli.Author {
2120 {
@@ -36,7 +35,7 @@ func main() {
3635 & cli.StringFlag {
3736 Name : "key" ,
3837 Usage : "Key file which will be created or overwritten" ,
39- Value : "ca_key .pem" ,
38+ Value : "ca.key .pem" ,
4039 },
4140 & cli.StringFlag {
4241 Name : "organization" ,
@@ -50,13 +49,36 @@ func main() {
5049 Name : "commonName" ,
5150 Value : "github.com/txthinking/mad" ,
5251 },
52+ & cli.StringFlag {
53+ Name : "start" ,
54+ Usage : "Certificate valid start time, such as: '2024-09-22T13:07:38+08:00'. If empty, it is the current time" ,
55+ },
56+ & cli.StringFlag {
57+ Name : "end" ,
58+ Usage : "Certificate valid end time, such as: '2024-09-22T13:07:38+08:00'. If empty, it is start time add 10 years" ,
59+ },
5360 & cli.BoolFlag {
5461 Name : "install" ,
55- Usage : "Install CA " ,
62+ Usage : "Install immediately after creation " ,
5663 },
5764 },
5865 Action : func (c * cli.Context ) error {
59- ca := mad .NewCa (c .String ("organization" ), c .String ("organizationUnit" ), c .String ("commonName" ))
66+ var err error
67+ start := time .Now ()
68+ if c .String ("start" ) != "" {
69+ start , err = time .Parse (time .RFC3339 , c .String ("start" ))
70+ if err != nil {
71+ return err
72+ }
73+ }
74+ end := start .AddDate (10 , 0 , 0 )
75+ if c .String ("end" ) != "" {
76+ end , err = time .Parse (time .RFC3339 , c .String ("end" ))
77+ if err != nil {
78+ return err
79+ }
80+ }
81+ ca := mad .NewCa (c .String ("organization" ), c .String ("organizationUnit" ), c .String ("commonName" ), start , end )
6082 if err := ca .Create (); err != nil {
6183 return err
6284 }
@@ -82,8 +104,12 @@ func main() {
82104 },
83105 & cli.StringFlag {
84106 Name : "ca_key" ,
107+ Usage : "Deprecated, please use --caKey" ,
108+ },
109+ & cli.StringFlag {
110+ Name : "caKey" ,
85111 Usage : "ROOT Key file path" ,
86- Value : "ca_key .pem" ,
112+ Value : "ca.key .pem" ,
87113 },
88114 & cli.StringFlag {
89115 Name : "cert" ,
@@ -93,7 +119,7 @@ func main() {
93119 & cli.StringFlag {
94120 Name : "key" ,
95121 Usage : "Certificate key file which will be created or overwritten" ,
96- Value : "cert_key .pem" ,
122+ Value : "cert.key .pem" ,
97123 },
98124 & cli.StringFlag {
99125 Name : "organization" ,
@@ -111,17 +137,51 @@ func main() {
111137 Name : "domain" ,
112138 Usage : "Domain name" ,
113139 },
140+ & cli.StringFlag {
141+ Name : "commonName" ,
142+ Usage : "If empty, the first domain or IP will be used" ,
143+ },
144+ & cli.StringFlag {
145+ Name : "start" ,
146+ Usage : "Certificate valid start time, such as: '2024-09-22T13:07:38+08:00'. If empty, it is the current time" ,
147+ },
148+ & cli.StringFlag {
149+ Name : "end" ,
150+ Usage : "Certificate valid end time, such as: '2024-09-22T13:07:38+08:00'. If empty, it is start time add 10 years" ,
151+ },
114152 },
115153 Action : func (c * cli.Context ) error {
116- ca , err := ioutil .ReadFile (c .String ("ca" ))
154+ ca , err := os .ReadFile (c .String ("ca" ))
117155 if err != nil {
118156 return err
119157 }
120- caKey , err := ioutil .ReadFile (c .String ("ca_key" ))
121- if err != nil {
122- return err
158+ var caKey []byte
159+ if c .String ("ca_key" ) != "" {
160+ caKey , err = os .ReadFile (c .String ("ca_key" ))
161+ if err != nil {
162+ return err
163+ }
164+ } else {
165+ caKey , err = os .ReadFile (c .String ("caKey" ))
166+ if err != nil {
167+ return err
168+ }
123169 }
124- cert := mad .NewCert (ca , caKey , c .String ("organization" ), c .String ("organizationUnit" ))
170+ start := time .Now ()
171+ if c .String ("start" ) != "" {
172+ start , err = time .Parse (time .RFC3339 , c .String ("start" ))
173+ if err != nil {
174+ return err
175+ }
176+ }
177+ end := start .AddDate (10 , 0 , 0 )
178+ if c .String ("end" ) != "" {
179+ end , err = time .Parse (time .RFC3339 , c .String ("end" ))
180+ if err != nil {
181+ return err
182+ }
183+ }
184+ cert := mad .NewCert (ca , caKey , c .String ("organization" ), c .String ("organizationUnit" ), start , end )
125185 ips := make ([]net.IP , 0 )
126186 for _ , v := range c .StringSlice ("ip" ) {
127187 ip := net .ParseIP (v )
@@ -130,8 +190,15 @@ func main() {
130190 }
131191 ips = append (ips , ip )
132192 }
133- cert .SetIPAddresses (ips )
134- cert .SetDNSNames (c .StringSlice ("domain" ))
193+ if len (ips ) > 0 {
194+ cert .SetIPAddresses (ips )
195+ }
196+ if len (c .StringSlice ("domain" )) > 0 {
197+ cert .SetDNSNames (c .StringSlice ("domain" ))
198+ }
199+ if c .String ("commonName" ) != "" {
200+ cert .SetCommonName (c .String ("commonName" ))
201+ }
135202 if err := cert .Create (); err != nil {
136203 return err
137204 }
0 commit comments