summaryrefslogtreecommitdiff
path: root/web/config.go
blob: 2479e38a455312b05f944245fc67dd84451bbded (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
package web

import (
	"encoding/json"
	"io/ioutil"
	"strings"

	"code.crute.me/mcrute/go_ddns_manager/bind"
	"code.crute.me/mcrute/go_ddns_manager/dns"
)

type ServerConfig struct {
	BindConfig     *bind.BINDConfig
	DNSClient      *dns.DNSClient
	AcmeView       string
	DynamicDnsView string
	DDNSSecrets    map[string]string         `json:"DDNS"`
	AcmeSecrets    map[string]map[string]int `json:"ACME"`
}

func LoadServerConfig(zonesFile, secretsFile, server, view string) (*ServerConfig, error) {
	scfg := &ServerConfig{
		DNSClient:      &dns.DNSClient{Server: server},
		AcmeView:       view,
		DynamicDnsView: view,
	}

	cfg, err := bind.ParseBINDConfig(zonesFile)
	if err != nil {
		return nil, err
	}
	scfg.BindConfig = cfg

	fd, err := ioutil.ReadFile(secretsFile)
	if err != nil {
		return nil, err
	}

	if err = json.Unmarshal(fd, scfg); err != nil {
		return nil, err
	}

	return scfg, nil
}

func (s *ServerConfig) GetDDNSZoneName(k string) string {
	v, _ := s.DDNSSecrets[k]
	return v
}

func (s *ServerConfig) AcmeSecretExists(k string) bool {
	_, ok := s.AcmeSecrets[k]
	return ok
}

func (s *ServerConfig) IsAcmeClientAllowed(key, zone string) bool {
	u, ok := s.AcmeSecrets[key]
	if !ok {
		return false
	}

	p, ok := u[zone]
	if ok && p == 1 {
		return true
	}

	p, ok = u[strings.TrimRight(zone, ".")]
	if ok && p == 1 {
		return true
	}

	return false
}