From 008a96b67e6e8c0bbfbd59839a30f082ffe1fad0 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 14 Nov 2010 12:12:45 -0500 Subject: [PATCH] Properly load the full config file and allow overrides from the flags. --- src/config.go | 9 +++++++++ src/main.go | 20 +++++++++++++++----- src/paths.go | 13 +++++++++---- src/server.go | 2 +- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/config.go b/src/config.go index 8a48d78..8b0a608 100644 --- a/src/config.go +++ b/src/config.go @@ -15,9 +15,18 @@ import ( ) type Configuration struct { + // The path to which all file operations are restricted. JailRoot string + + // The port on which the server back end runs. Port int + + // An array of URLs that the /proxy service will for which the back-end will + // forward GET requests and return the result. ProxyURLs []string + + // A map of usernames to MD5-encoded passwords that will be allowed to log in + // via a .htaccess style realm. Users map [string] string } diff --git a/src/main.go b/src/main.go index 6c63faf..2671e13 100644 --- a/src/main.go +++ b/src/main.go @@ -18,11 +18,13 @@ import ( ) func main() { + // Set up the basic flags. var configPath *string = flag.String("config", "~/.armadillo", "Path to the configuration file") - flag.StringVar(&paths.JailRoot, "jail", "/", "Restrict file operations to this directory root") - var port *int = flag.Int("port", 8080, "Port to run the server on") + var jailRoot *string = flag.String("jail", "", "Restrict file operations to this directory root") + var port *int = flag.Int("port", 0, "Port to run the server on") flag.Parse() + // Load the configuration file, if it is present. var configuration = new(config.Configuration) fmt.Printf("Reading configuration from %v\n", *configPath) if len(*configPath) > 0 { @@ -32,9 +34,17 @@ func main() { } } - configuration.JailRoot = paths.JailRoot - configuration.Port = *port + // Override configuration values with command line arguments. + if *jailRoot != "" { + configuration.JailRoot = *jailRoot + } + if *port != 0 { + configuration.Port = *port + } - fmt.Printf("Starting Armadillo on port %d with root:\n %v\n", *port, paths.JailRoot) + // Run the server. + fmt.Printf("Starting Armadillo on port %d with root:\n %v\n", + configuration.Port, configuration.JailRoot) + paths.SetConfig(configuration) server.RunBackEnd(configuration) } diff --git a/src/paths.go b/src/paths.go index 8125bf3..a589bb6 100644 --- a/src/paths.go +++ b/src/paths.go @@ -14,20 +14,25 @@ import ( "os" "path" "strings" + "./config" ) -var JailRoot string; +var gJailRoot string; + +func SetConfig(aConfig *config.Configuration) { + gJailRoot = aConfig.JailRoot +} func canonicalizePath(raw_path string) string { - raw_path = path.Join(JailRoot, raw_path) + raw_path = path.Join(gJailRoot, raw_path) return path.Clean(raw_path) } func checkInJail(the_path string) bool { - if len(the_path) < len(JailRoot) { + if len(the_path) < len(gJailRoot) { return false } - if the_path[0:len(JailRoot)] != JailRoot { + if the_path[0:len(gJailRoot)] != gJailRoot { return false } if strings.Index(the_path, "../") != -1 { diff --git a/src/server.go b/src/server.go index 4f500d0..6080914 100644 --- a/src/server.go +++ b/src/server.go @@ -128,7 +128,7 @@ func performProxy(url *http.URL, response *http.Conn, origRequest *http.Request) } func errorResponse(connection *http.Conn, message string) { - message = strings.Replace(message, paths.JailRoot, "/", -1) + message = strings.Replace(message, gConfig.JailRoot, "/", -1) response := map[string] string { "error" : "-1", "message" : message, -- 2.43.5