1 parent
8c4d463f1e
commit
de13d851a8
75 files changed
+4656
-442
No files matched your search
+48
-5
@@ -12,6 +12,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -32,14 +33,22 @@ type auth struct {
|
||||
}
|
||||
|
||||
func newAuth(cfg config.Config) (*auth, error) {
|
||||
p, err := os.ReadFile(cfg.PasswordFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
secret := cfg.Password
|
||||
if secret == "" {
|
||||
p, err := os.ReadFile(cfg.PasswordFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
secret = strings.TrimSpace(string(p))
|
||||
}
|
||||
secret := strings.TrimSpace(string(p))
|
||||
if len(secret) < 16 {
|
||||
return nil, errors.New("panel password must have at least 16 characters")
|
||||
}
|
||||
if cfg.SessionAuto {
|
||||
if err := ensureSessionKey(cfg.SessionKeyFile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
k, err := os.ReadFile(cfg.SessionKeyFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -51,7 +60,41 @@ func newAuth(cfg config.Config) (*auth, error) {
|
||||
if len(k) < 32 {
|
||||
return nil, errors.New("session key must contain at least 32 bytes")
|
||||
}
|
||||
return &auth{key: k, password: []byte(secret), username: cfg.Username, secure: cfg.CookieSecure, fail: make(map[string][]time.Time)}, nil
|
||||
// Derive the signing key from the persisted session secret and current
|
||||
// login credentials. Password/username changes invalidate old sessions.
|
||||
mac := hmac.New(sha256.New, k)
|
||||
mac.Write([]byte("easydns-ui-session-v1:" + cfg.Username + ":" + secret))
|
||||
return &auth{key: mac.Sum(nil), password: []byte(secret), username: cfg.Username, secure: cfg.CookieSecure, fail: make(map[string][]time.Time)}, nil
|
||||
}
|
||||
|
||||
// Creates the session secret only when explicitly enabled (Docker mode).
|
||||
// The same secret is reused across container recreations through a volume.
|
||||
func ensureSessionKey(path string) error {
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return nil
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
key := make([]byte, 32)
|
||||
if _, err := rand.Read(key); err != nil {
|
||||
return err
|
||||
}
|
||||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
|
||||
if errors.Is(err, os.ErrExist) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = f.WriteString(hex.EncodeToString(key) + "\n"); err != nil {
|
||||
f.Close()
|
||||
os.Remove(path)
|
||||
return err
|
||||
}
|
||||
return f.Close()
|
||||
}
|
||||
func bytesTrimSpace(v []byte) []byte { return []byte(strings.TrimSpace(string(v))) }
|
||||
func (a *auth) sign(v string) string {
|
||||
|
||||
Reference in new issue
Block a user