This commit is contained in:
sfigato 2020-11-18 19:25:51 +01:00
commit cca4d2bd89
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F
3 changed files with 110 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.abbiamoundominio.org/blallo/openportal
go 1.15

0
go.sum Normal file
View File

107
main.go Normal file
View File

@ -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, `<html>
<head>
<title>OpenPOD Management</title>
</head>
<body>
<h1>OpenPOD Management</h1>
<div>
<form action="/cmd" method="post">
<label for="version_tag">New version:</label>
<input type="text" id="version_tag" name="version_tag">
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>`)
}
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))
}