mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-20 07:05:24 +00:00
fix(proxies): preserve omitted expiry and fallback settings
This commit is contained in:
1 parent
772a0382f0
commit
38cfd7e2d9
8 files changed
+199
-37
No files matched your search
@@ -95,24 +95,27 @@ func (s *adminServiceImpl) UpdateProxy(ctx context.Context, id int64, input *Upd
|
||||
if input.BackupProxyID != nil && *input.BackupProxyID == id {
|
||||
return nil, infraerrors.BadRequest("PROXY_BACKUP_SELF", "backup proxy cannot be itself")
|
||||
}
|
||||
// 规范化 fallback_mode
|
||||
mode := input.FallbackMode
|
||||
if mode == "" {
|
||||
mode = FallbackModeNone
|
||||
}
|
||||
// 校验:mode=proxy 必须有 backup
|
||||
if mode == FallbackModeProxy && input.BackupProxyID == nil {
|
||||
return nil, infraerrors.BadRequest("PROXY_BACKUP_REQUIRED", "backup proxy required when fallback_mode=proxy")
|
||||
}
|
||||
if input.ExpiryWarnDays < 0 {
|
||||
return nil, infraerrors.BadRequest("PROXY_WARN_DAYS_INVALID", "expiry_warn_days must be >= 0")
|
||||
}
|
||||
|
||||
proxy, err := s.proxyRepo.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Merge only supplied fields, then validate the resulting fallback configuration.
|
||||
mode := proxy.FallbackMode
|
||||
if input.FallbackMode != "" {
|
||||
mode = input.FallbackMode
|
||||
}
|
||||
backupID := proxy.BackupProxyID
|
||||
if input.BackupProxyID != nil || input.ClearBackupID {
|
||||
backupID = input.BackupProxyID
|
||||
}
|
||||
if mode == FallbackModeProxy && backupID == nil {
|
||||
return nil, infraerrors.BadRequest("PROXY_BACKUP_REQUIRED", "backup proxy required when fallback_mode=proxy")
|
||||
}
|
||||
if input.ExpiryWarnDays != nil && *input.ExpiryWarnDays < 0 {
|
||||
return nil, infraerrors.BadRequest("PROXY_WARN_DAYS_INVALID", "expiry_warn_days must be >= 0")
|
||||
}
|
||||
|
||||
if input.Name != "" {
|
||||
proxy.Name = input.Name
|
||||
}
|
||||
@@ -134,11 +137,14 @@ func (s *adminServiceImpl) UpdateProxy(ctx context.Context, id int64, input *Upd
|
||||
if input.Status != "" {
|
||||
proxy.Status = input.Status
|
||||
}
|
||||
// 透传有效期与回退字段
|
||||
proxy.ExpiresAt = input.ExpiresAt
|
||||
if input.ExpiresAt != nil || input.ClearExpiresAt {
|
||||
proxy.ExpiresAt = input.ExpiresAt
|
||||
}
|
||||
proxy.FallbackMode = mode
|
||||
proxy.BackupProxyID = input.BackupProxyID
|
||||
proxy.ExpiryWarnDays = input.ExpiryWarnDays
|
||||
proxy.BackupProxyID = backupID
|
||||
if input.ExpiryWarnDays != nil {
|
||||
proxy.ExpiryWarnDays = *input.ExpiryWarnDays
|
||||
}
|
||||
|
||||
if err := s.proxyRepo.Update(ctx, proxy); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
//go:build unit
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAdminProxyPartialUpdatePreservesOmittedSettings(t *testing.T) {
|
||||
for _, input := range []*UpdateProxyInput{{Status: "inactive"}, {Name: "renamed"}, {Host: "new.example"}} {
|
||||
expiry := time.Now().Add(24 * time.Hour)
|
||||
backup := int64(10)
|
||||
original := &Proxy{ID: 9, Name: "original", Host: "old.example", Status: StatusActive, ExpiresAt: &expiry, FallbackMode: FallbackModeProxy, BackupProxyID: &backup, ExpiryWarnDays: 7}
|
||||
repo := &updatingProxyRepoStub{proxyRepoStub: &proxyRepoStub{}, proxy: original}
|
||||
svc := &adminServiceImpl{proxyRepo: repo}
|
||||
got, err := svc.UpdateProxy(context.Background(), 9, input)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, original.ExpiresAt, got.ExpiresAt)
|
||||
require.Equal(t, original.FallbackMode, got.FallbackMode)
|
||||
require.Equal(t, original.BackupProxyID, got.BackupProxyID)
|
||||
require.Equal(t, original.ExpiryWarnDays, got.ExpiryWarnDays)
|
||||
require.Equal(t, 1, repo.updateCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminProxyPartialUpdateClearsAndSetsSettings(t *testing.T) {
|
||||
expiry := time.Now().Add(time.Hour)
|
||||
backup := int64(10)
|
||||
zero := 0
|
||||
repo := &updatingProxyRepoStub{proxyRepoStub: &proxyRepoStub{}, proxy: &Proxy{ID: 9, ExpiresAt: &expiry, FallbackMode: FallbackModeProxy, BackupProxyID: &backup, ExpiryWarnDays: 7}}
|
||||
svc := &adminServiceImpl{proxyRepo: repo}
|
||||
got, err := svc.UpdateProxy(context.Background(), 9, &UpdateProxyInput{
|
||||
ClearExpiresAt: true, FallbackMode: FallbackModeNone, ClearBackupID: true, ExpiryWarnDays: &zero,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, got.ExpiresAt)
|
||||
require.Nil(t, got.BackupProxyID)
|
||||
require.Equal(t, FallbackModeNone, got.FallbackMode)
|
||||
require.Zero(t, got.ExpiryWarnDays)
|
||||
days := 3
|
||||
got, err = svc.UpdateProxy(context.Background(), 9, &UpdateProxyInput{
|
||||
ExpiresAt: &expiry, FallbackMode: FallbackModeProxy, BackupProxyID: &backup, ExpiryWarnDays: &days,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, &expiry, got.ExpiresAt)
|
||||
require.Equal(t, &backup, got.BackupProxyID)
|
||||
require.Equal(t, FallbackModeProxy, got.FallbackMode)
|
||||
require.Equal(t, days, got.ExpiryWarnDays)
|
||||
}
|
||||
|
||||
func TestAdminProxyPartialUpdateValidatesMergedFallback(t *testing.T) {
|
||||
backup := int64(10)
|
||||
self := int64(9)
|
||||
negative := -1
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
input UpdateProxyInput
|
||||
wantError bool
|
||||
}{
|
||||
{name: "reuse existing backup", input: UpdateProxyInput{FallbackMode: FallbackModeProxy}},
|
||||
{name: "clear required backup", input: UpdateProxyInput{ClearBackupID: true}, wantError: true},
|
||||
{name: "self backup", input: UpdateProxyInput{BackupProxyID: &self}, wantError: true},
|
||||
{name: "negative warning", input: UpdateProxyInput{ExpiryWarnDays: &negative}, wantError: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
repo := &updatingProxyRepoStub{proxyRepoStub: &proxyRepoStub{}, proxy: &Proxy{ID: 9, FallbackMode: FallbackModeProxy, BackupProxyID: &backup}}
|
||||
svc := &adminServiceImpl{proxyRepo: repo}
|
||||
_, err := svc.UpdateProxy(context.Background(), 9, &tc.input)
|
||||
if tc.wantError {
|
||||
require.Error(t, err)
|
||||
require.Zero(t, repo.updateCalls)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -534,6 +534,8 @@ type CreateProxyInput struct {
|
||||
ExpiryWarnDays int
|
||||
}
|
||||
|
||||
// UpdateProxyInput preserves omitted expiry/backup values; Clear flags explicitly
|
||||
// remove them. A nil ExpiryWarnDays preserves the current warning period.
|
||||
type UpdateProxyInput struct {
|
||||
Name string
|
||||
Protocol string
|
||||
@@ -543,9 +545,11 @@ type UpdateProxyInput struct {
|
||||
Password string
|
||||
Status string
|
||||
ExpiresAt *time.Time
|
||||
ClearExpiresAt bool
|
||||
FallbackMode string
|
||||
BackupProxyID *int64
|
||||
ExpiryWarnDays int
|
||||
ClearBackupID bool
|
||||
ExpiryWarnDays *int
|
||||
}
|
||||
|
||||
type GenerateRedeemCodesInput struct {
|
||||
|
||||
@@ -57,11 +57,12 @@ func TestBothProxyUpdateServicesUseRepositoryUpdateBoundary(t *testing.T) {
|
||||
},
|
||||
}
|
||||
svc := &adminServiceImpl{proxyRepo: repo}
|
||||
warnDays := 7
|
||||
|
||||
_, err := svc.UpdateProxy(context.Background(), 9, &UpdateProxyInput{
|
||||
Host: "new.example",
|
||||
FallbackMode: FallbackModeNone,
|
||||
ExpiryWarnDays: 7,
|
||||
ExpiryWarnDays: &warnDays,
|
||||
})
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in new issue
Block a user