mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-06 15:12:30 +09:00
Introduce a fallback polling transport and manager: add internal/transport (Manager + poll client) so the agent can fall back from WebSocket to REST long-poll when tunnels mangle WS frames. Wire the Manager into the Agent and expose transport mode in IPC status. Windows/service fixes: add service-scoped mutex to prevent concurrent service starts, probe IPC port on start to fail fast if occupied, and mirror main.Version into agent package at init. Fix spawned wizard visibility by setting HideWindow=false and add focus helpers that force foreground on Windows. UI/asset changes: enable DevTools by default (env to disable), inject a small JS shim to forward console errors/navigation readiness to Go logs, hide WebView host until bundle ready (with timeout), and wrap static assets with no-store headers to avoid stale cached bundles. Add local /local/status endpoint so the UI can detect registration when IPC is unreachable. Pairing/service handling: ensure service stop/start errors are surfaced, wait for service to reach RUNNING after pairing and surface helpful error messages if it doesn't. Implement platform-specific waitForServiceRunning and service control stubs. Auth/backend alignment: expand API key prefix from 8 to 12 chars to match backend panel expectations and avoid authentication mismatches. Misc: add focus_windows.go (Windows-only), various small refactors and logging improvements to make blank-page and pairing failures debuggable.
132 lines
3.8 KiB
Go
132 lines
3.8 KiB
Go
package auth
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// Authenticator handles HMAC-based authentication
|
|
type Authenticator struct {
|
|
agentID string
|
|
apiKey string
|
|
apiSecret string
|
|
}
|
|
|
|
// New creates a new Authenticator
|
|
func New(agentID, apiKey, apiSecret string) *Authenticator {
|
|
return &Authenticator{
|
|
agentID: agentID,
|
|
apiKey: apiKey,
|
|
apiSecret: apiSecret,
|
|
}
|
|
}
|
|
|
|
// SignMessage creates an HMAC signature for authentication
|
|
// The signature is computed as: HMAC-SHA256(agent_id:timestamp, api_secret)
|
|
func (a *Authenticator) SignMessage(timestamp int64) string {
|
|
message := fmt.Sprintf("%s:%d", a.agentID, timestamp)
|
|
return a.computeHMAC(message)
|
|
}
|
|
|
|
// SignMessageWithNonce creates an HMAC signature including a nonce for replay protection
|
|
// The signature is computed as: HMAC-SHA256(agent_id:timestamp:nonce, api_secret)
|
|
func (a *Authenticator) SignMessageWithNonce(timestamp int64, nonce string) string {
|
|
message := fmt.Sprintf("%s:%d:%s", a.agentID, timestamp, nonce)
|
|
return a.computeHMAC(message)
|
|
}
|
|
|
|
// UpdateCredentials updates the API credentials
|
|
func (a *Authenticator) UpdateCredentials(apiKey, apiSecret string) {
|
|
a.apiKey = apiKey
|
|
a.apiSecret = apiSecret
|
|
}
|
|
|
|
// GetAPIKey returns the full API key
|
|
func (a *Authenticator) GetAPIKey() string {
|
|
return a.apiKey
|
|
}
|
|
|
|
// GetAPISecret returns the API secret
|
|
func (a *Authenticator) GetAPISecret() string {
|
|
return a.apiSecret
|
|
}
|
|
|
|
// SignCommand creates an HMAC signature for a command
|
|
// Used to verify commands weren't tampered with
|
|
func (a *Authenticator) SignCommand(commandID string, action string, timestamp int64) string {
|
|
message := fmt.Sprintf("%s:%s:%d", commandID, action, timestamp)
|
|
return a.computeHMAC(message)
|
|
}
|
|
|
|
// VerifySignature verifies an HMAC signature
|
|
func (a *Authenticator) VerifySignature(message, signature string) bool {
|
|
expected := a.computeHMAC(message)
|
|
return hmac.Equal([]byte(expected), []byte(signature))
|
|
}
|
|
|
|
// VerifyTimestamp checks if a timestamp is within acceptable range
|
|
// Prevents replay attacks
|
|
func (a *Authenticator) VerifyTimestamp(timestamp int64, maxAgeSeconds int64) bool {
|
|
now := time.Now().UnixMilli()
|
|
diff := now - timestamp
|
|
if diff < 0 {
|
|
diff = -diff
|
|
}
|
|
return diff <= maxAgeSeconds*1000
|
|
}
|
|
|
|
// GetAPIKeyPrefix returns the first 12 characters of the API key for
|
|
// server-side identification. Must match the panel's storage width
|
|
// (backend/app/models/server.py: api_key_prefix = api_key[:12]) — the
|
|
// panel does direct string equality, so any drift between the two
|
|
// sides 401s every auth attempt. Was 8 here historically, which is a
|
|
// dormant bug only exposed once the polling fallback transport made
|
|
// auth failures visible (WS would just retry silently forever).
|
|
func (a *Authenticator) GetAPIKeyPrefix() string {
|
|
if len(a.apiKey) < 12 {
|
|
return a.apiKey
|
|
}
|
|
return a.apiKey[:12]
|
|
}
|
|
|
|
// AgentID returns the agent ID
|
|
func (a *Authenticator) AgentID() string {
|
|
return a.agentID
|
|
}
|
|
|
|
func (a *Authenticator) computeHMAC(message string) string {
|
|
h := hmac.New(sha256.New, []byte(a.apiSecret))
|
|
h.Write([]byte(message))
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
}
|
|
|
|
// GenerateNonce generates a cryptographically random nonce for request uniqueness
|
|
func GenerateNonce() string {
|
|
b := make([]byte, 16)
|
|
if _, err := rand.Read(b); err != nil {
|
|
// Fallback to time-based if random fails
|
|
return fmt.Sprintf("%d-%d", time.Now().UnixNano(), time.Now().UnixMilli())
|
|
}
|
|
return hex.EncodeToString(b)
|
|
}
|
|
|
|
// SessionToken represents an authenticated session
|
|
type SessionToken struct {
|
|
Token string
|
|
ExpiresAt time.Time
|
|
}
|
|
|
|
// IsExpired checks if the session token has expired
|
|
func (s *SessionToken) IsExpired() bool {
|
|
return time.Now().After(s.ExpiresAt)
|
|
}
|
|
|
|
// IsExpiringSoon checks if the token expires within the given duration
|
|
func (s *SessionToken) IsExpiringSoon(within time.Duration) bool {
|
|
return time.Now().Add(within).After(s.ExpiresAt)
|
|
}
|