2024-10-29 23:08:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
2024-11-08 15:00:17 +00:00
|
|
|
"errors"
|
2024-10-29 23:08:11 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-10-30 09:33:03 +00:00
|
|
|
"time"
|
2024-10-29 23:08:11 +00:00
|
|
|
|
2024-11-08 15:00:17 +00:00
|
|
|
"git.1e99.eu/1e99/passed/config"
|
2024-10-30 09:24:41 +00:00
|
|
|
"git.1e99.eu/1e99/passed/middlewares"
|
2024-10-29 23:08:11 +00:00
|
|
|
"git.1e99.eu/1e99/passed/routes"
|
|
|
|
"git.1e99.eu/1e99/passed/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed static/*
|
|
|
|
var embedFS embed.FS
|
|
|
|
|
|
|
|
func run() error {
|
2024-11-08 15:00:17 +00:00
|
|
|
storage, err := newStore()
|
2024-10-29 23:08:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-08 15:00:17 +00:00
|
|
|
var address string
|
|
|
|
var logRequests bool
|
|
|
|
var maxPasswordLength int
|
|
|
|
config.Env("PASSED_ADDRESS", &address, ":3000")
|
|
|
|
config.Env("PASSED_LOG_REQUESTS", &logRequests, "true")
|
|
|
|
config.Env("PASSED_MAX_LENGTH", &maxPasswordLength, "12288")
|
2024-10-29 23:21:25 +00:00
|
|
|
|
2024-10-29 23:08:11 +00:00
|
|
|
mux := http.NewServeMux()
|
2024-11-08 15:00:17 +00:00
|
|
|
handler := http.Handler(mux)
|
|
|
|
|
2024-10-29 23:08:11 +00:00
|
|
|
mux.Handle("GET /", routes.ServeFiles(embedFS, "static"))
|
2024-10-30 09:33:03 +00:00
|
|
|
mux.Handle(
|
|
|
|
"POST /api/password",
|
|
|
|
middlewares.RateLimiter(
|
2024-11-08 15:04:00 +00:00
|
|
|
routes.CreatePassword(storage, maxPasswordLength),
|
2024-10-30 09:33:03 +00:00
|
|
|
1*time.Minute,
|
|
|
|
5,
|
|
|
|
),
|
|
|
|
)
|
2024-11-08 15:04:00 +00:00
|
|
|
mux.Handle("GET /api/password/{id}", routes.GetPassword(storage))
|
2024-10-29 23:36:53 +00:00
|
|
|
mux.Handle("HEAD /api/password/{id}", routes.HasPassword(storage))
|
2024-10-29 23:08:11 +00:00
|
|
|
|
2024-11-08 15:00:17 +00:00
|
|
|
if logRequests {
|
|
|
|
handler = middlewares.Logger(handler)
|
2024-10-29 23:08:11 +00:00
|
|
|
}
|
|
|
|
|
2024-11-08 15:00:17 +00:00
|
|
|
log.Printf("Listening on %s.", address)
|
|
|
|
err = http.ListenAndServe(address, handler)
|
2024-10-29 23:08:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-08 15:00:17 +00:00
|
|
|
func newStore() (store storage.Store, err error) {
|
|
|
|
var storeType string
|
|
|
|
var clearInterval int
|
|
|
|
config.Env("PASSED_STORE", &storeType, "ram")
|
|
|
|
config.Env("PASSED_STORE_CLEAR_INTERVAL", &clearInterval, "30")
|
|
|
|
|
|
|
|
switch storeType {
|
|
|
|
case "ram":
|
|
|
|
store = storage.NewRamStore()
|
|
|
|
case "dir", "directory":
|
|
|
|
var path string
|
|
|
|
config.Env("PASSED_STORE_DIR_PATH", &path, "passwords")
|
|
|
|
|
|
|
|
store = storage.NewDirStore(path)
|
|
|
|
default:
|
|
|
|
err = errors.New("unknown storage type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
ticker := time.Tick(time.Duration(clearInterval) * time.Second)
|
|
|
|
for {
|
|
|
|
<-ticker
|
|
|
|
|
|
|
|
err := store.ClearExpired()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to clear expired passwords: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Cleared expired passwords.")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-29 23:08:11 +00:00
|
|
|
func main() {
|
|
|
|
err := run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%s", err)
|
|
|
|
}
|
|
|
|
}
|