replace abusive keywords
Some checks reported errors
continuous-integration/drone/push Build encountered an error
continuous-integration/drone/tag Build is passing

This commit is contained in:
Paul 2022-02-22 19:46:01 +01:00
parent 878239d5d5
commit 8512402488
2 changed files with 27 additions and 23 deletions

View File

@ -11,20 +11,20 @@ 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])?$)`
// GetWhitelists ... // GetTrustlists ...
func GetWhitelists(cfg config.Config) (res []string, err error) { func GetTrustlists(cfg config.Config) (res []string, err error) {
var w = Cfg{Key: "whitelist"} var w = Cfg{Key: "trustlist"}
if exists, _ := cfg.Db.Get(&w); exists { if exists, _ := cfg.Db.Get(&w); exists {
res = strings.Split(w.Value, ",") res = strings.Split(w.Value, ",")
} }
return return
} }
func (wl Whitelist) InsertOrUpdate(cfg config.Config) (err error) { func (wl Trustlist) InsertOrUpdate(cfg config.Config) (err error) {
var w = Cfg{Key: "whitelist"} var w = Cfg{Key: "trustlist"}
exists, _ := cfg.Db.Get(&w) exists, _ := cfg.Db.Get(&w)
if exists { if exists {
existing, _ := GetWhitelists(cfg) existing, _ := GetTrustlists(cfg)
for _, j := range existing { for _, j := range existing {
if j == wl.IP { if j == wl.IP {
return fmt.Errorf("ip %s already in config", j) return fmt.Errorf("ip %s already in config", j)
@ -34,15 +34,15 @@ func (wl Whitelist) InsertOrUpdate(cfg config.Config) (err error) {
w.Value = strings.Join(existing, ",") w.Value = strings.Join(existing, ",")
cfg.Db.ID(w.ID).Update(&w) cfg.Db.ID(w.ID).Update(&w)
} }
return fmt.Errorf("no whitelist updated") return fmt.Errorf("no trustlist updated")
} }
func (wl Whitelist) Delete(cfg config.Config, ip string) (err error) { func (wl Trustlist) Delete(cfg config.Config, ip string) (err error) {
var w = Cfg{Key: "whitelist"} var w = Cfg{Key: "trustlist"}
exists, _ := cfg.Db.Get(&w) exists, _ := cfg.Db.Get(&w)
var updated []string var updated []string
if exists { if exists {
existing, _ := GetWhitelists(cfg) existing, _ := GetTrustlists(cfg)
for _, sip := range existing { for _, sip := range existing {
if sip != ip { if sip != ip {
updated = append(updated, sip) updated = append(updated, sip)
@ -51,15 +51,15 @@ func (wl Whitelist) Delete(cfg config.Config, ip string) (err error) {
w.Value = strings.Join(updated, ",") w.Value = strings.Join(updated, ",")
cfg.Db.ID(w.ID).Update(&w) cfg.Db.ID(w.ID).Update(&w)
} }
return fmt.Errorf("no whitelist updated") return fmt.Errorf("no trustlist updated")
} }
func (wl Whitelist) Verify() bool { func (wl Trustlist) Verify() bool {
reg := regexp.MustCompile(ipv4_cidr_regex) reg := regexp.MustCompile(ipv4_cidr_regex)
return reg.MatchString(wl.IP) return reg.MatchString(wl.IP)
} }
type Whitelist struct { type Trustlist struct {
IP string `json:"ip"` IP string `json:"ip"`
} }

View File

@ -18,7 +18,7 @@ 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>
<body style="background-color: black"> <body style="background-color: black">
<p style="color:white">Welcome to ipbl software (<a href="https://git.paulbsd.com/paulbsd/ipbl">https://git.paulbsd.com/paulbsd/ipbl</a>)</p> <p style="color:trust">Welcome to ipbl software (<a href="https://git.paulbsd.com/paulbsd/ipbl">https://git.paulbsd.com/paulbsd/ipbl</a>)</p>
</body> </body>
</html>`) </html>`)
}) })
@ -72,21 +72,25 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
} }
numinsert, numupdate, _, _ := models.InsertIPBulk(cfg, &ips) numinsert, numupdate, _, _ := models.InsertIPBulk(cfg, &ips)
if numinsert > 0 { if numinsert > 0 {
msg = fmt.Sprintf("Inserted %d IP", numinsert) msg = fmt.Sprintf("inserted %d IP", numinsert)
log.Println(msg) log.Println(msg)
} }
if numupdate > 0 { if numupdate > 0 {
msg = fmt.Sprintf("Updated %d IP", numupdate) if len(msg) > 0 {
msg = fmt.Sprintf("%s, updated %d IP", msg, numupdate)
} else {
msg = fmt.Sprintf("updated %d IP", numupdate)
}
log.Println(msg) log.Println(msg)
} }
return Result(c, err, msg) return Result(c, err, msg)
}) })
e.GET("/ips/whitelist", func(c echo.Context) (err error) { e.GET("/ips/trustlist", func(c echo.Context) (err error) {
whitelists, err := models.GetWhitelists(*cfg) trustlists, err := models.GetTrustlists(*cfg)
return Result(c, err, whitelists) return Result(c, err, trustlists)
}) })
e.POST("/ips/whitelist", func(c echo.Context) (err error) { e.POST("/ips/trustlist", func(c echo.Context) (err error) {
var cidr models.Whitelist var cidr models.Trustlist
err = c.Bind(&cidr) err = c.Bind(&cidr)
if err == nil && cidr.Verify() { if err == nil && cidr.Verify() {
err = cidr.InsertOrUpdate(*cfg) err = cidr.InsertOrUpdate(*cfg)
@ -94,9 +98,9 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
} }
return Result(c, err, nil) return Result(c, err, nil)
}) })
e.DELETE("/ips/whitelist/:ip", func(c echo.Context) (err error) { e.DELETE("/ips/trustlist/:ip", func(c echo.Context) (err error) {
var ip = c.Param("ip") var ip = c.Param("ip")
var cidr models.Whitelist var cidr models.Trustlist
err = cidr.Delete(*cfg, ip) err = cidr.Delete(*cfg, ip)
if err != nil { if err != nil {
return c.JSON(http.StatusOK, "Deleted old CIDR") return c.JSON(http.StatusOK, "Deleted old CIDR")