ipbl/src/models/cfg.go
2022-03-13 12:08:44 +01:00

141 lines
3.3 KiB
Go

package models
import (
"encoding/json"
"fmt"
"regexp"
"strings"
"git.paulbsd.com/paulbsd/ipbl/src/config"
)
//var ipv4_regex = `^(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4})/`
var ipv4_cidr_regex = `^(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|)){4}\/([1-3])?([0-9])?$)`
func GetTrustlists(cfg config.Config) (res []string, err error) {
var w = Cfg{Key: "trustlist"}
if exists, _ := cfg.Db.Get(&w); exists {
res = strings.Split(w.Value, ",")
}
return
}
func (wl Trustlist) InsertOrUpdate(cfg config.Config) (err error) {
var w = Cfg{Key: "trustlist"}
exists, _ := cfg.Db.Get(&w)
if exists {
existing, _ := GetTrustlists(cfg)
for _, j := range existing {
if j == wl.IP {
return fmt.Errorf("ip %s already in config", j)
}
}
existing = append(existing, wl.IP)
w.Value = strings.Join(existing, ",")
cfg.Db.ID(w.ID).Update(&w)
}
return fmt.Errorf("no trustlist updated")
}
func (wl Trustlist) Delete(cfg config.Config, ip string) (err error) {
var w = Cfg{Key: "trustlist"}
exists, _ := cfg.Db.Get(&w)
var updated []string
if exists {
existing, _ := GetTrustlists(cfg)
for _, sip := range existing {
if sip != ip {
updated = append(updated, sip)
}
}
w.Value = strings.Join(updated, ",")
cfg.Db.ID(w.ID).Update(&w)
}
return fmt.Errorf("no trustlist updated")
}
func (wl Trustlist) Verify() bool {
reg := regexp.MustCompile(ipv4_cidr_regex)
return reg.MatchString(wl.IP)
}
func GetFolders(cfg config.Config) (res []Folder, err error) {
var w = Cfg{Key: "folders"}
if exists, _ := cfg.Db.Get(&w); exists {
err = json.Unmarshal([]byte(w.Value), &res)
}
return
}
func InsertOrUpdateFolders(cfg config.Config, folders []Folder) (res string, err error) {
var w = Cfg{Key: "folders"}
if exists, _ := cfg.Db.Get(&w); exists {
resbytes, err := json.Marshal(folders)
if err != nil {
return "", err
}
w.Value = string(resbytes)
_, err = cfg.Db.ID(w.ID).Update(&w)
if err != nil {
return "", err
}
}
return
}
func GetZMQ(cfg config.Config, key string) (res ZMQ, err error) {
var w = Cfg{Key: fmt.Sprintf("zmq%s", key)}
if exists, _ := cfg.Db.Get(&w); exists {
err = json.Unmarshal([]byte(w.Value), &res)
}
return
}
func DiscoverURLS(cfg config.Config) (Discovery, error) {
var disc Discovery
var urls []Url
urls = append(urls, Url{Key: "auth", Path: "/auth"})
urls = append(urls, Url{Key: "folders", Path: "/config/folders"})
urls = append(urls, Url{Key: "trustlist", Path: "/config/trustlist"})
urls = append(urls, Url{Key: "zmqrr", Path: "/config/zmqrr"})
urls = append(urls, Url{Key: "zmqps", Path: "/config/zmqps"})
disc = Discovery{Version: "1.0", URLs: urls}
return disc, nil
}
type Trustlist struct {
IP string `json:"ip"`
}
type Folder struct {
Path string `json:"path"`
Sets []Set `json:"sets"`
}
type ZMQ struct {
Hostname string `json:"hostname"`
Port int `json:"port"`
}
type Set struct {
Type string `json:"type"`
Filename string `json:"filename"`
Regex string `json:"regex"`
}
type Cfg struct {
ID int `xorm:"pk autoincr" json:"-"`
Key string `xorm:"text notnull unique" json:"key"`
Value string `xorm:"text default" json:"value"`
}
type Discovery struct {
Version string `json:"version"`
URLs []Url `json:"urls"`
}
type Url struct {
Key string `json:"key"`
Path string `json:"path"`
}