From 2ae0f15b824b77ba812507b1905d01818aa61fc2 Mon Sep 17 00:00:00 2001 From: Justin Reichardt Date: Fri, 19 Aug 2022 15:27:31 -0500 Subject: [PATCH] serve now pulls config and config panics if it fails to create Had to also add var to allow serve to pull certs If it cannot manage the config it needs to exit, so it made more sense to add it there then check every time it is called --- src/cfg/cfg.go | 10 ++++++---- src/cfg/linux.go | 18 +++++++++--------- src/cfg/windows.go | 13 +++++++------ src/hosts/hosts.go | 3 +-- src/rhosts.go | 2 +- src/serve/serve.go | 10 ++++++---- 6 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/cfg/cfg.go b/src/cfg/cfg.go index d2f623c..1fdcc8e 100644 --- a/src/cfg/cfg.go +++ b/src/cfg/cfg.go @@ -4,7 +4,6 @@ import ( "bufio" "log" "os" - "errors" ) const CFG = ` @@ -34,6 +33,7 @@ type Config struct { TmpDir string HostsLoc string CfgLoc string + Var string } } @@ -41,12 +41,14 @@ type Config struct { var configFuncs []func(*Config) // Create initialized a config to be used the entire session -func Create() (err error,cfg Config) { +func Create() (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() + if (cfg.System.OS == ""){log.Panic("Failed to detect the OS")} + err, cfg := cfg.Update() + + if (err != nil){log.Panic("Failed to handle the config file: " + err.Error())} return } diff --git a/src/cfg/linux.go b/src/cfg/linux.go index 1859124..c1e57e4 100644 --- a/src/cfg/linux.go +++ b/src/cfg/linux.go @@ -1,16 +1,16 @@ +//go:build linux // +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/" +func init() { + fp := func(c *Config) { + c.System.OS = "linux" + c.System.TmpDir = "/tmp/" + c.System.HostsLoc = "/etc/hosts" + c.System.CfgLoc = "/etc/rhosts/" + c.System.Var = "/var/lib/rhosts/" } - configFuncs = append(configFuncs,fp) + configFuncs = append(configFuncs, fp) } - diff --git a/src/cfg/windows.go b/src/cfg/windows.go index 1829598..4341646 100644 --- a/src/cfg/windows.go +++ b/src/cfg/windows.go @@ -1,19 +1,20 @@ +//go:build windows // +build windows package sys - -func init(){ +func init() { } -func init(){ - fp := func(c *Config){ - c.System.os = "windows" +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/" + c.System.Var = "/ProgramData/rhosts/" } - configFuncs = append(configFuncs,fp) + configFuncs = append(configFuncs, fp) } diff --git a/src/hosts/hosts.go b/src/hosts/hosts.go index 7b06f44..b0eae90 100644 --- a/src/hosts/hosts.go +++ b/src/hosts/hosts.go @@ -22,8 +22,7 @@ type siteEntry struct { } func Update() (err error) { - err, config := cfg.Create() - if (err != nil){return} + config := cfg.Create() var siteBuff []siteList err = error(nil) diff --git a/src/rhosts.go b/src/rhosts.go index 6671348..d699d65 100644 --- a/src/rhosts.go +++ b/src/rhosts.go @@ -83,7 +83,7 @@ func main() { } // Starting web server - serve.Start("blank") + serve.Start() // Update the hosts file if daemon == false { diff --git a/src/serve/serve.go b/src/serve/serve.go index b3b1544..7fe0951 100644 --- a/src/serve/serve.go +++ b/src/serve/serve.go @@ -3,11 +3,13 @@ package serve import ( "net/http" + "jbreich/rhosts/cfg" ) -func Start(certLoc string) { +func Start() { + config := cfg.Create() go httpServer() - //go httpsServer(&certLoc) + go httpsServer(config.System.Var + "/certs/") } @@ -15,8 +17,8 @@ func httpServer() (err error) { err = http.ListenAndServe("127.0.0.1:80", http.HandlerFunc(httpHandler)) return } -func httpsServer(certLoc *string) (err error) { - err = http.ListenAndServeTLS("127.0.0.1:80", *certLoc+"ca.crt", *certLoc+"ca.key", http.HandlerFunc(httpHandler)) +func httpsServer(certLoc string) (err error) { + err = http.ListenAndServeTLS("127.0.0.1:80", certLoc+"ca.crt", certLoc+"ca.key", http.HandlerFunc(httpHandler)) return }