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:
parent
fef4aebcb7
commit
bb5aae0c1b
1 changed files with 14 additions and 1 deletions
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue