1
0
Fork 0
mirror of https://github.com/Luzifer/named-blacklist.git synced 2024-11-09 16:00:09 +00:00
named-blacklist/config.go

99 lines
2.1 KiB
Go
Raw Normal View History

2019-08-25 18:38:08 +00:00
package main
import (
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"text/template"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
korvike "github.com/Luzifer/korvike/functions"
2019-08-25 18:38:08 +00:00
)
const defaultTemplate = `$TTL 1H
@ SOA LOCALHOST. dns-master.localhost. (1 1h 15m 30d 2h)
NS LOCALHOST.
; Blacklist entries
{{ range .blacklist -}}
{{ to_punycode .Domain }} CNAME . ; {{ .Comment }}
{{ end }}`
type providerType string
type providerAction string
const (
providerActionBlacklist providerAction = "blacklist"
providerActionWhitelist providerAction = "whitelist"
)
type configfile struct {
Providers []providerDefinition `yaml:"providers"`
Template string `yaml:"template"`
tpl *template.Template
}
type providerDefinition struct {
Action providerAction `yaml:"action"`
Content string `yaml:"content"`
File string `yaml:"file"`
Name string `yaml:"name"`
Type providerType `yaml:"type"`
URL string `yaml:"url"`
}
func (p providerDefinition) GetContent() (io.ReadCloser, error) {
switch {
case p.Content != "":
return ioutil.NopCloser(strings.NewReader(p.Content)), nil
case p.File != "":
return os.Open(p.File)
case p.URL != "":
2019-08-25 18:41:25 +00:00
resp, err := http.Get(p.URL) //nolint:bodyclose // This does not need to be closed here and would break stuff
2019-08-25 18:38:08 +00:00
return resp.Body, err
default:
return nil, errors.New("Neither file nor URL specified")
}
}
func loadConfigFile(filename string) (*configfile, error) {
if _, err := os.Stat(filename); err != nil {
return nil, errors.Wrap(err, "Unable to access given file")
}
f, err := os.Open(filename)
if err != nil {
return nil, errors.Wrap(err, "Unable to open given file")
}
defer f.Close()
out := &configfile{Template: defaultTemplate}
if err = yaml.NewDecoder(f).Decode(out); err != nil {
return nil, errors.Wrap(err, "Unable to parse given file")
}
funcs := korvike.GetFunctionMap()
funcs["to_punycode"] = domainToPunycode
if out.tpl, err = template.
New("configTemplate").
Funcs(funcs).
Parse(out.Template); err != nil {
2019-08-25 18:38:08 +00:00
return nil, errors.Wrap(err, "Unable to parse given template")
}
return out, nil
}