mirror of
https://github.com/truewhile/MeBox.git
synced 2026-09-28 11:16:37 +08:00
1dbd73b30b
== New domain models (7) ==
- UserPermission: per-user feature toggles (13 booleans)
- StorageConfig: encrypted Alist/S3/WebDAV adapters
- LicenseKey + LicenseActivation: offline license issuance
- DownloadClient: multi-client downloader configs
- AssistantSession + AssistantMessage: multi-turn AI chat persistence
== New services (5) ==
- PermissionService: admin grants always-true; user defaults seeded
- StorageConfigService: AES-GCM encryption + per-type connection probe
- LicenseService: 24-char hyphenated key generation, activation/heartbeat
- DownloadClientService: qB/Aria2/Transmission CRUD + WebUI test
- AssistantService: chat history + execute/undo stubs (op_id tracking)
== Extended AIService.Chat ==
- Multi-turn LLM call with chat history; offline fallback reply
== New endpoints (60+) ==
Auth:
POST /auth/refresh, /auth/logout, /auth/change-password
PATCH /auth/profile
GET /auth/permissions, /auth/me
Permissions admin:
GET/PUT /admin/users/:id/permissions
POST /admin/users/:id/permissions/reset
Search:
GET /search, /search/advanced, /search/tmdb, /search/sites
System:
GET /system/config, /settings/schema, /system/events/ticket
POST /admin/system/scheduler/:name/trigger
Stats:
GET /stats/user/:id, /stats/top-users
POST /stats/play
Sites:
GET /sites/:id/resource, /sites/:id/userdata
Subscriptions:
PUT /subscriptions/:id, POST /subscriptions/:id/search
Playlists:
POST /playlists/:id/reorder
DELETE /playlists/:id/items/by-id/:item_id
DLNA per-renderer:
POST /dlna/:uuid/{play,pause,stop}, GET /dlna/:uuid/status
Media:
POST/DELETE /media/:id/favorite, GET /media/:id/favorite/status
POST /media/:id/ai-scrape, /media/scrape/test, /media/organize
GET /favorites (alias)
Playback:
GET /playback/:id/info, /playback/:id/external-players, /playback/:id/external-url
POST /playback/:id/progress
GET /playback/transcode/:job_id/status
Downloads:
POST /download/:id/{pause,resume,organize}
POST /download/{organize,sync,start-auto-sync}
GET /download/tasks
Admin: full CRUD on /admin/download/clients + /admin/download/aria2/stats
License:
POST /license/{activate,heartbeat}
GET /license/{status,heartbeat-status}
Admin: /admin/license/{generate,list,:id/activations,:id/revoke,activation/:id/unbind}
Storage:
GET /admin/storage/{status,:type}
PUT /admin/storage/:type, POST /admin/storage/:type/test
Assistant (multi-turn AI):
GET/POST /admin/assistant/sessions
GET/DELETE /admin/assistant/session/:id
POST /admin/assistant/{chat,execute}
POST /admin/assistant/undo/:op_id
GET /admin/assistant/history
== New React pages (4) ==
- AssistantChatPage (/assistant): full multi-turn chat UI with sessions
sidebar, optimistic user-turn append, live AI response.
- DownloadClientsPage (/download-clients): typed CRUD form for
qBittorrent / Aria2 / Transmission + per-row Test action.
- LicensePage (/license): generate keys, list activations, revoke,
unbind individual devices.
- StorageConfigPage (/storage-config): tabbed Alist/WebDAV/S3 form
with secret-aware redaction + connection probe.
== New API helpers (5) ==
- assistant, download_clients, license, permissions, storage_config
== Layout ==
- Sidebar gains 4 new admin links (AI 对话, 下载器, 外部存储, 许可证).
161 lines
4.5 KiB
Go
161 lines
4.5 KiB
Go
// Package service — license key management.
|
|
//
|
|
// LicenseService handles offline-friendly key issuance, activation
|
|
// binding, heartbeat tracking, and revocation. Keys are 24 random
|
|
// uppercase chars in groups of four (e.g. ABCD-1234-EFGH-5678-IJKL-90MN)
|
|
// — the same shape the Vue admin UI expects.
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/ShukeBta/MediaStationGo/internal/model"
|
|
"github.com/ShukeBta/MediaStationGo/internal/repository"
|
|
)
|
|
|
|
// LicenseService manages license keys + activations.
|
|
type LicenseService struct {
|
|
log *zap.Logger
|
|
repo *repository.Container
|
|
}
|
|
|
|
// NewLicenseService is the constructor.
|
|
func NewLicenseService(log *zap.Logger, repo *repository.Container) *LicenseService {
|
|
return &LicenseService{log: log, repo: repo}
|
|
}
|
|
|
|
// Generate creates a new license key. ExpiresAt nil means "perpetual".
|
|
func (s *LicenseService) Generate(
|
|
ctx context.Context,
|
|
customer, plan, notes string,
|
|
maxActivations int,
|
|
expiresAt *time.Time,
|
|
) (*model.LicenseKey, error) {
|
|
if maxActivations <= 0 {
|
|
maxActivations = 1
|
|
}
|
|
k := &model.LicenseKey{
|
|
Key: randomLicenseKey(),
|
|
Customer: strings.TrimSpace(customer),
|
|
Plan: strings.TrimSpace(plan),
|
|
MaxActivations: maxActivations,
|
|
Notes: strings.TrimSpace(notes),
|
|
IssuedAt: time.Now(),
|
|
ExpiresAt: expiresAt,
|
|
}
|
|
if err := s.repo.License.Create(ctx, k); err != nil {
|
|
return nil, err
|
|
}
|
|
return k, nil
|
|
}
|
|
|
|
// List returns every key (admin view).
|
|
func (s *LicenseService) List(ctx context.Context) ([]model.LicenseKey, error) {
|
|
return s.repo.License.List(ctx)
|
|
}
|
|
|
|
// Activate binds a key to a device. Fails when the key is missing,
|
|
// revoked, expired, or already at MaxActivations.
|
|
func (s *LicenseService) Activate(
|
|
ctx context.Context,
|
|
key, deviceID, deviceName, ip string,
|
|
) (*model.LicenseActivation, error) {
|
|
k, err := s.repo.License.FindByKey(ctx, key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if k == nil {
|
|
return nil, errors.New("invalid key")
|
|
}
|
|
if k.Revoked {
|
|
return nil, errors.New("key revoked")
|
|
}
|
|
if k.ExpiresAt != nil && k.ExpiresAt.Before(time.Now()) {
|
|
return nil, errors.New("key expired")
|
|
}
|
|
count, err := s.repo.License.CountActiveActivations(ctx, k.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if int(count) >= k.MaxActivations {
|
|
return nil, errors.New("activation limit reached")
|
|
}
|
|
a := &model.LicenseActivation{
|
|
KeyID: k.ID,
|
|
DeviceID: strings.TrimSpace(deviceID),
|
|
DeviceName: strings.TrimSpace(deviceName),
|
|
IP: ip,
|
|
}
|
|
if err := s.repo.License.AddActivation(ctx, a); err != nil {
|
|
return nil, err
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
// ListActivations returns activations for a single key.
|
|
func (s *LicenseService) ListActivations(ctx context.Context, keyID string) ([]model.LicenseActivation, error) {
|
|
return s.repo.License.ListActivations(ctx, keyID)
|
|
}
|
|
|
|
// Unbind marks one activation as released.
|
|
func (s *LicenseService) Unbind(ctx context.Context, activationID string) error {
|
|
return s.repo.License.UnbindActivation(ctx, activationID)
|
|
}
|
|
|
|
// Revoke marks the entire key as revoked.
|
|
func (s *LicenseService) Revoke(ctx context.Context, keyID string) error {
|
|
return s.repo.License.Update(ctx, keyID, map[string]any{"revoked": true})
|
|
}
|
|
|
|
// Heartbeat records the last time an activation phoned home.
|
|
func (s *LicenseService) Heartbeat(ctx context.Context, activationID string) error {
|
|
return s.repo.License.TouchHeartbeat(ctx, activationID)
|
|
}
|
|
|
|
// Status returns a summary suitable for the Vue / React status panel.
|
|
func (s *LicenseService) Status(ctx context.Context, keyID string) (map[string]any, error) {
|
|
k, err := s.repo.License.FindByID(ctx, keyID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if k == nil {
|
|
return nil, errors.New("key not found")
|
|
}
|
|
count, _ := s.repo.License.CountActiveActivations(ctx, keyID)
|
|
valid := !k.Revoked
|
|
if k.ExpiresAt != nil && k.ExpiresAt.Before(time.Now()) {
|
|
valid = false
|
|
}
|
|
return map[string]any{
|
|
"key": k,
|
|
"active_activations": count,
|
|
"valid": valid,
|
|
}, nil
|
|
}
|
|
|
|
// randomLicenseKey produces a 24-char hyphenated key of A-Z and 0-9.
|
|
func randomLicenseKey() string {
|
|
const alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789" // omit confusables
|
|
out := make([]byte, 24)
|
|
buf := make([]byte, 24)
|
|
_, _ = rand.Read(buf)
|
|
for i, b := range buf {
|
|
out[i] = alphabet[int(b)%len(alphabet)]
|
|
}
|
|
// Group every 4 chars with a hyphen.
|
|
var sb strings.Builder
|
|
for i, c := range out {
|
|
if i > 0 && i%4 == 0 {
|
|
sb.WriteByte('-')
|
|
}
|
|
sb.WriteByte(byte(c))
|
|
}
|
|
return sb.String()
|
|
}
|