Update config logic

This commit is contained in:
d3m0k1d
2026-01-05 16:29:06 +03:00
parent 1ca8d180fe
commit f85ebe6ada
5 changed files with 49 additions and 15 deletions

View File

@@ -7,13 +7,6 @@ import (
"path/filepath"
)
var Firewalls = map[string]string{
"iptables": "iptables",
"nftables": "nft",
"ufw": "ufw",
"firewalld": "firewall-cmd",
}
var DetectedFirewall string
const (
@@ -51,15 +44,26 @@ func CreateConf() error {
return nil
}
func GetSysConf() error {
for name, binary := range Firewalls {
if _, err := exec.LookPath(binary); err == nil {
DetectedFirewall = name
fmt.Printf("found firewall: %s\n", name)
confstr := "firewall = \"" + name + "\""
os.WriteFile(ConfigDir+"/"+ConfigFile, []byte(confstr), 0644)
func FindFirewall() error {
if os.Getegid() != 0 {
fmt.Printf("Firewall settings needs sudo privileges\n")
os.Exit(1)
}
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 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")
}

View 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
View 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"`
}