added zmq configs

This commit is contained in:
Paul 2022-03-10 19:56:03 +01:00
parent 2bf11fdb75
commit b93cd8306e
9 changed files with 62 additions and 30 deletions

View File

@ -25,14 +25,12 @@ func main() {
os.Exit(0) os.Exit(0)
} }
// Initialize database app context
err := database.Initialize(&ctx, &cfg) err := database.Initialize(&ctx, &cfg)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
defer cfg.Db.Close() defer cfg.Db.Close()
// Handles IP with no reverse DNS
go models.ScanIP(&cfg) go models.ScanIP(&cfg)
// Add cron task to handle them // Add cron task to handle them
@ -42,7 +40,6 @@ func main() {
//}) //})
//cr.Start() //cr.Start()
// Run the ipbl web service
err = routers.RunServer(&ctx, &cfg) err = routers.RunServer(&ctx, &cfg)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)

View File

@ -1,6 +1,5 @@
package api package api
// IP describe IP objects via API calls
type IP struct { type IP struct {
ID int `json:"id"` ID int `json:"id"`
IP string `json:"ip"` IP string `json:"ip"`

View File

@ -8,7 +8,6 @@ import (
"xorm.io/xorm" "xorm.io/xorm"
) )
// GetConfig fetch configuration
func (cfg *Config) GetConfig() error { func (cfg *Config) GetConfig() error {
var configfile string var configfile string
var debug bool var debug bool
@ -50,7 +49,6 @@ func (cfg *Config) GetConfig() error {
return nil return nil
} }
// Config is the global config
type Config struct { type Config struct {
Db *xorm.Engine `json:"-"` Db *xorm.Engine `json:"-"`
DbParams struct { DbParams struct {

View File

@ -13,7 +13,6 @@ import (
"xorm.io/xorm/names" "xorm.io/xorm/names"
) )
// Init creates connection to database and exec Schema
func Initialize(ctx *context.Context, cfg *config.Config) (err error) { func Initialize(ctx *context.Context, cfg *config.Config) (err error) {
var databaseEngine = "postgres" var databaseEngine = "postgres"
var tables = []interface{}{models.IP{}, models.Cfg{}, models.Src{}} var tables = []interface{}{models.IP{}, models.Cfg{}, models.Src{}}

View File

@ -12,7 +12,6 @@ import (
//var ipv4_regex = `^(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4})/` //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])?$)` var ipv4_cidr_regex = `^(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|)){4}\/([1-3])?([0-9])?$)`
// GetTrustlists ...
func GetTrustlists(cfg config.Config) (res []string, err error) { func GetTrustlists(cfg config.Config) (res []string, err error) {
var w = Cfg{Key: "trustlist"} var w = Cfg{Key: "trustlist"}
if exists, _ := cfg.Db.Get(&w); exists { if exists, _ := cfg.Db.Get(&w); exists {
@ -60,7 +59,6 @@ func (wl Trustlist) Verify() bool {
return reg.MatchString(wl.IP) return reg.MatchString(wl.IP)
} }
// GetFolders ...
func GetFolders(cfg config.Config) (res []Folder, err error) { func GetFolders(cfg config.Config) (res []Folder, err error) {
var w = Cfg{Key: "folders"} var w = Cfg{Key: "folders"}
if exists, _ := cfg.Db.Get(&w); exists { if exists, _ := cfg.Db.Get(&w); exists {
@ -69,7 +67,6 @@ func GetFolders(cfg config.Config) (res []Folder, err error) {
return return
} }
// InsertOrUpdateFolders ...
func InsertOrUpdateFolders(cfg config.Config, folders []Folder) (res string, err error) { func InsertOrUpdateFolders(cfg config.Config, folders []Folder) (res string, err error) {
var w = Cfg{Key: "folders"} var w = Cfg{Key: "folders"}
if exists, _ := cfg.Db.Get(&w); exists { if exists, _ := cfg.Db.Get(&w); exists {
@ -86,6 +83,23 @@ func InsertOrUpdateFolders(cfg config.Config, folders []Folder) (res string, err
return 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: "folders", Path: "/config/folders"})
urls = append(urls, Url{Key: "trustlist", Path: "/config/trustlist"})
disc = Discovery{Version: "1.0", URLs: urls}
return disc, nil
}
type Trustlist struct { type Trustlist struct {
IP string `json:"ip"` IP string `json:"ip"`
} }
@ -95,15 +109,29 @@ type Folder struct {
Sets []Set `json:"sets"` Sets []Set `json:"sets"`
} }
type ZMQ struct {
Hostname string `json:"hostname"`
Port int `json:"port"`
}
type Set struct { type Set struct {
Type string `json:"type"` Type string `json:"type"`
Filename string `json:"filename"` Filename string `json:"filename"`
Regex string `json:"regex"` Regex string `json:"regex"`
} }
// Cfg is ipbl config
type Cfg struct { type Cfg struct {
ID int `xorm:"pk autoincr" json:"-"` ID int `xorm:"pk autoincr" json:"-"`
Key string `xorm:"text notnull unique" json:"key"` Key string `xorm:"text notnull unique" json:"key"`
Value string `xorm:"text default" json:"value"` 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"`
}

View File

@ -14,7 +14,6 @@ import (
var lastday = time.Now().Add(-(time.Hour * 24)) var lastday = time.Now().Add(-(time.Hour * 24))
// GetIPs ...
func GetIPs(ctx *context.Context, config *config.Config, limit int) (apimailboxes []*api.IP, err error) { func GetIPs(ctx *context.Context, config *config.Config, limit int) (apimailboxes []*api.IP, err error) {
var ips []IP var ips []IP
err = config.Db.Limit(limit).Desc("created").Find(&ips) err = config.Db.Limit(limit).Desc("created").Find(&ips)
@ -24,7 +23,6 @@ func GetIPs(ctx *context.Context, config *config.Config, limit int) (apimailboxe
return return
} }
// GetIPs ...
func GetIPsLast(ctx *context.Context, config *config.Config, interval string) (apimailboxes []*api.IP, err error) { func GetIPsLast(ctx *context.Context, config *config.Config, interval string) (apimailboxes []*api.IP, err error) {
var ips []IP var ips []IP
err = config.Db.Where(fmt.Sprintf("updated >= (now()-'%s'::interval)", interval)).GroupBy("ip").Find(&ips) err = config.Db.Where(fmt.Sprintf("updated >= (now()-'%s'::interval)", interval)).GroupBy("ip").Find(&ips)
@ -34,7 +32,6 @@ func GetIPsLast(ctx *context.Context, config *config.Config, interval string) (a
return return
} }
// GetIP ...
func GetIP(ctx *context.Context, config *config.Config, ipquery interface{}) (apiip *api.IP, err error) { func GetIP(ctx *context.Context, config *config.Config, ipquery interface{}) (apiip *api.IP, err error) {
var ip IP var ip IP
has, err := config.Db.Where("ip = ?", ipquery).Get(&ip) has, err := config.Db.Where("ip = ?", ipquery).Get(&ip)
@ -49,7 +46,6 @@ func GetIP(ctx *context.Context, config *config.Config, ipquery interface{}) (ap
return return
} }
// UpdateRDNS ...
func (i *IP) UpdateRDNS() (result string, err error) { func (i *IP) UpdateRDNS() (result string, err error) {
res, err := net.LookupAddr(i.IP) res, err := net.LookupAddr(i.IP)
if err != nil { if err != nil {
@ -60,13 +56,11 @@ func (i *IP) UpdateRDNS() (result string, err error) {
return return
} }
// InsertIP ...
func (i *IP) InsertIP(cfg *config.Config) (num int64, err error) { func (i *IP) InsertIP(cfg *config.Config) (num int64, err error) {
num, err = cfg.Db.Insert(i) num, err = cfg.Db.Insert(i)
return return
} }
// InsertIPBulk ...
func InsertIPBulk(cfg *config.Config, ips *[]IP) (numinserts int64, numupdates int64, numfail int64, err error) { func InsertIPBulk(cfg *config.Config, ips *[]IP) (numinserts int64, numupdates int64, numfail int64, err error) {
var iplist []string var iplist []string
for _, ip := range *ips { for _, ip := range *ips {
@ -88,7 +82,6 @@ func InsertIPBulk(cfg *config.Config, ips *[]IP) (numinserts int64, numupdates i
return return
} }
// ScanIP ...
func ScanIP(cfg *config.Config) (err error) { func ScanIP(cfg *config.Config) (err error) {
for { for {
orphans := []IP{} orphans := []IP{}
@ -109,7 +102,6 @@ func ScanIP(cfg *config.Config) (err error) {
} }
} }
// APIFormat returns a JSON formatted object of IP
func (ip *IP) APIFormat() *api.IP { func (ip *IP) APIFormat() *api.IP {
if ip == nil { if ip == nil {
return nil return nil
@ -121,7 +113,6 @@ func (ip *IP) APIFormat() *api.IP {
} }
} }
// IP describe IP objects
type IP struct { type IP struct {
ID int `xorm:"pk autoincr" json:"-"` ID int `xorm:"pk autoincr" json:"-"`
IP string `xorm:"text notnull unique(ipsrc)" json:"ip"` IP string `xorm:"text notnull unique(ipsrc)" json:"ip"`

View File

@ -1,6 +1,5 @@
package models package models
// Src is src types
type Src struct { type Src struct {
ID int `xorm:"pk autoincr" json:"-"` ID int `xorm:"pk autoincr" json:"-"`
Src string `xorm:"text notnull unique" json:"src"` Src string `xorm:"text notnull unique" json:"src"`

View File

@ -13,7 +13,6 @@ import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
// RegisterRoutes runs the main echo HTTP server
func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) { func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
e.GET("/", func(c echo.Context) error { e.GET("/", func(c echo.Context) error {
return c.HTML(http.StatusOK, `<html> return c.HTML(http.StatusOK, `<html>
@ -79,11 +78,11 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
} }
return Result(c, err, msg) return Result(c, err, msg)
}) })
e.GET("/ips/trustlist", func(c echo.Context) (err error) { e.GET("/config/trustlist", func(c echo.Context) (err error) {
trustlists, err := models.GetTrustlists(*cfg) trustlists, err := models.GetTrustlists(*cfg)
return Result(c, err, trustlists) return Result(c, err, trustlists)
}) })
e.POST("/ips/trustlist", func(c echo.Context) (err error) { e.POST("/config/trustlist", func(c echo.Context) (err error) {
var cidr models.Trustlist var cidr models.Trustlist
err = c.Bind(&cidr) err = c.Bind(&cidr)
if err == nil && cidr.Verify() { if err == nil && cidr.Verify() {
@ -92,7 +91,7 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
} }
return Result(c, err, nil) return Result(c, err, nil)
}) })
e.DELETE("/ips/trustlist/:ip", func(c echo.Context) (err error) { e.DELETE("/config/trustlist/:ip", func(c echo.Context) (err error) {
var ip = c.Param("ip") var ip = c.Param("ip")
var cidr models.Trustlist var cidr models.Trustlist
err = cidr.Delete(*cfg, ip) err = cidr.Delete(*cfg, ip)
@ -101,11 +100,11 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
} }
return return
}) })
e.GET("/ips/folders", func(c echo.Context) (err error) { e.GET("/config/folders", func(c echo.Context) (err error) {
folders, err := models.GetFolders(*cfg) folders, err := models.GetFolders(*cfg)
return Result(c, err, folders) return Result(c, err, folders)
}) })
e.POST("/ips/folders", func(c echo.Context) (err error) { e.POST("/config/folders", func(c echo.Context) (err error) {
var folders []models.Folder var folders []models.Folder
err = c.Bind(&folders) err = c.Bind(&folders)
if err != nil { if err != nil {
@ -114,6 +113,31 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
_, err = models.InsertOrUpdateFolders(*cfg, folders) _, err = models.InsertOrUpdateFolders(*cfg, folders)
return Result(c, err, folders) return Result(c, err, folders)
}) })
e.GET("/config/folders", func(c echo.Context) (err error) {
folders, err := models.GetFolders(*cfg)
return Result(c, err, folders)
})
e.POST("/config/folders", func(c echo.Context) (err error) {
var folders []models.Folder
err = c.Bind(&folders)
if err != nil {
return Result(c, err, "Unable to parse JSON")
}
_, err = models.InsertOrUpdateFolders(*cfg, folders)
return Result(c, err, folders)
})
e.GET("/config/zmqps", func(c echo.Context) (err error) {
folders, err := models.GetZMQ(*cfg, "ps")
return Result(c, err, folders)
})
e.GET("/config/zmqrr", func(c echo.Context) (err error) {
folders, err := models.GetZMQ(*cfg, "rr")
return Result(c, err, folders)
})
e.GET("/discovery", func(c echo.Context) (err error) {
disc, err := models.DiscoverURLS(*cfg)
return Result(c, err, disc)
})
e.Logger.Fatal( e.Logger.Fatal(
e.Start( e.Start(

View File

@ -10,7 +10,6 @@ import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
// RunServer runs the main echo server
func RunServer(ctx *context.Context, cfg *config.Config) (err error) { func RunServer(ctx *context.Context, cfg *config.Config) (err error) {
e := echo.New() e := echo.New()
e.HideBanner = true e.HideBanner = true
@ -21,7 +20,6 @@ func RunServer(ctx *context.Context, cfg *config.Config) (err error) {
return return
} }
// Result handles returns and error management on backend api
func Result(c echo.Context, inputerr error, data interface{}) (err error) { func Result(c echo.Context, inputerr error, data interface{}) (err error) {
if inputerr != nil { if inputerr != nil {
if inputerr.Error() == "Not Found" { if inputerr.Error() == "Not Found" {
@ -41,7 +39,6 @@ func Result(c echo.Context, inputerr error, data interface{}) (err error) {
return c.JSON(http.StatusOK, data) return c.JSON(http.StatusOK, data)
} }
// ConfigAccess make ip authorization to configuration
func ConfigAccess(cfg config.Config, ip string) (ret bool) { func ConfigAccess(cfg config.Config, ip string) (ret bool) {
switch ip { switch ip {
case "127.0.0.1": case "127.0.0.1":