Added sys to cfg

Creating a single cfg that can be passed around instead of several variables
testing
Justin Reichardt 2022-08-19 14:55:25 -05:00
parent e1c576f2a1
commit 83679a1033
7 changed files with 59 additions and 65 deletions

View File

@ -4,6 +4,7 @@ import (
"bufio"
"log"
"os"
"errors"
)
const CFG = `
@ -28,24 +29,37 @@ type Config struct {
Sites []string
Downloads []string
Whitelist []string
System struct {
OS string
TmpDir string
HostsLoc string
CfgLoc string
}
}
// Used to hold a list of functions to be run when building
var configFuncs []func(*Config)
// Create initialized a config to be used the entire session
func Create(cfgLoc string) (cfg Config) {
cfg.CfgLoc = cfgLoc
func Create() (err error,cfg Config) {
for _,fp := range(configFuncs){
fp(&cfg)
}
if (cfg.System.OS == ""){return errors.New("Failed to detect the OS"), cfg}
err, cfg = cfg.Update()
return
}
// cfgparse recieves the location of the config file and returns a list of sites to add and content to download
func (cfg Config) Update() (error, Config) {
l := (cfg.CfgLoc + "rhosts.cfg")
l := (cfg.System.CfgLoc + "rhosts.cfg")
var err error = nil
log.Print("Opening: ", l)
if _, err = os.Stat(cfg.CfgLoc); os.IsNotExist(err) {
log.Print(cfg.CfgLoc + " Does not exist, attempting to create it")
err = os.MkdirAll(cfg.CfgLoc, 0755)
if _, err = os.Stat(cfg.System.CfgLoc); os.IsNotExist(err) {
log.Print(cfg.System.CfgLoc + " Does not exist, attempting to create it")
err = os.MkdirAll(cfg.System.CfgLoc, 0755)
if err != nil {
log.Fatal("Could not create " + cfg.CfgLoc)
log.Fatal("Could not create " + cfg.System.CfgLoc)
}
}
if _, err = os.Stat(l); os.IsNotExist(err) {

16
src/cfg/linux.go Normal file
View File

@ -0,0 +1,16 @@
// +build linux
package cfg
func init(){
fp := func(c *Config){
c.System.OS = "linux"
c.System.TmpDir = "/tmp/"
c.System.HostsLoc = "/etc/hosts"
c.System.CfgLoc = "/etc/rhosts/"
}
configFuncs = append(configFuncs,fp)
}

19
src/cfg/windows.go Normal file
View File

@ -0,0 +1,19 @@
// +build windows
package sys
func init(){
}
func init(){
fp := func(c *Config){
c.System.os = "windows"
c.System.tmpdir = "/tmp/"
c.System.hostsloc = "/Windows/System32/drivers/etc/hosts"
c.System.cfgloc = "/ProgramData/rhosts/"
}
configFuncs = append(configFuncs,fp)
}

View File

@ -26,7 +26,6 @@ import (
"jbreich/rhosts/cfg"
"jbreich/rhosts/hosts"
"jbreich/rhosts/serve"
sysos "jbreich/rhosts/sys"
"log"
"time"
)
@ -53,9 +52,6 @@ const GPL = `
`
func main() {
tmpdir := ""
hostsloc := ""
cfgloc := ""
var daemon bool = false
var interval int = 1440
var versionflag bool = false
@ -87,28 +83,22 @@ func main() {
log.Print("interval:", interval)
}
sysos.Detect(&tmpdir, &hostsloc, &cfgloc)
_, config := cfg.Create()
// Read the config file
config := cfg.Create(cfgloc)
err, config := config.Update()
if err != nil {
log.Panic("Failed to parse config: " + cfgloc)
}
// Starting web server
serve.Start("blank")
// Update the hosts file
if daemon == false {
err := hosts.Update(config, tmpdir, hostsloc)
err := hosts.Update(config, config.System.TmpDir, config.System.HostsLoc)
if err != nil {
log.Print(err)
}
} else {
for true {
err := hosts.Update(config, tmpdir, hostsloc)
err := hosts.Update(config, config.System.TmpDir, config.System.HostsLoc)
if err != nil {
log.Print(err)
}

View File

@ -1,13 +0,0 @@
// +build linux
package sys
func init(){
system.os = "linux"
system.tmpdir = "/tmp/"
system.hostsloc = "/etc/hosts"
system.cfgloc = "/etc/rhosts/"
}

View File

@ -1,22 +0,0 @@
// This manages all the system specific settings
package sys
import (
"log"
)
var system struct {
os string
tmpdir string
hostsloc string
cfgloc string
}
func Detect (tmpdirp, hostslocp, cfglocp *string) {
if (system.os == "") {
log.Panic("This OS does not seem to be supported")
}
*tmpdirp = system.tmpdir
*hostslocp = system.hostsloc
*cfglocp = system.cfgloc
}

View File

@ -1,10 +0,0 @@
// +build windows
package sys
var os string = "windows"
var tmpdir string = "/tmp/"
var hostsloc string = "/Windows/System32/drivers/etc/hosts"
var cfgloc string = "/ProgramData/rhosts/"