189 lines
5.1 KiB
Go
189 lines
5.1 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type Config struct {
|
|
Job string `json:"job"`
|
|
Emails []string `json:"emails"`
|
|
}
|
|
|
|
func loadConfig() (*Config, error) {
|
|
data, err := os.ReadFile("data.json")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var c Config
|
|
return &c, json.Unmarshal(data, &c)
|
|
}
|
|
|
|
func saveConfig(c *Config) error {
|
|
data, err := json.MarshalIndent(c, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile("data.json", data, 0644)
|
|
}
|
|
|
|
// ---------- Main ----------
|
|
|
|
func main() {
|
|
|
|
err := godotenv.Load(".env")
|
|
if err != nil {
|
|
log.Fatal("Error loading .env file")
|
|
}
|
|
|
|
port := flag.String("port", "8081", "port to listen on")
|
|
flag.Parse()
|
|
|
|
fs := http.FileServer(http.Dir("static"))
|
|
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
|
|
|
http.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/index.html") })
|
|
http.HandleFunc("GET /infra", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/infra.html") })
|
|
http.HandleFunc("GET /finance", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/finance.html") })
|
|
http.HandleFunc("GET /cinema", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/cinema.html") })
|
|
http.HandleFunc("GET /engine", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/engine.html") })
|
|
http.HandleFunc("GET /42", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/42.html") })
|
|
http.HandleFunc("GET /secret", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/secret.html") })
|
|
http.HandleFunc("GET /favicon.ico", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/icon.png") })
|
|
http.HandleFunc("GET /login", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/login.html") })
|
|
|
|
http.HandleFunc("GET /admin", requireAuth(func(w http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(w, r, "./static/admin.html")
|
|
}))
|
|
|
|
http.HandleFunc("POST /newsletter/subscribe", func(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Email string `json:"email"`
|
|
}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&req)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
double := false
|
|
|
|
conf, err := loadConfig()
|
|
for _, e := range conf.Emails {
|
|
if e == req.Email {
|
|
double = true
|
|
}
|
|
}
|
|
if !double {
|
|
conf.Emails = append(conf.Emails, req.Email)
|
|
}
|
|
saveConfig(conf)
|
|
})
|
|
|
|
http.HandleFunc("GET /status", func(w http.ResponseWriter, r *http.Request) {
|
|
config, err := loadConfig()
|
|
if err != nil {
|
|
http.Error(w, "failed to load config", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
|
|
//<span ">samantha42.xyz</span>
|
|
|
|
if config.Job == "" || config.Job == "none" {
|
|
fmt.Fprint(w, `<span class="available" ><span class="dot dot--available"></span>available for work</span>`)
|
|
} else {
|
|
fmt.Fprintf(w, `<span class="busy" ><span class="dot dot--busy"></span>working at: <b>%s</b></span>`, config.Job)
|
|
}
|
|
})
|
|
|
|
http.HandleFunc("POST /login", func(w http.ResponseWriter, r *http.Request) {
|
|
r.ParseForm()
|
|
password := r.Form.Get("password")
|
|
auth := r.Form.Get("totp")
|
|
|
|
fmt.Println(os.Getenv("password"), os.Getenv("auth"))
|
|
if password == os.Getenv("password") && auth == os.Getenv("auth") {
|
|
fmt.Println("logged in")
|
|
token := generateSession()
|
|
|
|
sessionMu.Lock()
|
|
sessionStore[token] = time.Now().Add(12 * time.Hour)
|
|
sessionMu.Unlock()
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
Name: "session",
|
|
Value: token,
|
|
Path: "/",
|
|
HttpOnly: true,
|
|
Secure: true,
|
|
SameSite: http.SameSiteLaxMode,
|
|
Expires: time.Now().Add(12 * time.Hour),
|
|
})
|
|
|
|
// htmx reads this header and does the redirect client-side
|
|
w.Header().Set("HX-Redirect", "/admin")
|
|
w.WriteHeader(http.StatusOK)
|
|
} else {
|
|
// failure
|
|
w.WriteHeader(http.StatusOK)
|
|
fmt.Fprintf(w, `<div class="error-banner">// incorrect password or 2FA code</div>`)
|
|
}
|
|
})
|
|
|
|
//http.HandleFunc("/portfolio", Portfolio)
|
|
http.HandleFunc("/git", func(w http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(w, r, "https://git.samantha42.xyz", http.StatusFound)
|
|
})
|
|
|
|
fmt.Printf("running on http://localhost:%s/\n", *port)
|
|
if err := http.ListenAndServe(":"+*port, nil); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
var sessionStore = map[string]time.Time{}
|
|
var sessionMu sync.RWMutex
|
|
|
|
func generateSession() string {
|
|
b := make([]byte, 32)
|
|
rand.Read(b)
|
|
return hex.EncodeToString(b)
|
|
}
|
|
|
|
func requireAuth(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
cookie, err := r.Cookie("session")
|
|
if err != nil {
|
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
|
return
|
|
}
|
|
|
|
sessionMu.RLock()
|
|
expiry, ok := sessionStore[cookie.Value]
|
|
sessionMu.RUnlock()
|
|
|
|
if !ok || time.Now().After(expiry) {
|
|
sessionMu.Lock()
|
|
delete(sessionStore, cookie.Value)
|
|
sessionMu.Unlock()
|
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
|
return
|
|
}
|
|
|
|
next(w, r)
|
|
}
|
|
}
|