Recode sysconf logic
This commit is contained in:
@@ -4,18 +4,62 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"syscall"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateConf() {
|
var Firewalls = map[string]string{
|
||||||
if syscall.Geteuid() != 0 {
|
"iptables": "iptables",
|
||||||
os.Exit(1)
|
"nftables": "nft",
|
||||||
fmt.Printf("You must be root to run\n, use the sudo/doas")
|
"ufw": "ufw",
|
||||||
}
|
"firewalld": "firewall-cmd",
|
||||||
exec.Command("mkdir /etc/banforge")
|
|
||||||
exec.Command("touch /etc/banforge/config.toml")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckSysConf() {
|
var DetectedFirewall string
|
||||||
|
|
||||||
|
const (
|
||||||
|
ConfigDir = "/etc/banforge"
|
||||||
|
ConfigFile = "config.toml"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateConf() error {
|
||||||
|
if os.Geteuid() != 0 {
|
||||||
|
return fmt.Errorf("you must be root to run this command, use sudo/doas")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.MkdirAll(ConfigDir, 0755); err != nil {
|
||||||
|
return fmt.Errorf("failed to create config directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
configPath := filepath.Join(ConfigDir, ConfigFile)
|
||||||
|
|
||||||
|
if _, err := os.Stat(configPath); err == nil {
|
||||||
|
fmt.Printf("Config file already exists: %s\n", configPath)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Create(configPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create config file: %w", err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
if err := os.Chmod(configPath, 0644); err != nil {
|
||||||
|
return fmt.Errorf("failed to set permissions: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf(" Config file created: %s\n", configPath)
|
||||||
|
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)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf("no firewall found (checked iptables, nftables, ufw, firewalld) please install once of them")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package parser
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"github.com/d3m0k1d/BanForge/local/logger"
|
"github.com/d3m0k1d/BanForge/internal/logger"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user