-
Notifications
You must be signed in to change notification settings - Fork 34
/
client.go
114 lines (91 loc) · 3.17 KB
/
client.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
//
// simplecert
//
// Created by Philipp Mieden
// Contact: dreadl0ck@protonmail.ch
// Copyright © 2018 bestbytes. All rights reserved.
//
package simplecert
import (
"fmt"
"log"
"strings"
"github.com/go-acme/lego/v3/challenge/tlsalpn01"
"github.com/go-acme/lego/v3/certcrypto"
"github.com/go-acme/lego/v3/challenge/http01"
"github.com/go-acme/lego/v3/lego"
"github.com/go-acme/lego/v3/providers/dns"
"github.com/go-acme/lego/v3/registration"
)
/*
* ACMEClient
*/
func createClient(u SSLUser) (lego.Client, error) {
// create lego config
config := lego.NewConfig(&u)
config.CADirURL = c.DirectoryURL
config.Certificate.KeyType = certcrypto.RSA4096
// Create a new client instance
client, err := lego.NewClient(config)
if err != nil {
return lego.Client{}, fmt.Errorf("simplecert: failed to create client: %s", err)
}
log.Println("[INFO] simplecert: client creation complete")
// -------------------------------------------
// DNS Challenge
// -------------------------------------------
if c.DNSProvider != "" {
p, err := dns.NewDNSChallengeProviderByName(c.DNSProvider)
if err != nil {
return *client, fmt.Errorf("simplecert: setting DNS provider specified in config: %s", err)
}
err = client.Challenge.SetDNS01Provider(p)
if err != nil {
return *client, fmt.Errorf("simplecert: setting DNS challenge provider failed: %s", err)
}
log.Println("[INFO] simplecert: set DNS challenge")
}
// -------------------------------------------
// HTTP Challenges
// -------------------------------------------
if c.HTTPAddress != "" {
httpSlice := strings.Split(c.HTTPAddress, ":")
if len(httpSlice) != 2 {
return *client, fmt.Errorf("simplecert: invalid HTTP address: %s", c.HTTPAddress)
}
err = client.Challenge.SetHTTP01Provider(http01.NewProviderServer(httpSlice[0], httpSlice[1]))
if err != nil {
return *client, fmt.Errorf("simplecert: setting HTTP challenge provider failed: %s", err)
}
log.Println("[INFO] simplecert: set HTTP challenge")
}
// -------------------------------------------
// TLS Challenges
// -------------------------------------------
if c.TLSAddress != "" {
tlsSlice := strings.Split(c.TLSAddress, ":")
if len(tlsSlice) != 2 {
return *client, fmt.Errorf("simplecert: invalid TLS address: %s", c.TLSAddress)
}
err = client.Challenge.SetTLSALPN01Provider(tlsalpn01.NewProviderServer(tlsSlice[0], tlsSlice[1]))
if err != nil {
return *client, fmt.Errorf("simplecert: setting TLS challenge provider failed: %s", err)
}
log.Println("[INFO] simplecert: set TLS challenge")
}
if c.DNSProvider == "" && c.TLSAddress == "" && c.HTTPAddress == "" {
return *client, fmt.Errorf("simplecert: you must specify at least one of the challenge types: dns, http or tls")
}
// register if necessary
if u.Registration == nil {
// Register Client and agree to TOS
reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
if err != nil {
return *client, fmt.Errorf("simplecert: failed to register client: %s", err)
}
u.Registration = reg
log.Println("[INFO] simplecert: client registration complete: ", client)
saveUserToDisk(u, c.CacheDir)
}
return *client, nil
}