Update config logic
This commit is contained in:
1
go.mod
1
go.mod
@@ -3,6 +3,7 @@ module github.com/d3m0k1d/BanForge
|
|||||||
go 1.25.5
|
go 1.25.5
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/BurntSushi/toml v1.6.0 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/spf13/cobra v1.10.2 // indirect
|
github.com/spf13/cobra v1.10.2 // indirect
|
||||||
github.com/spf13/pflag v1.0.10 // indirect
|
github.com/spf13/pflag v1.0.10 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -1,3 +1,5 @@
|
|||||||
|
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
|
||||||
|
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
|
|||||||
@@ -7,13 +7,6 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Firewalls = map[string]string{
|
|
||||||
"iptables": "iptables",
|
|
||||||
"nftables": "nft",
|
|
||||||
"ufw": "ufw",
|
|
||||||
"firewalld": "firewall-cmd",
|
|
||||||
}
|
|
||||||
|
|
||||||
var DetectedFirewall string
|
var DetectedFirewall string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -51,15 +44,26 @@ func CreateConf() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSysConf() error {
|
func FindFirewall() error {
|
||||||
for name, binary := range Firewalls {
|
|
||||||
if _, err := exec.LookPath(binary); err == nil {
|
if os.Getegid() != 0 {
|
||||||
DetectedFirewall = name
|
fmt.Printf("Firewall settings needs sudo privileges\n")
|
||||||
fmt.Printf("found firewall: %s\n", name)
|
os.Exit(1)
|
||||||
confstr := "firewall = \"" + name + "\""
|
}
|
||||||
os.WriteFile(ConfigDir+"/"+ConfigFile, []byte(confstr), 0644)
|
firewalls := []string{"iptables", "nft", "firewall-cmd", "ufw"}
|
||||||
|
for _, firewall := range firewalls {
|
||||||
|
_, err := exec.LookPath(firewall)
|
||||||
|
if err == nil {
|
||||||
|
if firewall == "firewall-cmd" {
|
||||||
|
DetectedFirewall = "firewalld"
|
||||||
|
}
|
||||||
|
if firewall == "nft" {
|
||||||
|
DetectedFirewall = "nftables"
|
||||||
|
}
|
||||||
|
DetectedFirewall = firewall
|
||||||
|
fmt.Printf("Detected firewall: %s\n", firewall)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fmt.Errorf("no firewall found (checked iptables, nftables, ufw, firewalld) please install once of them")
|
return fmt.Errorf("no firewall found (checked ufw, firewall-cmd, iptables, nft) please install one of them")
|
||||||
}
|
}
|
||||||
|
|||||||
15
internal/config/template.go
Normal file
15
internal/config/template.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
const Base_config = `# This is a TOML config file for BanForge it's a simple config file
|
||||||
|
# https://github.com/d3m0k1d/BanForge
|
||||||
|
|
||||||
|
# Firewall settings block
|
||||||
|
[firewall]
|
||||||
|
name = "iptables" # Name one of the support firewall(iptables, nftables, firewalld, ufw)
|
||||||
|
ban_time = 1200
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
name = "nginx"
|
||||||
|
log_path = "/var/log/nginx/access.log"
|
||||||
|
enabled = true
|
||||||
|
`
|
||||||
12
internal/config/types.go
Normal file
12
internal/config/types.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
type Firewall struct {
|
||||||
|
Name string `toml:"name"`
|
||||||
|
Ban_time int `toml:ban_time`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
Name string `toml:"name"`
|
||||||
|
Log_path string `toml:"log_path"`
|
||||||
|
Enabled bool `toml:"enabled"`
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user