1
0
Fork 0
mirror of https://git.sr.ht/~ashkeel/strimertul synced 2024-09-18 01:50:50 +00:00

feat: generate a random Kilovolt password on first start

This commit is contained in:
Ash Keel 2022-12-07 11:48:23 +01:00
parent fef4aebcb7
commit bb5aae0c1b
No known key found for this signature in database
GPG key ID: BAD8D93E7314ED3E

View file

@ -2,9 +2,12 @@ package http
import (
"context"
crand "crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io/fs"
mrand "math/rand"
"net/http"
"net/http/pprof"
@ -49,7 +52,7 @@ func NewServer(db *database.LocalDBClient, logger *zap.Logger) (*Server, error)
server.Config.Set(ServerConfig{
Bind: "localhost:4337",
EnableStaticServer: false,
KVPassword: "",
KVPassword: generatePassword(),
})
// Save
err = db.PutJSON(ServerConfigKey, server.Config.Get())
@ -202,3 +205,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
s.mux.ServeHTTP(w, r)
}
func generatePassword() string {
b := make([]byte, 21) // To prevent padding characters, keep it a multiple of 3
_, err := crand.Read(b)
if err != nil {
// fallback to bad rand, but this will never fail
mrand.Read(b)
}
return base64.URLEncoding.EncodeToString(b)
}