219 lines
5.0 KiB
Go
219 lines
5.0 KiB
Go
package models
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"git.paulbsd.com/paulbsd/ipbl/src/config"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
const 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 ctl []CfgTrustlist
|
|
err = cfg.Db.Find(&ctl)
|
|
|
|
if len(ctl) > 0 {
|
|
for _, a := range ctl {
|
|
res = append(res, a.IP)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (tl CfgTrustlist) 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 == tl.IP {
|
|
return fmt.Errorf("ip %s already in config", j)
|
|
}
|
|
}
|
|
existing = append(existing, tl.IP)
|
|
w.Value = strings.Join(existing, ",")
|
|
cfg.Db.ID(w.ID).Update(&w)
|
|
}
|
|
return fmt.Errorf("no trustlist updated")
|
|
}
|
|
|
|
func (tl CfgTrustlist) Delete(cfg config.Config, ip string) (affected int64, err error) {
|
|
var w = CfgTrustlist{IP: ip}
|
|
exists, _ := cfg.Db.Get(&w)
|
|
|
|
if exists {
|
|
affected, err = cfg.Db.ID(w.ID).Update(&w)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (tl CfgTrustlist) Verify() bool {
|
|
reg := regexp.MustCompile(ipv4_cidr_regex)
|
|
return reg.MatchString(tl.IP)
|
|
}
|
|
|
|
func (cfgset *CfgSet) BuildRegex(cfg config.Config) {
|
|
var rr []CfgExpr
|
|
err := cfg.Db.Where("cfgset_id = $1 AND enabled", cfgset.ID).OrderBy("expr").Find(&rr)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
regs := []string{}
|
|
for _, v := range rr {
|
|
regs = append(regs, v.Expr)
|
|
}
|
|
cfgset.Regex = fmt.Sprintf("(%s)", strings.Join(regs, "|"))
|
|
}
|
|
|
|
func GetSets(cfg config.Config) (res []CfgSet, err error) {
|
|
var w = []CfgSet{}
|
|
|
|
if err := cfg.Db.Where("enabled").Find(&w); err == nil {
|
|
return w, err
|
|
}
|
|
return
|
|
}
|
|
|
|
func GetGlobalConfig(cfg config.Config) (res []Cfg, err error) {
|
|
var w = []Cfg{}
|
|
|
|
if err := cfg.Db.Find(&w); err == nil {
|
|
return w, err
|
|
}
|
|
return
|
|
}
|
|
|
|
func GetAllConfigv2(cfg config.Config) (res CfgAllv2, err error) {
|
|
var c = make(map[string]string)
|
|
var tmpcfg = []Cfg{}
|
|
if err := cfg.Db.Find(&tmpcfg); err == nil {
|
|
for _, v := range tmpcfg {
|
|
c[v.Key] = v.Value
|
|
}
|
|
res.Cfg = c
|
|
}
|
|
|
|
var sets = []CfgSet{}
|
|
if err := cfg.Db.Find(&sets); err == nil {
|
|
for i, _ := range sets {
|
|
sets[i].BuildRegex(cfg)
|
|
}
|
|
res.Sets = sets
|
|
}
|
|
|
|
var tl = []CfgTrustlist{}
|
|
if err := cfg.Db.Find(&tl); err == nil {
|
|
for _, v := range tl {
|
|
res.Trustlists = append(res.Trustlists, v.IP)
|
|
|
|
}
|
|
}
|
|
|
|
var ws = []CfgWS{}
|
|
if err := cfg.Db.Find(&ws); err == nil {
|
|
res.WS = ws
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func InsertOrUpdateSets(cfg config.Config, folders []CfgSet) (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 GetWS(cfg config.Config) (res []CfgWS, err error) {
|
|
var w = []CfgWS{}
|
|
|
|
if err = cfg.Db.Find(&w); err == nil {
|
|
return w, err
|
|
}
|
|
return
|
|
}
|
|
|
|
func DiscoverURLS(cfg config.Config, routes []*echo.Route) (Discovery, error) {
|
|
var disc Discovery
|
|
var urls = make(map[string]Url)
|
|
for _, j := range routes {
|
|
if strings.Contains(j.Path, ":") || j.Path == "/" || j.Path == "/discovery" {
|
|
continue
|
|
}
|
|
names := strings.Split(j.Path, "/")
|
|
name := names[len(names)-1]
|
|
urls[name] = Url{Key: name, Path: j.Path}
|
|
}
|
|
disc = Discovery{Version: "1.0", URLs: urls}
|
|
return disc, nil
|
|
}
|
|
|
|
type CfgAllv2 struct {
|
|
Cfg map[string]string `json:"cfg"`
|
|
Sets []CfgSet `json:"sets"`
|
|
Trustlists []string `json:"trustlists"`
|
|
WS []CfgWS `json:"ws"`
|
|
}
|
|
|
|
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 CfgSet struct {
|
|
ID int `xorm:"pk autoincr" json:"-"`
|
|
Path string `xorm:"text notnull default" json:"path"`
|
|
Src string `xorm:"text notnull" json:"src"`
|
|
Filename string `xorm:"text notnull" json:"filename"`
|
|
Regex string `xorm:"-" json:"regex"`
|
|
Blocktime int64 `xorm:"notnull default 60" json:"blocktime"`
|
|
TryFail int64 `xorm:"notnull default 5" json:"tryfail"`
|
|
Enabled bool `xorm:"notnull default true" json:"-"`
|
|
}
|
|
|
|
type CfgExpr struct {
|
|
ID int `xorm:"pk autoincr" json:"-"`
|
|
Expr string `xorm:"text notnull unique(exprindex) index default ''"`
|
|
Enabled bool `xorm:"notnull default true" json:"-"`
|
|
Set *CfgSet `xorm:"cfgset_id int unique(exprindex) default null"`
|
|
}
|
|
|
|
type CfgTrustlist struct {
|
|
ID int `xorm:"pk autoincr" json:"-"`
|
|
IP string `xorm:"text notnull" json:"ip"`
|
|
}
|
|
|
|
type CfgWS struct {
|
|
ID int `xorm:"pk autoincr" json:"-"`
|
|
Type string `xorm:"text notnull" json:"type"`
|
|
Endpoint string `xorm:"text notnull" json:"endpoint"`
|
|
Subscription string `json:"subscription"`
|
|
}
|
|
|
|
type Discovery struct {
|
|
Version string `json:"version"`
|
|
URLs map[string]Url `json:"urls"`
|
|
}
|
|
|
|
type Url struct {
|
|
Key string `json:"key"`
|
|
Path string `json:"path"`
|
|
}
|