From 8772226e2e7a2b7d6fcd862ec5e14b4efb77ab39 Mon Sep 17 00:00:00 2001 From: Caroline Larimore Date: Tue, 21 Jan 2025 21:27:15 -0800 Subject: Initial Commit --- status.go | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 status.go (limited to 'status.go') diff --git a/status.go b/status.go new file mode 100644 index 0000000..c06c877 --- /dev/null +++ b/status.go @@ -0,0 +1,92 @@ +package main + +import ( + "bufio" + "io" + "log" + "net/http" + "os" + "os/user" + "strings" + + "golang.org/x/crypto/bcrypt" +) + +func main() { + var msg string = "" + var adminHash []byte + var userHash []byte + + user, err := user.Current() + if err != nil { + log.Fatal(err.Error()) + } + + file, err := os.Open(user.HomeDir + "/.status/auth") + if err != nil { + log.Fatal(err.Error()) + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + + if after, found := strings.CutPrefix(line, "admin:"); found { + adminHash = []byte(after) + } + + if after, found := strings.CutPrefix(line, "user:"); found { + userHash = []byte(after) + } + } + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + username, password, ok := r.BasicAuth() + if ok { + err := bcrypt.CompareHashAndPassword(userHash, []byte(username+password)) + if err != nil { + deny(w) + return + } + + w.Write([]byte(msg)) + return + } + + deny(w) + }) + + http.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) { + username, password, ok := r.BasicAuth() + if ok { + err := bcrypt.CompareHashAndPassword(adminHash, []byte(username+password)) + if err != nil { + deny(w) + return + } + + if r.Method == "POST" { + buf, err := io.ReadAll(r.Body) + if err != nil { + http.Error(w, "Failed to read request body", http.StatusInternalServerError) + return + } + + log.Print(string(buf)) + msg = string(buf) + w.Write([]byte("success")) + return + } + } + + deny(w) + }) + + log.Fatal(http.ListenAndServe(":8080", nil)) +} + +func deny(w http.ResponseWriter) { + w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`) + http.Error(w, "Unauthorized", http.StatusUnauthorized) +} -- cgit v1.2.3