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

1
go.mod
View File

@@ -3,6 +3,7 @@ module github.com/d3m0k1d/BanForge
go 1.25.5
require (
github.com/BurntSushi/toml v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/cobra v1.10.2 // indirect
github.com/spf13/pflag v1.0.10 // indirect

2
go.sum
View File

@@ -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/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=

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