2024-05-04 01:30:04 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-05-04 01:39:28 +02:00
|
|
|
"fmt"
|
2024-05-04 01:30:04 +02:00
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2024-05-04 02:07:20 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/dgrijalva/jwt-go"
|
2024-05-04 01:30:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-05-04 01:51:52 +02:00
|
|
|
password = "hardcodedpassword"
|
|
|
|
lootPath = "Loot"
|
|
|
|
sessionCookieName = "auth_session"
|
2024-05-04 02:07:20 +02:00
|
|
|
secretKey = []byte("key"))
|
2024-05-04 01:30:04 +02:00
|
|
|
|
|
|
|
type PageData struct {
|
|
|
|
UIDs []string
|
|
|
|
Files []string
|
|
|
|
}
|
|
|
|
|
2024-05-04 02:07:20 +02:00
|
|
|
type Claims struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
jwt.StandardClaims
|
|
|
|
}
|
|
|
|
|
2024-05-04 01:30:04 +02:00
|
|
|
func main() {
|
2024-05-04 01:39:28 +02:00
|
|
|
http.HandleFunc("/", logMiddleware(loginHandler))
|
|
|
|
http.HandleFunc("/loot", logMiddleware(lootHandler))
|
|
|
|
http.HandleFunc("/logout", logMiddleware(logoutHandler))
|
|
|
|
http.HandleFunc("/files/", logMiddleware(fileHandler))
|
2024-05-04 01:30:04 +02:00
|
|
|
|
2024-05-04 01:39:28 +02:00
|
|
|
log.Fatal(http.ListenAndServe(":5647", nil))
|
|
|
|
fmt.Println("Server started")
|
|
|
|
}
|
|
|
|
|
|
|
|
func logMiddleware(next http.HandlerFunc) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Printf("[%s] %s %s\n", r.Method, r.RemoteAddr, r.URL.Path)
|
|
|
|
next(w, r)
|
|
|
|
}
|
2024-05-04 01:30:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func loginHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
renderTemplate(w, "login.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.FormValue("password") == password {
|
2024-05-04 02:07:20 +02:00
|
|
|
expirationTime := time.Now().Add(1 * time.Hour)
|
|
|
|
claims := &Claims{
|
|
|
|
Username: "root",
|
|
|
|
StandardClaims: jwt.StandardClaims{
|
|
|
|
ExpiresAt: expirationTime.Unix(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
|
tokenString, err := token.SignedString(secretKey)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-04 01:51:52 +02:00
|
|
|
http.SetCookie(w, &http.Cookie{
|
2024-05-04 02:07:20 +02:00
|
|
|
Name: sessionCookieName,
|
|
|
|
Value: tokenString,
|
|
|
|
Expires: expirationTime,
|
|
|
|
Path: "/",
|
2024-05-04 01:51:52 +02:00
|
|
|
})
|
2024-05-04 02:07:20 +02:00
|
|
|
|
2024-05-04 01:30:04 +02:00
|
|
|
http.Redirect(w, r, "/loot", http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
renderTemplate(w, "login.html", "Incorrect password")
|
|
|
|
}
|
|
|
|
|
|
|
|
func lootHandler(w http.ResponseWriter, r *http.Request) {
|
2024-05-04 01:51:52 +02:00
|
|
|
checkAuth(w, r)
|
2024-05-04 01:30:04 +02:00
|
|
|
|
|
|
|
uids, err := getDeviceUIDs()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data := PageData{UIDs: uids}
|
|
|
|
renderTemplate(w, "loot.html", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fileHandler(w http.ResponseWriter, r *http.Request) {
|
2024-05-04 01:51:52 +02:00
|
|
|
checkAuth(w, r)
|
|
|
|
|
2024-05-04 01:30:04 +02:00
|
|
|
requestedPath := strings.TrimPrefix(r.URL.Path, "/files/")
|
|
|
|
filePath := filepath.Join(lootPath, requestedPath)
|
|
|
|
http.ServeFile(w, r, filePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func logoutHandler(w http.ResponseWriter, r *http.Request) {
|
2024-05-04 01:51:52 +02:00
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: sessionCookieName,
|
|
|
|
Value: "",
|
|
|
|
Path: "/",
|
|
|
|
MaxAge: -1,
|
|
|
|
})
|
2024-05-04 01:30:04 +02:00
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
|
|
|
|
t, err := template.ParseFiles(tmpl)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Execute(w, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isAuthenticated(r *http.Request) bool {
|
2024-05-04 01:51:52 +02:00
|
|
|
sessionCookie, err := r.Cookie(sessionCookieName)
|
2024-05-04 02:07:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenString := sessionCookie.Value
|
|
|
|
token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (interface{}, error) {
|
|
|
|
return secretKey, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil || !token.Valid {
|
2024-05-04 01:51:52 +02:00
|
|
|
return false
|
|
|
|
}
|
2024-05-04 02:07:20 +02:00
|
|
|
|
2024-05-04 01:30:04 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDeviceUIDs() ([]string, error) {
|
|
|
|
var uids []string
|
|
|
|
files, err := ioutil.ReadDir(lootPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() {
|
|
|
|
uids = append(uids, file.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return uids, nil
|
|
|
|
}
|
|
|
|
|
2024-05-04 01:51:52 +02:00
|
|
|
func checkAuth(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !isAuthenticated(r) {
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|