From e2eb14dbc8fe14dcc8f4c063d376f25aa54da9e8 Mon Sep 17 00:00:00 2001 From: Justin Reichardt Date: Fri, 19 Aug 2022 12:00:29 -0500 Subject: [PATCH] Now detects the OS at compilation Using build tags was a more elegant way of determining the OS. Hopefully making less room for errors and easier configuration later Also it is now a negligibly smaller binary --- src/rhosts.go | 23 ++--------------------- src/sys/linux.go | 13 +++++++++++++ src/sys/sys.go | 22 ++++++++++++++++++++++ src/sys/windows.go | 10 ++++++++++ 4 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 src/sys/linux.go create mode 100644 src/sys/sys.go create mode 100644 src/sys/windows.go diff --git a/src/rhosts.go b/src/rhosts.go index f096612..45f490f 100644 --- a/src/rhosts.go +++ b/src/rhosts.go @@ -22,7 +22,6 @@ package main import ( - "runtime" "os" "io" "bufio" @@ -31,6 +30,7 @@ import ( "flag" "time" "fmt" + sysos "jbreich/rhosts/sys" ) // siteList holds the location of all the sites along with a list of their location type siteList struct { @@ -99,7 +99,7 @@ func main() { log.Print("interval:",interval) } - sysdetect (&tmpdir, &hostsloc, &cfgloc) + sysos.Detect (&tmpdir, &hostsloc, &cfgloc) for true { var sites, downloads, whitelist []string @@ -149,25 +149,6 @@ func main() { } } -// sysdetect determines which OS it is running on and set the default locations -func sysdetect (tmpdir, hostsloc, cfgloc *string) { - // Detect OS and set params - switch runtime.GOOS { - case "windows": - //log.Fatal("Windows is not supported") - *tmpdir = "/tmp" - *hostsloc = "/Windows/System32/drivers/etc/hosts" - *cfgloc = "/ProgramData/rhosts/" - case "linux": - *tmpdir = "/tmp/" - *hostsloc = "/etc/hosts" - *cfgloc ="/etc/rhosts/" - case "ios": - log.Fatal("IOS is not supported") - default: - log.Fatal(runtime.GOOS," is not supported") - } -} // cfgparse recieves the location of the config file and returns a list of sites to add and content to download func cfgparse (sites, downloads, whitelist *[]string, cfgloc string) (error){ diff --git a/src/sys/linux.go b/src/sys/linux.go new file mode 100644 index 0000000..9576a46 --- /dev/null +++ b/src/sys/linux.go @@ -0,0 +1,13 @@ +// +build linux + +package sys + + +func init(){ + + system.os = "linux" + system.tmpdir = "/tmp/" + system.hostsloc = "/etc/hosts" + system.cfgloc = "/etc/rhosts/" +} + diff --git a/src/sys/sys.go b/src/sys/sys.go new file mode 100644 index 0000000..db13bfc --- /dev/null +++ b/src/sys/sys.go @@ -0,0 +1,22 @@ +// 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 +} diff --git a/src/sys/windows.go b/src/sys/windows.go new file mode 100644 index 0000000..f02a866 --- /dev/null +++ b/src/sys/windows.go @@ -0,0 +1,10 @@ +// +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/" +