Skip to content

Commit b769fc5

Browse files
committed
Add acme letsencrypt for valid certs
1 parent e14b5a9 commit b769fc5

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

src/cmd/web.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,15 @@ var webCmd = &cobra.Command{
9999

100100
if TLS && (tlsKey == "" || tlsCertificate == "") {
101101
var err error
102-
cert, err = utils.GenerateTLSCertificate(commonName)
103-
if err != nil {
104-
return errors.New("Error while generating certificate: " + err.Error())
102+
if commonName == "" {
103+
cert, err = utils.GenerateTLSSelfSignedCertificate(commonName)
104+
if err != nil {
105+
return errors.New("Error while generating certificate: " + err.Error())
106+
}
107+
TLSConfig.Certificates = append(TLSConfig.Certificates, *cert)
108+
} else {
109+
TLSConfig, err = utils.GenerateTLSLetsencryptCertificate(commonName)
105110
}
106-
TLSConfig.Certificates = append(TLSConfig.Certificates, *cert)
107111
} else if (TLS || tlsKey != "" || tlsCertificate != "") && (!TLS || tlsKey == "" || tlsCertificate == "") {
108112
return errors.New("Tls, certificate and/or key arguments missing")
109113

@@ -189,7 +193,7 @@ func init() {
189193
webCmd.Flags().BoolVar(&promptPassword, "promptPassword", false, "Prompt for for basic auth's password")
190194

191195
webCmd.Flags().BoolVar(&TLS, "tls", false, "Enables HTTPS (for web and webdav)")
192-
webCmd.Flags().StringVarP(&commonName, "commonName", "n","", "Common name to use in the certificat")
196+
webCmd.Flags().StringVarP(&commonName, "commonName", "n", "", "Common name to use in the certificat")
193197
webCmd.Flags().StringVarP(&tlsCertificate, "certificate", "c", "", "HTTPS certificate : openssl req -new -x509 -sha256 -key server.key -out server.crt -days 365 (for web and webdav)")
194198
webCmd.Flags().StringVarP(&tlsKey, "key", "k", "", "HTTPS Key : openssl genrsa -out server.key 2048 (for web and webdav)")
195199

src/utils/certs.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ import (
66
"crypto/tls"
77
"crypto/x509"
88
"crypto/x509/pkix"
9+
"fmt"
10+
"log"
911
"math/big"
12+
"net/http"
1013
"time"
14+
15+
"golang.org/x/crypto/acme/autocert"
1116
)
1217

1318
// Credits: https://github.com/kgretzky/pwndrop/blob/master/core/gen_cert.go
14-
func GenerateTLSCertificate(common string) (*tls.Certificate, error) {
19+
func GenerateTLSSelfSignedCertificate(common string) (*tls.Certificate, error) {
1520
private_key, err := rsa.GenerateKey(rand.Reader, 2048)
1621
if err != nil {
1722
return nil, err
@@ -59,6 +64,31 @@ func GenerateTLSCertificate(common string) (*tls.Certificate, error) {
5964
return ret_tls, nil
6065
}
6166

67+
func GenerateTLSLetsencryptCertificate(common string) (tls.Config, error) {
68+
fmt.Printf("Generating certificate for %s\n", common)
69+
certManager := autocert.Manager{
70+
Prompt: autocert.AcceptTOS,
71+
HostPolicy: autocert.HostWhitelist(common), //Your domain here
72+
Cache: autocert.DirCache("certs"), //Folder for storing certificates
73+
}
74+
ret_tls := tls.Config{
75+
GetCertificate: certManager.GetCertificate,
76+
}
77+
go func() {
78+
srv := &http.Server{
79+
Addr: ":80",
80+
Handler: certManager.HTTPHandler(nil),
81+
IdleTimeout: time.Minute,
82+
ReadTimeout: 5 * time.Second,
83+
WriteTimeout: 10 * time.Second,
84+
}
85+
86+
err := srv.ListenAndServe()
87+
log.Fatal(err)
88+
}()
89+
return ret_tls, nil
90+
}
91+
6292
func genRandomString(n int) string {
6393
const lb = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
6494
b := make([]byte, n)

0 commit comments

Comments
 (0)