2021-04-07 11:29:21 +02:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2021-05-02 11:40:45 +02:00
|
|
|
"brainbaking.com/go-jamming/rest"
|
2021-04-11 11:08:27 +02:00
|
|
|
"encoding/json"
|
2021-04-09 18:00:40 +02:00
|
|
|
"errors"
|
2021-04-18 15:39:29 +02:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-04-18 10:18:00 +02:00
|
|
|
"io/fs"
|
2021-04-11 11:08:27 +02:00
|
|
|
"io/ioutil"
|
2021-04-08 12:16:33 +02:00
|
|
|
"strings"
|
2021-04-07 11:29:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2022-04-23 21:27:43 +02:00
|
|
|
// BaseURL should end with a / and is used to build URLs in notifications
|
|
|
|
BaseURL string `json:"baseURL"`
|
2022-04-24 16:25:00 +02:00
|
|
|
AdminEmail string `json:"adminEmail"`
|
2021-05-02 11:40:45 +02:00
|
|
|
Port int `json:"port"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
AllowedWebmentionSources []string `json:"allowedWebmentionSources"`
|
|
|
|
Blacklist []string `json:"blacklist"`
|
2022-04-23 11:35:53 +02:00
|
|
|
Whitelist []string `json:"whitelist"`
|
2021-05-02 11:40:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) IsBlacklisted(url string) bool {
|
2022-04-23 11:35:53 +02:00
|
|
|
return isListedIn(url, c.Blacklist)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) IsWhitelisted(url string) bool {
|
|
|
|
return isListedIn(url, c.Whitelist)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isListedIn(url string, list []string) bool {
|
2021-05-02 11:40:45 +02:00
|
|
|
if !strings.HasPrefix(url, "http") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
domain := rest.Domain(url)
|
2022-04-23 11:35:53 +02:00
|
|
|
return Includes(list, domain)
|
2021-04-11 11:08:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) missingKeys() []string {
|
|
|
|
keys := []string{}
|
|
|
|
if c.Port == 0 {
|
|
|
|
keys = append(keys, "port")
|
|
|
|
}
|
|
|
|
if c.Token == "" {
|
|
|
|
keys = append(keys, "token")
|
|
|
|
}
|
2022-04-23 21:27:43 +02:00
|
|
|
if c.BaseURL == "" {
|
|
|
|
keys = append(keys, "baseURL")
|
|
|
|
}
|
2021-04-11 11:08:27 +02:00
|
|
|
if len(c.AllowedWebmentionSources) == 0 {
|
|
|
|
keys = append(keys, "allowedWebmentionSources")
|
|
|
|
}
|
|
|
|
return keys
|
2021-04-07 11:29:21 +02:00
|
|
|
}
|
|
|
|
|
2021-05-02 11:40:45 +02:00
|
|
|
func (c *Config) IsAnAllowedDomain(domain string) bool {
|
|
|
|
return Includes(c.AllowedWebmentionSources, domain)
|
2021-04-09 18:00:40 +02:00
|
|
|
}
|
|
|
|
|
2021-04-08 12:16:33 +02:00
|
|
|
func (c *Config) FetchDomain(url string) (string, error) {
|
|
|
|
for _, domain := range c.AllowedWebmentionSources {
|
2021-04-08 16:37:04 +02:00
|
|
|
if strings.Contains(url, domain) {
|
2021-04-08 12:16:33 +02:00
|
|
|
return domain, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", errors.New("no allowed domain found for url " + url)
|
|
|
|
}
|
|
|
|
|
2021-04-16 16:17:09 +02:00
|
|
|
func Configure() *Config {
|
2021-04-19 20:22:38 +02:00
|
|
|
c := config()
|
|
|
|
for _, domain := range c.AllowedWebmentionSources {
|
2021-04-08 12:16:33 +02:00
|
|
|
log.Info().Str("allowedDomain", domain).Msg("Configured")
|
|
|
|
}
|
2021-04-19 20:22:38 +02:00
|
|
|
return c
|
2021-04-08 12:16:33 +02:00
|
|
|
}
|
|
|
|
|
2022-04-23 11:35:53 +02:00
|
|
|
// AddToBlacklist adds the given domain to the blacklist slice and persists to disk.
|
2022-04-18 10:18:00 +02:00
|
|
|
func (c *Config) AddToBlacklist(domain string) {
|
2022-04-23 11:35:53 +02:00
|
|
|
c.Blacklist = addToList(domain, c.Blacklist)
|
|
|
|
c.Save()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddToWhitelist adds the given domain to the whitelist slice and persists to disk.
|
|
|
|
func (c *Config) AddToWhitelist(domain string) {
|
|
|
|
c.Whitelist = addToList(domain, c.Whitelist)
|
|
|
|
c.Save()
|
|
|
|
}
|
|
|
|
|
|
|
|
func addToList(key string, arr []string) []string {
|
|
|
|
for _, d := range arr {
|
|
|
|
if d == key {
|
|
|
|
return arr
|
2022-04-18 10:18:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 11:35:53 +02:00
|
|
|
return append(arr, key)
|
2022-04-18 10:18:00 +02:00
|
|
|
}
|
|
|
|
|
2022-04-24 13:27:42 +02:00
|
|
|
func (c *Config) String() string {
|
|
|
|
bytes, _ := json.MarshalIndent(c, "", " ")
|
|
|
|
return string(bytes)
|
|
|
|
}
|
|
|
|
|
2022-04-18 10:18:00 +02:00
|
|
|
func (c *Config) Save() {
|
2022-04-24 13:27:42 +02:00
|
|
|
err := ioutil.WriteFile("config.json", []byte(c.String()), fs.ModePerm)
|
2022-04-18 10:18:00 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("Unable to save config.json to disk!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-16 16:17:09 +02:00
|
|
|
func config() *Config {
|
2021-04-11 11:08:27 +02:00
|
|
|
confData, err := ioutil.ReadFile("config.json")
|
2021-04-09 21:00:54 +02:00
|
|
|
if err != nil {
|
2021-04-11 11:08:27 +02:00
|
|
|
log.Warn().Msg("No config.json file found, reverting to defaults...")
|
|
|
|
return defaultConfig()
|
2021-04-07 11:29:21 +02:00
|
|
|
}
|
2021-04-11 11:08:27 +02:00
|
|
|
|
|
|
|
conf := &Config{}
|
|
|
|
err = json.Unmarshal(confData, conf)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Msg("config.json malformed JSON, reverting to defaults...")
|
|
|
|
return defaultConfig()
|
2021-04-07 11:29:21 +02:00
|
|
|
}
|
2021-04-11 11:08:27 +02:00
|
|
|
someMissingKeys := conf.missingKeys()
|
|
|
|
if len(someMissingKeys) > 0 {
|
|
|
|
log.Warn().Str("keys", strings.Join(someMissingKeys, ", ")).Msg("config.json is missing required keys, reverting to defaults...")
|
|
|
|
return defaultConfig()
|
|
|
|
}
|
|
|
|
return conf
|
|
|
|
}
|
2021-04-07 11:29:21 +02:00
|
|
|
|
2021-04-11 11:08:27 +02:00
|
|
|
func defaultConfig() *Config {
|
2022-04-23 21:27:43 +02:00
|
|
|
defaultConfig := &Config{
|
2022-04-29 08:36:36 +02:00
|
|
|
AdminEmail: "admin@mycooldomain.com",
|
|
|
|
BaseURL: "http://localhost:1337/",
|
2021-05-02 11:40:45 +02:00
|
|
|
Port: 1337,
|
|
|
|
Token: "miauwkes",
|
2022-04-29 08:36:36 +02:00
|
|
|
AllowedWebmentionSources: []string{"mycooldomain.com", "myotherdomain.com"},
|
2021-05-02 11:40:45 +02:00
|
|
|
Blacklist: []string{"youtube.com"},
|
2022-04-29 08:36:36 +02:00
|
|
|
Whitelist: []string{"mycooldomain.com"},
|
2021-04-07 11:29:21 +02:00
|
|
|
}
|
2022-04-23 21:27:43 +02:00
|
|
|
defaultConfig.Save()
|
|
|
|
return defaultConfig
|
2021-04-07 11:29:21 +02:00
|
|
|
}
|