commit cca4d2bd894548c30adeb2e4cbca88f782c0117f Author: Blallo Date: Wed Nov 18 19:25:51 2020 +0100 Init diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9dd999c --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.abbiamoundominio.org/blallo/openportal + +go 1.15 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/main.go b/main.go new file mode 100644 index 0000000..0438ecb --- /dev/null +++ b/main.go @@ -0,0 +1,107 @@ +package main + +import ( + "errors" + "fmt" + //"html/template" + "log" + "net/http" +) + +var ( + Unauthorized = errors.New("unauthorized") + Unauthenticated = errors.New("unauthenticated") + UnknownAuth = errors.New("unknown authentication method") + USER = "DUMMY_USER" + PASSWORD = "DUMMY_PASSWORD" + STORE_SECRET = []byte("DUMMY_SECRET") + //dockerClient = +) + +func init() { +} + +func verifyAuthentication(r *http.Request) error { + user, password, ok := r.BasicAuth() + if !ok { + return Unauthenticated + } + if user != USER || password != PASSWORD { + return Unauthorized + } + return nil +} + +func loginMiddleware(w http.ResponseWriter, r *http.Request) error { + err := verifyAuthentication(r) + switch err { + case Unauthorized: + http.Error(w, fmt.Sprint(err), http.StatusUnauthorized) + ip := r.Header.Get("X-Forwarded-For") + if ip == "" { + ip = r.RemoteAddr + } + log.Print("Failed auth from: ", ip) + return Unauthorized + case Unauthenticated: + w.Header().Set("www-authenticate", "Basic realm=\"OPENPOD\"") + http.Error(w, fmt.Sprint(err), http.StatusUnauthorized) + return Unauthenticated + } + return nil +} + +func manageHandler(w http.ResponseWriter, r *http.Request) { + err := loginMiddleware(w, r) + if err != nil { + return + } + fmt.Fprint(w, ` + + OpenPOD Management + + +

OpenPOD Management

+
+
+ + + +
+
+ + `) +} + +func cmdHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + r.ParseForm() + + newVersion := r.FormValue("version_tag") + if newVersion == "" { + newVersion = "latest" + } + + err := updateOpenPODVersion(newVersion) + if err != nil { + http.Error(w, fmt.Sprint(err), http.StatusInternalServerError) + return + } + fmt.Fprint(w, "Success") +} + +func updateOpenPODVersion(newVersion string) error { + log.Println("New OpenPOD version:", newVersion) + return nil +} + +func main() { + http.HandleFunc("/", manageHandler) + http.HandleFunc("/cmd", cmdHandler) + + log.Println("Starting on :8080") + log.Fatal(http.ListenAndServe(":8080", nil)) +}