Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend-go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ require (
github.com/robfig/cron/v3 v3.0.1
github.com/shirou/gopsutil/v4 v4.26.6
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
golang.org/x/crypto v0.54.0
golang.org/x/crypto v0.56.0
golang.org/x/net v0.57.0
gopkg.in/yaml.v3 v3.0.1
modernc.org/sqlite v1.53.0
)
Expand All @@ -38,7 +39,6 @@ require (
github.com/x448/float16 v0.8.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
golang.org/x/net v0.57.0 // indirect
golang.org/x/sync v0.22.0 // indirect
golang.org/x/sys v0.47.0 // indirect
golang.org/x/tools v0.47.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions backend-go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zU
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
golang.org/x/crypto v0.54.0 h1:YLIA59K4fiNzHzjnZt2tUJQjQtUWfWbeHBqKtk3eScw=
golang.org/x/crypto v0.54.0/go.mod h1:KWL8ny2AZdGR2cWmzeHrp2azQPGogOv+HeQaVEXC2dk=
golang.org/x/crypto v0.56.0 h1:GUh5Ii4J5jtcseSMiRqr1jXCNHoxjeV9Fmekc2oLy6Y=
golang.org/x/crypto v0.56.0/go.mod h1:OMW5y6CY9l38uPLmxU6l6pwcXp1obtLo3e6gT7gQR2I=
golang.org/x/mod v0.37.0 h1:vF1DjpVEshcIqoEaauuHebaLk1O1forxjxBaVn884JQ=
golang.org/x/mod v0.37.0/go.mod h1:m8S8VeM9r4dzDwjrKO0a1sZP3YjeMamRRlD+fmR2Q/0=
golang.org/x/net v0.57.0 h1:K5+3DljvIuDG9/Jv9rvyMywYNFCQ9RSUY6OOTTkT+tE=
Expand Down
6 changes: 5 additions & 1 deletion backend-go/internal/subscription/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/iwvw/api-monitor/backend-go/internal/config"
"github.com/iwvw/api-monitor/backend-go/internal/secure"
"github.com/iwvw/api-monitor/backend-go/internal/subscriptionledger"
"gopkg.in/yaml.v3"
_ "modernc.org/sqlite"
)
Expand Down Expand Up @@ -2254,8 +2255,11 @@ func TestGetSubscriptionUsage(t *testing.T) {
VALUES('sub-u','sub_default_nodes','plan-u','UsageSub','tok-u','uuid-u','pwd-u',1,'2026-08-01 00:00:00','panel')`); err != nil {
t.Fatal(err)
}
// 用与生产代码一致的 CycleWindow 计算「当前周期」,避免硬编码日期
// 在跨月后使种子数据不再属于当前周期(cycle used 变为 0)。
cycleStart, cycleEnd := subscriptionledger.CycleWindow(time.Now().UTC(), "monthly", 1, "2026-08-01 00:00:00")
if _, err := db.Exec(`INSERT INTO subscription_usage_cycles(subscription_id,cycle_start,cycle_end,upload_bytes,download_bytes,updated_at)
VALUES('sub-u','2026-08-01T00:00:00Z','2026-09-01T00:00:00Z',10,20,datetime('now'))`); err != nil {
VALUES('sub-u',?,?,10,20,datetime('now'))`, cycleStart, cycleEnd); err != nil {
t.Fatal(err)
}
hour := time.Now().UTC().Truncate(time.Hour).Format(time.RFC3339)
Expand Down
46 changes: 41 additions & 5 deletions src/js/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,27 @@ const persistAskAIOpen = (open) => {
}
};

// 网关/反向代理出错时响应体可能为空或非 JSON,response.json() 会抛引擎级
// SyntaxError;向用户展示原始英文报错没有意义,统一转成可读提示。
const readJsonSafely = async (response) => {
try {
return await response.json();
} catch {
return null;
}
};

const toFriendlyAuthError = (error) => {
const message = String(error?.message || '');
if (
message.toLowerCase().includes('failed to fetch') ||
message.toLowerCase().includes('unexpected end of json input')
) {
return '无法连接服务器,请检查网络后重试';
}
return message || '网络验证失败';
};

const useStore = create((set, get) => ({
// --- 1. 认证状态 ---
isAuthenticated: false,
Expand Down Expand Up @@ -1024,8 +1045,8 @@ const useStore = create((set, get) => ({
// 1. 优先检查当前 Session 是否已认证;显式退出后不再信任残留 Cookie。
if (!explicitlyLoggedOut) {
const sessionRes = await fetch('/api/auth/session');
const { authenticated } = await sessionRes.json();
if (authenticated) {
const sessionData = await readJsonSafely(sessionRes);
if (sessionData?.authenticated) {
clearPendingAuthProvider();
set({ isAuthenticated: true, showLoginModal: false, isCheckingAuth: false });
return true;
Expand All @@ -1034,7 +1055,13 @@ const useStore = create((set, get) => ({

// 2. 如果 Session 不存在,再检查基本配置并尝试自动登录
const res = await fetch('/api/auth/check-password');
const { hasPassword, isDemoMode } = await res.json();
const checkData = await readJsonSafely(res);
if (checkData === null) {
// 后端不可达或响应非 JSON:回到登录页,避免误判为首次安装
set({ showLoginModal: true });
return false;
}
const { hasPassword, isDemoMode } = checkData;
set({ isDemoMode });

if (isDemoMode) {
Expand Down Expand Up @@ -1080,7 +1107,16 @@ const useStore = create((set, get) => ({
body: JSON.stringify(requestBody),
});

const result = await response.json();
const result = await readJsonSafely(response);

if (result === null) {
const errorMsg = response.ok ? '服务器响应异常,请稍后重试' : '无法连接服务器,请检查网络后重试';
set({ loginError: errorMsg });
if (!silent) {
toastManager.error(errorMsg);
}
return false;
}

if (response.status === 429) {
const errorMsg = result.error || '登录过于频繁,请稍后再试';
Expand Down Expand Up @@ -1132,7 +1168,7 @@ const useStore = create((set, get) => ({
return false;
}
} catch (error) {
const catchMsg = error.message || '网络验证失败';
const catchMsg = toFriendlyAuthError(error);
set({ loginError: catchMsg });
if (!silent) {
toastManager.error(catchMsg);
Expand Down