aboutsummaryrefslogtreecommitdiff
path: root/cmd/batch/main.go
blob: 33cfcfc4e680a81e631304ac969a0e2811a78b75 (plain)
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
package main

import (
	"log"
	"path"

	"github.com/dim13/acme"
)

func startSolvers() (acme.Solvers, error) {
	httpSol, err := acme.NewHTTPSolver(*listen)
	if err != nil {
		return nil, err
	}
	tlsSol, err := acme.NewTLSSolver(*listenTLS)
	if err != nil {
		return nil, err
	}
	return acme.NewSolvers(httpSol, tlsSol), nil
}

func main() {
	log.Println("Scan files")
	crt, err := scanFiles(*baseDir)
	if err != nil {
		log.Fatal(err)
	}

	sol, err := startSolvers()
	if err != nil {
		log.Fatal(err)
	}

	for _, c := range crt {
		if c.IsValid(*graceTime) && !*force {
			log.Println("Skip", c)
			continue
		}
		csr, err := acme.NewCSR(c.PrivateKey, c.Leaf.DNSNames)
		if err != nil {
			log.Println(err)
			continue
		}
		_ = csr
	}
	if *email != "" && len(*domains) > 0 {
		key, err := acme.NewKey(*keySize)
		if err != nil {
			log.Fatal(err)
		}

		log.Println("Dial", *provider)
		prov, err := acme.DialProvider(*provider, key)
		if err != nil {
			log.Fatal(err)
		}

		var con acme.Contacts
		m, err := acme.NewMail(*email)
		if err != nil {
			log.Fatal(err)
		}
		con = append(con, m)

		log.Println("Register", con)
		err = prov.Register(con, agree)
		if err != nil {
			log.Fatal(err)
		}

		for _, domain := range *domains {
			log.Println("Authofize", domain)
			err := prov.Authorize(sol, domain)
			if err != nil {
				log.Fatal(err)
			}
		}

		log.Println("Request bundle for", *domains)
		cert, err := prov.Bundle(key, *domains)
		if err != nil {
			log.Fatal(err)
		}

		c := Cert{
			Certificate: cert,
			sigFile:     path.Join(*baseDir, "private", *email+".key"),
			keyFile:     path.Join(*baseDir, "private", (*domains)[0]+".key"),
			crtFile:     path.Join(*baseDir, "certs", (*domains)[0]+".pem"),
		}
		err = c.saveFiles()
		if err != nil {
			log.Fatal(err)
		}
	}
}

func agree(tos string) bool {
	log.Println("Agree to", tos)
	return true
}