Init
This commit is contained in:
commit
cca4d2bd89
3
go.mod
Normal file
3
go.mod
Normal file
|
@ -0,0 +1,3 @@
|
|||
module git.abbiamoundominio.org/blallo/openportal
|
||||
|
||||
go 1.15
|
107
main.go
Normal file
107
main.go
Normal 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))
|
||||
}
|
Loading…
Reference in New Issue
Block a user