-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
147 lines (114 loc) · 3.67 KB
/
main.go
File metadata and controls
147 lines (114 loc) · 3.67 KB
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
package main
import (
"context"
"github.com/jessevdk/go-flags"
log "github.com/sirupsen/logrus"
client "gitlab.inf.ethz.ch/PRV-PERRIG/netsec-course/project-acme/netsec-2020-acme/rferreira-acme-project/ACME-client"
"gitlab.inf.ethz.ch/PRV-PERRIG/netsec-course/project-acme/netsec-2020-acme/rferreira-acme-project/dnsServer"
"net/http"
"os"
"sync"
)
var opts struct {
ChallengeMode string `required:"true" choice:"dns01" choice:"http01" long:"cmd" description:"Indicates which ACME challenge type the client should perform. Valid options are dns01 and http01 for the dnsServer-01 and http-01 challenges, respectively."`
DirURL string `required:"true" long:"dir" description:"Directory URL of the ACME server"`
IPv4Addr string `required:"true" long:"record" description:"IPv4 address which must be returned by your DNS server for all A-record queries"`
Domain []string `required:"true" long:"domain" description:"Domain for which to request the certificate. If multiple --domain flags are present, a single certificate for multiple domains should be requested. Wildcard domains have no special flag and are simply denoted by, e.g., *.example.net."`
Revoke bool `long:"revoke" description:"If present, your application should immediately revoke the certificate after obtaining it. In both cases, your application should start its HTTPS server and set it up to use the newly obtained certificate."`
CACert string `long:"cert" description:"Path to the certificate of the CA"`
}
var wg = &sync.WaitGroup{}
func init() {
_, err := flags.Parse(&opts)
if err != nil {
log.Fatal(err)
}
}
func main() {
log.WithFields(log.Fields{
"Challenge mode": opts.ChallengeMode,
"Dir URL": opts.DirURL,
"IPv4 address": opts.IPv4Addr,
"Domains": opts.Domain,
"Revoke":opts.Revoke,
}).Info("Starting ACME protocol")
if opts.CACert == ""{
opts.CACert = "pebble.minica.pem"
}
cli := client.NewClient(opts.CACert)
cli.IPv4 = opts.IPv4Addr
wg.Add(1)
var dnsserver *dnsServer.DNSServer
var httpsServer *http.Server
stop := false
go func() {
defer wg.Done()
killSwitch := make(chan bool)
killServer := client.GetKillSwitch(cli, killSwitch)
go func() {
if err := killServer.ListenAndServe(); err != nil {
log.Error("Failed to set udp listener %s\n", err.Error())
}
}()
for kill := range killSwitch {
if kill {
if dnsserver != nil {
dnsserver.Shutdown()
}
if httpsServer != nil {
httpsServer.Shutdown(context.TODO())
}
if killServer != nil {
killServer.Shutdown(context.TODO())
}
os.Exit(1)
}
}
}()
dnsserver = dnsServer.InitDNSServer(opts.IPv4Addr)
wg.Add(1)
go func() {
defer wg.Done()
if err := dnsserver.ListenAndServe(); err != nil {
log.Error("Failed to set udp listener %s\n", err.Error())
}
}()
if err := cli.GetDirectory(opts.DirURL); err != nil || stop {
panic(err)
}
if err := cli.GetNewNonce(); err != nil || stop {
panic(err)
}
if err := cli.GetAccount(); err != nil || stop {
panic(err)
}
if err := cli.GetNewOrder(opts.Domain); err != nil || stop {
panic(err)
}
if err := cli.HandleAuthorizations(opts.ChallengeMode, dnsserver); err != nil || stop {
panic(err)
}
if err := cli.Finalize(); err != nil || stop {
panic(err)
}
if err := cli.DownloadCert(); err != nil || stop {
panic(err)
}
httpsServer, err := client.GetHTTPS(cli)
if err != nil {
panic(err)
}
wg.Add(1)
go func() {
defer wg.Done()
if err := httpsServer.ListenAndServeTLS("", ""); err != nil {
log.Error("Failed to set udp listener %s\n", err.Error())
}
}()
if opts.Revoke {
if err := cli.Revoke(); err != nil || stop {
panic(err)
}
}
wg.Wait()
}