Added sys to cfg
Creating a single cfg that can be passed around instead of several variablestesting
parent
e1c576f2a1
commit
83679a1033
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
const CFG = `
|
const CFG = `
|
||||||
|
|
@ -28,24 +29,37 @@ type Config struct {
|
||||||
Sites []string
|
Sites []string
|
||||||
Downloads []string
|
Downloads []string
|
||||||
Whitelist []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
|
// Create initialized a config to be used the entire session
|
||||||
func Create(cfgLoc string) (cfg Config) {
|
func Create() (err error,cfg Config) {
|
||||||
cfg.CfgLoc = cfgLoc
|
for _,fp := range(configFuncs){
|
||||||
|
fp(&cfg)
|
||||||
|
}
|
||||||
|
if (cfg.System.OS == ""){return errors.New("Failed to detect the OS"), cfg}
|
||||||
|
err, cfg = cfg.Update()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// cfgparse recieves the location of the config file and returns a list of sites to add and content to download
|
// 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) {
|
func (cfg Config) Update() (error, Config) {
|
||||||
l := (cfg.CfgLoc + "rhosts.cfg")
|
l := (cfg.System.CfgLoc + "rhosts.cfg")
|
||||||
var err error = nil
|
var err error = nil
|
||||||
log.Print("Opening: ", l)
|
log.Print("Opening: ", l)
|
||||||
if _, err = os.Stat(cfg.CfgLoc); os.IsNotExist(err) {
|
if _, err = os.Stat(cfg.System.CfgLoc); os.IsNotExist(err) {
|
||||||
log.Print(cfg.CfgLoc + " Does not exist, attempting to create it")
|
log.Print(cfg.System.CfgLoc + " Does not exist, attempting to create it")
|
||||||
err = os.MkdirAll(cfg.CfgLoc, 0755)
|
err = os.MkdirAll(cfg.System.CfgLoc, 0755)
|
||||||
if err != nil {
|
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) {
|
if _, err = os.Stat(l); os.IsNotExist(err) {
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -26,7 +26,6 @@ import (
|
||||||
"jbreich/rhosts/cfg"
|
"jbreich/rhosts/cfg"
|
||||||
"jbreich/rhosts/hosts"
|
"jbreich/rhosts/hosts"
|
||||||
"jbreich/rhosts/serve"
|
"jbreich/rhosts/serve"
|
||||||
sysos "jbreich/rhosts/sys"
|
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -53,9 +52,6 @@ const GPL = `
|
||||||
`
|
`
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
tmpdir := ""
|
|
||||||
hostsloc := ""
|
|
||||||
cfgloc := ""
|
|
||||||
var daemon bool = false
|
var daemon bool = false
|
||||||
var interval int = 1440
|
var interval int = 1440
|
||||||
var versionflag bool = false
|
var versionflag bool = false
|
||||||
|
|
@ -87,28 +83,22 @@ func main() {
|
||||||
log.Print("interval:", interval)
|
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
|
// Starting web server
|
||||||
serve.Start("blank")
|
serve.Start("blank")
|
||||||
|
|
||||||
// Update the hosts file
|
// Update the hosts file
|
||||||
if daemon == false {
|
if daemon == false {
|
||||||
err := hosts.Update(config, tmpdir, hostsloc)
|
err := hosts.Update(config, config.System.TmpDir, config.System.HostsLoc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
for true {
|
for true {
|
||||||
err := hosts.Update(config, tmpdir, hostsloc)
|
err := hosts.Update(config, config.System.TmpDir, config.System.HostsLoc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
// +build linux
|
|
||||||
|
|
||||||
package sys
|
|
||||||
|
|
||||||
|
|
||||||
func init(){
|
|
||||||
|
|
||||||
system.os = "linux"
|
|
||||||
system.tmpdir = "/tmp/"
|
|
||||||
system.hostsloc = "/etc/hosts"
|
|
||||||
system.cfgloc = "/etc/rhosts/"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
@ -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/"
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue