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
testing
Justin Reichardt 2022-08-19 15:27:31 -05:00
parent 15ee33eb01
commit 2ae0f15b82
6 changed files with 30 additions and 26 deletions

View File

@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"log" "log"
"os" "os"
"errors"
) )
const CFG = ` const CFG = `
@ -34,6 +33,7 @@ type Config struct {
TmpDir string TmpDir string
HostsLoc string HostsLoc string
CfgLoc string CfgLoc string
Var string
} }
} }
@ -41,12 +41,14 @@ type Config struct {
var configFuncs []func(*Config) 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() (err error,cfg Config) { func Create() (cfg Config) {
for _,fp := range(configFuncs){ for _,fp := range(configFuncs){
fp(&cfg) fp(&cfg)
} }
if (cfg.System.OS == ""){return errors.New("Failed to detect the OS"), cfg} if (cfg.System.OS == ""){log.Panic("Failed to detect the OS")}
err, cfg = cfg.Update() err, cfg := cfg.Update()
if (err != nil){log.Panic("Failed to handle the config file: " + err.Error())}
return return
} }

View File

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

View File

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

View File

@ -22,8 +22,7 @@ type siteEntry struct {
} }
func Update() (err error) { func Update() (err error) {
err, config := cfg.Create() config := cfg.Create()
if (err != nil){return}
var siteBuff []siteList var siteBuff []siteList
err = error(nil) err = error(nil)

View File

@ -83,7 +83,7 @@ func main() {
} }
// Starting web server // Starting web server
serve.Start("blank") serve.Start()
// Update the hosts file // Update the hosts file
if daemon == false { if daemon == false {

View File

@ -3,11 +3,13 @@ package serve
import ( import (
"net/http" "net/http"
"jbreich/rhosts/cfg"
) )
func Start(certLoc string) { func Start() {
config := cfg.Create()
go httpServer() 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)) err = http.ListenAndServe("127.0.0.1:80", http.HandlerFunc(httpHandler))
return return
} }
func httpsServer(certLoc *string) (err error) { func httpsServer(certLoc string) (err error) {
err = http.ListenAndServeTLS("127.0.0.1:80", *certLoc+"ca.crt", *certLoc+"ca.key", http.HandlerFunc(httpHandler)) err = http.ListenAndServeTLS("127.0.0.1:80", certLoc+"ca.crt", certLoc+"ca.key", http.HandlerFunc(httpHandler))
return return
} }