mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-11-16 09:34:22 +08:00
Compare commits
38 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d2645589da | |||
| 6684a3426b | |||
| 7a8595ec87 | |||
| 06f76d918f | |||
| feb357ff17 | |||
| 34581e0d18 | |||
| 61c5b5e759 | |||
| c8eb780c67 | |||
| 4bdfa50b0c | |||
| 21008ccb43 | |||
| 362d327bf9 | |||
| 9a650fcc8f | |||
| 804a5a29ea | |||
| 6189fe23f1 | |||
| 7ba90e8696 | |||
| 0eed74fe10 | |||
| 466cc65069 | |||
| 9c41410f17 | |||
| bc71c524e0 | |||
| f46b2b4d86 | |||
| 9f17d63cdc | |||
| 92f8ec47db | |||
| a97484cd9b | |||
| ee6bc8c50e | |||
| c94ab84ab9 | |||
| 84a03215f4 | |||
| def93749eb | |||
| 945a1c0dfc | |||
| d752e096a3 | |||
| 880a3b81b0 | |||
| 191aface2e | |||
| e121dadb90 | |||
| bafcfbde3a | |||
| 98c463c62b | |||
| daf34d0f6c | |||
| bb505d461d | |||
| 00be0ac31e | |||
| fc5624a190 |
@@ -1,12 +1,12 @@
|
|||||||
# PROJECT KNOWLEDGE BASE
|
# PROJECT KNOWLEDGE BASE
|
||||||
|
|
||||||
**Generated:** Thu Feb 19 2026
|
**Generated:** Thu Feb 26 2026
|
||||||
**Commit:** 137c34e
|
**Commit:** 21008cc
|
||||||
**Branch:** main
|
**Branch:** main
|
||||||
**Tag:** 2.1.4-rc2
|
**Tag:** 2.1.5-rc15
|
||||||
|
|
||||||
## OVERVIEW
|
## OVERVIEW
|
||||||
FLVX (formerly Flux Panel) is a traffic forwarding management system built on a forked GOST v3 stack. It ships as a Go-based admin API (SQLite) + Vite/React UI + Go forwarding agent, with optional mobile WebView wrappers.
|
FLVX (formerly Flux Panel) is a traffic forwarding management system built on a forked GOST v3 stack. It ships as a Go-based admin API (SQLite/PostgreSQL) + Vite/React UI + Go forwarding agent, with optional mobile WebView wrappers.
|
||||||
|
|
||||||
## STRUCTURE
|
## STRUCTURE
|
||||||
```
|
```
|
||||||
@@ -14,12 +14,14 @@ FLVX (formerly Flux Panel) is a traffic forwarding management system built on a
|
|||||||
├── go-gost/ # Go forwarding agent (forked gost + local x/)
|
├── go-gost/ # Go forwarding agent (forked gost + local x/)
|
||||||
│ └── x/ # Local fork of github.com/go-gost/x (replace => ./x)
|
│ └── x/ # Local fork of github.com/go-gost/x (replace => ./x)
|
||||||
├── go-backend/ # Go Admin API (GORM + SQLite/PostgreSQL, net/http)
|
├── go-backend/ # Go Admin API (GORM + SQLite/PostgreSQL, net/http)
|
||||||
|
│ └── tests/contract/ # Integration/contract tests
|
||||||
├── vite-frontend/ # React/Vite dashboard (shadcn bridge + Tailwind v4)
|
├── vite-frontend/ # React/Vite dashboard (shadcn bridge + Tailwind v4)
|
||||||
|
│ └── src/shadcn-bridge/heroui/ # HeroUI-compatible facade
|
||||||
├── docker-compose-v4.yml # Panel deploy (IPv4-only bridge)
|
├── docker-compose-v4.yml # Panel deploy (IPv4-only bridge)
|
||||||
├── docker-compose-v6.yml # Panel deploy (IPv6-enabled bridge)
|
├── docker-compose-v6.yml # Panel deploy (IPv6-enabled bridge)
|
||||||
├── panel_install.sh # Panel installer/upgrader (downloads compose)
|
├── panel_install.sh # Panel installer/upgrader (downloads compose)
|
||||||
├── install.sh # Node installer/upgrader (downloads gost binary)
|
├── install.sh # Node installer/upgrader (downloads gost binary)
|
||||||
└── .github/workflows/ # CI: build/push images + release artifacts
|
└── .github/workflows/ # CI: build/test + Docker push + release artifacts
|
||||||
```
|
```
|
||||||
|
|
||||||
## WHERE TO LOOK
|
## WHERE TO LOOK
|
||||||
@@ -29,12 +31,15 @@ FLVX (formerly Flux Panel) is a traffic forwarding management system built on a
|
|||||||
| **Deploy (IPv6)** | `docker-compose-v6.yml` | Same as v4 + IPv6-enabled bridge |
|
| **Deploy (IPv6)** | `docker-compose-v6.yml` | Same as v4 + IPv6-enabled bridge |
|
||||||
| **Panel install** | `panel_install.sh` | Picks v4/v6, generates `JWT_SECRET`, downloads compose |
|
| **Panel install** | `panel_install.sh` | Picks v4/v6, generates `JWT_SECRET`, downloads compose |
|
||||||
| **Node install** | `install.sh` | Installs `/etc/flux_agent/flux_agent` + writes `config.json`/`gost.json` + systemd `flux_agent.service` |
|
| **Node install** | `install.sh` | Installs `/etc/flux_agent/flux_agent` + writes `config.json`/`gost.json` + systemd `flux_agent.service` |
|
||||||
| **Admin API** | `go-backend/` | Go Admin API (SQLite) |
|
| **Admin API** | `go-backend/` | Go Admin API (SQLite/PostgreSQL) |
|
||||||
| **Web UI** | `vite-frontend/` | React/Vite dashboard (shadcn bridge + Tailwind v4) |
|
| **Web UI** | `vite-frontend/` | React/Vite dashboard (shadcn bridge + Tailwind v4) |
|
||||||
| **UI Compatibility** | `vite-frontend/src/shadcn-bridge/heroui/` | HeroUI-compatible API wrappers backed by shadcn/radix |
|
| **UI Compatibility** | `vite-frontend/src/shadcn-bridge/heroui/` | HeroUI-compatible API wrappers backed by shadcn/radix |
|
||||||
| **Theme Tokens** | `vite-frontend/src/styles/tailwind-theme.pcss` | Tailwind v4 `@theme inline` semantic color mapping |
|
| **Theme Tokens** | `vite-frontend/src/styles/tailwind-theme.pcss` | Tailwind v4 `@theme inline` semantic color mapping |
|
||||||
| **Go Agent** | `go-gost/` | Forwarding agent (forked gost + local x/) |
|
| **Go Agent** | `go-gost/` | Forwarding agent (forked gost + local x/) |
|
||||||
| **Go Core** | `go-gost/x/` | Handlers/listeners/dialers + management API |
|
| **Go Core** | `go-gost/x/` | Handlers/listeners/dialers + management API |
|
||||||
|
| **Repository Layer** | `go-backend/internal/store/repo/` | GORM data access (repository.go 83k LOC) |
|
||||||
|
| **Contract Tests** | `go-backend/tests/contract/` | Integration tests for auth, federation, tunnels |
|
||||||
|
| **CI Workflows** | `.github/workflows/` | ci-build.yml, docker-build.yml, deploy-docs.yml |
|
||||||
|
|
||||||
## CODE MAP
|
## CODE MAP
|
||||||
| Symbol | Type | Location | Role |
|
| Symbol | Type | Location | Role |
|
||||||
@@ -43,7 +48,9 @@ FLVX (formerly Flux Panel) is a traffic forwarding management system built on a
|
|||||||
| `main` | Func | `go-backend/cmd/paneld/main.go` | Backend Entry |
|
| `main` | Func | `go-backend/cmd/paneld/main.go` | Backend Entry |
|
||||||
| `App` | Component | `vite-frontend/src/App.tsx` | Frontend Entry |
|
| `App` | Component | `vite-frontend/src/App.tsx` | Frontend Entry |
|
||||||
| `main` | Func | `go-gost/main.go` | Agent Entry |
|
| `main` | Func | `go-gost/main.go` | Agent Entry |
|
||||||
|
| `Repository` | Struct | `go-backend/internal/store/repo/repository.go` | Data Access Layer |
|
||||||
|
| `Handler` | Struct | `go-backend/internal/http/handler/handler.go` | HTTP Handlers |
|
||||||
|
| `websocket_reporter` | Func | `go-gost/x/socket/websocket_reporter.go` | Panel Telemetry |
|
||||||
|
|
||||||
## CONVENTIONS
|
## CONVENTIONS
|
||||||
- **Auth**: `Authorization` header carries the raw JWT token (no `Bearer` prefix) between `vite-frontend/` and `go-backend/`.
|
- **Auth**: `Authorization` header carries the raw JWT token (no `Bearer` prefix) between `vite-frontend/` and `go-backend/`.
|
||||||
@@ -52,6 +59,7 @@ FLVX (formerly Flux Panel) is a traffic forwarding management system built on a
|
|||||||
- **API Envelope**: All REST responses follow `{code, msg, data, ts}` structure (code 0 = success).
|
- **API Envelope**: All REST responses follow `{code, msg, data, ts}` structure (code 0 = success).
|
||||||
- **Frontend UI Layer**: Import UI primitives from `src/shadcn-bridge/heroui/*` (legacy-compatible facade), not direct `@heroui/*` packages.
|
- **Frontend UI Layer**: Import UI primitives from `src/shadcn-bridge/heroui/*` (legacy-compatible facade), not direct `@heroui/*` packages.
|
||||||
- **Tailwind v4 Semantic Colors**: `src/styles/globals.css` must import `src/styles/tailwind-theme.pcss`; removing it breaks semantic classes like `bg-primary`, `text-foreground`, and `border-input`.
|
- **Tailwind v4 Semantic Colors**: `src/styles/globals.css` must import `src/styles/tailwind-theme.pcss`; removing it breaks semantic classes like `bg-primary`, `text-foreground`, and `border-input`.
|
||||||
|
- **Go Versions**: `go-backend` uses Go 1.24, `go-gost` uses Go 1.23, `go-gost/x` uses Go 1.22.
|
||||||
|
|
||||||
## ANTI-PATTERNS (THIS PROJECT)
|
## ANTI-PATTERNS (THIS PROJECT)
|
||||||
- **DO NOT EDIT** generated protobuf output: `go-gost/x/internal/util/grpc/proto/*.pb.go`, `go-gost/x/internal/util/grpc/proto/*_grpc.pb.go`.
|
- **DO NOT EDIT** generated protobuf output: `go-gost/x/internal/util/grpc/proto/*.pb.go`, `go-gost/x/internal/util/grpc/proto/*_grpc.pb.go`.
|
||||||
@@ -60,6 +68,8 @@ FLVX (formerly Flux Panel) is a traffic forwarding management system built on a
|
|||||||
- **DO NOT** let backend handlers call `repo.DB()` directly — add a Repository method instead.
|
- **DO NOT** let backend handlers call `repo.DB()` directly — add a Repository method instead.
|
||||||
- **DO NOT ADD** frontend tests - project has no test infrastructure (Vitest/Jest not configured).
|
- **DO NOT ADD** frontend tests - project has no test infrastructure (Vitest/Jest not configured).
|
||||||
- **DO NOT REINTRODUCE** `@heroui/*` or `@nextui-org/*` dependencies; migration is now shadcn bridge-based.
|
- **DO NOT REINTRODUCE** `@heroui/*` or `@nextui-org/*` dependencies; migration is now shadcn bridge-based.
|
||||||
|
- **DO NOT** use `type:jsonb` or `type:serial` in GORM tags (SQLite incompatible).
|
||||||
|
- **DO NOT** omit `TableName()` on new models — GORM pluralizes by default.
|
||||||
|
|
||||||
## COMMANDS
|
## COMMANDS
|
||||||
```bash
|
```bash
|
||||||
@@ -75,15 +85,21 @@ docker compose -f docker-compose-v6.yml up -d
|
|||||||
(cd go-backend && make build)
|
(cd go-backend && make build)
|
||||||
(cd vite-frontend && npm run dev)
|
(cd vite-frontend && npm run dev)
|
||||||
(cd go-gost && go run .)
|
(cd go-gost && go run .)
|
||||||
|
|
||||||
|
# Testing
|
||||||
|
(cd go-backend && go test ./...)
|
||||||
|
(cd go-backend && go test ./tests/contract/...)
|
||||||
```
|
```
|
||||||
|
|
||||||
## UNIQUE STYLES
|
## UNIQUE STYLES
|
||||||
- **Flat Monorepo**: Language-prefixed dirs (`go-backend`, `go-gost`, `vite-frontend`) instead of `apps/`/`libs/`.
|
- **Flat Monorepo**: Language-prefixed dirs (`go-backend`, `go-gost`, `vite-frontend`) instead of `apps/`/`libs/`.
|
||||||
- **Asymmetric Go Layout**: `go-backend` follows `cmd/<app>/main.go` while `go-gost` uses `root/main.go`.
|
- **Asymmetric Go Layout**: `go-backend` follows `cmd/<app>/main.go` while `go-gost` uses `root/main.go`.
|
||||||
- **Frontend Hybrid Mode**: `App.tsx` detects "H5 mode" (mobile WebView) vs desktop, dictating layout strategy.
|
- **Frontend Hybrid Mode**: `App.tsx` detects "H5 mode" (mobile WebView) vs desktop, dictating layout strategy.
|
||||||
|
- **Experimental Bundler**: `vite-frontend` uses `rolldown-vite` (Rust-based) instead of standard Vite.
|
||||||
|
- **Non-minified Builds**: `vite.config.ts` sets `minify: false`, `treeshake: false` for debugging.
|
||||||
|
|
||||||
## NOTES
|
## NOTES
|
||||||
- LSP servers are not installed in this environment (gopls/jdtls/typescript-language-server); rely on grep-based navigation.
|
- LSP servers are not installed in this environment (gopls/typescript-language-server); rely on grep-based navigation.
|
||||||
- `vite-frontend/vite.config.ts` sets `minify: false` and disables treeshake; expect larger bundles.
|
- `vite-frontend/vite.config.ts` sets `minify: false` and disables treeshake; expect larger bundles.
|
||||||
- `vite-frontend` uses `rolldown-vite` (experimental Rust bundler) instead of standard Vite.
|
- `vite-frontend` uses `rolldown-vite` (experimental Rust bundler) instead of standard Vite.
|
||||||
- Install scripts (`install.sh`, `panel_install.sh`) self-delete after execution - common pattern in one-liner installs.
|
- Install scripts (`install.sh`, `panel_install.sh`) self-delete after execution - common pattern in one-liner installs.
|
||||||
@@ -91,7 +107,9 @@ docker compose -f docker-compose-v6.yml up -d
|
|||||||
- CI dynamically injects `PINNED_VERSION` into install scripts and docker-compose files during releases.
|
- CI dynamically injects `PINNED_VERSION` into install scripts and docker-compose files during releases.
|
||||||
- `panel_install.sh` auto-detects IPv6 and modifies `/etc/docker/daemon.json` to enable IPv6 bridge.
|
- `panel_install.sh` auto-detects IPv6 and modifies `/etc/docker/daemon.json` to enable IPv6 bridge.
|
||||||
- Download proxy `https://gcode.hostcentral.cc/` used for GitHub downloads in China/restricted environments.
|
- Download proxy `https://gcode.hostcentral.cc/` used for GitHub downloads in China/restricted environments.
|
||||||
- Backend has contract tests in `go-backend/tests/contract/` - frontend has no test infrastructure (Vitest/Jest not configured).
|
- Backend has contract tests in `go-backend/tests/contract/` - frontend has no test infrastructure.
|
||||||
- `analysis/3x-ui/` contains a separate git repo for reference/comparison - not part of FLVX core.
|
- `analysis/3x-ui/` contains a separate git repo for reference/comparison - not part of FLVX core.
|
||||||
- PR `#144` (shadcn migration) and PR `#142` (user-group binding) are merged into `main`; release tag `2.1.4-rc2` points to commit `137c34e`.
|
- CI workflows: `ci-build.yml` (build check), `docker-build.yml` (multi-arch images + release), `deploy-docs.yml` (MkDocs).
|
||||||
- Button visual parity relies on `vite-frontend/src/shadcn-bridge/heroui/button.tsx` color mapping + `vite-frontend/src/styles/tailwind-theme.pcss` token export.
|
- PostgreSQL migration supported via `panel_install.sh` menu option using pgloader.
|
||||||
|
- Repository layer is large: `repository.go` (83k LOC), `repository_mutations.go` (43k LOC).
|
||||||
|
- Button visual parity relies on `vite-frontend/src/shadcn-bridge/heroui/button.tsx` color mapping + `vite-frontend/src/styles/tailwind-theme.pcss` token export.
|
||||||
@@ -0,0 +1,148 @@
|
|||||||
|
# 限速功能重构实施计划
|
||||||
|
|
||||||
|
## 一、需求概述
|
||||||
|
|
||||||
|
**原始需求**: 限速功能当前绑定到具体隧道,需要改为不绑定隧道,创建限速后可以自由在隧道上限速,也可以在转发上限速。
|
||||||
|
|
||||||
|
**核心变更**:
|
||||||
|
1. 限速规则(SpeedLimit)与隧道的绑定关系改为可选
|
||||||
|
2. 转发(Forward)支持独立的限速规则
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 二、实施计划清单
|
||||||
|
|
||||||
|
### 2.0 计划状态(审计更新:2026-02-26)
|
||||||
|
|
||||||
|
- 总体状态:**进行中(未验收通过)**
|
||||||
|
- 已完成:模型、仓储查询、限速 CRUD、控制面优先级、限速页与类型改造、编译与测试通过
|
||||||
|
- 未完成:**Forward 独立限速写入链路**(前端表单 -> API handler -> repository 落库 `forward.speed_id`)
|
||||||
|
|
||||||
|
### 2.1 后端模型层 (Model)
|
||||||
|
|
||||||
|
| 序号 | 任务 | 文件 | 状态 |
|
||||||
|
|------|------|------|------|
|
||||||
|
| M1 | SpeedLimit.TunnelID 改为 sql.NullInt64 (可空) | `go-backend/internal/store/model/model.go` | ✅ 完成 |
|
||||||
|
| M2 | SpeedLimit.TunnelName 改为 sql.NullString (可空) | `go-backend/internal/store/model/model.go` | ✅ 完成 |
|
||||||
|
| M3 | Forward 添加 SpeedID sql.NullInt64 字段 | `go-backend/internal/store/model/model.go` | ✅ 完成 |
|
||||||
|
| M4 | ForwardRecord 添加 SpeedID sql.NullInt64 字段 | `go-backend/internal/store/model/model.go` | ✅ 完成 |
|
||||||
|
| M5 | SpeedLimitBackup.TunnelID 改为指针类型 | `go-backend/internal/store/model/model.go` | ✅ 完成 |
|
||||||
|
| M6 | ForwardBackup 添加 SpeedID *int64 字段 | `go-backend/internal/store/model/model.go` | ✅ 完成 |
|
||||||
|
|
||||||
|
### 2.2 后端仓储层 (Repository)
|
||||||
|
|
||||||
|
| 序号 | 任务 | 文件 | 状态 |
|
||||||
|
|------|------|------|------|
|
||||||
|
| R1 | ListSpeedLimits() 返回可空 tunnelId/tunnelName | `go-backend/internal/store/repo/repository.go` | ✅ 完成 |
|
||||||
|
| R2 | ListForwards() 返回 speedId 字段 | `go-backend/internal/store/repo/repository.go` | ✅ 完成 |
|
||||||
|
| R3 | CreateSpeedLimit() 参数 tunnelID 改为 *int64 | `go-backend/internal/store/repo/repository_mutations.go` | ✅ 完成 |
|
||||||
|
| R4 | UpdateSpeedLimit() 参数 tunnelID 改为 *int64 | `go-backend/internal/store/repo/repository_mutations.go` | ✅ 完成 |
|
||||||
|
| R5 | GetSpeedLimitTunnelID() 返回 sql.NullInt64 | `go-backend/internal/store/repo/repository_mutations.go` | ✅ 完成 |
|
||||||
|
| R6 | exportSpeedLimits() 处理可空字段 | `go-backend/internal/store/repo/repository.go` | ✅ 完成 |
|
||||||
|
| R7 | importSpeedLimits() 处理可空字段 | `go-backend/internal/store/repo/repository.go` | ✅ 完成 |
|
||||||
|
| R8 | GetSpeedLimitSpeed() 新增方法 | `go-backend/internal/store/repo/repository_flow.go` | ✅ 完成 |
|
||||||
|
| R9 | ListForwardsByTunnel() 返回 SpeedID | `go-backend/internal/store/repo/repository_control.go` | ✅ 完成 |
|
||||||
|
| R10 | ListActiveForwardsByUser() 返回 SpeedID | `go-backend/internal/store/repo/repository_flow.go` | ✅ 完成 |
|
||||||
|
| R11 | ListActiveForwardsByUserTunnel() 返回 SpeedID | `go-backend/internal/store/repo/repository_flow.go` | ✅ 完成 |
|
||||||
|
| R12 | GetForwardRecord() 返回 SpeedID | `go-backend/internal/store/repo/repository_flow.go` | ✅ 完成 |
|
||||||
|
|
||||||
|
### 2.3 后端处理器层 (Handler)
|
||||||
|
|
||||||
|
| 序号 | 任务 | 文件 | 状态 |
|
||||||
|
|------|------|------|------|
|
||||||
|
| H1 | speedLimitCreate 处理可选 tunnelId | `go-backend/internal/http/handler/mutations.go` | ✅ 完成 |
|
||||||
|
| H2 | speedLimitUpdate 处理可选 tunnelId | `go-backend/internal/http/handler/mutations.go` | ✅ 完成 |
|
||||||
|
| H3 | speedLimitDelete 处理可空 tunnelID | `go-backend/internal/http/handler/mutations.go` | ✅ 完成 |
|
||||||
|
|
||||||
|
### 2.4 后端控制平面 (Control Plane)
|
||||||
|
|
||||||
|
| 序号 | 任务 | 文件 | 状态 |
|
||||||
|
|------|------|------|------|
|
||||||
|
| C1 | syncForwardServices 优先使用 Forward.SpeedID | `go-backend/internal/http/handler/control_plane.go` | ✅ 完成 |
|
||||||
|
| C2 | 回退到 UserTunnel 的 speed limit | `go-backend/internal/http/handler/control_plane.go` | ✅ 完成 |
|
||||||
|
|
||||||
|
### 2.5 前端类型定义 (TypeScript Types)
|
||||||
|
|
||||||
|
| 序号 | 任务 | 文件 | 状态 |
|
||||||
|
|------|------|------|------|
|
||||||
|
| T1 | SpeedLimitApiItem.tunnelId 改为可选 | `vite-frontend/src/api/types.ts` | ✅ 完成 |
|
||||||
|
| T2 | ForwardApiItem 添加 speedId 字段 | `vite-frontend/src/api/types.ts` | ✅ 完成 |
|
||||||
|
| T3 | ForwardMutationPayload 添加 speedId 字段 | `vite-frontend/src/api/types.ts` | ✅ 完成 |
|
||||||
|
| T4 | SpeedLimitMutationPayload.tunnelId 改为可选 | `vite-frontend/src/api/types.ts` | ✅ 完成 |
|
||||||
|
|
||||||
|
### 2.6 前端页面组件
|
||||||
|
|
||||||
|
| 序号 | 任务 | 文件 | 状态 |
|
||||||
|
|------|------|------|------|
|
||||||
|
| F1 | SpeedLimitRule 接口更新 | `vite-frontend/src/pages/limit.tsx` | ✅ 完成 |
|
||||||
|
| F2 | SpeedLimitForm 接口更新 | `vite-frontend/src/pages/limit.tsx` | ✅ 完成 |
|
||||||
|
| F3 | validateForm 移除 tunnelId 必填校验 | `vite-frontend/src/pages/limit.tsx` | ✅ 完成 |
|
||||||
|
| F4 | Select 组件改为可选 | `vite-frontend/src/pages/limit.tsx` | ✅ 完成 |
|
||||||
|
| F5 | 显示"未绑定"状态 | `vite-frontend/src/pages/limit.tsx` | ✅ 完成 |
|
||||||
|
|
||||||
|
### 2.7 编译验证
|
||||||
|
|
||||||
|
| 序号 | 任务 | 状态 |
|
||||||
|
|------|------|------|
|
||||||
|
| B1 | Go 后端编译通过 | ✅ 完成 |
|
||||||
|
| B2 | TypeScript 类型检查通过 | ✅ 完成 |
|
||||||
|
| B3 | `go test ./...` 全量通过 | ✅ 完成 |
|
||||||
|
| B4 | `go test ./tests/contract/... -run SpeedLimit` 通过 | ✅ 完成 |
|
||||||
|
|
||||||
|
### 2.8 Forward 独立限速写入链路补全(新增)
|
||||||
|
|
||||||
|
| 序号 | 任务 | 文件 | 状态 |
|
||||||
|
|------|------|------|------|
|
||||||
|
| N1 | forwardCreate 支持接收并校验可选 speedId,写入 Forward.SpeedID | `go-backend/internal/http/handler/mutations.go` | ✅ 完成 |
|
||||||
|
| N2 | forwardUpdate 支持更新/清空 speedId,并触发服务重下发 | `go-backend/internal/http/handler/mutations.go` | ✅ 完成 |
|
||||||
|
| N3 | CreateForwardTx 支持落库 speed_id | `go-backend/internal/store/repo/repository_mutations.go` | ✅ 完成 |
|
||||||
|
| N4 | UpdateForward 支持更新 speed_id | `go-backend/internal/store/repo/repository_mutations.go` | ✅ 完成 |
|
||||||
|
| N5 | Forward 页面新增限速选择并透传 speedId | `vite-frontend/src/pages/forward.tsx` | ✅ 完成 |
|
||||||
|
| N6 | Forward 相关契约测试补充 speedId 写入/清空断言 | `go-backend/tests/contract/forward_contract_test.go` | ✅ 完成 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 三、优先级说明
|
||||||
|
|
||||||
|
限速规则应用优先级:
|
||||||
|
1. **Forward.SpeedID** - 转发级别的限速 (最高优先)
|
||||||
|
2. **UserTunnel.SpeedID** - 用户隧道权限级别的限速 (回退)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 四、数据库兼容性
|
||||||
|
|
||||||
|
- SpeedLimit 表: `tunnel_id` 和 `tunnel_name` 字段改为可空 (GORM AutoMigrate 自动处理)
|
||||||
|
- Forward 表: 新增 `speed_id` 可空字段 (GORM AutoMigrate 自动处理)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 五、验证检查项
|
||||||
|
|
||||||
|
### 5.1 功能验证(审计后)
|
||||||
|
|
||||||
|
- [x] 创建不限速规则的限速 (不绑定隧道)
|
||||||
|
- [x] 创建绑定隧道的限速 (兼容旧逻辑)
|
||||||
|
- [x] 编辑限速规则,切换隧道绑定状态
|
||||||
|
- [ ] 删除限速规则
|
||||||
|
- [ ] 转发列表正确显示 speedId
|
||||||
|
|
||||||
|
### 5.2 API 验证(审计后)
|
||||||
|
|
||||||
|
- [x] GET /api/speed-limit/list 返回可选 tunnelId
|
||||||
|
- [x] POST /api/speed-limit/create 接受可选 tunnelId
|
||||||
|
- [x] POST /api/speed-limit/update 接受可选 tunnelId
|
||||||
|
- [ ] GET /api/forward/list 返回 speedId
|
||||||
|
|
||||||
|
### 5.3 兼容性验证(审计后)
|
||||||
|
|
||||||
|
- [x] 现有绑定隧道的限速规则继续正常工作
|
||||||
|
- [ ] 现有 UserTunnel 的限速继续正常工作
|
||||||
|
- [ ] 备份/恢复功能正常
|
||||||
|
|
||||||
|
### 5.4 Forward 独立限速闭环验证(新增)
|
||||||
|
|
||||||
|
- [x] POST /api/forward/create 接受 speedId 并写入 `forward.speed_id`
|
||||||
|
- [x] POST /api/forward/update 可更新/清空 speedId
|
||||||
|
- [x] Forward 表单可选择限速并提交 speedId
|
||||||
|
- [ ] `syncForwardServices` 实际使用 Forward.SpeedID 而非仅回退 UserTunnel.SpeedID
|
||||||
+12
-8
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
## OVERVIEW
|
## OVERVIEW
|
||||||
Go-based Admin API for FLVX. Replaced legacy Spring Boot backend.
|
Go-based Admin API for FLVX. Replaced legacy Spring Boot backend.
|
||||||
**Stack:** Go 1.23, net/http (std lib), GORM + SQLite/PostgreSQL (glebarez/sqlite - CGO-free).
|
**Stack:** Go 1.24, net/http (std lib), GORM + SQLite/PostgreSQL (glebarez/sqlite - CGO-free).
|
||||||
|
|
||||||
## STRUCTURE
|
## STRUCTURE
|
||||||
```
|
```
|
||||||
@@ -17,14 +17,15 @@ go-backend/
|
|||||||
│ ├── store/
|
│ ├── store/
|
||||||
│ │ ├── model/model.go # GORM model structs (single source of truth)
|
│ │ ├── model/model.go # GORM model structs (single source of truth)
|
||||||
│ │ └── repo/ # Data Access Layer (Repository pattern, GORM)
|
│ │ └── repo/ # Data Access Layer (Repository pattern, GORM)
|
||||||
│ │ ├── repository.go # Core queries, Open/OpenPostgres, AutoMigrate
|
│ │ ├── repository.go # Core queries, Open/OpenPostgres, AutoMigrate (83k LOC)
|
||||||
│ │ ├── repository_mutations.go # Mutation helpers (user/node/tunnel/forward CRUD)
|
│ │ ├── repository_mutations.go # Mutation helpers (user/node/tunnel/forward CRUD, 43k LOC)
|
||||||
│ │ ├── repository_federation.go# Federation-specific queries
|
│ │ ├── repository_federation.go # Federation-specific queries
|
||||||
│ │ ├── repository_flow.go # Flow/forward status queries
|
│ │ ├── repository_flow.go # Flow/forward status queries
|
||||||
│ │ └── repository_control.go # Control plane queries
|
│ │ ├── repository_control.go # Control plane queries
|
||||||
|
│ │ └── repository_groups.go # Group management queries
|
||||||
│ └── auth/ # Auth logic
|
│ └── auth/ # Auth logic
|
||||||
├── tests/ # Integration/Contract tests
|
├── tests/contract/ # Integration/contract tests (14 tests)
|
||||||
├── Dockerfile # Multi-stage build (alpine)
|
├── Dockerfile # Multi-stage build (golang:1.24-bookworm → debian:bookworm-slim)
|
||||||
└── Makefile # Build commands
|
└── Makefile # Build commands
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -36,6 +37,7 @@ go-backend/
|
|||||||
| **Repository** | `go-backend/internal/store/repo/` | GORM-based queries, all DB ops encapsulated |
|
| **Repository** | `go-backend/internal/store/repo/` | GORM-based queries, all DB ops encapsulated |
|
||||||
| **Auth Middleware** | `go-backend/internal/http/middleware/jwt.go` | Extracts `Authorization` header |
|
| **Auth Middleware** | `go-backend/internal/http/middleware/jwt.go` | Extracts `Authorization` header |
|
||||||
| **WebSocket** | `go-backend/internal/ws/` | Real-time updates (traffic, status) |
|
| **WebSocket** | `go-backend/internal/ws/` | Real-time updates (traffic, status) |
|
||||||
|
| **Contract Tests** | `go-backend/tests/contract/` | Integration tests for auth, federation, tunnels |
|
||||||
|
|
||||||
## CONVENTIONS
|
## CONVENTIONS
|
||||||
- **GORM ORM**: Uses GORM with `glebarez/sqlite` (CGO-free) and `gorm.io/driver/postgres`.
|
- **GORM ORM**: Uses GORM with `glebarez/sqlite` (CGO-free) and `gorm.io/driver/postgres`.
|
||||||
@@ -47,6 +49,7 @@ go-backend/
|
|||||||
- **API Envelope**: All responses use `response.R{code, msg, data, ts}` structure.
|
- **API Envelope**: All responses use `response.R{code, msg, data, ts}` structure.
|
||||||
- **Config**: Loaded from environment variables (see `cmd/paneld/main.go`).
|
- **Config**: Loaded from environment variables (see `cmd/paneld/main.go`).
|
||||||
- **SQLite Constraints**: `MaxOpenConns(1)`, WAL mode, busy_timeout=5000.
|
- **SQLite Constraints**: `MaxOpenConns(1)`, WAL mode, busy_timeout=5000.
|
||||||
|
- **PostgreSQL**: Supported via `DB_TYPE=postgres` and `DATABASE_URL` env vars.
|
||||||
|
|
||||||
## ANTI-PATTERNS
|
## ANTI-PATTERNS
|
||||||
- **DO NOT** let handlers call `repo.DB()` directly — add a Repository method instead.
|
- **DO NOT** let handlers call `repo.DB()` directly — add a Repository method instead.
|
||||||
@@ -58,6 +61,7 @@ go-backend/
|
|||||||
```bash
|
```bash
|
||||||
cd go-backend
|
cd go-backend
|
||||||
go run ./cmd/paneld # Default: SERVER_ADDR=:6365
|
go run ./cmd/paneld # Default: SERVER_ADDR=:6365
|
||||||
go test ./...
|
go test ./... # Unit tests
|
||||||
|
go test ./tests/contract/... # Contract tests
|
||||||
make build
|
make build
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
# BACKEND HTTP HANDLER KNOWLEDGE BASE
|
# BACKEND HTTP HANDLER KNOWLEDGE BASE
|
||||||
|
|
||||||
**Generated:** Sun Feb 15 2026
|
**Generated:** Thu Feb 26 2026
|
||||||
|
|
||||||
## OVERVIEW
|
## OVERVIEW
|
||||||
HTTP request handlers for FLVX Admin API. Core business logic layer.
|
HTTP request handlers for FLVX Admin API. Core business logic layer.
|
||||||
**Stack:** Go 1.23, net/http, GORM via Repository pattern.
|
**Stack:** Go 1.24, net/http, GORM via Repository pattern.
|
||||||
|
|
||||||
## STRUCTURE
|
## STRUCTURE
|
||||||
```
|
```
|
||||||
@@ -14,7 +14,7 @@ handler/
|
|||||||
├── federation.go # Federation/cluster sync API
|
├── federation.go # Federation/cluster sync API
|
||||||
├── flow_policy.go # Traffic policy API
|
├── flow_policy.go # Traffic policy API
|
||||||
├── jobs.go # Background job management (sync, cleanup)
|
├── jobs.go # Background job management (sync, cleanup)
|
||||||
├── mutations.go # CRUD for users, tunnels, forwards (largest: 100k+ LOC)
|
├── mutations.go # CRUD for users, tunnels, forwards (~3700 LOC)
|
||||||
└── upgrade.go # System upgrade API
|
└── upgrade.go # System upgrade API
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -26,10 +26,11 @@ handler/
|
|||||||
| **Federation Sync** | `federation.go` | Panel-to-panel sync |
|
| **Federation Sync** | `federation.go` | Panel-to-panel sync |
|
||||||
| **Traffic Policies** | `flow_policy.go` | Flow limiting, quota management |
|
| **Traffic Policies** | `flow_policy.go` | Flow limiting, quota management |
|
||||||
| **Background Jobs** | `jobs.go` | Scheduled sync/cleanup tasks |
|
| **Background Jobs** | `jobs.go` | Scheduled sync/cleanup tasks |
|
||||||
|
| **Node Control** | `control_plane.go` | Node add/delete/list operations |
|
||||||
|
|
||||||
## CONVENTIONS
|
## CONVENTIONS
|
||||||
- Inherits from parent: GORM via Repository pattern, JWT in Authorization header.
|
- Inherits from parent: GORM via Repository pattern, JWT in Authorization header.
|
||||||
- Large files expected (`mutations.go` 3716 LOC - central mutation hub).
|
- Large files expected (`mutations.go` ~3700 LOC - central mutation hub).
|
||||||
- Uses `repo.Repository` for DB access via `h.repo.XXX()` methods.
|
- Uses `repo.Repository` for DB access via `h.repo.XXX()` methods.
|
||||||
- Handlers never call `repo.DB()` directly — all queries go through Repository methods.
|
- Handlers never call `repo.DB()` directly — all queries go through Repository methods.
|
||||||
- Domain-driven file split: one file per functional area (federation, jobs, etc.).
|
- Domain-driven file split: one file per functional area (federation, jobs, etc.).
|
||||||
|
|||||||
@@ -152,11 +152,32 @@ func (h *Handler) syncForwardServices(forward *forwardRecord, method string, all
|
|||||||
return errors.New("转发入口端口不存在")
|
return errors.New("转发入口端口不存在")
|
||||||
}
|
}
|
||||||
|
|
||||||
userTunnelID, limiterID, speed, err := h.resolveUserTunnelAndLimiter(forward.UserID, forward.TunnelID)
|
// Determine limiter from forward's SpeedID first, fallback to UserTunnel's limiter
|
||||||
if err != nil {
|
var limiterID *int64
|
||||||
return err
|
var speed *int
|
||||||
|
|
||||||
|
if forward.SpeedID.Valid && forward.SpeedID.Int64 > 0 {
|
||||||
|
// Forward has its own speed limit
|
||||||
|
speedVal, err := h.repo.GetSpeedLimitSpeed(forward.SpeedID.Int64)
|
||||||
|
if err == nil && speedVal > 0 {
|
||||||
|
limiterID = &forward.SpeedID.Int64
|
||||||
|
speed = &speedVal
|
||||||
|
}
|
||||||
}
|
}
|
||||||
serviceBase := buildForwardServiceBase(forward.ID, forward.UserID, userTunnelID)
|
|
||||||
|
if limiterID == nil {
|
||||||
|
// Fall back to UserTunnel speed limit
|
||||||
|
var utLimiterID *int64
|
||||||
|
var utSpeed *int
|
||||||
|
_, utLimiterID, utSpeed, err = h.resolveUserTunnelAndLimiter(forward.UserID, forward.TunnelID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
limiterID = utLimiterID
|
||||||
|
speed = utSpeed
|
||||||
|
}
|
||||||
|
|
||||||
|
serviceBase := buildForwardServiceBase(forward.ID, forward.UserID, 0)
|
||||||
tunnelTLSProtocol, err := h.isTunnelSelectedTLSProtocol(forward.TunnelID)
|
tunnelTLSProtocol, err := h.isTunnelSelectedTLSProtocol(forward.TunnelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -164,7 +185,9 @@ func (h *Handler) syncForwardServices(forward *forwardRecord, method string, all
|
|||||||
|
|
||||||
for _, fp := range ports {
|
for _, fp := range ports {
|
||||||
if limiterID != nil && speed != nil {
|
if limiterID != nil && speed != nil {
|
||||||
h.ensureLimiterOnNode(fp.NodeID, *limiterID, *speed)
|
if err := h.ensureLimiterOnNode(fp.NodeID, *limiterID, *speed); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
node, err := h.getNodeRecord(fp.NodeID)
|
node, err := h.getNodeRecord(fp.NodeID)
|
||||||
@@ -1030,12 +1053,16 @@ func (h *Handler) sendDeleteLimiterConfig(limiterID int64, tunnelID int64) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) ensureLimiterOnNode(nodeID int64, limiterID int64, speed int) {
|
func (h *Handler) ensureLimiterOnNode(nodeID int64, limiterID int64, speed int) error {
|
||||||
rate := float64(speed) / 8.0
|
rate := float64(speed) / 8.0
|
||||||
limitStr := fmt.Sprintf("$ %.1fMB %.1fMB", rate, rate)
|
limitStr := fmt.Sprintf("$ %.1fMB %.1fMB", rate, rate)
|
||||||
payload := map[string]interface{}{
|
payload := map[string]interface{}{
|
||||||
"name": strconv.FormatInt(limiterID, 10),
|
"name": strconv.FormatInt(limiterID, 10),
|
||||||
"limits": []string{limitStr},
|
"limits": []string{limitStr},
|
||||||
}
|
}
|
||||||
_, _ = h.sendNodeCommand(nodeID, "AddLimiters", payload, false, false)
|
if _, err := h.sendNodeCommand(nodeID, "AddLimiters", payload, false, false); err != nil {
|
||||||
|
return fmt.Errorf("限速规则下发失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -477,9 +477,14 @@ func (h *Handler) federationRemoteUsageList(w http.ResponseWriter, r *http.Reque
|
|||||||
response.WriteJSON(w, response.Err(-2, err.Error()))
|
response.WriteJSON(w, response.Err(-2, err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
forwardPortRows, err := h.repo.ListActiveForwardPortsForNode(nodeID)
|
||||||
|
if err != nil {
|
||||||
|
response.WriteJSON(w, response.Err(-2, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
usedSet := make(map[int]struct{})
|
usedSet := make(map[int]struct{})
|
||||||
bindings := make([]remoteUsageBindingItem, 0, len(bindingRows))
|
bindings := make([]remoteUsageBindingItem, 0, len(bindingRows)+len(forwardPortRows))
|
||||||
for _, b := range bindingRows {
|
for _, b := range bindingRows {
|
||||||
bindings = append(bindings, remoteUsageBindingItem{
|
bindings = append(bindings, remoteUsageBindingItem{
|
||||||
BindingID: b.ID,
|
BindingID: b.ID,
|
||||||
@@ -496,6 +501,29 @@ func (h *Handler) federationRemoteUsageList(w http.ResponseWriter, r *http.Reque
|
|||||||
usedSet[b.AllocatedPort] = struct{}{}
|
usedSet[b.AllocatedPort] = struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, fp := range forwardPortRows {
|
||||||
|
bindings = append(bindings, remoteUsageBindingItem{
|
||||||
|
BindingID: -fp.ForwardID,
|
||||||
|
TunnelID: fp.TunnelID,
|
||||||
|
TunnelName: fp.TunnelName,
|
||||||
|
ChainType: 1,
|
||||||
|
HopInx: 0,
|
||||||
|
AllocatedPort: fp.Port,
|
||||||
|
ResourceKey: fmt.Sprintf("forward:%d", fp.ForwardID),
|
||||||
|
RemoteBindingID: "",
|
||||||
|
UpdatedTime: fp.UpdatedTime,
|
||||||
|
})
|
||||||
|
if fp.Port > 0 {
|
||||||
|
usedSet[fp.Port] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(bindings, func(i, j int) bool {
|
||||||
|
if bindings[i].AllocatedPort == bindings[j].AllocatedPort {
|
||||||
|
return bindings[i].BindingID < bindings[j].BindingID
|
||||||
|
}
|
||||||
|
return bindings[i].AllocatedPort < bindings[j].AllocatedPort
|
||||||
|
})
|
||||||
|
|
||||||
usedPorts := make([]int, 0, len(usedSet))
|
usedPorts := make([]int, 0, len(usedSet))
|
||||||
for port := range usedSet {
|
for port := range usedSet {
|
||||||
@@ -766,6 +794,37 @@ func (h *Handler) federationTunnelCreate(w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
usedPorts, err := h.repo.ListUsedPortsOnNode(share.NodeID)
|
||||||
|
if err != nil {
|
||||||
|
response.WriteJSON(w, response.Err(-2, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, port := range usedPorts {
|
||||||
|
if port == req.RemotePort {
|
||||||
|
response.WriteJSON(w, response.Err(403, "Port already in use"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
runtimeOnPort, err := h.repo.GetActiveForwardPeerShareRuntimeByPort(share.ID, req.RemotePort)
|
||||||
|
if err != nil {
|
||||||
|
response.WriteJSON(w, response.Err(-2, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if runtimeOnPort != nil {
|
||||||
|
response.WriteJSON(w, response.Err(403, "Port already in use"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
existsOnNodePort, err := h.repo.ExistsActivePeerShareRuntimeOnNodePort(share.NodeID, req.RemotePort)
|
||||||
|
if err != nil {
|
||||||
|
response.WriteJSON(w, response.Err(-2, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if existsOnNodePort {
|
||||||
|
response.WriteJSON(w, response.Err(403, "Port already in use"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
now := time.Now().UnixMilli()
|
now := time.Now().UnixMilli()
|
||||||
tunnelID, err := h.repo.CreateFederationTunnel(
|
tunnelID, err := h.repo.CreateFederationTunnel(
|
||||||
fmt.Sprintf("Share-%d-Port-%d", share.ID, req.RemotePort),
|
fmt.Sprintf("Share-%d-Port-%d", share.ID, req.RemotePort),
|
||||||
@@ -780,6 +839,30 @@ func (h *Handler) federationTunnelCreate(w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runtime := &repo.PeerShareRuntime{
|
||||||
|
ShareID: share.ID,
|
||||||
|
NodeID: share.NodeID,
|
||||||
|
ReservationID: randomToken(24),
|
||||||
|
ResourceKey: fmt.Sprintf("federation-forward-%d-%d-%d", share.ID, tunnelID, req.RemotePort),
|
||||||
|
BindingID: "",
|
||||||
|
Role: "forward",
|
||||||
|
ChainName: "",
|
||||||
|
ServiceName: "",
|
||||||
|
Protocol: defaultString(req.Protocol, "tcp"),
|
||||||
|
Strategy: "fifo",
|
||||||
|
Port: req.RemotePort,
|
||||||
|
Target: strings.TrimSpace(req.Target),
|
||||||
|
Applied: 0,
|
||||||
|
Status: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}
|
||||||
|
if err := h.repo.CreatePeerShareRuntime(runtime); err != nil {
|
||||||
|
_ = h.deleteTunnelByID(tunnelID)
|
||||||
|
response.WriteJSON(w, response.Err(-2, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
h.wsServer.SendCommand(share.NodeID, "reload", nil, time.Second*5)
|
h.wsServer.SendCommand(share.NodeID, "reload", nil, time.Second*5)
|
||||||
|
|
||||||
response.WriteJSON(w, response.OK(map[string]interface{}{
|
response.WriteJSON(w, response.OK(map[string]interface{}{
|
||||||
@@ -1211,9 +1294,182 @@ func (h *Handler) federationRuntimeCommand(w http.ResponseWriter, r *http.Reques
|
|||||||
response.WriteJSON(w, response.ErrDefault(err.Error()))
|
response.WriteJSON(w, response.ErrDefault(err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if strings.EqualFold(cmd, "addservice") || strings.EqualFold(cmd, "updateservice") {
|
||||||
|
h.bindPeerShareForwardRuntimeServices(share, req.Data)
|
||||||
|
} else if strings.EqualFold(cmd, "deleteservice") {
|
||||||
|
h.releasePeerShareForwardRuntimeServices(share, req.Data)
|
||||||
|
}
|
||||||
response.WriteJSON(w, response.OK(res))
|
response.WriteJSON(w, response.OK(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type federationForwardServiceBinding struct {
|
||||||
|
Name string
|
||||||
|
Port int
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractFederationServiceEntries(data interface{}) []map[string]interface{} {
|
||||||
|
if data == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if entries := asMapSlice(data); len(entries) > 0 {
|
||||||
|
return entries
|
||||||
|
}
|
||||||
|
|
||||||
|
dataMap, ok := data.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if entries := asMapSlice(dataMap["services"]); len(entries) > 0 {
|
||||||
|
return entries
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseFederationForwardServiceBindings(data interface{}) []federationForwardServiceBinding {
|
||||||
|
serviceList := extractFederationServiceEntries(data)
|
||||||
|
bindings := make([]federationForwardServiceBinding, 0, len(serviceList))
|
||||||
|
for _, svcMap := range serviceList {
|
||||||
|
name := normalizeForwardRuntimeServiceName(asString(svcMap["name"]))
|
||||||
|
if name == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, _, _, ok := parseFlowServiceIDs(name); !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
addr := strings.TrimSpace(asString(svcMap["addr"]))
|
||||||
|
if addr == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
_, portStr, err := net.SplitHostPort(addr)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
port, err := strconv.Atoi(portStr)
|
||||||
|
if err != nil || port <= 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
bindings = append(bindings, federationForwardServiceBinding{Name: name, Port: port})
|
||||||
|
}
|
||||||
|
return bindings
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseFederationForwardServiceNamesForRelease(data interface{}) []string {
|
||||||
|
names := make(map[string]struct{})
|
||||||
|
appendName := func(raw string) {
|
||||||
|
name := normalizeForwardRuntimeServiceName(raw)
|
||||||
|
if name == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, _, _, ok := parseFlowServiceIDs(name); !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
names[name] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, svcMap := range extractFederationServiceEntries(data) {
|
||||||
|
appendName(asString(svcMap["name"]))
|
||||||
|
}
|
||||||
|
|
||||||
|
if dataMap, ok := data.(map[string]interface{}); ok {
|
||||||
|
for _, item := range asAnySlice(dataMap["services"]) {
|
||||||
|
appendName(asString(item))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range asAnySlice(data) {
|
||||||
|
appendName(asString(item))
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(names) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
out := make([]string, 0, len(names))
|
||||||
|
for name := range names {
|
||||||
|
out = append(out, name)
|
||||||
|
}
|
||||||
|
sort.Strings(out)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) bindPeerShareForwardRuntimeServices(share *repo.PeerShare, data interface{}) {
|
||||||
|
if h == nil || h.repo == nil || share == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
bindings := parseFederationForwardServiceBindings(data)
|
||||||
|
if len(bindings) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
for _, binding := range bindings {
|
||||||
|
runtime, err := h.repo.GetActiveForwardPeerShareRuntimeByPort(share.ID, binding.Port)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if runtime == nil {
|
||||||
|
runtime, err = h.repo.GetActiveForwardPeerShareRuntimeByServiceName(share.ID, binding.Name)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if runtime == nil {
|
||||||
|
_ = h.repo.CreatePeerShareRuntime(&repo.PeerShareRuntime{
|
||||||
|
ShareID: share.ID,
|
||||||
|
NodeID: share.NodeID,
|
||||||
|
ReservationID: randomToken(24),
|
||||||
|
ResourceKey: fmt.Sprintf("forward-runtime:%d:%s:%d:%s", share.ID, binding.Name, binding.Port, randomToken(8)),
|
||||||
|
BindingID: "",
|
||||||
|
Role: "forward",
|
||||||
|
ChainName: "",
|
||||||
|
ServiceName: binding.Name,
|
||||||
|
Protocol: "tcp",
|
||||||
|
Strategy: "fifo",
|
||||||
|
Port: binding.Port,
|
||||||
|
Target: "",
|
||||||
|
Applied: 1,
|
||||||
|
Status: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if runtime.ServiceName == binding.Name && runtime.Applied == 1 && runtime.Port == binding.Port && runtime.Status == 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
runtime.ServiceName = binding.Name
|
||||||
|
runtime.Port = binding.Port
|
||||||
|
runtime.Applied = 1
|
||||||
|
runtime.Status = 1
|
||||||
|
runtime.UpdatedTime = now
|
||||||
|
if strings.TrimSpace(runtime.Protocol) == "" {
|
||||||
|
runtime.Protocol = "tcp"
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(runtime.Strategy) == "" {
|
||||||
|
runtime.Strategy = "fifo"
|
||||||
|
}
|
||||||
|
_ = h.repo.UpdatePeerShareRuntime(runtime)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) releasePeerShareForwardRuntimeServices(share *repo.PeerShare, data interface{}) {
|
||||||
|
if h == nil || h.repo == nil || share == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
names := parseFederationForwardServiceNamesForRelease(data)
|
||||||
|
if len(names) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
for _, name := range names {
|
||||||
|
_ = h.repo.MarkForwardPeerShareRuntimeReleasedByServiceName(share.ID, name, now)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func isFederationRuntimeCommandAllowed(commandType string) bool {
|
func isFederationRuntimeCommandAllowed(commandType string) bool {
|
||||||
switch strings.ToLower(strings.TrimSpace(commandType)) {
|
switch strings.ToLower(strings.TrimSpace(commandType)) {
|
||||||
case "addservice", "updateservice", "deleteservice", "pauseservice", "resumeservice", "addchains", "deletechains", "addlimiters", "deletelimiters", "tcpping", "reload":
|
case "addservice", "updateservice", "deleteservice", "pauseservice", "resumeservice", "addchains", "deletechains", "addlimiters", "deletelimiters", "tcpping", "reload":
|
||||||
@@ -1236,36 +1492,26 @@ func validateFederationCommandPorts(share *repo.PeerShare, data interface{}) err
|
|||||||
if share == nil || (share.PortRangeStart <= 0 && share.PortRangeEnd <= 0) {
|
if share == nil || (share.PortRangeStart <= 0 && share.PortRangeEnd <= 0) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
dataMap, ok := data.(map[string]interface{})
|
|
||||||
if !ok {
|
serviceList := extractFederationServiceEntries(data)
|
||||||
|
if len(serviceList) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
for _, svcMap := range serviceList {
|
||||||
if services, ok := dataMap["services"]; ok {
|
addr := asString(svcMap["addr"])
|
||||||
serviceList, ok := services.([]interface{})
|
if addr == "" {
|
||||||
if !ok {
|
continue
|
||||||
return fmt.Errorf("invalid services format")
|
|
||||||
}
|
}
|
||||||
for _, svc := range serviceList {
|
_, portStr, err := net.SplitHostPort(addr)
|
||||||
svcMap, ok := svc.(map[string]interface{})
|
if err != nil {
|
||||||
if !ok {
|
return fmt.Errorf("invalid service address: %s", addr)
|
||||||
return fmt.Errorf("invalid service entry format")
|
}
|
||||||
}
|
port, err := strconv.Atoi(portStr)
|
||||||
addr, ok := svcMap["addr"].(string)
|
if err != nil || port <= 0 {
|
||||||
if !ok || addr == "" {
|
return fmt.Errorf("invalid port in service address: %s", addr)
|
||||||
continue
|
}
|
||||||
}
|
if port < share.PortRangeStart || port > share.PortRangeEnd {
|
||||||
_, portStr, err := net.SplitHostPort(addr)
|
return fmt.Errorf("port %d out of allowed range %d-%d", port, share.PortRangeStart, share.PortRangeEnd)
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("invalid service address: %s", addr)
|
|
||||||
}
|
|
||||||
port, err := strconv.Atoi(portStr)
|
|
||||||
if err != nil || port <= 0 {
|
|
||||||
return fmt.Errorf("invalid port in service address: %s", addr)
|
|
||||||
}
|
|
||||||
if port < share.PortRangeStart || port > share.PortRangeEnd {
|
|
||||||
return fmt.Errorf("port %d out of allowed range %d-%d", port, share.PortRangeStart, share.PortRangeEnd)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -414,6 +414,445 @@ func TestFederationShareResetFlow(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFederationTunnelCreateCreatesPeerShareRuntime(t *testing.T) {
|
||||||
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open sqlite: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = r.Close() })
|
||||||
|
|
||||||
|
h := New(r, "test-jwt-secret")
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO node(name, secret, server_ip, server_ip_v4, server_ip_v6, port, interface_name, version, http, tls, socks, created_time, updated_time, status, tcp_listen_addr, udp_listen_addr, inx, is_remote, remote_url, remote_token, remote_config)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, "federation-forward-node", "federation-forward-secret", "10.90.80.70", "10.90.80.70", "", "24000-24020", "", "v1", 1, 1, 1, now, now, 1, "[::]", "[::]", 0, 0, "", "", "").Error; err != nil {
|
||||||
|
t.Fatalf("insert node: %v", err)
|
||||||
|
}
|
||||||
|
nodeID := mustLastInsertID(t, r, "federation-forward-node")
|
||||||
|
|
||||||
|
if err := r.CreatePeerShare(&repo.PeerShare{
|
||||||
|
Name: "federation-forward-share",
|
||||||
|
NodeID: nodeID,
|
||||||
|
Token: "federation-forward-token",
|
||||||
|
MaxBandwidth: 0,
|
||||||
|
CurrentFlow: 0,
|
||||||
|
PortRangeStart: 24000,
|
||||||
|
PortRangeEnd: 24020,
|
||||||
|
IsActive: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create share: %v", err)
|
||||||
|
}
|
||||||
|
share, err := r.GetPeerShareByToken("federation-forward-token")
|
||||||
|
if err != nil || share == nil {
|
||||||
|
t.Fatalf("load share: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := json.Marshal(federationTunnelRequest{
|
||||||
|
Protocol: "tcp",
|
||||||
|
RemotePort: 24001,
|
||||||
|
Target: "1.1.1.1:443",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("marshal request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/federation/tunnel/create", bytes.NewReader(body))
|
||||||
|
req.Header.Set("Authorization", "Bearer "+share.Token)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
|
||||||
|
h.federationTunnelCreate(res, req)
|
||||||
|
|
||||||
|
if res.Code != http.StatusOK {
|
||||||
|
t.Fatalf("expected status %d, got %d", http.StatusOK, res.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
var payload response.R
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(&payload); err != nil {
|
||||||
|
t.Fatalf("decode response: %v", err)
|
||||||
|
}
|
||||||
|
if payload.Code != 0 {
|
||||||
|
t.Fatalf("expected response code 0, got %d (%s)", payload.Code, payload.Msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
runtimeCount := mustQueryInt(t, r, `SELECT COUNT(1) FROM peer_share_runtime WHERE share_id = ? AND port = ? AND status = 1`, share.ID, 24001)
|
||||||
|
if runtimeCount != 1 {
|
||||||
|
t.Fatalf("expected 1 runtime row for new federation forward tunnel, got %d", runtimeCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFederationTunnelCreateRejectsOccupiedPort(t *testing.T) {
|
||||||
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open sqlite: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = r.Close() })
|
||||||
|
|
||||||
|
h := New(r, "test-jwt-secret")
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO node(name, secret, server_ip, server_ip_v4, server_ip_v6, port, interface_name, version, http, tls, socks, created_time, updated_time, status, tcp_listen_addr, udp_listen_addr, inx, is_remote, remote_url, remote_token, remote_config)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, "federation-port-check-node", "federation-port-check-secret", "10.91.80.70", "10.91.80.70", "", "24100-24120", "", "v1", 1, 1, 1, now, now, 1, "[::]", "[::]", 0, 0, "", "", "").Error; err != nil {
|
||||||
|
t.Fatalf("insert node: %v", err)
|
||||||
|
}
|
||||||
|
nodeID := mustLastInsertID(t, r, "federation-port-check-node")
|
||||||
|
|
||||||
|
if err := r.CreatePeerShare(&repo.PeerShare{
|
||||||
|
Name: "federation-port-check-share",
|
||||||
|
NodeID: nodeID,
|
||||||
|
Token: "federation-port-check-token",
|
||||||
|
MaxBandwidth: 0,
|
||||||
|
CurrentFlow: 0,
|
||||||
|
PortRangeStart: 24100,
|
||||||
|
PortRangeEnd: 24120,
|
||||||
|
IsActive: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create share: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
create := func() response.R {
|
||||||
|
body, err := json.Marshal(federationTunnelRequest{Protocol: "tcp", RemotePort: 24101, Target: "1.1.1.1:443"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("marshal request: %v", err)
|
||||||
|
}
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/federation/tunnel/create", bytes.NewReader(body))
|
||||||
|
req.Header.Set("Authorization", "Bearer federation-port-check-token")
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
h.federationTunnelCreate(res, req)
|
||||||
|
if res.Code != http.StatusOK {
|
||||||
|
t.Fatalf("expected status %d, got %d", http.StatusOK, res.Code)
|
||||||
|
}
|
||||||
|
var payload response.R
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(&payload); err != nil {
|
||||||
|
t.Fatalf("decode response: %v", err)
|
||||||
|
}
|
||||||
|
return payload
|
||||||
|
}
|
||||||
|
|
||||||
|
first := create()
|
||||||
|
if first.Code != 0 {
|
||||||
|
t.Fatalf("expected first create success, got %d (%s)", first.Code, first.Msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
second := create()
|
||||||
|
if second.Code != 403 {
|
||||||
|
t.Fatalf("expected second create to be rejected with 403, got %d (%s)", second.Code, second.Msg)
|
||||||
|
}
|
||||||
|
if second.Msg != "Port already in use" {
|
||||||
|
t.Fatalf("expected occupied port message, got %q", second.Msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDeleteTunnelReleasesFederationForwardRuntimeByPort(t *testing.T) {
|
||||||
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open sqlite: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = r.Close() })
|
||||||
|
|
||||||
|
h := New(r, "test-jwt-secret")
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
|
||||||
|
if err := r.CreatePeerShare(&repo.PeerShare{
|
||||||
|
Name: "delete-forward-share",
|
||||||
|
NodeID: 1,
|
||||||
|
Token: "delete-forward-token",
|
||||||
|
MaxBandwidth: 0,
|
||||||
|
CurrentFlow: 0,
|
||||||
|
PortRangeStart: 25000,
|
||||||
|
PortRangeEnd: 25020,
|
||||||
|
IsActive: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create share: %v", err)
|
||||||
|
}
|
||||||
|
share, err := r.GetPeerShareByToken("delete-forward-token")
|
||||||
|
if err != nil || share == nil {
|
||||||
|
t.Fatalf("load share: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tunnelName := fmt.Sprintf("Share-%d-Port-%d", share.ID, 25001)
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO tunnel(id, name, traffic_ratio, type, protocol, flow, created_time, updated_time, status, in_ip, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, 1, tunnelName, 1.0, 1, "tcp", 1, now, now, 1, nil, 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert tunnel: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO peer_share_runtime(share_id, node_id, reservation_id, resource_key, binding_id, role, chain_name, service_name, protocol, strategy, port, target, applied, status, created_time, updated_time)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, share.ID, share.NodeID, "del-r1", "del-rk1", "", "forward", "", "20_2_10", "tcp", "fifo", 25001, "", 1, 1, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert runtime: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.deleteTunnelByID(1); err != nil {
|
||||||
|
t.Fatalf("delete tunnel: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
activeCount := mustQueryInt(t, r, `SELECT COUNT(1) FROM peer_share_runtime WHERE share_id = ? AND port = ? AND status = 1`, share.ID, 25001)
|
||||||
|
if activeCount != 0 {
|
||||||
|
t.Fatalf("expected runtime released after tunnel delete, active rows=%d", activeCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBindPeerShareForwardRuntimeServicesOnlyBindsForwardRole(t *testing.T) {
|
||||||
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open sqlite: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = r.Close() })
|
||||||
|
|
||||||
|
h := New(r, "test-jwt-secret")
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
|
||||||
|
if err := r.CreatePeerShare(&repo.PeerShare{
|
||||||
|
Name: "bind-forward-role-share",
|
||||||
|
NodeID: 1,
|
||||||
|
Token: "bind-forward-role-token",
|
||||||
|
MaxBandwidth: 0,
|
||||||
|
CurrentFlow: 0,
|
||||||
|
PortRangeStart: 26000,
|
||||||
|
PortRangeEnd: 26020,
|
||||||
|
IsActive: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create share: %v", err)
|
||||||
|
}
|
||||||
|
share, err := r.GetPeerShareByToken("bind-forward-role-token")
|
||||||
|
if err != nil || share == nil {
|
||||||
|
t.Fatalf("load share: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO peer_share_runtime(id, share_id, node_id, reservation_id, resource_key, binding_id, role, chain_name, service_name, protocol, strategy, port, target, applied, status, created_time, updated_time)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?),
|
||||||
|
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`,
|
||||||
|
1, share.ID, share.NodeID, "bind-r1", "bind-rk1", "", "forward", "", "", "tcp", "fifo", 26001, "", 0, 1, now, now,
|
||||||
|
2, share.ID, share.NodeID, "bind-r2", "bind-rk2", "", "middle", "", "", "tcp", "round", 26002, "", 0, 1, now, now,
|
||||||
|
).Error; err != nil {
|
||||||
|
t.Fatalf("insert runtimes: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
h.bindPeerShareForwardRuntimeServices(share, map[string]interface{}{
|
||||||
|
"services": []interface{}{
|
||||||
|
map[string]interface{}{"name": "77_2_10_tcp", "addr": "[::]:26001"},
|
||||||
|
map[string]interface{}{"name": "88_2_10_tcp", "addr": "[::]:26002"},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
forwardServiceName := ""
|
||||||
|
middleServiceName := ""
|
||||||
|
if err := r.DB().Raw(`SELECT service_name FROM peer_share_runtime WHERE id = 1`).Scan(&forwardServiceName).Error; err != nil {
|
||||||
|
t.Fatalf("load forward runtime service name: %v", err)
|
||||||
|
}
|
||||||
|
if err := r.DB().Raw(`SELECT service_name FROM peer_share_runtime WHERE id = 2`).Scan(&middleServiceName).Error; err != nil {
|
||||||
|
t.Fatalf("load middle runtime service name: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if forwardServiceName != "77_2_10" {
|
||||||
|
t.Fatalf("expected forward runtime service name bound, got %q", forwardServiceName)
|
||||||
|
}
|
||||||
|
if middleServiceName != "" {
|
||||||
|
t.Fatalf("expected non-forward runtime unchanged, got %q", middleServiceName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBindPeerShareForwardRuntimeServicesAcceptsTopLevelServiceArray(t *testing.T) {
|
||||||
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open sqlite: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = r.Close() })
|
||||||
|
|
||||||
|
h := New(r, "test-jwt-secret")
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
|
||||||
|
if err := r.CreatePeerShare(&repo.PeerShare{
|
||||||
|
Name: "bind-array-share",
|
||||||
|
NodeID: 1,
|
||||||
|
Token: "bind-array-token",
|
||||||
|
MaxBandwidth: 0,
|
||||||
|
CurrentFlow: 0,
|
||||||
|
PortRangeStart: 26100,
|
||||||
|
PortRangeEnd: 26120,
|
||||||
|
IsActive: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create share: %v", err)
|
||||||
|
}
|
||||||
|
share, err := r.GetPeerShareByToken("bind-array-token")
|
||||||
|
if err != nil || share == nil {
|
||||||
|
t.Fatalf("load share: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO peer_share_runtime(id, share_id, node_id, reservation_id, resource_key, binding_id, role, chain_name, service_name, protocol, strategy, port, target, applied, status, created_time, updated_time)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`,
|
||||||
|
1, share.ID, share.NodeID, "bind-array-r1", "bind-array-rk1", "", "forward", "", "", "tcp", "fifo", 26101, "", 0, 1, now, now,
|
||||||
|
).Error; err != nil {
|
||||||
|
t.Fatalf("insert runtime: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
h.bindPeerShareForwardRuntimeServices(share, []interface{}{
|
||||||
|
map[string]interface{}{"name": "99_2_10_tcp", "addr": "[::]:26101"},
|
||||||
|
map[string]interface{}{"name": "99_2_10_udp", "addr": "[::]:26101"},
|
||||||
|
})
|
||||||
|
|
||||||
|
forwardServiceName := ""
|
||||||
|
if err := r.DB().Raw(`SELECT service_name FROM peer_share_runtime WHERE id = 1`).Scan(&forwardServiceName).Error; err != nil {
|
||||||
|
t.Fatalf("load forward runtime service name: %v", err)
|
||||||
|
}
|
||||||
|
if forwardServiceName != "99_2_10" {
|
||||||
|
t.Fatalf("expected forward runtime service name bound from top-level array, got %q", forwardServiceName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBindPeerShareForwardRuntimeServicesCreatesRuntimeWhenMissing(t *testing.T) {
|
||||||
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel-bind-create-runtime.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open sqlite: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = r.Close() })
|
||||||
|
|
||||||
|
h := New(r, "test-jwt-secret")
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
|
||||||
|
if err := r.CreatePeerShare(&repo.PeerShare{
|
||||||
|
Name: "bind-create-runtime-share",
|
||||||
|
NodeID: 1,
|
||||||
|
Token: "bind-create-runtime-token",
|
||||||
|
MaxBandwidth: 0,
|
||||||
|
CurrentFlow: 0,
|
||||||
|
PortRangeStart: 26300,
|
||||||
|
PortRangeEnd: 26320,
|
||||||
|
IsActive: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create share: %v", err)
|
||||||
|
}
|
||||||
|
share, err := r.GetPeerShareByToken("bind-create-runtime-token")
|
||||||
|
if err != nil || share == nil {
|
||||||
|
t.Fatalf("load share: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
h.bindPeerShareForwardRuntimeServices(share, map[string]interface{}{
|
||||||
|
"services": []interface{}{
|
||||||
|
map[string]interface{}{"name": "55_2_10_tcp", "addr": "[::]:26301"},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
var count int64
|
||||||
|
if err := r.DB().Raw(`SELECT COUNT(1) FROM peer_share_runtime WHERE share_id = ? AND role = ? AND status = 1`, share.ID, "forward").Scan(&count).Error; err != nil {
|
||||||
|
t.Fatalf("query runtime count: %v", err)
|
||||||
|
}
|
||||||
|
if count != 1 {
|
||||||
|
t.Fatalf("expected 1 active forward runtime row, got %d", count)
|
||||||
|
}
|
||||||
|
|
||||||
|
var serviceName string
|
||||||
|
var port int
|
||||||
|
var applied int
|
||||||
|
if err := r.DB().Raw(`SELECT service_name, port, applied FROM peer_share_runtime WHERE share_id = ? AND role = ? ORDER BY id DESC LIMIT 1`, share.ID, "forward").Row().Scan(&serviceName, &port, &applied); err != nil {
|
||||||
|
t.Fatalf("query created runtime: %v", err)
|
||||||
|
}
|
||||||
|
if serviceName != "55_2_10" {
|
||||||
|
t.Fatalf("expected service_name=55_2_10, got %q", serviceName)
|
||||||
|
}
|
||||||
|
if port != 26301 {
|
||||||
|
t.Fatalf("expected port=26301, got %d", port)
|
||||||
|
}
|
||||||
|
if applied != 1 {
|
||||||
|
t.Fatalf("expected applied=1, got %d", applied)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReleasePeerShareForwardRuntimeServicesMarksRuntimeReleased(t *testing.T) {
|
||||||
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel-release-runtime.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open sqlite: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = r.Close() })
|
||||||
|
|
||||||
|
h := New(r, "test-jwt-secret")
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
|
||||||
|
if err := r.CreatePeerShare(&repo.PeerShare{
|
||||||
|
Name: "release-runtime-share",
|
||||||
|
NodeID: 1,
|
||||||
|
Token: "release-runtime-token",
|
||||||
|
MaxBandwidth: 0,
|
||||||
|
CurrentFlow: 0,
|
||||||
|
PortRangeStart: 26400,
|
||||||
|
PortRangeEnd: 26420,
|
||||||
|
IsActive: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create share: %v", err)
|
||||||
|
}
|
||||||
|
share, err := r.GetPeerShareByToken("release-runtime-token")
|
||||||
|
if err != nil || share == nil {
|
||||||
|
t.Fatalf("load share: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO peer_share_runtime(share_id, node_id, reservation_id, resource_key, binding_id, role, chain_name, service_name, protocol, strategy, port, target, applied, status, created_time, updated_time)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, share.ID, share.NodeID, "release-r1", "release-rk1", "", "forward", "", "77_2_10", "tcp", "fifo", 26401, "", 1, 1, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert runtime: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
h.releasePeerShareForwardRuntimeServices(share, map[string]interface{}{
|
||||||
|
"services": []interface{}{"77_2_10_tcp"},
|
||||||
|
})
|
||||||
|
|
||||||
|
var status int
|
||||||
|
var applied int
|
||||||
|
var serviceName string
|
||||||
|
if err := r.DB().Raw(`SELECT status, applied, service_name FROM peer_share_runtime WHERE share_id = ? AND role = ? ORDER BY id DESC LIMIT 1`, share.ID, "forward").Row().Scan(&status, &applied, &serviceName); err != nil {
|
||||||
|
t.Fatalf("query released runtime: %v", err)
|
||||||
|
}
|
||||||
|
if status != 0 {
|
||||||
|
t.Fatalf("expected status=0 after release, got %d", status)
|
||||||
|
}
|
||||||
|
if applied != 0 {
|
||||||
|
t.Fatalf("expected applied=0 after release, got %d", applied)
|
||||||
|
}
|
||||||
|
if serviceName != "" {
|
||||||
|
t.Fatalf("expected service_name cleared after release, got %q", serviceName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateFederationCommandPortsAcceptsTopLevelServiceArray(t *testing.T) {
|
||||||
|
share := &repo.PeerShare{
|
||||||
|
PortRangeStart: 26200,
|
||||||
|
PortRangeEnd: 26210,
|
||||||
|
}
|
||||||
|
err := validateFederationCommandPorts(share, []interface{}{
|
||||||
|
map[string]interface{}{"name": "11_2_10_tcp", "addr": "[::]:26201"},
|
||||||
|
map[string]interface{}{"name": "11_2_10_udp", "addr": "[::]:26201"},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected top-level service array to pass port validation, got: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFederationRemoteUsageList(t *testing.T) {
|
func TestFederationRemoteUsageList(t *testing.T) {
|
||||||
r, err := repo.Open(filepath.Join(t.TempDir(), "panel.db"))
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel.db"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -502,6 +941,110 @@ func TestFederationRemoteUsageList(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFederationRemoteUsageListIncludesForwardPorts(t *testing.T) {
|
||||||
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel-forward-usage.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open sqlite: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = r.Close() })
|
||||||
|
|
||||||
|
h := New(r, "test-jwt-secret")
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO node(name, secret, server_ip, server_ip_v4, server_ip_v6, port, interface_name, version, http, tls, socks, created_time, updated_time, status, tcp_listen_addr, udp_listen_addr, inx, is_remote, remote_url, remote_token, remote_config)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, "forward-usage-remote-node", "forward-usage-secret", "10.60.70.80", "10.60.70.80", "", "33000-33010", "", "v1", 1, 1, 1, now, now, 1, "[::]", "[::]", 0, 1, "", "", `{"shareId":99,"maxBandwidth":0,"currentFlow":0,"portRangeStart":33000,"portRangeEnd":33010}`).Error; err != nil {
|
||||||
|
t.Fatalf("insert remote node: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var nodeID int64
|
||||||
|
if err := r.DB().Raw(`SELECT id FROM node WHERE name = ? ORDER BY id DESC LIMIT 1`, "forward-usage-remote-node").Row().Scan(&nodeID); err != nil {
|
||||||
|
t.Fatalf("query node id: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO tunnel(name, type, protocol, flow, created_time, updated_time, status, in_ip, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, "forward-usage-tunnel", 1, "tls", 1, now, now, 1, "", 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert tunnel: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var tunnelID int64
|
||||||
|
if err := r.DB().Raw(`SELECT id FROM tunnel WHERE name = ? ORDER BY id DESC LIMIT 1`, "forward-usage-tunnel").Row().Scan(&tunnelID); err != nil {
|
||||||
|
t.Fatalf("query tunnel id: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO forward(user_id, user_name, name, tunnel_id, remote_addr, strategy, in_flow, out_flow, created_time, updated_time, status, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, 1, "tester", "forward-usage-item", tunnelID, "1.1.1.1:443", "fifo", 0, 0, now, now, 1, 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert forward: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var forwardID int64
|
||||||
|
if err := r.DB().Raw(`SELECT id FROM forward WHERE name = ? ORDER BY id DESC LIMIT 1`, "forward-usage-item").Row().Scan(&forwardID); err != nil {
|
||||||
|
t.Fatalf("query forward id: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`INSERT INTO forward_port(forward_id, node_id, port) VALUES(?, ?, ?)`, forwardID, nodeID, 33001).Error; err != nil {
|
||||||
|
t.Fatalf("insert forward_port: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/federation/share/remote-usage/list", nil)
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
h.federationRemoteUsageList(res, req)
|
||||||
|
|
||||||
|
if res.Code != http.StatusOK {
|
||||||
|
t.Fatalf("expected status %d, got %d", http.StatusOK, res.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
var payload response.R
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(&payload); err != nil {
|
||||||
|
t.Fatalf("decode response: %v", err)
|
||||||
|
}
|
||||||
|
if payload.Code != 0 {
|
||||||
|
t.Fatalf("expected response code 0, got %d (%s)", payload.Code, payload.Msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, ok := payload.Data.([]interface{})
|
||||||
|
if !ok || len(rows) == 0 {
|
||||||
|
t.Fatalf("expected non-empty usage list, got %T", payload.Data)
|
||||||
|
}
|
||||||
|
|
||||||
|
first, ok := rows[0].(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected usage row map, got %T", rows[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
usedPortsRaw, ok := first["usedPorts"].([]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected usedPorts array, got %T", first["usedPorts"])
|
||||||
|
}
|
||||||
|
if len(usedPortsRaw) != 1 || int(usedPortsRaw[0].(float64)) != 33001 {
|
||||||
|
t.Fatalf("expected usedPorts [33001], got %v", usedPortsRaw)
|
||||||
|
}
|
||||||
|
|
||||||
|
bindingsRaw, ok := first["bindings"].([]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected bindings array, got %T", first["bindings"])
|
||||||
|
}
|
||||||
|
if len(bindingsRaw) != 1 {
|
||||||
|
t.Fatalf("expected 1 binding row from forward usage, got %d", len(bindingsRaw))
|
||||||
|
}
|
||||||
|
|
||||||
|
binding, ok := bindingsRaw[0].(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected binding row object, got %T", bindingsRaw[0])
|
||||||
|
}
|
||||||
|
if int(binding["allocatedPort"].(float64)) != 33001 {
|
||||||
|
t.Fatalf("expected allocatedPort=33001, got %v", binding["allocatedPort"])
|
||||||
|
}
|
||||||
|
if int(binding["chainType"].(float64)) != 1 {
|
||||||
|
t.Fatalf("expected chainType=1 for forward usage row, got %v", binding["chainType"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestAuthPeerAllowedIPs(t *testing.T) {
|
func TestAuthPeerAllowedIPs(t *testing.T) {
|
||||||
r, err := repo.Open(filepath.Join(t.TempDir(), "panel.db"))
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel.db"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -2,9 +2,12 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go-backend/internal/store/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
const bytesPerGB int64 = 1024 * 1024 * 1024
|
const bytesPerGB int64 = 1024 * 1024 * 1024
|
||||||
@@ -30,7 +33,7 @@ type namedConfigItem struct {
|
|||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) processFlowItem(item flowItem) {
|
func (h *Handler) processFlowItem(nodeID int64, item flowItem) {
|
||||||
serviceName := strings.TrimSpace(item.N)
|
serviceName := strings.TrimSpace(item.N)
|
||||||
if serviceName == "" || serviceName == "web_api" {
|
if serviceName == "" || serviceName == "web_api" {
|
||||||
return
|
return
|
||||||
@@ -40,6 +43,7 @@ func (h *Handler) processFlowItem(item flowItem) {
|
|||||||
if ok {
|
if ok {
|
||||||
inFlow, outFlow := h.scaleFlowByTunnel(forwardID, item.D, item.U)
|
inFlow, outFlow := h.scaleFlowByTunnel(forwardID, item.D, item.U)
|
||||||
_ = h.repo.AddFlow(forwardID, userID, userTunnelID, inFlow, outFlow)
|
_ = h.repo.AddFlow(forwardID, userID, userTunnelID, inFlow, outFlow)
|
||||||
|
h.processPeerShareFlowFromForward(forwardID, nodeID, serviceName, item)
|
||||||
|
|
||||||
if userTunnelID > 0 {
|
if userTunnelID > 0 {
|
||||||
h.enforceFlowPolicies(userID, userTunnelID)
|
h.enforceFlowPolicies(userID, userTunnelID)
|
||||||
@@ -87,6 +91,45 @@ func parsePeerShareRuntimeServiceID(serviceName string) (int64, bool) {
|
|||||||
return runtimeID, true
|
return runtimeID, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parsePeerShareInfoFromFederationTunnelName(tunnelName string) (int64, int, bool) {
|
||||||
|
tunnelName = strings.TrimSpace(tunnelName)
|
||||||
|
if !strings.HasPrefix(tunnelName, "Share-") {
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
raw := strings.TrimPrefix(tunnelName, "Share-")
|
||||||
|
idx := strings.Index(raw, "-Port-")
|
||||||
|
if idx <= 0 {
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
shareID, err := strconv.ParseInt(raw[:idx], 10, 64)
|
||||||
|
if err != nil || shareID <= 0 {
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
portValue := strings.TrimSpace(raw[idx+len("-Port-"):])
|
||||||
|
port, err := strconv.Atoi(portValue)
|
||||||
|
if err != nil || port <= 0 {
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
return shareID, port, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func parsePeerShareIDFromFederationTunnelName(tunnelName string) (int64, bool) {
|
||||||
|
tunnelName = strings.TrimSpace(tunnelName)
|
||||||
|
if !strings.HasPrefix(tunnelName, "Share-") {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
raw := strings.TrimPrefix(tunnelName, "Share-")
|
||||||
|
idx := strings.Index(raw, "-Port-")
|
||||||
|
if idx <= 0 {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
shareID, err := strconv.ParseInt(raw[:idx], 10, 64)
|
||||||
|
if err != nil || shareID <= 0 {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
return shareID, true
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handler) processPeerShareFlow(runtimeID int64, item flowItem) {
|
func (h *Handler) processPeerShareFlow(runtimeID int64, item flowItem) {
|
||||||
if h == nil || h.repo == nil || runtimeID <= 0 {
|
if h == nil || h.repo == nil || runtimeID <= 0 {
|
||||||
return
|
return
|
||||||
@@ -113,6 +156,121 @@ func (h *Handler) processPeerShareFlow(runtimeID int64, item flowItem) {
|
|||||||
h.enforcePeerShareFlowLimit(share.ID)
|
h.enforcePeerShareFlowLimit(share.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Handler) processPeerShareFlowFromForward(forwardID int64, nodeID int64, serviceName string, item flowItem) {
|
||||||
|
if h == nil || h.repo == nil || forwardID <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
delta := item.D + item.U
|
||||||
|
if delta <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
forward, err := h.getForwardRecord(forwardID)
|
||||||
|
if err != nil || forward == nil {
|
||||||
|
// Forward not found in local database - might be a federation port-forward
|
||||||
|
// Try to find by service name in peer_share_runtime
|
||||||
|
h.processPeerShareFlowByServiceName(nodeID, serviceName, item)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tunnelName, err := h.repo.GetTunnelName(forward.TunnelID)
|
||||||
|
if err != nil {
|
||||||
|
h.processPeerShareFlowByServiceName(nodeID, serviceName, item)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
shareID, ok := parsePeerShareIDFromFederationTunnelName(tunnelName)
|
||||||
|
if !ok {
|
||||||
|
h.processPeerShareFlowByServiceName(nodeID, serviceName, item)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.repo.AddPeerShareCurrentFlow(shareID, delta); err != nil {
|
||||||
|
h.processPeerShareFlowByServiceName(nodeID, serviceName, item)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
share, err := h.repo.GetPeerShare(shareID)
|
||||||
|
if err != nil || share == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !isPeerShareFlowExceeded(share) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.enforcePeerShareFlowLimit(share.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeForwardRuntimeServiceName(serviceName string) string {
|
||||||
|
name := strings.TrimSpace(serviceName)
|
||||||
|
if strings.HasSuffix(name, "_tcp") {
|
||||||
|
return strings.TrimSuffix(name, "_tcp")
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(name, "_udp") {
|
||||||
|
return strings.TrimSuffix(name, "_udp")
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) processPeerShareFlowByServiceName(nodeID int64, serviceName string, item flowItem) {
|
||||||
|
if h == nil || h.repo == nil || strings.TrimSpace(serviceName) == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
delta := item.D + item.U
|
||||||
|
if delta <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
normalized := normalizeForwardRuntimeServiceName(serviceName)
|
||||||
|
var runtimes []model.PeerShareRuntime
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// Try node-scoped query first if nodeID is valid
|
||||||
|
if nodeID > 0 {
|
||||||
|
runtimes, err = h.repo.ListActiveForwardPeerShareRuntimesByNodeAndServiceName(nodeID, normalized)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(runtimes) == 0 && normalized != serviceName {
|
||||||
|
runtimes, err = h.repo.ListActiveForwardPeerShareRuntimesByNodeAndServiceName(nodeID, serviceName)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback to global query if node-scoped query returned nothing or nodeID is invalid
|
||||||
|
if len(runtimes) == 0 {
|
||||||
|
runtimes, err = h.repo.ListActiveForwardPeerShareRuntimesByServiceName(normalized)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(runtimes) == 0 && normalized != serviceName {
|
||||||
|
runtimes, err = h.repo.ListActiveForwardPeerShareRuntimesByServiceName(serviceName)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(runtimes) != 1 {
|
||||||
|
if len(runtimes) > 1 {
|
||||||
|
log.Printf("WARN: ambiguous peer share runtime match for service=%s nodeID=%d count=%d", serviceName, nodeID, len(runtimes))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
runtime := runtimes[0]
|
||||||
|
|
||||||
|
_ = h.repo.AddPeerShareCurrentFlow(runtime.ShareID, delta)
|
||||||
|
|
||||||
|
matchedShare, err := h.repo.GetPeerShare(runtime.ShareID)
|
||||||
|
if err != nil || matchedShare == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if isPeerShareFlowExceeded(matchedShare) {
|
||||||
|
h.enforcePeerShareFlowLimit(matchedShare.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handler) enforcePeerShareFlowLimit(shareID int64) {
|
func (h *Handler) enforcePeerShareFlowLimit(shareID int64) {
|
||||||
if h == nil || h.repo == nil || shareID <= 0 {
|
if h == nil || h.repo == nil || shareID <= 0 {
|
||||||
return
|
return
|
||||||
@@ -271,15 +429,46 @@ func (h *Handler) cleanNodeConfigs(nodeID int64, rawConfig string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) cleanOrphanedServices(nodeID int64, services []namedConfigItem) {
|
func (h *Handler) cleanOrphanedServices(nodeID int64, services []namedConfigItem) {
|
||||||
|
runtimeServiceNames, err := h.repo.ListActiveForwardPeerShareRuntimeServiceNamesByNode(nodeID)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
minUpdatedTime := time.Now().Add(-10 * time.Minute).UnixMilli()
|
||||||
|
hasUnboundForwardPeerRuntime, err := h.repo.HasRecentUnboundForwardPeerShareRuntimeOnNode(nodeID, minUpdatedTime)
|
||||||
|
if err != nil {
|
||||||
|
hasUnboundForwardPeerRuntime = false
|
||||||
|
}
|
||||||
|
runtimeServiceSet := make(map[string]struct{}, len(runtimeServiceNames))
|
||||||
|
for _, serviceName := range runtimeServiceNames {
|
||||||
|
serviceName = strings.TrimSpace(serviceName)
|
||||||
|
if serviceName == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
runtimeServiceSet[serviceName] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
for _, item := range services {
|
for _, item := range services {
|
||||||
name := strings.TrimSpace(item.Name)
|
name := strings.TrimSpace(item.Name)
|
||||||
if name == "" || name == "web_api" {
|
if name == "" || name == "web_api" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if strings.HasPrefix(name, "fed_svc_") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
normalizedName := normalizeForwardRuntimeServiceName(name)
|
||||||
|
if _, ok := runtimeServiceSet[normalizedName]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, ok := runtimeServiceSet[name]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
parts := strings.Split(name, "_")
|
parts := strings.Split(name, "_")
|
||||||
if len(parts) >= 3 {
|
if len(parts) >= 3 {
|
||||||
forwardID, err := strconv.ParseInt(parts[0], 10, 64)
|
forwardID, err := strconv.ParseInt(parts[0], 10, 64)
|
||||||
|
if err == nil && forwardID > 0 && hasUnboundForwardPeerRuntime {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if err == nil && forwardID > 0 && !h.forwardExists(forwardID) {
|
if err == nil && forwardID > 0 && !h.forwardExists(forwardID) {
|
||||||
_, _ = h.sendNodeCommand(nodeID, "DeleteService", map[string]interface{}{"services": []string{name, parts[0] + "_" + parts[1] + "_" + parts[2], parts[0] + "_" + parts[1] + "_" + parts[2] + "_tcp", parts[0] + "_" + parts[1] + "_" + parts[2] + "_udp"}}, false, true)
|
_, _ = h.sendNodeCommand(nodeID, "DeleteService", map[string]interface{}{"services": []string{name, parts[0] + "_" + parts[1] + "_" + parts[2], parts[0] + "_" + parts[1] + "_" + parts[2] + "_tcp", parts[0] + "_" + parts[1] + "_" + parts[2] + "_udp"}}, false, true)
|
||||||
continue
|
continue
|
||||||
@@ -299,6 +488,9 @@ func (h *Handler) cleanOrphanedServices(nodeID int64, services []namedConfigItem
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
forwardID, err := strconv.ParseInt(parts[0], 10, 64)
|
forwardID, err := strconv.ParseInt(parts[0], 10, 64)
|
||||||
|
if err == nil && forwardID > 0 && hasUnboundForwardPeerRuntime {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if err != nil || forwardID <= 0 || h.forwardExists(forwardID) {
|
if err != nil || forwardID <= 0 || h.forwardExists(forwardID) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -43,7 +44,7 @@ func TestProcessFlowItemTracksPeerShareFlowAndEnforcesLimit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
h := &Handler{repo: r}
|
h := &Handler{repo: r}
|
||||||
h.processFlowItem(flowItem{N: "fed_svc_17", U: 1200, D: 900})
|
h.processFlowItem(1, flowItem{N: "fed_svc_17", U: 1200, D: 900})
|
||||||
|
|
||||||
updatedShare, err := r.GetPeerShare(share.ID)
|
updatedShare, err := r.GetPeerShare(share.ID)
|
||||||
if err != nil || updatedShare == nil {
|
if err != nil || updatedShare == nil {
|
||||||
@@ -61,3 +62,345 @@ func TestProcessFlowItemTracksPeerShareFlowAndEnforcesLimit(t *testing.T) {
|
|||||||
t.Fatalf("expected runtime status=0 after limit enforcement, got %d", runtime.Status)
|
t.Fatalf("expected runtime status=0 after limit enforcement, got %d", runtime.Status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProcessFlowItemTracksPeerShareFlowForFederationPortForward(t *testing.T) {
|
||||||
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel-forward.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open repo: %v", err)
|
||||||
|
}
|
||||||
|
defer r.Close()
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
if err := r.CreatePeerShare(&repo.PeerShare{
|
||||||
|
Name: "forward-share",
|
||||||
|
NodeID: 1,
|
||||||
|
Token: "forward-share-token",
|
||||||
|
MaxBandwidth: 0,
|
||||||
|
CurrentFlow: 0,
|
||||||
|
PortRangeStart: 30000,
|
||||||
|
PortRangeEnd: 30010,
|
||||||
|
IsActive: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create peer share: %v", err)
|
||||||
|
}
|
||||||
|
share, err := r.GetPeerShareByToken("forward-share-token")
|
||||||
|
if err != nil || share == nil {
|
||||||
|
t.Fatalf("load peer share: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO user(id, user, pwd, role_id, exp_time, flow, in_flow, out_flow, flow_reset_time, num, created_time, updated_time, status)
|
||||||
|
VALUES(2, 'u2', 'x', 1, ?, 99999, 0, 0, 1, 1, ?, ?, 1)
|
||||||
|
`, now+24*60*60*1000, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert user: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tunnelName := "Share-" + strconv.FormatInt(share.ID, 10) + "-Port-30001"
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO tunnel(id, name, traffic_ratio, type, protocol, flow, created_time, updated_time, status, in_ip, inx)
|
||||||
|
VALUES(1, ?, 1.0, 1, 'tls', 1, ?, ?, 1, NULL, 0)
|
||||||
|
`, tunnelName, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert tunnel: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO user_tunnel(id, user_id, tunnel_id, speed_id, num, flow, in_flow, out_flow, flow_reset_time, exp_time, status)
|
||||||
|
VALUES(10, 2, 1, NULL, 1, 99999, 0, 0, 1, ?, 1)
|
||||||
|
`, now+24*60*60*1000).Error; err != nil {
|
||||||
|
t.Fatalf("insert user_tunnel: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO forward(id, user_id, user_name, name, tunnel_id, remote_addr, strategy, in_flow, out_flow, created_time, updated_time, status, inx)
|
||||||
|
VALUES(20, 2, 'u2', 'f20', 1, '1.1.1.1:443', 'fifo', 0, 0, ?, ?, 1, 0)
|
||||||
|
`, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert forward: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
h := &Handler{repo: r}
|
||||||
|
h.processFlowItem(1, flowItem{N: "20_2_10", U: 120, D: 80})
|
||||||
|
|
||||||
|
updatedShare, err := r.GetPeerShare(share.ID)
|
||||||
|
if err != nil || updatedShare == nil {
|
||||||
|
t.Fatalf("reload share: %v", err)
|
||||||
|
}
|
||||||
|
if updatedShare.CurrentFlow != 200 {
|
||||||
|
t.Fatalf("expected current_flow=200, got %d", updatedShare.CurrentFlow)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProcessFlowItemTracksPeerShareFlowByForwardServiceName(t *testing.T) {
|
||||||
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel-forward-service.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open repo: %v", err)
|
||||||
|
}
|
||||||
|
defer r.Close()
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
if err := r.CreatePeerShare(&repo.PeerShare{
|
||||||
|
Name: "forward-service-share",
|
||||||
|
NodeID: 1,
|
||||||
|
Token: "forward-service-token",
|
||||||
|
MaxBandwidth: 0,
|
||||||
|
CurrentFlow: 0,
|
||||||
|
PortRangeStart: 31000,
|
||||||
|
PortRangeEnd: 31010,
|
||||||
|
IsActive: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create peer share: %v", err)
|
||||||
|
}
|
||||||
|
share, err := r.GetPeerShareByToken("forward-service-token")
|
||||||
|
if err != nil || share == nil {
|
||||||
|
t.Fatalf("load peer share: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO peer_share_runtime(share_id, node_id, reservation_id, resource_key, binding_id, role, chain_name, service_name, protocol, strategy, port, target, applied, status, created_time, updated_time)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, share.ID, share.NodeID, "svc-r1", "svc-rk1", "", "forward", "", "20_2_10", "tcp", "fifo", 31001, "", 1, 1, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert peer_share_runtime: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
h := &Handler{repo: r}
|
||||||
|
h.processFlowItem(1, flowItem{N: "20_2_10_tcp", U: 120, D: 80})
|
||||||
|
|
||||||
|
updatedShare, err := r.GetPeerShare(share.ID)
|
||||||
|
if err != nil || updatedShare == nil {
|
||||||
|
t.Fatalf("reload share: %v", err)
|
||||||
|
}
|
||||||
|
if updatedShare.CurrentFlow != 200 {
|
||||||
|
t.Fatalf("expected current_flow=200, got %d", updatedShare.CurrentFlow)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProcessFlowItemFallsBackToServiceNameWhenForwardIDCollidesAcrossPanels(t *testing.T) {
|
||||||
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel-forward-collision.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open repo: %v", err)
|
||||||
|
}
|
||||||
|
defer r.Close()
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
if err := r.CreatePeerShare(&repo.PeerShare{
|
||||||
|
Name: "collision-share",
|
||||||
|
NodeID: 1,
|
||||||
|
Token: "collision-token",
|
||||||
|
MaxBandwidth: 0,
|
||||||
|
CurrentFlow: 0,
|
||||||
|
PortRangeStart: 31400,
|
||||||
|
PortRangeEnd: 31410,
|
||||||
|
IsActive: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create peer share: %v", err)
|
||||||
|
}
|
||||||
|
share, err := r.GetPeerShareByToken("collision-token")
|
||||||
|
if err != nil || share == nil {
|
||||||
|
t.Fatalf("load peer share: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO peer_share_runtime(share_id, node_id, reservation_id, resource_key, binding_id, role, chain_name, service_name, protocol, strategy, port, target, applied, status, created_time, updated_time)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, share.ID, share.NodeID, "collision-r1", "collision-rk1", "", "forward", "", "20_2_10", "tcp", "fifo", 31401, "", 1, 1, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert peer_share_runtime: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO tunnel(id, name, traffic_ratio, type, protocol, flow, created_time, updated_time, status, in_ip, inx)
|
||||||
|
VALUES(2, 'local-tunnel-with-colliding-forward-id', 1.0, 1, 'tls', 1, ?, ?, 1, NULL, 0)
|
||||||
|
`, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert local tunnel: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO forward(id, user_id, user_name, name, tunnel_id, remote_addr, strategy, in_flow, out_flow, created_time, updated_time, status, inx)
|
||||||
|
VALUES(20, 1, 'local-user', 'local-f20', 2, '8.8.8.8:53', 'fifo', 0, 0, ?, ?, 1, 0)
|
||||||
|
`, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert local forward: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
h := &Handler{repo: r}
|
||||||
|
h.processFlowItem(1, flowItem{N: "20_2_10_tcp", U: 120, D: 80})
|
||||||
|
|
||||||
|
updatedShare, err := r.GetPeerShare(share.ID)
|
||||||
|
if err != nil || updatedShare == nil {
|
||||||
|
t.Fatalf("reload share: %v", err)
|
||||||
|
}
|
||||||
|
if updatedShare.CurrentFlow != 200 {
|
||||||
|
t.Fatalf("expected current_flow=200, got %d", updatedShare.CurrentFlow)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProcessFlowItemSkipsPeerShareFlowWhenServiceNameIsAmbiguous(t *testing.T) {
|
||||||
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel-forward-ambiguous.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open repo: %v", err)
|
||||||
|
}
|
||||||
|
defer r.Close()
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
if err := r.CreatePeerShare(&repo.PeerShare{
|
||||||
|
Name: "ambiguous-share-a",
|
||||||
|
NodeID: 1,
|
||||||
|
Token: "ambiguous-token-a",
|
||||||
|
MaxBandwidth: 0,
|
||||||
|
CurrentFlow: 0,
|
||||||
|
PortRangeStart: 31100,
|
||||||
|
PortRangeEnd: 31110,
|
||||||
|
IsActive: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create share A: %v", err)
|
||||||
|
}
|
||||||
|
if err := r.CreatePeerShare(&repo.PeerShare{
|
||||||
|
Name: "ambiguous-share-b",
|
||||||
|
NodeID: 1,
|
||||||
|
Token: "ambiguous-token-b",
|
||||||
|
MaxBandwidth: 0,
|
||||||
|
CurrentFlow: 0,
|
||||||
|
PortRangeStart: 31200,
|
||||||
|
PortRangeEnd: 31210,
|
||||||
|
IsActive: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create share B: %v", err)
|
||||||
|
}
|
||||||
|
shareA, _ := r.GetPeerShareByToken("ambiguous-token-a")
|
||||||
|
shareB, _ := r.GetPeerShareByToken("ambiguous-token-b")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO peer_share_runtime(share_id, node_id, reservation_id, resource_key, binding_id, role, chain_name, service_name, protocol, strategy, port, target, applied, status, created_time, updated_time)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?),
|
||||||
|
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`,
|
||||||
|
shareA.ID, 1, "amb-r1", "amb-rk1", "", "forward", "", "99_2_10", "tcp", "fifo", 31101, "", 1, 1, now, now,
|
||||||
|
shareB.ID, 1, "amb-r2", "amb-rk2", "", "forward", "", "99_2_10", "tcp", "fifo", 31201, "", 1, 1, now, now,
|
||||||
|
).Error; err != nil {
|
||||||
|
t.Fatalf("insert ambiguous runtimes: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
h := &Handler{repo: r}
|
||||||
|
h.processFlowItem(1, flowItem{N: "99_2_10_tcp", U: 120, D: 80})
|
||||||
|
|
||||||
|
updatedA, _ := r.GetPeerShare(shareA.ID)
|
||||||
|
updatedB, _ := r.GetPeerShare(shareB.ID)
|
||||||
|
if updatedA.CurrentFlow != 0 || updatedB.CurrentFlow != 0 {
|
||||||
|
t.Fatalf("expected ambiguous service flow to be skipped, got shareA=%d shareB=%d", updatedA.CurrentFlow, updatedB.CurrentFlow)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCleanOrphanedServicesSkipsActiveSharedForwardRuntimeServices(t *testing.T) {
|
||||||
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel-cleanup-runtime.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open repo: %v", err)
|
||||||
|
}
|
||||||
|
defer r.Close()
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
if err := r.CreatePeerShare(&repo.PeerShare{
|
||||||
|
Name: "cleanup-runtime-share",
|
||||||
|
NodeID: 1,
|
||||||
|
Token: "cleanup-runtime-token",
|
||||||
|
MaxBandwidth: 0,
|
||||||
|
CurrentFlow: 0,
|
||||||
|
PortRangeStart: 31300,
|
||||||
|
PortRangeEnd: 31310,
|
||||||
|
IsActive: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create peer share: %v", err)
|
||||||
|
}
|
||||||
|
share, err := r.GetPeerShareByToken("cleanup-runtime-token")
|
||||||
|
if err != nil || share == nil {
|
||||||
|
t.Fatalf("load peer share: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO peer_share_runtime(share_id, node_id, reservation_id, resource_key, binding_id, role, chain_name, service_name, protocol, strategy, port, target, applied, status, created_time, updated_time)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, share.ID, share.NodeID, "cleanup-r1", "cleanup-rk1", "", "forward", "", "20_2_10", "tcp", "fifo", 31301, "", 1, 1, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert peer_share_runtime: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
h := &Handler{repo: r}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if rec := recover(); rec != nil {
|
||||||
|
t.Fatalf("cleanOrphanedServices should skip active shared runtime service; got panic: %v", rec)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
h.cleanOrphanedServices(share.NodeID, []namedConfigItem{{Name: "20_2_10_tcp"}})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCleanOrphanedServicesSkipsFederationServicePrefix(t *testing.T) {
|
||||||
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel-cleanup-fed-svc.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open repo: %v", err)
|
||||||
|
}
|
||||||
|
defer r.Close()
|
||||||
|
|
||||||
|
h := &Handler{repo: r}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if rec := recover(); rec != nil {
|
||||||
|
t.Fatalf("cleanOrphanedServices should skip fed_svc_ service names; got panic: %v", rec)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
h.cleanOrphanedServices(1, []namedConfigItem{{Name: "fed_svc_999_tcp"}})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCleanOrphanedServicesSkipsForwardPatternWhenNodeHasActivePeerShareForwardRuntime(t *testing.T) {
|
||||||
|
r, err := repo.Open(filepath.Join(t.TempDir(), "panel-cleanup-forward-runtime-empty-service.db"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open repo: %v", err)
|
||||||
|
}
|
||||||
|
defer r.Close()
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
if err := r.CreatePeerShare(&repo.PeerShare{
|
||||||
|
Name: "cleanup-forward-runtime-empty-service",
|
||||||
|
NodeID: 1,
|
||||||
|
Token: "cleanup-forward-runtime-empty-service-token",
|
||||||
|
MaxBandwidth: 0,
|
||||||
|
CurrentFlow: 0,
|
||||||
|
PortRangeStart: 31420,
|
||||||
|
PortRangeEnd: 31430,
|
||||||
|
IsActive: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create peer share: %v", err)
|
||||||
|
}
|
||||||
|
share, err := r.GetPeerShareByToken("cleanup-forward-runtime-empty-service-token")
|
||||||
|
if err != nil || share == nil {
|
||||||
|
t.Fatalf("load peer share: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO peer_share_runtime(share_id, node_id, reservation_id, resource_key, binding_id, role, chain_name, service_name, protocol, strategy, port, target, applied, status, created_time, updated_time)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, share.ID, share.NodeID, "cleanup-forward-empty-r1", "cleanup-forward-empty-rk1", "", "forward", "", "", "tcp", "fifo", 31421, "", 0, 1, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert peer_share_runtime with empty service name: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
h := &Handler{repo: r}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if rec := recover(); rec != nil {
|
||||||
|
t.Fatalf("cleanOrphanedServices should skip forward-pattern services when active peer-share forward runtime exists; got panic: %v", rec)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
h.cleanOrphanedServices(share.NodeID, []namedConfigItem{{Name: "20_2_10_tcp"}})
|
||||||
|
}
|
||||||
|
|||||||
@@ -219,7 +219,7 @@ func (h *Handler) login(w http.ResponseWriter, r *http.Request) {
|
|||||||
response.WriteJSON(w, response.Err(-2, err.Error()))
|
response.WriteJSON(w, response.Err(-2, err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if captchaEnabled {
|
if captchaEnabled && !h.apiClientCaptchaBypassEnabled(r) {
|
||||||
captchaID := strings.TrimSpace(req.CaptchaID)
|
captchaID := strings.TrimSpace(req.CaptchaID)
|
||||||
if captchaID == "" {
|
if captchaID == "" {
|
||||||
response.WriteJSON(w, response.ErrDefault("验证码校验失败"))
|
response.WriteJSON(w, response.ErrDefault("验证码校验失败"))
|
||||||
@@ -704,7 +704,8 @@ func (h *Handler) flowConfig(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func (h *Handler) flowUpload(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) flowUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
secret := r.URL.Query().Get("secret")
|
secret := r.URL.Query().Get("secret")
|
||||||
if ok, _ := h.repo.NodeExistsBySecret(secret); !ok {
|
node, _ := h.repo.GetNodeBySecret(secret)
|
||||||
|
if node == nil {
|
||||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
_, _ = w.Write([]byte("ok"))
|
_, _ = w.Write([]byte("ok"))
|
||||||
return
|
return
|
||||||
@@ -715,7 +716,7 @@ func (h *Handler) flowUpload(w http.ResponseWriter, r *http.Request) {
|
|||||||
var items []flowItem
|
var items []flowItem
|
||||||
if json.Unmarshal([]byte(raw), &items) == nil {
|
if json.Unmarshal([]byte(raw), &items) == nil {
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
h.processFlowItem(item)
|
h.processFlowItem(node.ID, item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -994,6 +995,20 @@ func (h *Handler) captchaEnabled() (bool, error) {
|
|||||||
return strings.EqualFold(cfg.Value, "true"), nil
|
return strings.EqualFold(cfg.Value, "true"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Handler) apiClientCaptchaBypassEnabled(r *http.Request) bool {
|
||||||
|
if r == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
client := strings.ToLower(strings.TrimSpace(r.Header.Get("X-FLVX-API-Client")))
|
||||||
|
switch client {
|
||||||
|
case "whmcs", "whmcs-module":
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handler) markCaptchaToken(token string) {
|
func (h *Handler) markCaptchaToken(token string) {
|
||||||
if h == nil {
|
if h == nil {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -69,6 +69,13 @@ func TestRunResetAndExpiryJobResetsFlowAndDisablesExpiredRecords(t *testing.T) {
|
|||||||
t.Fatalf("insert expired user: %v", err)
|
t.Fatalf("insert expired user: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO user(id, user, pwd, role_id, exp_time, flow, in_flow, out_flow, flow_reset_time, num, created_time, updated_time, status)
|
||||||
|
VALUES(3, 'non_expiring_user', 'x', 1, 0, 100, 1000, 2000, 15, 1, ?, ?, 1)
|
||||||
|
`, nowMs, nowMs).Error; err != nil {
|
||||||
|
t.Fatalf("insert non-expiring user: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
if err := r.DB().Exec(`
|
if err := r.DB().Exec(`
|
||||||
INSERT INTO tunnel(id, name, traffic_ratio, type, protocol, flow, created_time, updated_time, status, in_ip, inx)
|
INSERT INTO tunnel(id, name, traffic_ratio, type, protocol, flow, created_time, updated_time, status, in_ip, inx)
|
||||||
VALUES(1, 't1', 1.0, 1, 'tls', 1, ?, ?, 1, NULL, 0)
|
VALUES(1, 't1', 1.0, 1, 'tls', 1, ?, ?, 1, NULL, 0)
|
||||||
@@ -83,6 +90,13 @@ func TestRunResetAndExpiryJobResetsFlowAndDisablesExpiredRecords(t *testing.T) {
|
|||||||
t.Fatalf("insert expired user_tunnel: %v", err)
|
t.Fatalf("insert expired user_tunnel: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO user_tunnel(id, user_id, tunnel_id, speed_id, num, flow, in_flow, out_flow, flow_reset_time, exp_time, status)
|
||||||
|
VALUES(11, 3, 1, NULL, 1, 1, 300, 400, 15, 0, 1)
|
||||||
|
`).Error; err != nil {
|
||||||
|
t.Fatalf("insert non-expiring user_tunnel: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
if err := r.DB().Exec(`
|
if err := r.DB().Exec(`
|
||||||
INSERT INTO forward(id, user_id, user_name, name, tunnel_id, remote_addr, strategy, in_flow, out_flow, created_time, updated_time, status, inx)
|
INSERT INTO forward(id, user_id, user_name, name, tunnel_id, remote_addr, strategy, in_flow, out_flow, created_time, updated_time, status, inx)
|
||||||
VALUES(20, 2, 'expired_user', 'f1', 1, '1.1.1.1:443', 'fifo', 0, 0, ?, ?, 1, 0)
|
VALUES(20, 2, 'expired_user', 'f1', 1, '1.1.1.1:443', 'fifo', 0, 0, ?, ?, 1, 0)
|
||||||
@@ -90,6 +104,13 @@ func TestRunResetAndExpiryJobResetsFlowAndDisablesExpiredRecords(t *testing.T) {
|
|||||||
t.Fatalf("insert forward: %v", err)
|
t.Fatalf("insert forward: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO forward(id, user_id, user_name, name, tunnel_id, remote_addr, strategy, in_flow, out_flow, created_time, updated_time, status, inx)
|
||||||
|
VALUES(21, 3, 'non_expiring_user', 'f2', 1, '1.1.1.1:443', 'fifo', 0, 0, ?, ?, 1, 1)
|
||||||
|
`, nowMs, nowMs).Error; err != nil {
|
||||||
|
t.Fatalf("insert non-expiring forward: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
h.runResetAndExpiryJob(now)
|
h.runResetAndExpiryJob(now)
|
||||||
|
|
||||||
userIn, userOut, userStatus := mustQueryInt64Int64Int(t, r, `SELECT in_flow, out_flow, status FROM user WHERE id = 2`)
|
userIn, userOut, userStatus := mustQueryInt64Int64Int(t, r, `SELECT in_flow, out_flow, status FROM user WHERE id = 2`)
|
||||||
@@ -106,4 +127,19 @@ func TestRunResetAndExpiryJobResetsFlowAndDisablesExpiredRecords(t *testing.T) {
|
|||||||
if forwardStatus != 0 {
|
if forwardStatus != 0 {
|
||||||
t.Fatalf("expected forward status=0 after expiry handling, got %d", forwardStatus)
|
t.Fatalf("expected forward status=0 after expiry handling, got %d", forwardStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nonExpUserStatus := mustQueryInt(t, r, `SELECT status FROM user WHERE id = 3`)
|
||||||
|
if nonExpUserStatus != 1 {
|
||||||
|
t.Fatalf("expected non-expiring user to remain enabled, got status=%d", nonExpUserStatus)
|
||||||
|
}
|
||||||
|
|
||||||
|
nonExpTunnelStatus := mustQueryInt(t, r, `SELECT status FROM user_tunnel WHERE id = 11`)
|
||||||
|
if nonExpTunnelStatus != 1 {
|
||||||
|
t.Fatalf("expected non-expiring user_tunnel to remain enabled, got status=%d", nonExpTunnelStatus)
|
||||||
|
}
|
||||||
|
|
||||||
|
nonExpForwardStatus := mustQueryInt(t, r, `SELECT status FROM forward WHERE id = 21`)
|
||||||
|
if nonExpForwardStatus != 1 {
|
||||||
|
t.Fatalf("expected non-expiring forward to remain enabled, got status=%d", nonExpForwardStatus)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1048,21 +1048,55 @@ func (h *Handler) userTunnelUpdate(w http.ResponseWriter, r *http.Request) {
|
|||||||
response.WriteJSON(w, response.ErrDefault("权限ID不能为空"))
|
response.WriteJSON(w, response.ErrDefault("权限ID不能为空"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
speedID := asAnyToInt64Ptr(req["speedId"])
|
||||||
|
if err := h.validateSpeedLimitReference(speedID); err != nil {
|
||||||
|
response.WriteJSON(w, response.Err(-2, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userID, tunnelID, utErr := h.repo.GetUserTunnelUserAndTunnel(id)
|
||||||
|
if utErr != nil {
|
||||||
|
response.WriteJSON(w, response.Err(-2, utErr.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, oldFlow, oldNum, oldExpTime, oldFlowReset, oldSpeedID, oldStatus, oldErr :=
|
||||||
|
h.repo.GetExistingUserTunnel(userID, tunnelID)
|
||||||
|
if oldErr != nil {
|
||||||
|
response.WriteJSON(w, response.Err(-2, oldErr.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if err := h.repo.UpdateUserTunnel(id,
|
if err := h.repo.UpdateUserTunnel(id,
|
||||||
asInt64(req["flow"], 0),
|
asInt64(req["flow"], 0),
|
||||||
asInt(req["num"], 0),
|
asInt(req["num"], 0),
|
||||||
asInt64(req["expTime"], time.Now().Add(365*24*time.Hour).UnixMilli()),
|
asInt64(req["expTime"], time.Now().Add(365*24*time.Hour).UnixMilli()),
|
||||||
asInt64(req["flowResetTime"], 1),
|
asInt64(req["flowResetTime"], 1),
|
||||||
nullableInt(asAnyToInt64Ptr(req["speedId"])),
|
nullableInt(speedID),
|
||||||
asInt(req["status"], 1),
|
asInt(req["status"], 1),
|
||||||
); err != nil {
|
); err != nil {
|
||||||
response.WriteJSON(w, response.Err(-2, err.Error()))
|
response.WriteJSON(w, response.Err(-2, err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userID, tunnelID, utErr := h.repo.GetUserTunnelUserAndTunnel(id)
|
if syncErr := h.syncUserTunnelForwards(userID, tunnelID); syncErr != nil {
|
||||||
if utErr == nil {
|
rollbackErr := h.repo.UpdateUserTunnel(
|
||||||
h.syncUserTunnelForwards(userID, tunnelID)
|
id,
|
||||||
|
oldFlow,
|
||||||
|
int(oldNum),
|
||||||
|
oldExpTime,
|
||||||
|
oldFlowReset,
|
||||||
|
oldSpeedID,
|
||||||
|
oldStatus,
|
||||||
|
)
|
||||||
|
if rollbackErr != nil {
|
||||||
|
response.WriteJSON(w, response.Err(-2, fmt.Sprintf("下发失败且回滚失败: %v; 回滚错误: %v", syncErr, rollbackErr)))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
response.WriteJSON(w, response.Err(-2, fmt.Sprintf("下发失败,已回滚: %v", syncErr)))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response.WriteJSON(w, response.OKEmpty())
|
response.WriteJSON(w, response.OKEmpty())
|
||||||
@@ -1103,6 +1137,18 @@ func (h *Handler) forwardCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
response.WriteJSON(w, response.ErrDefault("转发名称和目标地址不能为空"))
|
response.WriteJSON(w, response.ErrDefault("转发名称和目标地址不能为空"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
speedID := asAnyToInt64Ptr(req["speedId"])
|
||||||
|
if speedID != nil {
|
||||||
|
exists, speedErr := h.repo.SpeedLimitExists(*speedID)
|
||||||
|
if speedErr != nil {
|
||||||
|
response.WriteJSON(w, response.Err(-2, speedErr.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
response.WriteJSON(w, response.ErrDefault("限速规则不存在"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
port := asInt(req["inPort"], 0)
|
port := asInt(req["inPort"], 0)
|
||||||
if port <= 0 {
|
if port <= 0 {
|
||||||
port = h.pickTunnelPort(tunnelID)
|
port = h.pickTunnelPort(tunnelID)
|
||||||
@@ -1127,7 +1173,7 @@ func (h *Handler) forwardCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
if userName == "" {
|
if userName == "" {
|
||||||
userName = "user"
|
userName = "user"
|
||||||
}
|
}
|
||||||
forwardID, err := h.repo.CreateForwardTx(userID, userName, name, tunnelID, remoteAddr, defaultString(asString(req["strategy"]), "fifo"), now, inx, entryNodes, port)
|
forwardID, err := h.repo.CreateForwardTx(userID, userName, name, tunnelID, remoteAddr, defaultString(asString(req["strategy"]), "fifo"), now, inx, entryNodes, port, nullableInt(speedID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.WriteJSON(w, response.Err(-2, err.Error()))
|
response.WriteJSON(w, response.Err(-2, err.Error()))
|
||||||
return
|
return
|
||||||
@@ -1202,6 +1248,24 @@ func (h *Handler) forwardUpdate(w http.ResponseWriter, r *http.Request) {
|
|||||||
if strategy == "" {
|
if strategy == "" {
|
||||||
strategy = forward.Strategy
|
strategy = forward.Strategy
|
||||||
}
|
}
|
||||||
|
speedID := asAnyToInt64Ptr(req["speedId"])
|
||||||
|
if speedID != nil {
|
||||||
|
exists, speedErr := h.repo.SpeedLimitExists(*speedID)
|
||||||
|
if speedErr != nil {
|
||||||
|
response.WriteJSON(w, response.Err(-2, speedErr.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
response.WriteJSON(w, response.ErrDefault("限速规则不存在"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newSpeedID := forward.SpeedID
|
||||||
|
if speedID != nil {
|
||||||
|
newSpeedID = sql.NullInt64{Int64: *speedID, Valid: true}
|
||||||
|
} else if _, ok := req["speedId"]; ok {
|
||||||
|
newSpeedID = sql.NullInt64{Valid: false}
|
||||||
|
}
|
||||||
|
|
||||||
port := asInt(req["inPort"], 0)
|
port := asInt(req["inPort"], 0)
|
||||||
if port <= 0 {
|
if port <= 0 {
|
||||||
@@ -1225,7 +1289,7 @@ func (h *Handler) forwardUpdate(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
now := time.Now().UnixMilli()
|
now := time.Now().UnixMilli()
|
||||||
if err := h.repo.UpdateForward(id, name, tunnelID, remoteAddr, strategy, now); err != nil {
|
if err := h.repo.UpdateForward(id, name, tunnelID, remoteAddr, strategy, now, newSpeedID); err != nil {
|
||||||
response.WriteJSON(w, response.Err(-2, err.Error()))
|
response.WriteJSON(w, response.Err(-2, err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -1586,29 +1650,37 @@ func (h *Handler) speedLimitCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
response.WriteJSON(w, response.ErrDefault("请求参数错误"))
|
response.WriteJSON(w, response.ErrDefault("请求参数错误"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tunnelID := asInt64(req["tunnelId"], 0)
|
|
||||||
if tunnelID <= 0 {
|
|
||||||
response.WriteJSON(w, response.ErrDefault("隧道ID不能为空"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
name := asString(req["name"])
|
name := asString(req["name"])
|
||||||
if name == "" {
|
if name == "" {
|
||||||
response.WriteJSON(w, response.ErrDefault("名称不能为空"))
|
response.WriteJSON(w, response.ErrDefault("名称不能为空"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tunnelName := h.repo.GetTunnelNameByID(tunnelID)
|
|
||||||
if tunnelName == "" {
|
|
||||||
response.WriteJSON(w, response.ErrDefault("隧道不存在"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
now := time.Now().UnixMilli()
|
|
||||||
speed := asInt(req["speed"], 100)
|
speed := asInt(req["speed"], 100)
|
||||||
|
|
||||||
|
var tunnelID *int64
|
||||||
|
var tunnelName string
|
||||||
|
if tid := asInt64(req["tunnelId"], 0); tid > 0 {
|
||||||
|
tunnelID = &tid
|
||||||
|
tunnelName = h.repo.GetTunnelNameByID(tid)
|
||||||
|
if tunnelName == "" {
|
||||||
|
response.WriteJSON(w, response.ErrDefault("隧道不存在"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
id, err := h.repo.CreateSpeedLimit(name, speed, tunnelID, tunnelName, now, asInt(req["status"], 1))
|
id, err := h.repo.CreateSpeedLimit(name, speed, tunnelID, tunnelName, now, asInt(req["status"], 1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.WriteJSON(w, response.Err(-2, err.Error()))
|
response.WriteJSON(w, response.Err(-2, err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_ = h.sendLimiterConfig(id, speed, tunnelID)
|
|
||||||
|
if tunnelID != nil && *tunnelID > 0 {
|
||||||
|
_ = h.sendLimiterConfig(id, speed, *tunnelID)
|
||||||
|
}
|
||||||
|
|
||||||
response.WriteJSON(w, response.OKEmpty())
|
response.WriteJSON(w, response.OKEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1618,23 +1690,41 @@ func (h *Handler) speedLimitUpdate(w http.ResponseWriter, r *http.Request) {
|
|||||||
response.WriteJSON(w, response.ErrDefault("请求参数错误"))
|
response.WriteJSON(w, response.ErrDefault("请求参数错误"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
id := asInt64(req["id"], 0)
|
id := asInt64(req["id"], 0)
|
||||||
tunnelID := asInt64(req["tunnelId"], 0)
|
if id <= 0 {
|
||||||
if id <= 0 || tunnelID <= 0 {
|
response.WriteJSON(w, response.ErrDefault("限速规则ID不能为空"))
|
||||||
response.WriteJSON(w, response.ErrDefault("请求参数错误"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tunnelName := h.repo.GetTunnelNameByID(tunnelID)
|
|
||||||
if tunnelName == "" {
|
name := asString(req["name"])
|
||||||
response.WriteJSON(w, response.ErrDefault("隧道不存在"))
|
if name == "" {
|
||||||
|
response.WriteJSON(w, response.ErrDefault("名称不能为空"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
speed := asInt(req["speed"], 100)
|
speed := asInt(req["speed"], 100)
|
||||||
if err := h.repo.UpdateSpeedLimit(id, asString(req["name"]), speed, tunnelID, tunnelName, asInt(req["status"], 1), time.Now().UnixMilli()); err != nil {
|
|
||||||
|
var tunnelID *int64
|
||||||
|
var tunnelName string
|
||||||
|
if tid := asInt64(req["tunnelId"], 0); tid > 0 {
|
||||||
|
tunnelID = &tid
|
||||||
|
tunnelName = h.repo.GetTunnelNameByID(tid)
|
||||||
|
if tunnelName == "" {
|
||||||
|
response.WriteJSON(w, response.ErrDefault("隧道不存在"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.repo.UpdateSpeedLimit(id, name, speed, tunnelID, tunnelName, asInt(req["status"], 1), time.Now().UnixMilli()); err != nil {
|
||||||
response.WriteJSON(w, response.Err(-2, err.Error()))
|
response.WriteJSON(w, response.Err(-2, err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_ = h.sendLimiterConfig(id, speed, tunnelID)
|
|
||||||
|
if tunnelID != nil && *tunnelID > 0 {
|
||||||
|
_ = h.sendLimiterConfig(id, speed, *tunnelID)
|
||||||
|
}
|
||||||
|
|
||||||
response.WriteJSON(w, response.OKEmpty())
|
response.WriteJSON(w, response.OKEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1643,15 +1733,18 @@ func (h *Handler) speedLimitDelete(w http.ResponseWriter, r *http.Request) {
|
|||||||
if id <= 0 {
|
if id <= 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tunnelID := h.repo.GetSpeedLimitTunnelID(id)
|
tunnelID := h.repo.GetSpeedLimitTunnelID(id)
|
||||||
|
|
||||||
if err := h.repo.DeleteSpeedLimit(id); err != nil {
|
if err := h.repo.DeleteSpeedLimit(id); err != nil {
|
||||||
response.WriteJSON(w, response.Err(-2, err.Error()))
|
response.WriteJSON(w, response.Err(-2, err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if tunnelID > 0 {
|
|
||||||
_ = h.sendDeleteLimiterConfig(id, tunnelID)
|
if tunnelID.Valid && tunnelID.Int64 > 0 {
|
||||||
|
_ = h.sendDeleteLimiterConfig(id, tunnelID.Int64)
|
||||||
}
|
}
|
||||||
|
|
||||||
response.WriteJSON(w, response.OKEmpty())
|
response.WriteJSON(w, response.OKEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2713,7 +2806,7 @@ func pickNodeAddressV6(node *nodeRecord) string {
|
|||||||
func (h *Handler) replaceTunnelChainsTx(tx *gorm.DB, tunnelID int64, req map[string]interface{}) error {
|
func (h *Handler) replaceTunnelChainsTx(tx *gorm.DB, tunnelID int64, req map[string]interface{}) error {
|
||||||
allocated := map[int64]int{}
|
allocated := map[int64]int{}
|
||||||
inNodes := asMapSlice(req["inNodeId"])
|
inNodes := asMapSlice(req["inNodeId"])
|
||||||
for _, n := range inNodes {
|
for i, n := range inNodes {
|
||||||
nodeID := asInt64(n["nodeId"], 0)
|
nodeID := asInt64(n["nodeId"], 0)
|
||||||
if nodeID <= 0 {
|
if nodeID <= 0 {
|
||||||
continue
|
continue
|
||||||
@@ -2725,13 +2818,13 @@ func (h *Handler) replaceTunnelChainsTx(tx *gorm.DB, tunnelID int64, req map[str
|
|||||||
nodeID,
|
nodeID,
|
||||||
sql.NullInt64{},
|
sql.NullInt64{},
|
||||||
defaultString(asString(n["strategy"]), "round"),
|
defaultString(asString(n["strategy"]), "round"),
|
||||||
0,
|
i+1,
|
||||||
defaultString(asString(n["protocol"]), "tls"),
|
defaultString(asString(n["protocol"]), "tls"),
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, n := range asMapSlice(req["outNodeId"]) {
|
for i, n := range asMapSlice(req["outNodeId"]) {
|
||||||
nodeID := asInt64(n["nodeId"], 0)
|
nodeID := asInt64(n["nodeId"], 0)
|
||||||
if nodeID <= 0 {
|
if nodeID <= 0 {
|
||||||
continue
|
continue
|
||||||
@@ -2751,7 +2844,7 @@ func (h *Handler) replaceTunnelChainsTx(tx *gorm.DB, tunnelID int64, req map[str
|
|||||||
nodeID,
|
nodeID,
|
||||||
sql.NullInt64{Int64: int64(port), Valid: true},
|
sql.NullInt64{Int64: int64(port), Valid: true},
|
||||||
defaultString(asString(n["strategy"]), "round"),
|
defaultString(asString(n["strategy"]), "round"),
|
||||||
0,
|
i+1,
|
||||||
defaultString(asString(n["protocol"]), "tls"),
|
defaultString(asString(n["protocol"]), "tls"),
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -2794,7 +2887,21 @@ func (h *Handler) deleteNodeByID(id int64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) deleteTunnelByID(id int64) error {
|
func (h *Handler) deleteTunnelByID(id int64) error {
|
||||||
return h.repo.DeleteTunnelCascade(id)
|
if h == nil || h.repo == nil {
|
||||||
|
return errors.New("repository not initialized")
|
||||||
|
}
|
||||||
|
|
||||||
|
tunnelName, _ := h.repo.GetTunnelName(id)
|
||||||
|
if err := h.repo.DeleteTunnelCascade(id); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
shareID, port, ok := parsePeerShareInfoFromFederationTunnelName(tunnelName)
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return h.repo.MarkPeerShareRuntimeReleasedByPort(shareID, port, time.Now().UnixMilli())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) deleteForwardByID(id int64) error {
|
func (h *Handler) deleteForwardByID(id int64) error {
|
||||||
@@ -2963,6 +3070,7 @@ func (h *Handler) rollbackForwardMutation(oldForward *forwardRecord, oldPorts []
|
|||||||
h.repo.RollbackForwardFields(
|
h.repo.RollbackForwardFields(
|
||||||
oldForward.ID, oldForward.UserID, oldForward.UserName, oldForward.Name,
|
oldForward.ID, oldForward.UserID, oldForward.UserName, oldForward.Name,
|
||||||
oldForward.TunnelID, oldForward.RemoteAddr, oldForward.Strategy, oldForward.Status,
|
oldForward.TunnelID, oldForward.RemoteAddr, oldForward.Strategy, oldForward.Status,
|
||||||
|
oldForward.SpeedID,
|
||||||
time.Now().UnixMilli(),
|
time.Now().UnixMilli(),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -2984,6 +3092,10 @@ func (h *Handler) upsertUserTunnel(req map[string]interface{}) error {
|
|||||||
h.repo.GetExistingUserTunnel(userID, tunnelID)
|
h.repo.GetExistingUserTunnel(userID, tunnelID)
|
||||||
|
|
||||||
speedID := asAnyToInt64Ptr(req["speedId"])
|
speedID := asAnyToInt64Ptr(req["speedId"])
|
||||||
|
if err := h.validateSpeedLimitReference(speedID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
reqFlow := asInt64(req["flow"], -1)
|
reqFlow := asInt64(req["flow"], -1)
|
||||||
reqNum := asInt(req["num"], -1)
|
reqNum := asInt(req["num"], -1)
|
||||||
reqExpTime := asInt64(req["expTime"], -1)
|
reqExpTime := asInt64(req["expTime"], -1)
|
||||||
@@ -3024,7 +3136,24 @@ func (h *Handler) upsertUserTunnel(req map[string]interface{}) error {
|
|||||||
reqStatus = 1
|
reqStatus = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return h.repo.InsertUserTunnel(userID, tunnelID, nullableInt(speedID), reqNum, reqFlow, reqFlowReset, reqExpTime, reqStatus)
|
if err := h.repo.InsertUserTunnel(userID, tunnelID, nullableInt(speedID), reqNum, reqFlow, reqFlowReset, reqExpTime, reqStatus); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if syncErr := h.syncUserTunnelForwards(userID, tunnelID); syncErr != nil {
|
||||||
|
insertedID, _, _, _, _, _, _, lookupErr := h.repo.GetExistingUserTunnel(userID, tunnelID)
|
||||||
|
if lookupErr != nil {
|
||||||
|
return fmt.Errorf("下发失败且回滚失败: %v; 回滚查询错误: %w", syncErr, lookupErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rollbackErr := h.repo.DeleteUserTunnel(insertedID); rollbackErr != nil {
|
||||||
|
return fmt.Errorf("下发失败且回滚失败: %v; 回滚删除错误: %w", syncErr, rollbackErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("下发失败,已回滚: %w", syncErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -3062,25 +3191,61 @@ func (h *Handler) upsertUserTunnel(req map[string]interface{}) error {
|
|||||||
newSpeedID = sql.NullInt64{Valid: false}
|
newSpeedID = sql.NullInt64{Valid: false}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.repo.UpdateUserTunnelFields(existingID, newSpeedID, newFlow, newNum, newExpTime, newFlowReset, newStatus)
|
if err := h.repo.UpdateUserTunnelFields(existingID, newSpeedID, newFlow, newNum, newExpTime, newFlowReset, newStatus); err != nil {
|
||||||
|
return err
|
||||||
if err == nil {
|
|
||||||
h.syncUserTunnelForwards(userID, tunnelID)
|
|
||||||
}
|
}
|
||||||
return err
|
|
||||||
|
if syncErr := h.syncUserTunnelForwards(userID, tunnelID); syncErr != nil {
|
||||||
|
rollbackErr := h.repo.UpdateUserTunnelFields(
|
||||||
|
existingID,
|
||||||
|
currentSpeedID,
|
||||||
|
currentFlow,
|
||||||
|
int(currentNum),
|
||||||
|
currentExpTime,
|
||||||
|
currentFlowReset,
|
||||||
|
currentStatus,
|
||||||
|
)
|
||||||
|
if rollbackErr != nil {
|
||||||
|
return fmt.Errorf("下发失败且回滚失败: %v; 回滚错误: %w", syncErr, rollbackErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("下发失败,已回滚: %w", syncErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) syncUserTunnelForwards(userID, tunnelID int64) {
|
func (h *Handler) syncUserTunnelForwards(userID, tunnelID int64) error {
|
||||||
forwards, err := h.listForwardsByTunnel(tunnelID)
|
forwards, err := h.listForwardsByTunnel(tunnelID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return err
|
||||||
}
|
}
|
||||||
for i := range forwards {
|
for i := range forwards {
|
||||||
f := &forwards[i]
|
f := &forwards[i]
|
||||||
if f.UserID == userID {
|
if f.UserID == userID {
|
||||||
_ = h.syncForwardServices(f, "UpdateService", true)
|
if err := h.syncForwardServices(f, "UpdateService", true); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) validateSpeedLimitReference(speedID *int64) error {
|
||||||
|
if speedID == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
exists, err := h.repo.SpeedLimitExists(*speedID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
return errors.New("限速规则不存在")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func asAnySlice(v interface{}) []interface{} {
|
func asAnySlice(v interface{}) []interface{} {
|
||||||
|
|||||||
@@ -29,19 +29,20 @@ func (User) TableName() string { return "user" }
|
|||||||
|
|
||||||
// Forward maps to the "forward" table.
|
// Forward maps to the "forward" table.
|
||||||
type Forward struct {
|
type Forward struct {
|
||||||
ID int64 `gorm:"primaryKey;autoIncrement"`
|
ID int64 `gorm:"primaryKey;autoIncrement"`
|
||||||
UserID int64 `gorm:"column:user_id;not null"`
|
UserID int64 `gorm:"column:user_id;not null"`
|
||||||
UserName string `gorm:"column:user_name;type:varchar(100);not null"`
|
UserName string `gorm:"column:user_name;type:varchar(100);not null"`
|
||||||
Name string `gorm:"type:varchar(100);not null"`
|
Name string `gorm:"type:varchar(100);not null"`
|
||||||
TunnelID int64 `gorm:"column:tunnel_id;not null"`
|
TunnelID int64 `gorm:"column:tunnel_id;not null"`
|
||||||
RemoteAddr string `gorm:"column:remote_addr;type:text;not null"`
|
RemoteAddr string `gorm:"column:remote_addr;type:text;not null"`
|
||||||
Strategy string `gorm:"type:varchar(100);not null;default:'fifo'"`
|
Strategy string `gorm:"type:varchar(100);not null;default:'fifo'"`
|
||||||
InFlow int64 `gorm:"column:in_flow;not null;default:0"`
|
InFlow int64 `gorm:"not null;default:0"`
|
||||||
OutFlow int64 `gorm:"column:out_flow;not null;default:0"`
|
OutFlow int64 `gorm:"column:out_flow;not null;default:0"`
|
||||||
CreatedTime int64 `gorm:"column:created_time;not null"`
|
CreatedTime int64 `gorm:"column:created_time;not null"`
|
||||||
UpdatedTime int64 `gorm:"column:updated_time;not null"`
|
UpdatedTime int64 `gorm:"column:updated_time;not null"`
|
||||||
Status int `gorm:"not null"`
|
Status int `gorm:"not null"`
|
||||||
Inx int `gorm:"not null;default:0"`
|
Inx int `gorm:"not null;default:0"`
|
||||||
|
SpeedID sql.NullInt64 `gorm:"column:speed_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Forward) TableName() string { return "forward" }
|
func (Forward) TableName() string { return "forward" }
|
||||||
@@ -83,14 +84,14 @@ type Node struct {
|
|||||||
func (Node) TableName() string { return "node" }
|
func (Node) TableName() string { return "node" }
|
||||||
|
|
||||||
type SpeedLimit struct {
|
type SpeedLimit struct {
|
||||||
ID int64 `gorm:"primaryKey;autoIncrement"`
|
ID int64 `gorm:"primaryKey;autoIncrement"`
|
||||||
Name string `gorm:"type:varchar(100);not null"`
|
Name string `gorm:"type:varchar(100);not null"`
|
||||||
Speed int `gorm:"not null"`
|
Speed int `gorm:"not null"`
|
||||||
TunnelID int64 `gorm:"column:tunnel_id;not null"`
|
TunnelID sql.NullInt64 `gorm:"column:tunnel_id"`
|
||||||
TunnelName string `gorm:"column:tunnel_name;type:varchar(100);not null"`
|
TunnelName sql.NullString `gorm:"column:tunnel_name;type:varchar(100)"`
|
||||||
CreatedTime int64 `gorm:"column:created_time;not null"`
|
CreatedTime int64 `gorm:"column:created_time;not null"`
|
||||||
UpdatedTime sql.NullInt64 `gorm:"column:updated_time"`
|
UpdatedTime sql.NullInt64 `gorm:"column:updated_time"`
|
||||||
Status int `gorm:"not null"`
|
Status int `gorm:"not null"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (SpeedLimit) TableName() string { return "speed_limit" }
|
func (SpeedLimit) TableName() string { return "speed_limit" }
|
||||||
@@ -395,6 +396,7 @@ type ForwardBackup struct {
|
|||||||
UpdatedTime int64 `json:"updatedTime"`
|
UpdatedTime int64 `json:"updatedTime"`
|
||||||
Status int `json:"status"`
|
Status int `json:"status"`
|
||||||
Inx int `json:"inx"`
|
Inx int `json:"inx"`
|
||||||
|
SpeedID *int64 `json:"speedId,omitempty"`
|
||||||
ForwardPorts *[]ForwardPortBackup `json:"forwardPorts,omitempty"`
|
ForwardPorts *[]ForwardPortBackup `json:"forwardPorts,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -421,8 +423,8 @@ type SpeedLimitBackup struct {
|
|||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Speed int64 `json:"speed"`
|
Speed int64 `json:"speed"`
|
||||||
TunnelID int64 `json:"tunnelId"`
|
TunnelID *int64 `json:"tunnelId,omitempty"`
|
||||||
TunnelName string `json:"tunnelName"`
|
TunnelName string `json:"tunnelName,omitempty"`
|
||||||
CreatedTime int64 `json:"createdTime"`
|
CreatedTime int64 `json:"createdTime"`
|
||||||
UpdatedTime int64 `json:"updatedTime,omitempty"`
|
UpdatedTime int64 `json:"updatedTime,omitempty"`
|
||||||
Status int `json:"status"`
|
Status int `json:"status"`
|
||||||
@@ -492,6 +494,7 @@ type ForwardRecord struct {
|
|||||||
RemoteAddr string
|
RemoteAddr string
|
||||||
Strategy string
|
Strategy string
|
||||||
Status int
|
Status int
|
||||||
|
SpeedID sql.NullInt64
|
||||||
}
|
}
|
||||||
|
|
||||||
// TunnelRecord is a minimal tunnel view used by control plane.
|
// TunnelRecord is a minimal tunnel view used by control plane.
|
||||||
|
|||||||
@@ -656,7 +656,7 @@ func (r *Repository) ListUsers() ([]map[string]interface{}, error) {
|
|||||||
return nil, errors.New("repository not initialized")
|
return nil, errors.New("repository not initialized")
|
||||||
}
|
}
|
||||||
var users []model.User
|
var users []model.User
|
||||||
if err := r.db.Where("role_id != ?", 0).Order("id ASC").Find(&users).Error; err != nil {
|
if err := r.db.Where("role_id != ?", 0).Order("id DESC").Find(&users).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
items := make([]map[string]interface{}, 0, len(users))
|
items := make([]map[string]interface{}, 0, len(users))
|
||||||
@@ -678,17 +678,23 @@ func (r *Repository) ListSpeedLimits() ([]map[string]interface{}, error) {
|
|||||||
return nil, errors.New("repository not initialized")
|
return nil, errors.New("repository not initialized")
|
||||||
}
|
}
|
||||||
var limits []model.SpeedLimit
|
var limits []model.SpeedLimit
|
||||||
if err := r.db.Order("id ASC").Find(&limits).Error; err != nil {
|
if err := r.db.Order("id DESC").Find(&limits).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
items := make([]map[string]interface{}, 0, len(limits))
|
items := make([]map[string]interface{}, 0, len(limits))
|
||||||
for _, sl := range limits {
|
for _, sl := range limits {
|
||||||
items = append(items, map[string]interface{}{
|
item := map[string]interface{}{
|
||||||
"id": sl.ID, "name": sl.Name, "speed": sl.Speed,
|
"id": sl.ID, "name": sl.Name, "speed": sl.Speed,
|
||||||
"tunnelId": sl.TunnelID, "tunnelName": sl.TunnelName,
|
|
||||||
"status": sl.Status, "createdTime": sl.CreatedTime,
|
"status": sl.Status, "createdTime": sl.CreatedTime,
|
||||||
"updatedTime": nullableInt64(sl.UpdatedTime),
|
"updatedTime": nullableInt64(sl.UpdatedTime),
|
||||||
})
|
}
|
||||||
|
if sl.TunnelID.Valid {
|
||||||
|
item["tunnelId"] = sl.TunnelID.Int64
|
||||||
|
}
|
||||||
|
if sl.TunnelName.Valid {
|
||||||
|
item["tunnelName"] = sl.TunnelName.String
|
||||||
|
}
|
||||||
|
items = append(items, item)
|
||||||
}
|
}
|
||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
@@ -712,11 +718,12 @@ func (r *Repository) ListForwards() ([]map[string]interface{}, error) {
|
|||||||
CreatedTime int64
|
CreatedTime int64
|
||||||
Status int
|
Status int
|
||||||
Inx int
|
Inx int
|
||||||
|
SpeedID sql.NullInt64
|
||||||
}
|
}
|
||||||
|
|
||||||
var rows []fwdRow
|
var rows []fwdRow
|
||||||
err := r.db.Model(&model.Forward{}).
|
err := r.db.Model(&model.Forward{}).
|
||||||
Select("forward.id, forward.user_id, forward.user_name, forward.name, forward.tunnel_id, COALESCE(tunnel.name, '') AS tunnel_name, forward.remote_addr, COALESCE(forward.strategy, 'fifo') AS strategy, forward.in_flow, forward.out_flow, forward.created_time, forward.status, forward.inx").
|
Select("forward.id, forward.user_id, forward.user_name, forward.name, forward.tunnel_id, COALESCE(tunnel.name, '') AS tunnel_name, forward.remote_addr, COALESCE(forward.strategy, 'fifo') AS strategy, forward.in_flow, forward.out_flow, forward.created_time, forward.status, forward.inx, forward.speed_id").
|
||||||
Joins("LEFT JOIN tunnel ON tunnel.id = forward.tunnel_id").
|
Joins("LEFT JOIN tunnel ON tunnel.id = forward.tunnel_id").
|
||||||
Order("forward.inx ASC, forward.id ASC").
|
Order("forward.inx ASC, forward.id ASC").
|
||||||
Find(&rows).Error
|
Find(&rows).Error
|
||||||
@@ -730,14 +737,18 @@ func (r *Repository) ListForwards() ([]map[string]interface{}, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
items = append(items, map[string]interface{}{
|
item := map[string]interface{}{
|
||||||
"id": row.ID, "userId": row.UserID, "userName": row.UserName,
|
"id": row.ID, "userId": row.UserID, "userName": row.UserName,
|
||||||
"name": row.Name, "tunnelId": row.TunnelID, "tunnelName": row.TunnelName,
|
"name": row.Name, "tunnelId": row.TunnelID, "tunnelName": row.TunnelName,
|
||||||
"inIp": nullableForwardIngress(inIP), "inPort": nullableInt64(inPort),
|
"inIp": nullableForwardIngress(inIP), "inPort": nullableInt64(inPort),
|
||||||
"remoteAddr": row.RemoteAddr, "strategy": row.Strategy,
|
"remoteAddr": row.RemoteAddr, "strategy": row.Strategy,
|
||||||
"inFlow": row.InFlow, "outFlow": row.OutFlow,
|
"inFlow": row.InFlow, "outFlow": row.OutFlow,
|
||||||
"createdTime": row.CreatedTime, "status": row.Status, "inx": int64(row.Inx),
|
"createdTime": row.CreatedTime, "status": row.Status, "inx": int64(row.Inx),
|
||||||
})
|
}
|
||||||
|
if row.SpeedID.Valid {
|
||||||
|
item["speedId"] = row.SpeedID.Int64
|
||||||
|
}
|
||||||
|
items = append(items, item)
|
||||||
}
|
}
|
||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
@@ -1270,6 +1281,175 @@ func (r *Repository) ListActivePeerShareRuntimePorts(shareID int64, nodeID int64
|
|||||||
return ports, nil
|
return ports, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Repository) ListActiveForwardPeerShareRuntimesByServiceName(serviceName string) ([]model.PeerShareRuntime, error) {
|
||||||
|
if r == nil || r.db == nil {
|
||||||
|
return nil, errors.New("repository not initialized")
|
||||||
|
}
|
||||||
|
var items []model.PeerShareRuntime
|
||||||
|
err := r.db.Where("service_name = ? AND status = 1 AND role = ?", serviceName, "forward").
|
||||||
|
Order("id ASC").
|
||||||
|
Find(&items).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if items == nil {
|
||||||
|
items = make([]model.PeerShareRuntime, 0)
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repository) ListActiveForwardPeerShareRuntimesByNodeAndServiceName(nodeID int64, serviceName string) ([]model.PeerShareRuntime, error) {
|
||||||
|
if r == nil || r.db == nil {
|
||||||
|
return nil, errors.New("repository not initialized")
|
||||||
|
}
|
||||||
|
serviceName = strings.TrimSpace(serviceName)
|
||||||
|
if serviceName == "" {
|
||||||
|
return []model.PeerShareRuntime{}, nil
|
||||||
|
}
|
||||||
|
var items []model.PeerShareRuntime
|
||||||
|
err := r.db.Where("node_id = ? AND service_name = ? AND status = 1 AND role = ?", nodeID, serviceName, "forward").
|
||||||
|
Order("id ASC").
|
||||||
|
Find(&items).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if items == nil {
|
||||||
|
items = make([]model.PeerShareRuntime, 0)
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repository) ListActiveForwardPeerShareRuntimeServiceNamesByNode(nodeID int64) ([]string, error) {
|
||||||
|
if r == nil || r.db == nil {
|
||||||
|
return nil, errors.New("repository not initialized")
|
||||||
|
}
|
||||||
|
var names []string
|
||||||
|
err := r.db.Model(&model.PeerShareRuntime{}).
|
||||||
|
Where("node_id = ? AND status = 1 AND role = ? AND service_name <> ''", nodeID, "forward").
|
||||||
|
Pluck("service_name", &names).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if names == nil {
|
||||||
|
names = make([]string, 0)
|
||||||
|
}
|
||||||
|
return names, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repository) HasRecentUnboundForwardPeerShareRuntimeOnNode(nodeID int64, minUpdatedTime int64) (bool, error) {
|
||||||
|
if r == nil || r.db == nil {
|
||||||
|
return false, errors.New("repository not initialized")
|
||||||
|
}
|
||||||
|
var count int64
|
||||||
|
err := r.db.Model(&model.PeerShareRuntime{}).
|
||||||
|
Where("node_id = ? AND status = 1 AND role = ? AND applied = 0 AND updated_time >= ? AND (service_name = '' OR service_name IS NULL)", nodeID, "forward", minUpdatedTime).
|
||||||
|
Count(&count).Error
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return count > 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repository) GetActiveForwardPeerShareRuntimeByPort(shareID int64, port int) (*model.PeerShareRuntime, error) {
|
||||||
|
if r == nil || r.db == nil {
|
||||||
|
return nil, errors.New("repository not initialized")
|
||||||
|
}
|
||||||
|
var item model.PeerShareRuntime
|
||||||
|
err := r.db.Where("share_id = ? AND port = ? AND status = 1 AND role = ?", shareID, port, "forward").First(&item).Error
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &item, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repository) GetActiveForwardPeerShareRuntimeByServiceName(shareID int64, serviceName string) (*model.PeerShareRuntime, error) {
|
||||||
|
if r == nil || r.db == nil {
|
||||||
|
return nil, errors.New("repository not initialized")
|
||||||
|
}
|
||||||
|
serviceName = strings.TrimSpace(serviceName)
|
||||||
|
if shareID <= 0 || serviceName == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
var item model.PeerShareRuntime
|
||||||
|
err := r.db.Where("share_id = ? AND service_name = ? AND status = 1 AND role = ?", shareID, serviceName, "forward").
|
||||||
|
Order("id ASC").
|
||||||
|
First(&item).Error
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &item, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repository) ExistsActivePeerShareRuntimeOnNodePort(nodeID int64, port int) (bool, error) {
|
||||||
|
if r == nil || r.db == nil {
|
||||||
|
return false, errors.New("repository not initialized")
|
||||||
|
}
|
||||||
|
var count int64
|
||||||
|
err := r.db.Model(&model.PeerShareRuntime{}).
|
||||||
|
Where("node_id = ? AND port = ? AND status = 1", nodeID, port).
|
||||||
|
Count(&count).Error
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return count > 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repository) UpdatePeerShareRuntimeServiceName(id int64, serviceName string, updatedTime int64) error {
|
||||||
|
if r == nil || r.db == nil {
|
||||||
|
return errors.New("repository not initialized")
|
||||||
|
}
|
||||||
|
return r.db.Model(&model.PeerShareRuntime{}).Where("id = ?", id).Updates(map[string]interface{}{
|
||||||
|
"service_name": serviceName,
|
||||||
|
"applied": 1,
|
||||||
|
"updated_time": updatedTime,
|
||||||
|
}).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repository) MarkPeerShareRuntimeReleasedByPort(shareID int64, port int, updatedTime int64) error {
|
||||||
|
if r == nil || r.db == nil {
|
||||||
|
return errors.New("repository not initialized")
|
||||||
|
}
|
||||||
|
if shareID <= 0 || port <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if updatedTime <= 0 {
|
||||||
|
updatedTime = unixMilliNow()
|
||||||
|
}
|
||||||
|
return r.db.Model(&model.PeerShareRuntime{}).Where("share_id = ? AND port = ? AND status = 1", shareID, port).Updates(map[string]interface{}{
|
||||||
|
"status": 0,
|
||||||
|
"applied": 0,
|
||||||
|
"service_name": "",
|
||||||
|
"updated_time": updatedTime,
|
||||||
|
}).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Repository) MarkForwardPeerShareRuntimeReleasedByServiceName(shareID int64, serviceName string, updatedTime int64) error {
|
||||||
|
if r == nil || r.db == nil {
|
||||||
|
return errors.New("repository not initialized")
|
||||||
|
}
|
||||||
|
serviceName = strings.TrimSpace(serviceName)
|
||||||
|
if shareID <= 0 || serviceName == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if updatedTime <= 0 {
|
||||||
|
updatedTime = unixMilliNow()
|
||||||
|
}
|
||||||
|
return r.db.Model(&model.PeerShareRuntime{}).
|
||||||
|
Where("share_id = ? AND status = 1 AND role = ? AND service_name = ?", shareID, "forward", serviceName).
|
||||||
|
Updates(map[string]interface{}{
|
||||||
|
"status": 0,
|
||||||
|
"applied": 0,
|
||||||
|
"service_name": "",
|
||||||
|
"updated_time": updatedTime,
|
||||||
|
}).Error
|
||||||
|
}
|
||||||
|
|
||||||
// ─── FederationTunnelBinding ─────────────────────────────────────────
|
// ─── FederationTunnelBinding ─────────────────────────────────────────
|
||||||
|
|
||||||
func (r *Repository) UpsertFederationTunnelBinding(item *model.FederationTunnelBinding) error {
|
func (r *Repository) UpsertFederationTunnelBinding(item *model.FederationTunnelBinding) error {
|
||||||
@@ -1643,9 +1823,15 @@ func (r *Repository) exportSpeedLimits() ([]model.SpeedLimitBackup, error) {
|
|||||||
for _, sl := range sls {
|
for _, sl := range sls {
|
||||||
b := model.SpeedLimitBackup{
|
b := model.SpeedLimitBackup{
|
||||||
ID: sl.ID, Name: sl.Name, Speed: int64(sl.Speed),
|
ID: sl.ID, Name: sl.Name, Speed: int64(sl.Speed),
|
||||||
TunnelID: sl.TunnelID, TunnelName: sl.TunnelName,
|
|
||||||
CreatedTime: sl.CreatedTime, Status: sl.Status,
|
CreatedTime: sl.CreatedTime, Status: sl.Status,
|
||||||
}
|
}
|
||||||
|
if sl.TunnelID.Valid {
|
||||||
|
tid := sl.TunnelID.Int64
|
||||||
|
b.TunnelID = &tid
|
||||||
|
}
|
||||||
|
if sl.TunnelName.Valid {
|
||||||
|
b.TunnelName = sl.TunnelName.String
|
||||||
|
}
|
||||||
if sl.UpdatedTime.Valid {
|
if sl.UpdatedTime.Valid {
|
||||||
b.UpdatedTime = sl.UpdatedTime.Int64
|
b.UpdatedTime = sl.UpdatedTime.Int64
|
||||||
}
|
}
|
||||||
@@ -2016,12 +2202,18 @@ func importSpeedLimits(tx *gorm.DB, speedLimits []model.SpeedLimitBackup, now in
|
|||||||
ID: sl.ID,
|
ID: sl.ID,
|
||||||
Name: sl.Name,
|
Name: sl.Name,
|
||||||
Speed: int(sl.Speed),
|
Speed: int(sl.Speed),
|
||||||
TunnelID: sl.TunnelID,
|
TunnelID: sql.NullInt64{Int64: 0, Valid: false},
|
||||||
TunnelName: sl.TunnelName,
|
TunnelName: sql.NullString{String: "", Valid: false},
|
||||||
CreatedTime: sl.CreatedTime,
|
CreatedTime: sl.CreatedTime,
|
||||||
UpdatedTime: sql.NullInt64{Int64: now, Valid: true},
|
UpdatedTime: sql.NullInt64{Int64: now, Valid: true},
|
||||||
Status: sl.Status,
|
Status: sl.Status,
|
||||||
}
|
}
|
||||||
|
if sl.TunnelID != nil {
|
||||||
|
item.TunnelID = sql.NullInt64{Int64: *sl.TunnelID, Valid: true}
|
||||||
|
}
|
||||||
|
if sl.TunnelName != "" {
|
||||||
|
item.TunnelName = sql.NullString{String: sl.TunnelName, Valid: true}
|
||||||
|
}
|
||||||
err := tx.Clauses(clause.OnConflict{
|
err := tx.Clauses(clause.OnConflict{
|
||||||
Columns: []clause.Column{{Name: "id"}},
|
Columns: []clause.Column{{Name: "id"}},
|
||||||
DoUpdates: clause.AssignmentColumns([]string{
|
DoUpdates: clause.AssignmentColumns([]string{
|
||||||
@@ -2235,7 +2427,7 @@ func (r *Repository) ListExpiredActiveUserIDs(nowMs int64) ([]int64, error) {
|
|||||||
}
|
}
|
||||||
var ids []int64
|
var ids []int64
|
||||||
err := r.db.Model(&model.User{}).
|
err := r.db.Model(&model.User{}).
|
||||||
Where("role_id != 0 AND status = 1 AND exp_time IS NOT NULL AND exp_time < ?", nowMs).
|
Where("role_id != 0 AND status = 1 AND exp_time > 0 AND exp_time < ?", nowMs).
|
||||||
Pluck("id", &ids).Error
|
Pluck("id", &ids).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -2255,7 +2447,7 @@ func (r *Repository) ListExpiredActiveUserTunnels(nowMs int64) ([]model.ExpiredU
|
|||||||
return nil, errors.New("repository not initialized")
|
return nil, errors.New("repository not initialized")
|
||||||
}
|
}
|
||||||
var uts []model.UserTunnel
|
var uts []model.UserTunnel
|
||||||
err := r.db.Where("status = 1 AND exp_time IS NOT NULL AND exp_time < ?", nowMs).Find(&uts).Error
|
err := r.db.Where("status = 1 AND exp_time > 0 AND exp_time < ?", nowMs).Find(&uts).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ func (r *Repository) ListForwardsByTunnel(tunnelID int64) ([]model.ForwardRecord
|
|||||||
RemoteAddr: f.RemoteAddr,
|
RemoteAddr: f.RemoteAddr,
|
||||||
Strategy: f.Strategy,
|
Strategy: f.Strategy,
|
||||||
Status: f.Status,
|
Status: f.Status,
|
||||||
|
SpeedID: f.SpeedID,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
for i := range rows {
|
for i := range rows {
|
||||||
|
|||||||
@@ -38,6 +38,14 @@ type FederationBindingRow struct {
|
|||||||
UpdatedTime int64
|
UpdatedTime int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ActiveForwardPortRow struct {
|
||||||
|
ForwardID int64
|
||||||
|
TunnelID int64
|
||||||
|
TunnelName string
|
||||||
|
Port int
|
||||||
|
UpdatedTime int64
|
||||||
|
}
|
||||||
|
|
||||||
// ListRemoteNodes returns all nodes with is_remote=1, ordered by id desc.
|
// ListRemoteNodes returns all nodes with is_remote=1, ordered by id desc.
|
||||||
func (r *Repository) ListRemoteNodes() ([]RemoteNodeRow, error) {
|
func (r *Repository) ListRemoteNodes() ([]RemoteNodeRow, error) {
|
||||||
if r == nil || r.db == nil {
|
if r == nil || r.db == nil {
|
||||||
@@ -87,6 +95,27 @@ func (r *Repository) ListActiveBindingsForNode(nodeID int64) ([]FederationBindin
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Repository) ListActiveForwardPortsForNode(nodeID int64) ([]ActiveForwardPortRow, error) {
|
||||||
|
if r == nil || r.db == nil {
|
||||||
|
return nil, errors.New("repository not initialized")
|
||||||
|
}
|
||||||
|
var result []ActiveForwardPortRow
|
||||||
|
err := r.db.Model(&model.ForwardPort{}).
|
||||||
|
Select("forward_port.forward_id, forward.tunnel_id, COALESCE(tunnel.name, '') AS tunnel_name, forward_port.port, forward.updated_time").
|
||||||
|
Joins("JOIN forward ON forward.id = forward_port.forward_id").
|
||||||
|
Joins("LEFT JOIN tunnel ON tunnel.id = forward.tunnel_id").
|
||||||
|
Where("forward_port.node_id = ? AND forward_port.port > 0", nodeID).
|
||||||
|
Order("forward_port.port ASC, forward_port.id ASC").
|
||||||
|
Find(&result).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if result == nil {
|
||||||
|
result = make([]ActiveForwardPortRow, 0)
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetNodeBasicInfo returns the name, server_ip, and status for a given node.
|
// GetNodeBasicInfo returns the name, server_ip, and status for a given node.
|
||||||
func (r *Repository) GetNodeBasicInfo(nodeID int64) (*NodeBasicInfo, error) {
|
func (r *Repository) GetNodeBasicInfo(nodeID int64) (*NodeBasicInfo, error) {
|
||||||
if r == nil || r.db == nil {
|
if r == nil || r.db == nil {
|
||||||
@@ -199,7 +228,6 @@ func (r *Repository) ListTunnelIDsByNamePrefix(prefix string) ([]int64, error) {
|
|||||||
return ids, nil
|
return ids, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NextIndex returns COALESCE(MAX(inx), -1) + 1 for the given table.
|
|
||||||
func (r *Repository) NextIndex(table string) int {
|
func (r *Repository) NextIndex(table string) int {
|
||||||
if r == nil || r.db == nil {
|
if r == nil || r.db == nil {
|
||||||
return 0
|
return 0
|
||||||
@@ -222,7 +250,7 @@ func (r *Repository) NextIndex(table string) int {
|
|||||||
var row inxRow
|
var row inxRow
|
||||||
err := r.db.Model(modelRef).
|
err := r.db.Model(modelRef).
|
||||||
Select("inx").
|
Select("inx").
|
||||||
Order("inx DESC").
|
Order("inx ASC, id ASC").
|
||||||
Limit(1).
|
Limit(1).
|
||||||
Take(&row).Error
|
Take(&row).Error
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
@@ -231,10 +259,7 @@ func (r *Repository) NextIndex(table string) int {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
if row.Inx < 0 {
|
return row.Inx - 1
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return row.Inx + 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateRemoteNode inserts a new remote node.
|
// CreateRemoteNode inserts a new remote node.
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ func (r *Repository) ListActiveForwardsByUser(userID int64) ([]model.ForwardReco
|
|||||||
RemoteAddr: f.RemoteAddr,
|
RemoteAddr: f.RemoteAddr,
|
||||||
Strategy: f.Strategy,
|
Strategy: f.Strategy,
|
||||||
Status: f.Status,
|
Status: f.Status,
|
||||||
|
SpeedID: f.SpeedID,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
for i := range rows {
|
for i := range rows {
|
||||||
@@ -68,6 +69,7 @@ func (r *Repository) ListActiveForwardsByUserTunnel(userID, tunnelID int64) ([]m
|
|||||||
RemoteAddr: f.RemoteAddr,
|
RemoteAddr: f.RemoteAddr,
|
||||||
Strategy: f.Strategy,
|
Strategy: f.Strategy,
|
||||||
Status: f.Status,
|
Status: f.Status,
|
||||||
|
SpeedID: f.SpeedID,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
for i := range rows {
|
for i := range rows {
|
||||||
@@ -99,6 +101,7 @@ func (r *Repository) GetForwardRecord(forwardID int64) (*model.ForwardRecord, er
|
|||||||
RemoteAddr: f.RemoteAddr,
|
RemoteAddr: f.RemoteAddr,
|
||||||
Strategy: f.Strategy,
|
Strategy: f.Strategy,
|
||||||
Status: f.Status,
|
Status: f.Status,
|
||||||
|
SpeedID: f.SpeedID,
|
||||||
}
|
}
|
||||||
if strings.TrimSpace(fr.Strategy) == "" {
|
if strings.TrimSpace(fr.Strategy) == "" {
|
||||||
fr.Strategy = "fifo"
|
fr.Strategy = "fifo"
|
||||||
@@ -169,3 +172,15 @@ func (r *Repository) SpeedLimitExists(id int64) (bool, error) {
|
|||||||
}
|
}
|
||||||
return count > 0, nil
|
return count > 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Repository) GetSpeedLimitSpeed(id int64) (int, error) {
|
||||||
|
if r == nil || r.db == nil {
|
||||||
|
return 0, errors.New("repository not initialized")
|
||||||
|
}
|
||||||
|
var sl model.SpeedLimit
|
||||||
|
err := r.db.Select("speed").Where("id = ?", id).First(&sl).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return sl.Speed, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -657,7 +657,7 @@ func (r *Repository) GetMinForwardPort(forwardID int64) sql.NullInt64 {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repository) UpdateForward(id int64, name string, tunnelID int64, remoteAddr, strategy string, now int64) error {
|
func (r *Repository) UpdateForward(id int64, name string, tunnelID int64, remoteAddr, strategy string, now int64, speedID interface{}) error {
|
||||||
if r == nil || r.db == nil {
|
if r == nil || r.db == nil {
|
||||||
return errors.New("repository not initialized")
|
return errors.New("repository not initialized")
|
||||||
}
|
}
|
||||||
@@ -668,6 +668,7 @@ func (r *Repository) UpdateForward(id int64, name string, tunnelID int64, remote
|
|||||||
"tunnel_id": tunnelID,
|
"tunnel_id": tunnelID,
|
||||||
"remote_addr": remoteAddr,
|
"remote_addr": remoteAddr,
|
||||||
"strategy": strategy,
|
"strategy": strategy,
|
||||||
|
"speed_id": nullInt64FromInterface(speedID),
|
||||||
"updated_time": now,
|
"updated_time": now,
|
||||||
}).Error
|
}).Error
|
||||||
}
|
}
|
||||||
@@ -724,7 +725,7 @@ func (r *Repository) ReplaceForwardPorts(forwardID int64, entries []struct {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repository) RollbackForwardFields(id, userID int64, userName, name string, tunnelID int64, remoteAddr, strategy string, status int, now int64) {
|
func (r *Repository) RollbackForwardFields(id, userID int64, userName, name string, tunnelID int64, remoteAddr, strategy string, status int, speedID interface{}, now int64) {
|
||||||
if r == nil || r.db == nil {
|
if r == nil || r.db == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -738,6 +739,7 @@ func (r *Repository) RollbackForwardFields(id, userID int64, userName, name stri
|
|||||||
"remote_addr": remoteAddr,
|
"remote_addr": remoteAddr,
|
||||||
"strategy": strategy,
|
"strategy": strategy,
|
||||||
"status": status,
|
"status": status,
|
||||||
|
"speed_id": nullInt64FromInterface(speedID),
|
||||||
"updated_time": now,
|
"updated_time": now,
|
||||||
}).Error
|
}).Error
|
||||||
}
|
}
|
||||||
@@ -764,51 +766,66 @@ func (r *Repository) GetUsedPortsOnNodeAsMap(nodeID int64) (map[int]bool, error)
|
|||||||
return used, nil
|
return used, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repository) CreateSpeedLimit(name string, speed int, tunnelID int64, tunnelName string, now int64, status int) (int64, error) {
|
func (r *Repository) CreateSpeedLimit(name string, speed int, tunnelID *int64, tunnelName string, now int64, status int) (int64, error) {
|
||||||
if r == nil || r.db == nil {
|
if r == nil || r.db == nil {
|
||||||
return 0, errors.New("repository not initialized")
|
return 0, errors.New("repository not initialized")
|
||||||
}
|
}
|
||||||
sl := model.SpeedLimit{
|
sl := model.SpeedLimit{
|
||||||
Name: name,
|
Name: name,
|
||||||
Speed: speed,
|
Speed: speed,
|
||||||
TunnelID: tunnelID,
|
TunnelID: sql.NullInt64{Int64: 0, Valid: false},
|
||||||
TunnelName: tunnelName,
|
TunnelName: sql.NullString{String: "", Valid: false},
|
||||||
CreatedTime: now,
|
CreatedTime: now,
|
||||||
UpdatedTime: sql.NullInt64{Int64: now, Valid: true},
|
UpdatedTime: sql.NullInt64{Int64: now, Valid: true},
|
||||||
Status: status,
|
Status: status,
|
||||||
}
|
}
|
||||||
|
if tunnelID != nil {
|
||||||
|
sl.TunnelID = sql.NullInt64{Int64: *tunnelID, Valid: true}
|
||||||
|
}
|
||||||
|
if tunnelName != "" {
|
||||||
|
sl.TunnelName = sql.NullString{String: tunnelName, Valid: true}
|
||||||
|
}
|
||||||
if err := r.db.Create(&sl).Error; err != nil {
|
if err := r.db.Create(&sl).Error; err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
return sl.ID, nil
|
return sl.ID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repository) UpdateSpeedLimit(id int64, name string, speed int, tunnelID int64, tunnelName string, status int, now int64) error {
|
func (r *Repository) UpdateSpeedLimit(id int64, name string, speed int, tunnelID *int64, tunnelName string, status int, now int64) error {
|
||||||
if r == nil || r.db == nil {
|
if r == nil || r.db == nil {
|
||||||
return errors.New("repository not initialized")
|
return errors.New("repository not initialized")
|
||||||
}
|
}
|
||||||
|
updates := map[string]interface{}{
|
||||||
|
"name": name,
|
||||||
|
"speed": speed,
|
||||||
|
"status": status,
|
||||||
|
"updated_time": sql.NullInt64{
|
||||||
|
Int64: now,
|
||||||
|
Valid: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if tunnelID != nil {
|
||||||
|
updates["tunnel_id"] = sql.NullInt64{Int64: *tunnelID, Valid: true}
|
||||||
|
} else {
|
||||||
|
updates["tunnel_id"] = sql.NullInt64{Int64: 0, Valid: false}
|
||||||
|
}
|
||||||
|
if tunnelName != "" {
|
||||||
|
updates["tunnel_name"] = sql.NullString{String: tunnelName, Valid: true}
|
||||||
|
} else {
|
||||||
|
updates["tunnel_name"] = sql.NullString{String: "", Valid: false}
|
||||||
|
}
|
||||||
return r.db.Model(&model.SpeedLimit{}).
|
return r.db.Model(&model.SpeedLimit{}).
|
||||||
Where("id = ?", id).
|
Where("id = ?", id).
|
||||||
Updates(map[string]interface{}{
|
Updates(updates).Error
|
||||||
"name": name,
|
|
||||||
"speed": speed,
|
|
||||||
"tunnel_id": tunnelID,
|
|
||||||
"tunnel_name": tunnelName,
|
|
||||||
"status": status,
|
|
||||||
"updated_time": sql.NullInt64{
|
|
||||||
Int64: now,
|
|
||||||
Valid: true,
|
|
||||||
},
|
|
||||||
}).Error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repository) GetSpeedLimitTunnelID(speedLimitID int64) int64 {
|
func (r *Repository) GetSpeedLimitTunnelID(speedLimitID int64) sql.NullInt64 {
|
||||||
if r == nil || r.db == nil {
|
if r == nil || r.db == nil {
|
||||||
return 0
|
return sql.NullInt64{Valid: false}
|
||||||
}
|
}
|
||||||
var sl model.SpeedLimit
|
var sl model.SpeedLimit
|
||||||
if err := r.db.Select("tunnel_id").Where("id = ?", speedLimitID).First(&sl).Error; err != nil {
|
if err := r.db.Select("tunnel_id").Where("id = ?", speedLimitID).First(&sl).Error; err != nil {
|
||||||
return 0
|
return sql.NullInt64{Valid: false}
|
||||||
}
|
}
|
||||||
return sl.TunnelID
|
return sl.TunnelID
|
||||||
}
|
}
|
||||||
@@ -1190,7 +1207,7 @@ func (r *Repository) EnsureUserTunnelGrant(userID, tunnelID int64) (int64, bool,
|
|||||||
return ut.ID, true, nil
|
return ut.ID, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repository) CreateForwardTx(userID int64, userName, name string, tunnelID int64, remoteAddr, strategy string, now int64, inx int, entryNodeIDs []int64, port int) (int64, error) {
|
func (r *Repository) CreateForwardTx(userID int64, userName, name string, tunnelID int64, remoteAddr, strategy string, now int64, inx int, entryNodeIDs []int64, port int, speedID interface{}) (int64, error) {
|
||||||
if r == nil || r.db == nil {
|
if r == nil || r.db == nil {
|
||||||
return 0, errors.New("repository not initialized")
|
return 0, errors.New("repository not initialized")
|
||||||
}
|
}
|
||||||
@@ -1209,6 +1226,7 @@ func (r *Repository) CreateForwardTx(userID int64, userName, name string, tunnel
|
|||||||
UpdatedTime: now,
|
UpdatedTime: now,
|
||||||
Status: 1,
|
Status: 1,
|
||||||
Inx: inx,
|
Inx: inx,
|
||||||
|
SpeedID: nullInt64FromInterface(speedID),
|
||||||
}
|
}
|
||||||
if err := tx.Create(&fwd).Error; err != nil {
|
if err := tx.Create(&fwd).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -166,7 +166,7 @@ func TestFederationDualPanelMiddleExitAutoPortContract(t *testing.T) {
|
|||||||
|
|
||||||
assertCount(t, providerRepo, `SELECT COUNT(1) FROM peer_share_runtime WHERE share_id = ? AND status = 1 AND applied = 1`, middleShareID, 1)
|
assertCount(t, providerRepo, `SELECT COUNT(1) FROM peer_share_runtime WHERE share_id = ? AND status = 1 AND applied = 1`, middleShareID, 1)
|
||||||
assertCount(t, providerRepo, `SELECT COUNT(1) FROM peer_share_runtime WHERE share_id = ? AND status = 1 AND applied = 1`, exitShareID, 1)
|
assertCount(t, providerRepo, `SELECT COUNT(1) FROM peer_share_runtime WHERE share_id = ? AND status = 1 AND applied = 1`, exitShareID, 1)
|
||||||
assertCount(t, providerRepo, `SELECT COUNT(1) FROM peer_share_runtime WHERE share_id = ?`, entryShareID, 0)
|
assertCount(t, providerRepo, `SELECT COUNT(1) FROM peer_share_runtime WHERE share_id = ? AND status = 1 AND applied = 1`, entryShareID, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFederationDualPanelRemoteDiagnosisContract(t *testing.T) {
|
func TestFederationDualPanelRemoteDiagnosisContract(t *testing.T) {
|
||||||
|
|||||||
@@ -0,0 +1,693 @@
|
|||||||
|
package contract_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"go-backend/internal/auth"
|
||||||
|
"go-backend/internal/http/response"
|
||||||
|
"go-backend/internal/store/repo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFederationForwardCardFlowLinkageContract(t *testing.T) {
|
||||||
|
secret := "federation-forward-flow-contract-jwt"
|
||||||
|
router, r := setupContractRouter(t, secret)
|
||||||
|
|
||||||
|
adminToken, err := auth.GenerateToken(1, "admin_user", 0, secret)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("generate admin token: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO node(name, secret, server_ip, server_ip_v4, server_ip_v6, port, interface_name, version, http, tls, socks, created_time, updated_time, status, tcp_listen_addr, udp_listen_addr, inx, is_remote, remote_url, remote_token, remote_config)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, "flow-local-node", "flow-local-secret", "10.20.30.40", "10.20.30.40", "", "32000-32020", "", "v1", 1, 1, 1, now, now, 1, "[::]", "[::]", 0, 0, "", "", "").Error; err != nil {
|
||||||
|
t.Fatalf("insert local node: %v", err)
|
||||||
|
}
|
||||||
|
nodeID := mustLastInsertID(t, r, "flow-local-node")
|
||||||
|
|
||||||
|
shareToken := "flow-linkage-share-token"
|
||||||
|
if err := r.CreatePeerShare(&repo.PeerShare{
|
||||||
|
Name: "flow-linkage-share",
|
||||||
|
NodeID: nodeID,
|
||||||
|
Token: shareToken,
|
||||||
|
MaxBandwidth: 0,
|
||||||
|
CurrentFlow: 1536,
|
||||||
|
ExpiryTime: 0,
|
||||||
|
PortRangeStart: 32000,
|
||||||
|
PortRangeEnd: 32020,
|
||||||
|
IsActive: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create share: %v", err)
|
||||||
|
}
|
||||||
|
share, err := r.GetPeerShareByToken(shareToken)
|
||||||
|
if err != nil || share == nil {
|
||||||
|
t.Fatalf("load share: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tunnelName := fmt.Sprintf("Share-%d-Port-%d", share.ID, 32001)
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO tunnel(name, type, protocol, flow, created_time, updated_time, status, in_ip, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, tunnelName, 1, "tcp", 1, now, now, 1, "", 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert share tunnel: %v", err)
|
||||||
|
}
|
||||||
|
tunnelID := mustLastInsertID(t, r, "flow-share-tunnel")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO forward(user_id, user_name, name, tunnel_id, remote_addr, strategy, in_flow, out_flow, created_time, updated_time, status, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, 1, "admin_user", "flow-linkage-forward", tunnelID, "1.1.1.1:443", "fifo", 0, 0, now, now, 1, 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert forward: %v", err)
|
||||||
|
}
|
||||||
|
forwardID := mustLastInsertID(t, r, "flow-linkage-forward")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`INSERT INTO forward_port(forward_id, node_id, port) VALUES(?, ?, ?)`, forwardID, nodeID, 32001).Error; err != nil {
|
||||||
|
t.Fatalf("insert forward_port: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
forwardOut := requestContractEnvelope(t, router, adminToken, "/api/v1/forward/list", nil)
|
||||||
|
if forwardOut.Code != 0 {
|
||||||
|
t.Fatalf("forward list failed: code=%d msg=%q", forwardOut.Code, forwardOut.Msg)
|
||||||
|
}
|
||||||
|
forwardRows := mustContractSlice(t, forwardOut.Data, "forward list data")
|
||||||
|
|
||||||
|
var targetForward map[string]interface{}
|
||||||
|
for _, row := range forwardRows {
|
||||||
|
m, ok := row.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if contractValueAsInt64(m["id"]) == forwardID {
|
||||||
|
targetForward = m
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if targetForward == nil {
|
||||||
|
t.Fatalf("target forward %d not found in /forward/list response", forwardID)
|
||||||
|
}
|
||||||
|
|
||||||
|
shareOut := requestContractEnvelope(t, router, adminToken, "/api/v1/federation/share/list", nil)
|
||||||
|
if shareOut.Code != 0 {
|
||||||
|
t.Fatalf("share list failed: code=%d msg=%q", shareOut.Code, shareOut.Msg)
|
||||||
|
}
|
||||||
|
localShareRows := mustContractSlice(t, shareOut.Data, "share list data")
|
||||||
|
|
||||||
|
remoteUsageOut := requestContractEnvelope(t, router, adminToken, "/api/v1/federation/share/remote-usage/list", nil)
|
||||||
|
if remoteUsageOut.Code != 0 {
|
||||||
|
t.Fatalf("remote usage list failed: code=%d msg=%q", remoteUsageOut.Code, remoteUsageOut.Msg)
|
||||||
|
}
|
||||||
|
remoteUsageRows := mustContractSlice(t, remoteUsageOut.Data, "remote usage data")
|
||||||
|
if len(remoteUsageRows) != 0 {
|
||||||
|
t.Fatalf("expected no remote usage rows in local-only fixture, got %d", len(remoteUsageRows))
|
||||||
|
}
|
||||||
|
|
||||||
|
flowByShare := make(map[int64]int64)
|
||||||
|
for _, row := range remoteUsageRows {
|
||||||
|
m, ok := row.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
shareID := contractValueAsInt64(m["shareId"])
|
||||||
|
currentFlow := contractValueAsInt64(m["currentFlow"])
|
||||||
|
if shareID > 0 && currentFlow > 0 {
|
||||||
|
if currentFlow > flowByShare[shareID] {
|
||||||
|
flowByShare[shareID] = currentFlow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, row := range localShareRows {
|
||||||
|
m, ok := row.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
shareID := contractValueAsInt64(m["id"])
|
||||||
|
currentFlow := contractValueAsInt64(m["currentFlow"])
|
||||||
|
if shareID > 0 && currentFlow > 0 {
|
||||||
|
if currentFlow > flowByShare[shareID] {
|
||||||
|
flowByShare[shareID] = currentFlow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parsedShareID := contractParseShareIDFromTunnelName(contractValueAsString(targetForward["tunnelName"]))
|
||||||
|
if parsedShareID != share.ID {
|
||||||
|
t.Fatalf("expected parsed shareID=%d, got %d (tunnelName=%q)", share.ID, parsedShareID, contractValueAsString(targetForward["tunnelName"]))
|
||||||
|
}
|
||||||
|
|
||||||
|
forwardCountByShare := make(map[int64]int)
|
||||||
|
for _, row := range forwardRows {
|
||||||
|
m, ok := row.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sid := contractParseShareIDFromTunnelName(contractValueAsString(m["tunnelName"]))
|
||||||
|
if sid > 0 && flowByShare[sid] > 0 {
|
||||||
|
forwardCountByShare[sid] = forwardCountByShare[sid] + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
directFlow := contractValueAsInt64(targetForward["inFlow"]) + contractValueAsInt64(targetForward["outFlow"])
|
||||||
|
if directFlow != 0 {
|
||||||
|
t.Fatalf("fixture expectation failed: directFlow should be 0, got %d", directFlow)
|
||||||
|
}
|
||||||
|
|
||||||
|
shareFlow := flowByShare[parsedShareID]
|
||||||
|
if shareFlow <= 0 {
|
||||||
|
t.Fatalf("expected merged share flow > 0 for share %d", parsedShareID)
|
||||||
|
}
|
||||||
|
|
||||||
|
count := forwardCountByShare[parsedShareID]
|
||||||
|
if count <= 0 {
|
||||||
|
count = 1
|
||||||
|
}
|
||||||
|
estimated := shareFlow / int64(count)
|
||||||
|
if estimated < 1 {
|
||||||
|
estimated = 1
|
||||||
|
}
|
||||||
|
displayFlow := estimated
|
||||||
|
|
||||||
|
if displayFlow <= 0 {
|
||||||
|
t.Fatalf("expected displayFlow > 0 after frontend-style merge, got %d", displayFlow)
|
||||||
|
}
|
||||||
|
if displayFlow != share.CurrentFlow {
|
||||||
|
t.Fatalf("expected displayFlow=%d, got %d", share.CurrentFlow, displayFlow)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFederationForwardCardFlowLinkageContractSplitShareFlowAcrossMultipleForwards(t *testing.T) {
|
||||||
|
secret := "federation-forward-split-flow-contract-jwt"
|
||||||
|
router, r := setupContractRouter(t, secret)
|
||||||
|
|
||||||
|
adminToken, err := auth.GenerateToken(1, "admin_user", 0, secret)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("generate admin token: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO node(name, secret, server_ip, server_ip_v4, server_ip_v6, port, interface_name, version, http, tls, socks, created_time, updated_time, status, tcp_listen_addr, udp_listen_addr, inx, is_remote, remote_url, remote_token, remote_config)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, "flow-split-local-node", "flow-split-local-secret", "10.21.31.41", "10.21.31.41", "", "32100-32120", "", "v1", 1, 1, 1, now, now, 1, "[::]", "[::]", 0, 0, "", "", "").Error; err != nil {
|
||||||
|
t.Fatalf("insert local node: %v", err)
|
||||||
|
}
|
||||||
|
nodeID := mustLastInsertID(t, r, "flow-split-local-node")
|
||||||
|
|
||||||
|
shareToken := "flow-split-share-token"
|
||||||
|
if err := r.CreatePeerShare(&repo.PeerShare{
|
||||||
|
Name: "flow-split-share",
|
||||||
|
NodeID: nodeID,
|
||||||
|
Token: shareToken,
|
||||||
|
MaxBandwidth: 0,
|
||||||
|
CurrentFlow: 4097,
|
||||||
|
ExpiryTime: 0,
|
||||||
|
PortRangeStart: 32100,
|
||||||
|
PortRangeEnd: 32120,
|
||||||
|
IsActive: 1,
|
||||||
|
CreatedTime: now,
|
||||||
|
UpdatedTime: now,
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("create share: %v", err)
|
||||||
|
}
|
||||||
|
share, err := r.GetPeerShareByToken(shareToken)
|
||||||
|
if err != nil || share == nil {
|
||||||
|
t.Fatalf("load share: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
createShareForward := func(name string, port int) int64 {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
tunnelName := fmt.Sprintf("Share-%d-Port-%d", share.ID, port)
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO tunnel(name, type, protocol, flow, created_time, updated_time, status, in_ip, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, tunnelName, 1, "tcp", 1, now, now, 1, "", 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert share tunnel: %v", err)
|
||||||
|
}
|
||||||
|
tunnelID := mustLastInsertID(t, r, "flow-split-tunnel")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO forward(user_id, user_name, name, tunnel_id, remote_addr, strategy, in_flow, out_flow, created_time, updated_time, status, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, 1, "admin_user", name, tunnelID, "1.1.1.1:443", "fifo", 0, 0, now, now, 1, 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert forward: %v", err)
|
||||||
|
}
|
||||||
|
forwardID := mustLastInsertID(t, r, name)
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`INSERT INTO forward_port(forward_id, node_id, port) VALUES(?, ?, ?)`, forwardID, nodeID, port).Error; err != nil {
|
||||||
|
t.Fatalf("insert forward_port: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return forwardID
|
||||||
|
}
|
||||||
|
|
||||||
|
forwardIDA := createShareForward("flow-split-forward-a", 32101)
|
||||||
|
forwardIDB := createShareForward("flow-split-forward-b", 32102)
|
||||||
|
|
||||||
|
forwardOut := requestContractEnvelope(t, router, adminToken, "/api/v1/forward/list", nil)
|
||||||
|
if forwardOut.Code != 0 {
|
||||||
|
t.Fatalf("forward list failed: code=%d msg=%q", forwardOut.Code, forwardOut.Msg)
|
||||||
|
}
|
||||||
|
forwardRows := mustContractSlice(t, forwardOut.Data, "forward list data")
|
||||||
|
|
||||||
|
shareOut := requestContractEnvelope(t, router, adminToken, "/api/v1/federation/share/list", nil)
|
||||||
|
if shareOut.Code != 0 {
|
||||||
|
t.Fatalf("share list failed: code=%d msg=%q", shareOut.Code, shareOut.Msg)
|
||||||
|
}
|
||||||
|
localShareRows := mustContractSlice(t, shareOut.Data, "share list data")
|
||||||
|
|
||||||
|
remoteUsageOut := requestContractEnvelope(t, router, adminToken, "/api/v1/federation/share/remote-usage/list", nil)
|
||||||
|
if remoteUsageOut.Code != 0 {
|
||||||
|
t.Fatalf("remote usage list failed: code=%d msg=%q", remoteUsageOut.Code, remoteUsageOut.Msg)
|
||||||
|
}
|
||||||
|
remoteUsageRows := mustContractSlice(t, remoteUsageOut.Data, "remote usage data")
|
||||||
|
if len(remoteUsageRows) != 0 {
|
||||||
|
t.Fatalf("expected no remote usage rows in local-only fixture, got %d", len(remoteUsageRows))
|
||||||
|
}
|
||||||
|
|
||||||
|
flowByShare := make(map[int64]int64)
|
||||||
|
for _, row := range remoteUsageRows {
|
||||||
|
m, ok := row.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
shareID := contractValueAsInt64(m["shareId"])
|
||||||
|
currentFlow := contractValueAsInt64(m["currentFlow"])
|
||||||
|
if shareID > 0 && currentFlow > 0 {
|
||||||
|
if currentFlow > flowByShare[shareID] {
|
||||||
|
flowByShare[shareID] = currentFlow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, row := range localShareRows {
|
||||||
|
m, ok := row.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
shareID := contractValueAsInt64(m["id"])
|
||||||
|
currentFlow := contractValueAsInt64(m["currentFlow"])
|
||||||
|
if shareID > 0 && currentFlow > 0 {
|
||||||
|
if currentFlow > flowByShare[shareID] {
|
||||||
|
flowByShare[shareID] = currentFlow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
shareFlow := flowByShare[share.ID]
|
||||||
|
if shareFlow <= 0 {
|
||||||
|
t.Fatalf("expected merged share flow > 0 for share %d", share.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
forwardCountByShare := make(map[int64]int)
|
||||||
|
for _, row := range forwardRows {
|
||||||
|
m, ok := row.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sid := contractParseShareIDFromTunnelName(contractValueAsString(m["tunnelName"]))
|
||||||
|
if sid > 0 && flowByShare[sid] > 0 {
|
||||||
|
forwardCountByShare[sid] = forwardCountByShare[sid] + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
count := forwardCountByShare[share.ID]
|
||||||
|
if count != 2 {
|
||||||
|
t.Fatalf("expected 2 forwards sharing share %d, got %d", share.ID, count)
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedEach := shareFlow / int64(count)
|
||||||
|
if expectedEach < 1 {
|
||||||
|
expectedEach = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
findForward := func(forwardID int64) map[string]interface{} {
|
||||||
|
t.Helper()
|
||||||
|
for _, row := range forwardRows {
|
||||||
|
m, ok := row.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if contractValueAsInt64(m["id"]) == forwardID {
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Fatalf("forward %d not found in /forward/list response", forwardID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, forwardID := range []int64{forwardIDA, forwardIDB} {
|
||||||
|
forward := findForward(forwardID)
|
||||||
|
sid := contractParseShareIDFromTunnelName(contractValueAsString(forward["tunnelName"]))
|
||||||
|
if sid != share.ID {
|
||||||
|
t.Fatalf("expected parsed shareID=%d, got %d for forward %d", share.ID, sid, forwardID)
|
||||||
|
}
|
||||||
|
|
||||||
|
directFlow := contractValueAsInt64(forward["inFlow"]) + contractValueAsInt64(forward["outFlow"])
|
||||||
|
if directFlow != 0 {
|
||||||
|
t.Fatalf("fixture expectation failed: directFlow should be 0 for forward %d, got %d", forwardID, directFlow)
|
||||||
|
}
|
||||||
|
|
||||||
|
displayFlow := int64(0)
|
||||||
|
if directFlow > 0 {
|
||||||
|
displayFlow = directFlow
|
||||||
|
} else {
|
||||||
|
shareFlowForForward := flowByShare[sid]
|
||||||
|
if shareFlowForForward > 0 {
|
||||||
|
cnt := forwardCountByShare[sid]
|
||||||
|
if cnt <= 0 {
|
||||||
|
cnt = 1
|
||||||
|
}
|
||||||
|
estimated := shareFlowForForward / int64(cnt)
|
||||||
|
if estimated < 1 {
|
||||||
|
estimated = 1
|
||||||
|
}
|
||||||
|
displayFlow = estimated
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if displayFlow <= 0 {
|
||||||
|
t.Fatalf("expected displayFlow > 0 for forward %d, got %d", forwardID, displayFlow)
|
||||||
|
}
|
||||||
|
if displayFlow != expectedEach {
|
||||||
|
t.Fatalf("expected displayFlow=%d for forward %d, got %d", expectedEach, forwardID, displayFlow)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFederationForwardCardFlowLinkageContractResolvesShareByTunnelBindingWhenTunnelNameIsCustom(t *testing.T) {
|
||||||
|
secret := "federation-forward-binding-flow-contract-jwt"
|
||||||
|
router, r := setupContractRouter(t, secret)
|
||||||
|
|
||||||
|
adminToken, err := auth.GenerateToken(1, "admin_user", 0, secret)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("generate admin token: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
remoteShareID := int64(901)
|
||||||
|
remoteShareFlow := int64(5000)
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO node(name, secret, server_ip, server_ip_v4, server_ip_v6, port, interface_name, version, http, tls, socks, created_time, updated_time, status, tcp_listen_addr, udp_listen_addr, inx, is_remote, remote_url, remote_token, remote_config)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`,
|
||||||
|
"flow-binding-remote-node", "flow-binding-remote-secret", "10.31.41.51", "10.31.41.51", "", "33000-33020", "", "v1", 1, 1, 1, now, now, 1, "[::]", "[::]", 0, 1, "", "", fmt.Sprintf(`{"shareId":%d,"maxBandwidth":0,"currentFlow":%d,"portRangeStart":33000,"portRangeEnd":33020}`, remoteShareID, remoteShareFlow),
|
||||||
|
).Error; err != nil {
|
||||||
|
t.Fatalf("insert remote node: %v", err)
|
||||||
|
}
|
||||||
|
remoteNodeID := mustLastInsertID(t, r, "flow-binding-remote-node")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO tunnel(name, type, protocol, flow, created_time, updated_time, status, in_ip, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, "federation-port-forward-custom-name", 1, "tcp", 1, now, now, 1, "", 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert custom tunnel: %v", err)
|
||||||
|
}
|
||||||
|
tunnelID := mustLastInsertID(t, r, "flow-binding-custom-tunnel")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO forward(user_id, user_name, name, tunnel_id, remote_addr, strategy, in_flow, out_flow, created_time, updated_time, status, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, 1, "admin_user", "flow-binding-forward", tunnelID, "1.1.1.1:443", "fifo", 0, 0, now, now, 1, 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert forward: %v", err)
|
||||||
|
}
|
||||||
|
forwardID := mustLastInsertID(t, r, "flow-binding-forward")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`INSERT INTO forward_port(forward_id, node_id, port) VALUES(?, ?, ?)`, forwardID, remoteNodeID, 33001).Error; err != nil {
|
||||||
|
t.Fatalf("insert forward_port: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
forwardOut := requestContractEnvelope(t, router, adminToken, "/api/v1/forward/list", nil)
|
||||||
|
if forwardOut.Code != 0 {
|
||||||
|
t.Fatalf("forward list failed: code=%d msg=%q", forwardOut.Code, forwardOut.Msg)
|
||||||
|
}
|
||||||
|
forwardRows := mustContractSlice(t, forwardOut.Data, "forward list data")
|
||||||
|
|
||||||
|
shareOut := requestContractEnvelope(t, router, adminToken, "/api/v1/federation/share/list", nil)
|
||||||
|
if shareOut.Code != 0 {
|
||||||
|
t.Fatalf("share list failed: code=%d msg=%q", shareOut.Code, shareOut.Msg)
|
||||||
|
}
|
||||||
|
localShareRows := mustContractSlice(t, shareOut.Data, "share list data")
|
||||||
|
|
||||||
|
remoteUsageOut := requestContractEnvelope(t, router, adminToken, "/api/v1/federation/share/remote-usage/list", nil)
|
||||||
|
if remoteUsageOut.Code != 0 {
|
||||||
|
t.Fatalf("remote usage list failed: code=%d msg=%q", remoteUsageOut.Code, remoteUsageOut.Msg)
|
||||||
|
}
|
||||||
|
remoteUsageRows := mustContractSlice(t, remoteUsageOut.Data, "remote usage data")
|
||||||
|
if len(remoteUsageRows) == 0 {
|
||||||
|
t.Fatalf("expected non-empty remote usage rows")
|
||||||
|
}
|
||||||
|
|
||||||
|
findForward := func(id int64) map[string]interface{} {
|
||||||
|
t.Helper()
|
||||||
|
for _, row := range forwardRows {
|
||||||
|
m, ok := row.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if contractValueAsInt64(m["id"]) == id {
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Fatalf("forward %d not found in /forward/list response", id)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
flowByShare := make(map[int64]int64)
|
||||||
|
shareIDsByTunnel := make(map[int64]map[int64]struct{})
|
||||||
|
|
||||||
|
for _, row := range remoteUsageRows {
|
||||||
|
m, ok := row.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
shareID := contractValueAsInt64(m["shareId"])
|
||||||
|
currentFlow := contractValueAsInt64(m["currentFlow"])
|
||||||
|
if shareID > 0 && currentFlow > 0 {
|
||||||
|
if currentFlow > flowByShare[shareID] {
|
||||||
|
flowByShare[shareID] = currentFlow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bindings, _ := m["bindings"].([]interface{})
|
||||||
|
for _, bindingRaw := range bindings {
|
||||||
|
binding, ok := bindingRaw.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
tunnelIDVal := contractValueAsInt64(binding["tunnelId"])
|
||||||
|
chainType := contractValueAsInt64(binding["chainType"])
|
||||||
|
if shareID <= 0 || tunnelIDVal <= 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if chainType != 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
setByTunnel, ok := shareIDsByTunnel[tunnelIDVal]
|
||||||
|
if !ok {
|
||||||
|
setByTunnel = make(map[int64]struct{})
|
||||||
|
shareIDsByTunnel[tunnelIDVal] = setByTunnel
|
||||||
|
}
|
||||||
|
setByTunnel[shareID] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, row := range localShareRows {
|
||||||
|
m, ok := row.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
shareID := contractValueAsInt64(m["id"])
|
||||||
|
currentFlow := contractValueAsInt64(m["currentFlow"])
|
||||||
|
if shareID > 0 && currentFlow > 0 {
|
||||||
|
if currentFlow > flowByShare[shareID] {
|
||||||
|
flowByShare[shareID] = currentFlow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
targetForward := findForward(forwardID)
|
||||||
|
parsedByName := contractParseShareIDFromTunnelName(contractValueAsString(targetForward["tunnelName"]))
|
||||||
|
if parsedByName != 0 {
|
||||||
|
t.Fatalf("expected custom tunnel name cannot be parsed as Share-*-Port-*, got %d", parsedByName)
|
||||||
|
}
|
||||||
|
|
||||||
|
resolveShareIDForForward := func(forward map[string]interface{}) int64 {
|
||||||
|
candidates := make(map[int64]struct{})
|
||||||
|
|
||||||
|
shareIDFromName := contractParseShareIDFromTunnelName(contractValueAsString(forward["tunnelName"]))
|
||||||
|
if shareIDFromName > 0 {
|
||||||
|
candidates[shareIDFromName] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
tunnelIDVal := contractValueAsInt64(forward["tunnelId"])
|
||||||
|
if setByTunnel, ok := shareIDsByTunnel[tunnelIDVal]; ok {
|
||||||
|
for sid := range setByTunnel {
|
||||||
|
candidates[sid] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var bestShareID int64
|
||||||
|
bestFlow := int64(0)
|
||||||
|
for sid := range candidates {
|
||||||
|
flow := flowByShare[sid]
|
||||||
|
if flow > bestFlow {
|
||||||
|
bestFlow = flow
|
||||||
|
bestShareID = sid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bestShareID
|
||||||
|
}
|
||||||
|
|
||||||
|
resolvedShareID := resolveShareIDForForward(targetForward)
|
||||||
|
if resolvedShareID != remoteShareID {
|
||||||
|
t.Fatalf("expected resolved shareID=%d via tunnel binding, got %d", remoteShareID, resolvedShareID)
|
||||||
|
}
|
||||||
|
|
||||||
|
forwardCountByShare := make(map[int64]int)
|
||||||
|
resolvedByForwardID := make(map[int64]int64)
|
||||||
|
for _, row := range forwardRows {
|
||||||
|
m, ok := row.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fid := contractValueAsInt64(m["id"])
|
||||||
|
sid := resolveShareIDForForward(m)
|
||||||
|
if sid > 0 {
|
||||||
|
resolvedByForwardID[fid] = sid
|
||||||
|
}
|
||||||
|
if sid > 0 && flowByShare[sid] > 0 {
|
||||||
|
forwardCountByShare[sid] = forwardCountByShare[sid] + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
directFlow := contractValueAsInt64(targetForward["inFlow"]) + contractValueAsInt64(targetForward["outFlow"])
|
||||||
|
if directFlow != 0 {
|
||||||
|
t.Fatalf("fixture expectation failed: directFlow should be 0, got %d", directFlow)
|
||||||
|
}
|
||||||
|
|
||||||
|
shareFlow := flowByShare[resolvedByForwardID[forwardID]]
|
||||||
|
if shareFlow <= 0 {
|
||||||
|
t.Fatalf("expected merged share flow > 0 for resolved share %d", resolvedByForwardID[forwardID])
|
||||||
|
}
|
||||||
|
|
||||||
|
count := forwardCountByShare[resolvedByForwardID[forwardID]]
|
||||||
|
if count <= 0 {
|
||||||
|
count = 1
|
||||||
|
}
|
||||||
|
estimated := shareFlow / int64(count)
|
||||||
|
if estimated < 1 {
|
||||||
|
estimated = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
displayFlow := estimated
|
||||||
|
if displayFlow <= 0 {
|
||||||
|
t.Fatalf("expected displayFlow > 0 after tunnel-binding-based merge, got %d", displayFlow)
|
||||||
|
}
|
||||||
|
if displayFlow != remoteShareFlow {
|
||||||
|
t.Fatalf("expected displayFlow=%d, got %d", remoteShareFlow, displayFlow)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func requestContractEnvelope(t *testing.T, router http.Handler, token string, path string, body interface{}) response.R {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
payload := []byte("{}")
|
||||||
|
if body != nil {
|
||||||
|
raw, err := json.Marshal(body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("marshal request body for %s: %v", path, err)
|
||||||
|
}
|
||||||
|
payload = raw
|
||||||
|
}
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodPost, path, bytes.NewReader(payload))
|
||||||
|
req.Header.Set("Authorization", token)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(res, req)
|
||||||
|
|
||||||
|
if res.Code != http.StatusOK {
|
||||||
|
t.Fatalf("expected http 200 for %s, got %d", path, res.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
var out response.R
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(&out); err != nil {
|
||||||
|
t.Fatalf("decode response for %s: %v", path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustContractSlice(t *testing.T, data interface{}, label string) []interface{} {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
rows, ok := data.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected %s to be []interface{}, got %T", label, data)
|
||||||
|
}
|
||||||
|
return rows
|
||||||
|
}
|
||||||
|
|
||||||
|
func contractParseShareIDFromTunnelName(tunnelName string) int64 {
|
||||||
|
normalized := strings.TrimSpace(tunnelName)
|
||||||
|
if !strings.HasPrefix(normalized, "Share-") {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
raw := strings.TrimPrefix(normalized, "Share-")
|
||||||
|
idx := strings.Index(raw, "-Port-")
|
||||||
|
if idx <= 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
shareID, err := strconv.ParseInt(strings.TrimSpace(raw[:idx]), 10, 64)
|
||||||
|
if err != nil || shareID <= 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return shareID
|
||||||
|
}
|
||||||
|
|
||||||
|
func contractValueAsInt64(v interface{}) int64 {
|
||||||
|
switch n := v.(type) {
|
||||||
|
case int64:
|
||||||
|
return n
|
||||||
|
case int:
|
||||||
|
return int64(n)
|
||||||
|
case float64:
|
||||||
|
return int64(n)
|
||||||
|
case json.Number:
|
||||||
|
i, err := n.Int64()
|
||||||
|
if err == nil {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
f, err := n.Float64()
|
||||||
|
if err == nil {
|
||||||
|
return int64(f)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
case string:
|
||||||
|
i, err := strconv.ParseInt(strings.TrimSpace(n), 10, 64)
|
||||||
|
if err == nil {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func contractValueAsString(v interface{}) string {
|
||||||
|
s, _ := v.(string)
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package contract_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
@@ -471,6 +472,144 @@ func TestUserTunnelReassignmentKeepsStableID(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestForwardSpeedIDWriteAndClearContracts(t *testing.T) {
|
||||||
|
secret := "contract-jwt-secret"
|
||||||
|
router, repo := setupContractRouter(t, secret)
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
|
||||||
|
adminToken, err := auth.GenerateToken(1, "admin_user", 0, secret)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("generate admin token: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := repo.DB().Exec(`
|
||||||
|
INSERT INTO user(id, user, pwd, role_id, exp_time, flow, in_flow, out_flow, flow_reset_time, num, created_time, updated_time, status)
|
||||||
|
VALUES(2, 'speed_user', '3c85cdebade1c51cf64ca9f3c09d182d', 1, 2727251700000, 99999, 0, 0, 1, 99999, ?, ?, 1)
|
||||||
|
`, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert user: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := repo.DB().Exec(`
|
||||||
|
INSERT INTO tunnel(name, traffic_ratio, type, protocol, flow, created_time, updated_time, status, in_ip, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, "forward-speed-tunnel", 1.0, 1, "tls", 99999, now, now, 1, nil, 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert tunnel: %v", err)
|
||||||
|
}
|
||||||
|
tunnelID := mustLastInsertID(t, repo, "forward-speed-tunnel")
|
||||||
|
|
||||||
|
if err := repo.DB().Exec(`
|
||||||
|
INSERT INTO node(name, secret, server_ip, server_ip_v4, server_ip_v6, port, interface_name, version, http, tls, socks, created_time, updated_time, status, tcp_listen_addr, udp_listen_addr, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, "forward-speed-node", "forward-speed-secret", "10.30.0.1", "10.30.0.1", "", "31000-31010", "", "v1", 1, 1, 1, now, now, 1, "[::]", "[::]", 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert node: %v", err)
|
||||||
|
}
|
||||||
|
nodeID := mustLastInsertID(t, repo, "forward-speed-node")
|
||||||
|
|
||||||
|
if err := repo.DB().Exec(`
|
||||||
|
INSERT INTO chain_tunnel(tunnel_id, chain_type, node_id, port, strategy, inx, protocol)
|
||||||
|
VALUES(?, 1, ?, 31001, 'round', 1, 'tls')
|
||||||
|
`, tunnelID, nodeID).Error; err != nil {
|
||||||
|
t.Fatalf("insert chain_tunnel: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := repo.DB().Exec(`
|
||||||
|
INSERT INTO speed_limit(name, speed, tunnel_id, tunnel_name, created_time, updated_time, status)
|
||||||
|
VALUES(?, ?, NULL, NULL, ?, NULL, ?)
|
||||||
|
`, "forward-speed-limit-a", 2048, now, 1).Error; err != nil {
|
||||||
|
t.Fatalf("insert speed limit a: %v", err)
|
||||||
|
}
|
||||||
|
speedIDA := mustLastInsertID(t, repo, "forward-speed-limit-a")
|
||||||
|
|
||||||
|
if err := repo.DB().Exec(`
|
||||||
|
INSERT INTO speed_limit(name, speed, tunnel_id, tunnel_name, created_time, updated_time, status)
|
||||||
|
VALUES(?, ?, NULL, NULL, ?, NULL, ?)
|
||||||
|
`, "forward-speed-limit-b", 4096, now, 1).Error; err != nil {
|
||||||
|
t.Fatalf("insert speed limit b: %v", err)
|
||||||
|
}
|
||||||
|
speedIDB := mustLastInsertID(t, repo, "forward-speed-limit-b")
|
||||||
|
|
||||||
|
server := httptest.NewServer(router)
|
||||||
|
defer server.Close()
|
||||||
|
stopNode := startMockNodeSession(t, server.URL, "forward-speed-secret")
|
||||||
|
defer stopNode()
|
||||||
|
|
||||||
|
createPayload := map[string]interface{}{
|
||||||
|
"name": "forward-speed-target",
|
||||||
|
"tunnelId": tunnelID,
|
||||||
|
"remoteAddr": "1.1.1.1:443",
|
||||||
|
"strategy": "fifo",
|
||||||
|
"speedId": speedIDA,
|
||||||
|
}
|
||||||
|
createBody, err := json.Marshal(createPayload)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("marshal create payload: %v", err)
|
||||||
|
}
|
||||||
|
createReq := httptest.NewRequest(http.MethodPost, "/api/v1/forward/create", bytes.NewReader(createBody))
|
||||||
|
createReq.Header.Set("Authorization", adminToken)
|
||||||
|
createReq.Header.Set("Content-Type", "application/json")
|
||||||
|
createRes := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(createRes, createReq)
|
||||||
|
assertCode(t, createRes, 0)
|
||||||
|
|
||||||
|
forwardID := mustLastInsertID(t, repo, "forward-speed-target")
|
||||||
|
storedSpeed := repo.DB().Raw(`SELECT speed_id FROM forward WHERE id = ?`, forwardID).Row()
|
||||||
|
var createdSpeed sql.NullInt64
|
||||||
|
if err := storedSpeed.Scan(&createdSpeed); err != nil {
|
||||||
|
t.Fatalf("query created forward speed_id: %v", err)
|
||||||
|
}
|
||||||
|
if !createdSpeed.Valid || createdSpeed.Int64 != speedIDA {
|
||||||
|
t.Fatalf("expected created speed_id=%d, got valid=%v value=%d", speedIDA, createdSpeed.Valid, createdSpeed.Int64)
|
||||||
|
}
|
||||||
|
|
||||||
|
updateToBPayload := map[string]interface{}{
|
||||||
|
"id": forwardID,
|
||||||
|
"speedId": speedIDB,
|
||||||
|
}
|
||||||
|
updateToBBody, err := json.Marshal(updateToBPayload)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("marshal update-to-b payload: %v", err)
|
||||||
|
}
|
||||||
|
updateToBReq := httptest.NewRequest(http.MethodPost, "/api/v1/forward/update", bytes.NewReader(updateToBBody))
|
||||||
|
updateToBReq.Header.Set("Authorization", adminToken)
|
||||||
|
updateToBReq.Header.Set("Content-Type", "application/json")
|
||||||
|
updateToBRes := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(updateToBRes, updateToBReq)
|
||||||
|
assertCode(t, updateToBRes, 0)
|
||||||
|
|
||||||
|
storedSpeed = repo.DB().Raw(`SELECT speed_id FROM forward WHERE id = ?`, forwardID).Row()
|
||||||
|
var updatedSpeed sql.NullInt64
|
||||||
|
if err := storedSpeed.Scan(&updatedSpeed); err != nil {
|
||||||
|
t.Fatalf("query updated forward speed_id: %v", err)
|
||||||
|
}
|
||||||
|
if !updatedSpeed.Valid || updatedSpeed.Int64 != speedIDB {
|
||||||
|
t.Fatalf("expected updated speed_id=%d, got valid=%v value=%d", speedIDB, updatedSpeed.Valid, updatedSpeed.Int64)
|
||||||
|
}
|
||||||
|
|
||||||
|
clearPayload := map[string]interface{}{
|
||||||
|
"id": forwardID,
|
||||||
|
"speedId": nil,
|
||||||
|
}
|
||||||
|
clearBody, err := json.Marshal(clearPayload)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("marshal clear payload: %v", err)
|
||||||
|
}
|
||||||
|
clearReq := httptest.NewRequest(http.MethodPost, "/api/v1/forward/update", bytes.NewReader(clearBody))
|
||||||
|
clearReq.Header.Set("Authorization", adminToken)
|
||||||
|
clearReq.Header.Set("Content-Type", "application/json")
|
||||||
|
clearRes := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(clearRes, clearReq)
|
||||||
|
assertCode(t, clearRes, 0)
|
||||||
|
|
||||||
|
storedSpeed = repo.DB().Raw(`SELECT speed_id FROM forward WHERE id = ?`, forwardID).Row()
|
||||||
|
var clearedSpeed sql.NullInt64
|
||||||
|
if err := storedSpeed.Scan(&clearedSpeed); err != nil {
|
||||||
|
t.Fatalf("query cleared forward speed_id: %v", err)
|
||||||
|
}
|
||||||
|
if clearedSpeed.Valid {
|
||||||
|
t.Fatalf("expected cleared speed_id to be NULL, got %d", clearedSpeed.Int64)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func jsonNumber(v int64) string {
|
func jsonNumber(v int64) string {
|
||||||
return strconv.FormatInt(v, 10)
|
return strconv.FormatInt(v, 10)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,402 @@
|
|||||||
|
package contract_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
|
||||||
|
"go-backend/internal/auth"
|
||||||
|
"go-backend/internal/http/response"
|
||||||
|
"go-backend/internal/security"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestForwardCreateRollbackWhenLimiterDispatchFailsContract(t *testing.T) {
|
||||||
|
secret := "contract-jwt-secret"
|
||||||
|
router, r := setupContractRouter(t, secret)
|
||||||
|
server := httptest.NewServer(router)
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
adminToken, err := auth.GenerateToken(1, "admin_user", 0, secret)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("generate admin token: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO tunnel(name, traffic_ratio, type, protocol, flow, created_time, updated_time, status, in_ip, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, "limiter-fail-tunnel", 1.0, 1, "tls", 99999, now, now, 1, nil, 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert tunnel: %v", err)
|
||||||
|
}
|
||||||
|
tunnelID := mustLastInsertID(t, r, "limiter-fail-tunnel")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO node(name, secret, server_ip, server_ip_v4, server_ip_v6, port, interface_name, version, http, tls, socks, created_time, updated_time, status, tcp_listen_addr, udp_listen_addr, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, "limiter-fail-node", "limiter-fail-secret", "10.20.0.1", "10.20.0.1", "", "32000-32010", "", "v1", 1, 1, 1, now, now, 1, "[::]", "[::]", 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert node: %v", err)
|
||||||
|
}
|
||||||
|
nodeID := mustLastInsertID(t, r, "limiter-fail-node")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO chain_tunnel(tunnel_id, chain_type, node_id, port, strategy, inx, protocol)
|
||||||
|
VALUES(?, 1, ?, 32001, 'round', 1, 'tls')
|
||||||
|
`, tunnelID, nodeID).Error; err != nil {
|
||||||
|
t.Fatalf("insert chain_tunnel: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO speed_limit(name, speed, tunnel_id, tunnel_name, created_time, updated_time, status)
|
||||||
|
VALUES(?, ?, NULL, NULL, ?, NULL, ?)
|
||||||
|
`, "limiter-fail-rule", 1024, now, 1).Error; err != nil {
|
||||||
|
t.Fatalf("insert speed limit: %v", err)
|
||||||
|
}
|
||||||
|
speedID := mustLastInsertID(t, r, "limiter-fail-rule")
|
||||||
|
|
||||||
|
stopNode := startMockNodeSessionWithCommandFailures(t, server.URL, "limiter-fail-secret", map[string]string{
|
||||||
|
"addlimiters": "mock add limiters failed",
|
||||||
|
})
|
||||||
|
defer stopNode()
|
||||||
|
|
||||||
|
payload := map[string]interface{}{
|
||||||
|
"name": "limiter-fail-forward",
|
||||||
|
"tunnelId": tunnelID,
|
||||||
|
"remoteAddr": "1.1.1.1:443",
|
||||||
|
"strategy": "fifo",
|
||||||
|
"speedId": speedID,
|
||||||
|
}
|
||||||
|
body, err := json.Marshal(payload)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("marshal payload: %v", err)
|
||||||
|
}
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/forward/create", bytes.NewReader(body))
|
||||||
|
req.Header.Set("Authorization", adminToken)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(res, req)
|
||||||
|
|
||||||
|
var out response.R
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(&out); err != nil {
|
||||||
|
t.Fatalf("decode response: %v", err)
|
||||||
|
}
|
||||||
|
if out.Code == 0 {
|
||||||
|
t.Fatalf("expected create failure on limiter dispatch, got code=0")
|
||||||
|
}
|
||||||
|
|
||||||
|
forwardCount := mustQueryInt(t, r, `SELECT COUNT(1) FROM forward WHERE name = ?`, "limiter-fail-forward")
|
||||||
|
if forwardCount != 0 {
|
||||||
|
t.Fatalf("expected forward rollback delete on limiter failure, got count=%d", forwardCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBatchAssignRollbackWhenLimiterDispatchFailsContract(t *testing.T) {
|
||||||
|
secret := "contract-jwt-secret"
|
||||||
|
router, r := setupContractRouter(t, secret)
|
||||||
|
server := httptest.NewServer(router)
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
adminToken, err := auth.GenerateToken(1, "admin_user", 0, secret)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("generate admin token: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO user(id, user, pwd, role_id, exp_time, flow, in_flow, out_flow, flow_reset_time, num, created_time, updated_time, status)
|
||||||
|
VALUES(2, 'assign_user', '3c85cdebade1c51cf64ca9f3c09d182d', 1, 2727251700000, 99999, 0, 0, 1, 99999, ?, ?, 1)
|
||||||
|
`, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert user: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO tunnel(name, traffic_ratio, type, protocol, flow, created_time, updated_time, status, in_ip, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, "assign-limiter-fail-tunnel", 1.0, 1, "tls", 99999, now, now, 1, nil, 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert tunnel: %v", err)
|
||||||
|
}
|
||||||
|
tunnelID := mustLastInsertID(t, r, "assign-limiter-fail-tunnel")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO node(name, secret, server_ip, server_ip_v4, server_ip_v6, port, interface_name, version, http, tls, socks, created_time, updated_time, status, tcp_listen_addr, udp_listen_addr, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, "assign-limiter-fail-node", "assign-limiter-fail-secret", "10.21.0.1", "10.21.0.1", "", "33000-33010", "", "v1", 1, 1, 1, now, now, 1, "[::]", "[::]", 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert node: %v", err)
|
||||||
|
}
|
||||||
|
nodeID := mustLastInsertID(t, r, "assign-limiter-fail-node")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO chain_tunnel(tunnel_id, chain_type, node_id, port, strategy, inx, protocol)
|
||||||
|
VALUES(?, 1, ?, 33001, 'round', 1, 'tls')
|
||||||
|
`, tunnelID, nodeID).Error; err != nil {
|
||||||
|
t.Fatalf("insert chain_tunnel: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO speed_limit(name, speed, tunnel_id, tunnel_name, created_time, updated_time, status)
|
||||||
|
VALUES(?, ?, NULL, NULL, ?, NULL, ?)
|
||||||
|
`, "assign-limiter-fail-rule", 2048, now, 1).Error; err != nil {
|
||||||
|
t.Fatalf("insert speed limit: %v", err)
|
||||||
|
}
|
||||||
|
speedID := mustLastInsertID(t, r, "assign-limiter-fail-rule")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO user_tunnel(id, user_id, tunnel_id, speed_id, num, flow, in_flow, out_flow, flow_reset_time, exp_time, status)
|
||||||
|
VALUES(21, 2, ?, NULL, 999, 99999, 0, 0, 1, 2727251700000, 1)
|
||||||
|
`, tunnelID).Error; err != nil {
|
||||||
|
t.Fatalf("insert user_tunnel: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO forward(user_id, user_name, name, tunnel_id, remote_addr, strategy, in_flow, out_flow, created_time, updated_time, status, inx)
|
||||||
|
VALUES(2, 'assign_user', 'assign-limiter-fail-forward', ?, '9.9.9.9:53', 'fifo', 0, 0, ?, ?, 1, 0)
|
||||||
|
`, tunnelID, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert forward: %v", err)
|
||||||
|
}
|
||||||
|
forwardID := mustLastInsertID(t, r, "assign-limiter-fail-forward")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`INSERT INTO forward_port(forward_id, node_id, port) VALUES(?, ?, ?)`, forwardID, nodeID, 33001).Error; err != nil {
|
||||||
|
t.Fatalf("insert forward_port: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stopNode := startMockNodeSessionWithCommandFailures(t, server.URL, "assign-limiter-fail-secret", map[string]string{
|
||||||
|
"addlimiters": "mock add limiters failed",
|
||||||
|
})
|
||||||
|
defer stopNode()
|
||||||
|
|
||||||
|
assignPayload := map[string]interface{}{
|
||||||
|
"userId": 2,
|
||||||
|
"tunnels": []map[string]interface{}{{
|
||||||
|
"tunnelId": tunnelID,
|
||||||
|
"speedId": speedID,
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
body, err := json.Marshal(assignPayload)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("marshal assign payload: %v", err)
|
||||||
|
}
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/tunnel/user/batch-assign", bytes.NewReader(body))
|
||||||
|
req.Header.Set("Authorization", adminToken)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(res, req)
|
||||||
|
|
||||||
|
var out response.R
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(&out); err != nil {
|
||||||
|
t.Fatalf("decode response: %v", err)
|
||||||
|
}
|
||||||
|
if out.Code == 0 {
|
||||||
|
t.Fatalf("expected assign failure on limiter dispatch, got code=0")
|
||||||
|
}
|
||||||
|
|
||||||
|
var persistedSpeedID sql.NullInt64
|
||||||
|
if err := r.DB().Raw(`SELECT speed_id FROM user_tunnel WHERE user_id = 2 AND tunnel_id = ?`, tunnelID).Row().Scan(&persistedSpeedID); err != nil {
|
||||||
|
t.Fatalf("query user_tunnel speed_id: %v", err)
|
||||||
|
}
|
||||||
|
if persistedSpeedID.Valid {
|
||||||
|
t.Fatalf("expected speed_id rollback to NULL, got %d", persistedSpeedID.Int64)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBatchAssignInsertRollbackWhenLimiterDispatchFailsContract(t *testing.T) {
|
||||||
|
secret := "contract-jwt-secret"
|
||||||
|
router, r := setupContractRouter(t, secret)
|
||||||
|
server := httptest.NewServer(router)
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
adminToken, err := auth.GenerateToken(1, "admin_user", 0, secret)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("generate admin token: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO user(id, user, pwd, role_id, exp_time, flow, in_flow, out_flow, flow_reset_time, num, created_time, updated_time, status)
|
||||||
|
VALUES(3, 'assign_insert_user', '3c85cdebade1c51cf64ca9f3c09d182d', 1, 2727251700000, 99999, 0, 0, 1, 99999, ?, ?, 1)
|
||||||
|
`, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert user: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO tunnel(name, traffic_ratio, type, protocol, flow, created_time, updated_time, status, in_ip, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, "assign-insert-limiter-fail-tunnel", 1.0, 1, "tls", 99999, now, now, 1, nil, 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert tunnel: %v", err)
|
||||||
|
}
|
||||||
|
tunnelID := mustLastInsertID(t, r, "assign-insert-limiter-fail-tunnel")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO node(name, secret, server_ip, server_ip_v4, server_ip_v6, port, interface_name, version, http, tls, socks, created_time, updated_time, status, tcp_listen_addr, udp_listen_addr, inx)
|
||||||
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`, "assign-insert-limiter-fail-node", "assign-insert-limiter-fail-secret", "10.22.0.1", "10.22.0.1", "", "34000-34010", "", "v1", 1, 1, 1, now, now, 1, "[::]", "[::]", 0).Error; err != nil {
|
||||||
|
t.Fatalf("insert node: %v", err)
|
||||||
|
}
|
||||||
|
nodeID := mustLastInsertID(t, r, "assign-insert-limiter-fail-node")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO chain_tunnel(tunnel_id, chain_type, node_id, port, strategy, inx, protocol)
|
||||||
|
VALUES(?, 1, ?, 34001, 'round', 1, 'tls')
|
||||||
|
`, tunnelID, nodeID).Error; err != nil {
|
||||||
|
t.Fatalf("insert chain_tunnel: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO speed_limit(name, speed, tunnel_id, tunnel_name, created_time, updated_time, status)
|
||||||
|
VALUES(?, ?, NULL, NULL, ?, NULL, ?)
|
||||||
|
`, "assign-insert-limiter-fail-rule", 3072, now, 1).Error; err != nil {
|
||||||
|
t.Fatalf("insert speed limit: %v", err)
|
||||||
|
}
|
||||||
|
speedID := mustLastInsertID(t, r, "assign-insert-limiter-fail-rule")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO forward(user_id, user_name, name, tunnel_id, remote_addr, strategy, in_flow, out_flow, created_time, updated_time, status, inx)
|
||||||
|
VALUES(3, 'assign_insert_user', 'assign-insert-limiter-fail-forward', ?, '8.8.4.4:53', 'fifo', 0, 0, ?, ?, 1, 0)
|
||||||
|
`, tunnelID, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("insert forward: %v", err)
|
||||||
|
}
|
||||||
|
forwardID := mustLastInsertID(t, r, "assign-insert-limiter-fail-forward")
|
||||||
|
|
||||||
|
if err := r.DB().Exec(`INSERT INTO forward_port(forward_id, node_id, port) VALUES(?, ?, ?)`, forwardID, nodeID, 34001).Error; err != nil {
|
||||||
|
t.Fatalf("insert forward_port: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stopNode := startMockNodeSessionWithCommandFailures(t, server.URL, "assign-insert-limiter-fail-secret", map[string]string{
|
||||||
|
"addlimiters": "mock add limiters failed",
|
||||||
|
})
|
||||||
|
defer stopNode()
|
||||||
|
|
||||||
|
assignPayload := map[string]interface{}{
|
||||||
|
"userId": 3,
|
||||||
|
"tunnels": []map[string]interface{}{{
|
||||||
|
"tunnelId": tunnelID,
|
||||||
|
"speedId": speedID,
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
body, err := json.Marshal(assignPayload)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("marshal assign payload: %v", err)
|
||||||
|
}
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/tunnel/user/batch-assign", bytes.NewReader(body))
|
||||||
|
req.Header.Set("Authorization", adminToken)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(res, req)
|
||||||
|
|
||||||
|
var out response.R
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(&out); err != nil {
|
||||||
|
t.Fatalf("decode response: %v", err)
|
||||||
|
}
|
||||||
|
if out.Code == 0 {
|
||||||
|
t.Fatalf("expected assign(insert) failure on limiter dispatch, got code=0")
|
||||||
|
}
|
||||||
|
|
||||||
|
insertedCount := mustQueryInt(t, r, `SELECT COUNT(1) FROM user_tunnel WHERE user_id = 3 AND tunnel_id = ?`, tunnelID)
|
||||||
|
if insertedCount != 0 {
|
||||||
|
t.Fatalf("expected inserted user_tunnel rollback delete, got count=%d", insertedCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func startMockNodeSessionWithCommandFailures(t *testing.T, baseURL string, nodeSecret string, failCommands map[string]string) func() {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
u, err := url.Parse(baseURL)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("parse provider url: %v", err)
|
||||||
|
}
|
||||||
|
if strings.EqualFold(u.Scheme, "https") {
|
||||||
|
u.Scheme = "wss"
|
||||||
|
} else {
|
||||||
|
u.Scheme = "ws"
|
||||||
|
}
|
||||||
|
u.Path = "/system-info"
|
||||||
|
q := u.Query()
|
||||||
|
q.Set("type", "1")
|
||||||
|
q.Set("secret", nodeSecret)
|
||||||
|
q.Set("version", "v1")
|
||||||
|
q.Set("http", "1")
|
||||||
|
q.Set("tls", "1")
|
||||||
|
q.Set("socks", "1")
|
||||||
|
u.RawQuery = q.Encode()
|
||||||
|
|
||||||
|
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("dial mock node websocket: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
for {
|
||||||
|
_, raw, readErr := conn.ReadMessage()
|
||||||
|
if readErr != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
plain := raw
|
||||||
|
var wrap struct {
|
||||||
|
Encrypted bool `json:"encrypted"`
|
||||||
|
Data string `json:"data"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(raw, &wrap); err == nil && wrap.Encrypted && strings.TrimSpace(wrap.Data) != "" {
|
||||||
|
crypto, cryptoErr := security.NewAESCrypto(nodeSecret)
|
||||||
|
if cryptoErr == nil {
|
||||||
|
if dec, decErr := crypto.Decrypt(wrap.Data); decErr == nil {
|
||||||
|
plain = []byte(dec)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var cmd struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
RequestID string `json:"requestId"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(plain, &cmd); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(cmd.RequestID) == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdType := strings.TrimSpace(cmd.Type)
|
||||||
|
failMsg, shouldFail := failCommands[strings.ToLower(cmdType)]
|
||||||
|
|
||||||
|
respType := fmt.Sprintf("%sResponse", cmdType)
|
||||||
|
respPayload := map[string]interface{}{
|
||||||
|
"type": respType,
|
||||||
|
"success": !shouldFail,
|
||||||
|
"message": "OK",
|
||||||
|
"requestId": cmd.RequestID,
|
||||||
|
}
|
||||||
|
if shouldFail {
|
||||||
|
if strings.TrimSpace(failMsg) == "" {
|
||||||
|
failMsg = "mock command failed"
|
||||||
|
}
|
||||||
|
respPayload["message"] = failMsg
|
||||||
|
}
|
||||||
|
|
||||||
|
respBytes, err := json.Marshal(respPayload)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
_ = conn.WriteMessage(websocket.TextMessage, respBytes)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
var stopOnce sync.Once
|
||||||
|
return func() {
|
||||||
|
stopOnce.Do(func() {
|
||||||
|
_ = conn.Close()
|
||||||
|
wg.Wait()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,6 +45,18 @@ func TestCaptchaVerifyLoginContract(t *testing.T) {
|
|||||||
assertCodeMsg(t, resp, -1, "验证码校验失败")
|
assertCodeMsg(t, resp, -1, "验证码校验失败")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("whmcs api client bypasses captcha", func(t *testing.T) {
|
||||||
|
body := bytes.NewBufferString(`{"username":"admin_user","password":"admin_user","captchaId":""}`)
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/user/login", body)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("X-FLVX-API-Client", "whmcs")
|
||||||
|
resp := httptest.NewRecorder()
|
||||||
|
|
||||||
|
router.ServeHTTP(resp, req)
|
||||||
|
|
||||||
|
assertCode(t, resp, 0)
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("captcha token is one-time and consumed by login", func(t *testing.T) {
|
t.Run("captcha token is one-time and consumed by login", func(t *testing.T) {
|
||||||
verifyReq := httptest.NewRequest(http.MethodPost, "/api/v1/captcha/verify", bytes.NewBufferString(`{"id":"captcha-token-1","data":"ok"}`))
|
verifyReq := httptest.NewRequest(http.MethodPost, "/api/v1/captcha/verify", bytes.NewBufferString(`{"id":"captcha-token-1","data":"ok"}`))
|
||||||
verifyReq.Header.Set("Content-Type", "application/json")
|
verifyReq.Header.Set("Content-Type", "application/json")
|
||||||
|
|||||||
@@ -0,0 +1,462 @@
|
|||||||
|
package contract_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"go-backend/internal/auth"
|
||||||
|
"go-backend/internal/http/response"
|
||||||
|
"go-backend/internal/store/repo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestSpeedLimitWithoutTunnelContract tests that speed limits can be created without binding to a tunnel
|
||||||
|
func TestSpeedLimitWithoutTunnelContract(t *testing.T) {
|
||||||
|
secret := "contract-jwt-secret"
|
||||||
|
router, _ := setupContractRouter(t, secret)
|
||||||
|
|
||||||
|
adminToken, err := auth.GenerateToken(1, "admin_user", 0, secret)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("generate admin token: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a speed limit without tunnel binding
|
||||||
|
t.Run("create speed limit without tunnel", func(t *testing.T) {
|
||||||
|
body := `{"name":"test-limit-no-tunnel","speed":100,"status":1}`
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/speed-limit/create", bytes.NewBufferString(body))
|
||||||
|
req.Header.Set("Authorization", adminToken)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(res, req)
|
||||||
|
|
||||||
|
assertCode(t, res, 0)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Verify the speed limit has null tunnelId
|
||||||
|
t.Run("list speed limits shows null tunnelId", func(t *testing.T) {
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/speed-limit/list", nil)
|
||||||
|
req.Header.Set("Authorization", adminToken)
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(res, req)
|
||||||
|
|
||||||
|
var out response.R
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(&out); err != nil {
|
||||||
|
t.Fatalf("decode response: %v", err)
|
||||||
|
}
|
||||||
|
if out.Code != 0 {
|
||||||
|
t.Fatalf("expected code 0, got %d", out.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
data, ok := out.Data.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected data to be array, got %T", out.Data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find our speed limit
|
||||||
|
var found bool
|
||||||
|
for _, item := range data {
|
||||||
|
m, ok := item.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if m["name"] == "test-limit-no-tunnel" {
|
||||||
|
found = true
|
||||||
|
// tunnelId should be nil/not present for unbound speed limits
|
||||||
|
if tunnelID, exists := m["tunnelId"]; exists && tunnelID != nil {
|
||||||
|
t.Fatalf("expected tunnelId to be nil for unbound speed limit, got %v", tunnelID)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
t.Fatal("speed limit 'test-limit-no-tunnel' not found in list")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSpeedLimitWithTunnelContract tests that speed limits can still be bound to tunnels
|
||||||
|
func TestSpeedLimitWithTunnelContract(t *testing.T) {
|
||||||
|
secret := "contract-jwt-secret"
|
||||||
|
router, r := setupContractRouter(t, secret)
|
||||||
|
|
||||||
|
adminToken, err := auth.GenerateToken(1, "admin_user", 0, secret)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("generate admin token: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// First create a tunnel
|
||||||
|
tunnelID := mustCreateSpeedLimitTunnel(t, r, "test-tunnel-for-limit")
|
||||||
|
|
||||||
|
// Create a speed limit with tunnel binding
|
||||||
|
t.Run("create speed limit with tunnel", func(t *testing.T) {
|
||||||
|
body := `{"name":"test-limit-with-tunnel","speed":200,"tunnelId":` + jsonInt(tunnelID) + `,"status":1}`
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/speed-limit/create", bytes.NewBufferString(body))
|
||||||
|
req.Header.Set("Authorization", adminToken)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(res, req)
|
||||||
|
|
||||||
|
assertCode(t, res, 0)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Verify the speed limit has the tunnelId
|
||||||
|
t.Run("list speed limits shows tunnelId", func(t *testing.T) {
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/speed-limit/list", nil)
|
||||||
|
req.Header.Set("Authorization", adminToken)
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(res, req)
|
||||||
|
|
||||||
|
var out response.R
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(&out); err != nil {
|
||||||
|
t.Fatalf("decode response: %v", err)
|
||||||
|
}
|
||||||
|
if out.Code != 0 {
|
||||||
|
t.Fatalf("expected code 0, got %d", out.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
data, ok := out.Data.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected data to be array, got %T", out.Data)
|
||||||
|
}
|
||||||
|
|
||||||
|
var found bool
|
||||||
|
for _, item := range data {
|
||||||
|
m, ok := item.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if m["name"] == "test-limit-with-tunnel" {
|
||||||
|
found = true
|
||||||
|
tunnelIDVal, exists := m["tunnelId"]
|
||||||
|
if !exists || tunnelIDVal == nil {
|
||||||
|
t.Fatal("expected tunnelId to be present for bound speed limit")
|
||||||
|
}
|
||||||
|
// Verify tunnelId matches
|
||||||
|
if tunnelIDFloat, ok := tunnelIDVal.(float64); ok {
|
||||||
|
if int64(tunnelIDFloat) != tunnelID {
|
||||||
|
t.Fatalf("expected tunnelId %d, got %d", tunnelID, int64(tunnelIDFloat))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
t.Fatal("speed limit 'test-limit-with-tunnel' not found in list")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSpeedLimitUpdateTunnelBindingContract tests updating speed limit tunnel binding
|
||||||
|
func TestSpeedLimitUpdateTunnelBindingContract(t *testing.T) {
|
||||||
|
secret := "contract-jwt-secret"
|
||||||
|
router, r := setupContractRouter(t, secret)
|
||||||
|
|
||||||
|
adminToken, err := auth.GenerateToken(1, "admin_user", 0, secret)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("generate admin token: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a tunnel
|
||||||
|
tunnelID := mustCreateSpeedLimitTunnel(t, r, "test-tunnel-update")
|
||||||
|
|
||||||
|
// Create a speed limit without tunnel
|
||||||
|
speedLimitID := mustCreateSpeedLimitRepo(t, r, "test-limit-update", 0)
|
||||||
|
|
||||||
|
// Update to bind to tunnel
|
||||||
|
t.Run("update speed limit to bind tunnel", func(t *testing.T) {
|
||||||
|
body := `{"id":` + jsonInt(speedLimitID) + `,"name":"test-limit-update","speed":150,"tunnelId":` + jsonInt(tunnelID) + `,"status":1}`
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/speed-limit/update", bytes.NewBufferString(body))
|
||||||
|
req.Header.Set("Authorization", adminToken)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(res, req)
|
||||||
|
|
||||||
|
assertCode(t, res, 0)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Verify binding
|
||||||
|
t.Run("verify tunnel binding after update", func(t *testing.T) {
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/speed-limit/list", nil)
|
||||||
|
req.Header.Set("Authorization", adminToken)
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(res, req)
|
||||||
|
|
||||||
|
var out response.R
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(&out); err != nil {
|
||||||
|
t.Fatalf("decode response: %v", err)
|
||||||
|
}
|
||||||
|
if out.Code != 0 {
|
||||||
|
t.Fatalf("expected code 0, got %d", out.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
data, ok := out.Data.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected data to be array, got %T", out.Data)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range data {
|
||||||
|
m, ok := item.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if m["name"] == "test-limit-update" {
|
||||||
|
tunnelIDVal, exists := m["tunnelId"]
|
||||||
|
if !exists || tunnelIDVal == nil {
|
||||||
|
t.Fatal("expected tunnelId to be present after update")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Fatal("speed limit 'test-limit-update' not found")
|
||||||
|
})
|
||||||
|
|
||||||
|
// Update to unbind from tunnel (set tunnelId to null)
|
||||||
|
t.Run("update speed limit to unbind tunnel", func(t *testing.T) {
|
||||||
|
body := `{"id":` + jsonInt(speedLimitID) + `,"name":"test-limit-update","speed":150,"status":1}`
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/speed-limit/update", bytes.NewBufferString(body))
|
||||||
|
req.Header.Set("Authorization", adminToken)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(res, req)
|
||||||
|
|
||||||
|
assertCode(t, res, 0)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Verify unbinding
|
||||||
|
t.Run("verify tunnel unbinding after update", func(t *testing.T) {
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/speed-limit/list", nil)
|
||||||
|
req.Header.Set("Authorization", adminToken)
|
||||||
|
res := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(res, req)
|
||||||
|
|
||||||
|
var out response.R
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(&out); err != nil {
|
||||||
|
t.Fatalf("decode response: %v", err)
|
||||||
|
}
|
||||||
|
if out.Code != 0 {
|
||||||
|
t.Fatalf("expected code 0, got %d", out.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
data, ok := out.Data.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected data to be array, got %T", out.Data)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range data {
|
||||||
|
m, ok := item.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if m["name"] == "test-limit-update" {
|
||||||
|
if tunnelIDVal, exists := m["tunnelId"]; exists && tunnelIDVal != nil {
|
||||||
|
t.Fatalf("expected tunnelId to be nil after unbinding, got %v", tunnelIDVal)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Fatal("speed limit 'test-limit-update' not found")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSpeedLimitDatabaseNullableFields tests database-level nullable fields
|
||||||
|
func TestSpeedLimitDatabaseNullableFields(t *testing.T) {
|
||||||
|
dbPath := filepath.Join(t.TempDir(), "speed-limit-null.db")
|
||||||
|
r, err := repo.Open(dbPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open sqlite: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = r.Close() })
|
||||||
|
|
||||||
|
// Create speed limit via repository
|
||||||
|
t.Run("repository create speed limit without tunnel", func(t *testing.T) {
|
||||||
|
id, err := r.CreateSpeedLimit("db-test-limit", 100, nil, "", 1, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateSpeedLimit failed: %v", err)
|
||||||
|
}
|
||||||
|
if id <= 0 {
|
||||||
|
t.Fatalf("expected valid id, got %d", id)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Verify TunnelID is null in database
|
||||||
|
t.Run("verify null TunnelID in database", func(t *testing.T) {
|
||||||
|
var tunnelID sql.NullInt64
|
||||||
|
var tunnelName sql.NullString
|
||||||
|
err := r.DB().Raw("SELECT tunnel_id, tunnel_name FROM speed_limit WHERE name = ?", "db-test-limit").Row().Scan(&tunnelID, &tunnelName)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("query failed: %v", err)
|
||||||
|
}
|
||||||
|
if tunnelID.Valid {
|
||||||
|
t.Fatalf("expected TunnelID to be NULL, got %d", tunnelID.Int64)
|
||||||
|
}
|
||||||
|
if tunnelName.Valid && tunnelName.String != "" {
|
||||||
|
t.Fatalf("expected TunnelName to be NULL or empty, got %s", tunnelName.String)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Create a tunnel for binding test
|
||||||
|
tunnelID := mustCreateSpeedLimitTunnel(t, r, "db-test-tunnel")
|
||||||
|
|
||||||
|
// Create speed limit with tunnel
|
||||||
|
t.Run("repository create speed limit with tunnel", func(t *testing.T) {
|
||||||
|
id, err := r.CreateSpeedLimit("db-test-limit-with-tunnel", 200, &tunnelID, "db-test-tunnel", 1, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateSpeedLimit failed: %v", err)
|
||||||
|
}
|
||||||
|
if id <= 0 {
|
||||||
|
t.Fatalf("expected valid id, got %d", id)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Verify TunnelID is set
|
||||||
|
t.Run("verify TunnelID is set in database", func(t *testing.T) {
|
||||||
|
var dbTunnelID sql.NullInt64
|
||||||
|
var dbTunnelName sql.NullString
|
||||||
|
err := r.DB().Raw("SELECT tunnel_id, tunnel_name FROM speed_limit WHERE name = ?", "db-test-limit-with-tunnel").Row().Scan(&dbTunnelID, &dbTunnelName)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("query failed: %v", err)
|
||||||
|
}
|
||||||
|
if !dbTunnelID.Valid {
|
||||||
|
t.Fatal("expected TunnelID to be valid")
|
||||||
|
}
|
||||||
|
if dbTunnelID.Int64 != tunnelID {
|
||||||
|
t.Fatalf("expected TunnelID %d, got %d", tunnelID, dbTunnelID.Int64)
|
||||||
|
}
|
||||||
|
if !dbTunnelName.Valid || dbTunnelName.String != "db-test-tunnel" {
|
||||||
|
t.Fatalf("expected TunnelName 'db-test-tunnel', got %v", dbTunnelName.String)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test GetSpeedLimitTunnelID returns correct nullability
|
||||||
|
t.Run("GetSpeedLimitTunnelID returns null for unbound limit", func(t *testing.T) {
|
||||||
|
result := r.GetSpeedLimitTunnelID(1) // First speed limit (db-test-limit)
|
||||||
|
if result.Valid {
|
||||||
|
t.Fatalf("expected GetSpeedLimitTunnelID to return invalid/null, got valid with value %d", result.Int64)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("GetSpeedLimitTunnelID returns value for bound limit", func(t *testing.T) {
|
||||||
|
result := r.GetSpeedLimitTunnelID(2) // Second speed limit (db-test-limit-with-tunnel)
|
||||||
|
if !result.Valid {
|
||||||
|
t.Fatal("expected GetSpeedLimitTunnelID to return valid result for bound limit")
|
||||||
|
}
|
||||||
|
if result.Int64 != tunnelID {
|
||||||
|
t.Fatalf("expected TunnelID %d, got %d", tunnelID, result.Int64)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSpeedLimitUpdateUnbindFromTunnel tests unbinding a speed limit from a tunnel
|
||||||
|
func TestSpeedLimitUpdateUnbindFromTunnel(t *testing.T) {
|
||||||
|
dbPath := filepath.Join(t.TempDir(), "speed-limit-unbind.db")
|
||||||
|
r, err := repo.Open(dbPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open sqlite: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = r.Close() })
|
||||||
|
|
||||||
|
// Create tunnel
|
||||||
|
tunnelID := mustCreateSpeedLimitTunnel(t, r, "unbind-test-tunnel")
|
||||||
|
|
||||||
|
// Create speed limit bound to tunnel
|
||||||
|
speedLimitID, err := r.CreateSpeedLimit("unbind-test-limit", 300, &tunnelID, "unbind-test-tunnel", 1, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("create speed limit: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify initial binding
|
||||||
|
t.Run("verify initial binding", func(t *testing.T) {
|
||||||
|
result := r.GetSpeedLimitTunnelID(speedLimitID)
|
||||||
|
if !result.Valid {
|
||||||
|
t.Fatal("expected initial binding to tunnel")
|
||||||
|
}
|
||||||
|
if result.Int64 != tunnelID {
|
||||||
|
t.Fatalf("expected tunnel ID %d, got %d", tunnelID, result.Int64)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Update to unbind
|
||||||
|
t.Run("unbind speed limit from tunnel via UpdateSpeedLimit", func(t *testing.T) {
|
||||||
|
err := r.UpdateSpeedLimit(speedLimitID, "unbind-test-limit", 300, nil, "", 1, time.Now().UnixMilli())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("UpdateSpeedLimit failed: %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Verify unbinding
|
||||||
|
t.Run("verify unbinding after update", func(t *testing.T) {
|
||||||
|
result := r.GetSpeedLimitTunnelID(speedLimitID)
|
||||||
|
if result.Valid {
|
||||||
|
t.Fatalf("expected GetSpeedLimitTunnelID to return invalid/null after unbind, got valid with value %d", result.Int64)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSpeedLimitGetSpeed tests the GetSpeedLimitSpeed function
|
||||||
|
func TestSpeedLimitGetSpeed(t *testing.T) {
|
||||||
|
dbPath := filepath.Join(t.TempDir(), "speed-limit-getspeed.db")
|
||||||
|
r, err := repo.Open(dbPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("open sqlite: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = r.Close() })
|
||||||
|
|
||||||
|
// Create speed limit
|
||||||
|
speedLimitID, err := r.CreateSpeedLimit("get-speed-test", 500, nil, "", 1, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("create speed limit: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test GetSpeedLimitSpeed
|
||||||
|
t.Run("GetSpeedLimitSpeed returns correct speed", func(t *testing.T) {
|
||||||
|
speed, err := r.GetSpeedLimitSpeed(speedLimitID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetSpeedLimitSpeed failed: %v", err)
|
||||||
|
}
|
||||||
|
if speed != 500 {
|
||||||
|
t.Fatalf("expected speed 500, got %d", speed)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("GetSpeedLimitSpeed returns error for non-existent id", func(t *testing.T) {
|
||||||
|
_, err := r.GetSpeedLimitSpeed(99999)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for non-existent speed limit ID")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper functions
|
||||||
|
|
||||||
|
func mustCreateSpeedLimitTunnel(t *testing.T, r *repo.Repository, name string) int64 {
|
||||||
|
t.Helper()
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
if err := r.DB().Exec(`
|
||||||
|
INSERT INTO tunnel(name, traffic_ratio, type, protocol, flow, created_time, updated_time, status, in_ip, inx)
|
||||||
|
VALUES(?, 1.0, 1, 'tls', 99999, ?, ?, 1, NULL, 0)
|
||||||
|
`, name, now, now).Error; err != nil {
|
||||||
|
t.Fatalf("create tunnel failed: %v", err)
|
||||||
|
}
|
||||||
|
return mustLastInsertID(t, r, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func mustCreateSpeedLimitRepo(t *testing.T, r *repo.Repository, name string, tunnelID int64) int64 {
|
||||||
|
t.Helper()
|
||||||
|
now := time.Now().UnixMilli()
|
||||||
|
var tid *int64
|
||||||
|
if tunnelID > 0 {
|
||||||
|
tid = &tunnelID
|
||||||
|
}
|
||||||
|
id, err := r.CreateSpeedLimit(name, 100, tid, "", now, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("create speed limit failed: %v", err)
|
||||||
|
}
|
||||||
|
return id
|
||||||
|
}
|
||||||
+7
-5
@@ -1,6 +1,6 @@
|
|||||||
# GO-GOST SERVICE KNOWLEDGE BASE
|
# GO-GOST SERVICE KNOWLEDGE BASE
|
||||||
|
|
||||||
**Generated:** Sun Feb 15 2026
|
**Generated:** Thu Feb 26 2026
|
||||||
|
|
||||||
## OVERVIEW
|
## OVERVIEW
|
||||||
Forwarding agent built on GOST v3 with a local fork of `github.com/go-gost/x` under `x/`.
|
Forwarding agent built on GOST v3 with a local fork of `github.com/go-gost/x` under `x/`.
|
||||||
@@ -19,16 +19,18 @@ go-gost/
|
|||||||
## WHERE TO LOOK
|
## WHERE TO LOOK
|
||||||
| Task | Location | Notes |
|
| Task | Location | Notes |
|
||||||
|------|----------|-------|
|
|------|----------|-------|
|
||||||
| Panel integration config | `go-gost/config.go` | Expects `config.json` in cwd by default |
|
| **Panel integration config** | `go-gost/config.go` | Expects `config.json` in cwd by default |
|
||||||
| Service lifecycle/reload | `go-gost/program.go` | Parses config; handles SIGHUP reload |
|
| **Service lifecycle/reload** | `go-gost/program.go` | Parses config; handles SIGHUP reload |
|
||||||
| WebSocket reporting | `go-gost/main.go` | Starts reporter + sets HTTP report URL |
|
| **WebSocket reporting** | `go-gost/main.go` | Starts reporter + sets HTTP report URL |
|
||||||
| Protocol behaviors | `go-gost/x/` | Handlers/listeners/dialers live here |
|
| **Protocol behaviors** | `go-gost/x/` | Handlers/listeners/dialers live here |
|
||||||
|
| **Build** | `go-gost/Makefile` | Cross-compile targets for amd64/arm64 |
|
||||||
|
|
||||||
## CONVENTIONS
|
## CONVENTIONS
|
||||||
- Two configs exist: panel integration uses `config.json`; forwarding services use GOST config (defaults to `gost.{json,yaml}` via viper search paths).
|
- Two configs exist: panel integration uses `config.json`; forwarding services use GOST config (defaults to `gost.{json,yaml}` via viper search paths).
|
||||||
- `go-gost/x/` is the primary extension surface; avoid editing vendored deps.
|
- `go-gost/x/` is the primary extension surface; avoid editing vendored deps.
|
||||||
- Agent communicates with panel via WebSocket (real-time commands) + HTTP (batch traffic reports).
|
- Agent communicates with panel via WebSocket (real-time commands) + HTTP (batch traffic reports).
|
||||||
- All panel communication uses AES encryption with node `secret` as PSK.
|
- All panel communication uses AES encryption with node `secret` as PSK.
|
||||||
|
- CI builds with `CGO_ENABLED=0` for static binaries, then compresses with UPX.
|
||||||
|
|
||||||
## ANTI-PATTERNS
|
## ANTI-PATTERNS
|
||||||
- **DO NOT EDIT** generated protobuf in `x/internal/util/grpc/proto/`.
|
- **DO NOT EDIT** generated protobuf in `x/internal/util/grpc/proto/`.
|
||||||
|
|||||||
+11
-10
@@ -6,27 +6,28 @@ Local fork of `github.com/go-gost/x` used by `go-gost/` via `replace github.com/
|
|||||||
## STRUCTURE
|
## STRUCTURE
|
||||||
```
|
```
|
||||||
go-gost/x/
|
go-gost/x/
|
||||||
├── api/ # Gin management API + embedded swagger docs
|
├── api/ # Gin management API + embedded swagger docs (22 files)
|
||||||
├── config/ # Config model + parsing/load/reload
|
├── config/ # Config model + parsing/load/reload
|
||||||
├── connector/ # Outbound connect implementations
|
├── connector/ # Outbound connect implementations
|
||||||
├── dialer/ # Outbound dialers (tcp/tls/ws/quic/...)
|
├── dialer/ # Outbound dialers (tcp/tls/ws/quic/...)
|
||||||
├── handler/ # Protocol handlers (socks/http/tunnel/relay/...)
|
├── handler/ # Protocol handlers (socks/http/tunnel/relay/...)
|
||||||
├── listener/ # Inbound listeners (tcp/udp/tun/tap/redirect/...)
|
├── listener/ # Inbound listeners (tcp/udp/tun/tap/redirect/...)
|
||||||
├── limiter/ # Traffic/rate/conn limiters
|
├── limiter/ # Traffic/rate/conn limiters
|
||||||
├── registry/ # Registries for services/handlers/listeners/etc
|
├── registry/ # Registries for services/handlers/listeners/etc (20 files)
|
||||||
├── service/ # Service wrappers + reporting hooks
|
├── service/ # Service wrappers + reporting hooks
|
||||||
├── socket/ # WebSocket reporter / panel integration
|
├── socket/ # WebSocket reporter / panel integration (6 files)
|
||||||
└── internal/ # Shared internals (grpc proto, net utils, sniffing, tls, ...)
|
└── internal/ # Shared internals (grpc proto, net utils, sniffing, tls, ...)
|
||||||
```
|
```
|
||||||
|
|
||||||
## WHERE TO LOOK
|
## WHERE TO LOOK
|
||||||
| Task | Location | Notes |
|
| Task | Location | Notes |
|
||||||
|------|----------|-------|
|
|------|----------|-------|
|
||||||
| Management API routes/auth | `go-gost/x/api/api.go` | `/docs`, `/config/*`; BasicAuth + interceptor |
|
| **Management API routes/auth** | `go-gost/x/api/api.go` | `/docs`, `/config/*`; BasicAuth + interceptor |
|
||||||
| Service config parsing | `go-gost/x/config/parsing/` | Converts config to running services |
|
| **Service config parsing** | `go-gost/x/config/parsing/` | Converts config to running services |
|
||||||
| Add a handler | `go-gost/x/handler/` | Per-protocol subdirs |
|
| **Add a handler** | `go-gost/x/handler/` | Per-protocol subdirs |
|
||||||
| Add a listener/dialer | `go-gost/x/listener/`, `go-gost/x/dialer/` | Transport variants |
|
| **Add a listener/dialer** | `go-gost/x/listener/`, `go-gost/x/dialer/` | Transport variants |
|
||||||
| Panel reporting | `go-gost/x/socket/` | WebSocket + HTTP report URL hooks |
|
| **Panel reporting** | `go-gost/x/socket/` | WebSocket + HTTP report URL hooks |
|
||||||
|
| **Register new component** | `go-gost/x/registry/` | `Register{Type}(name, creator)` |
|
||||||
|
|
||||||
## CONVENTIONS
|
## CONVENTIONS
|
||||||
- `go-gost/x/` is a standalone Go module (`go-gost/x/go.mod`); run go tooling from this dir when debugging module resolution.
|
- `go-gost/x/` is a standalone Go module (`go-gost/x/go.mod`); run go tooling from this dir when debugging module resolution.
|
||||||
@@ -41,4 +42,4 @@ go-gost/x/
|
|||||||
```bash
|
```bash
|
||||||
cd go-gost/x
|
cd go-gost/x
|
||||||
go test ./...
|
go test ./...
|
||||||
```
|
```
|
||||||
+11
-12
@@ -1,9 +1,9 @@
|
|||||||
# VITE FRONTEND KNOWLEDGE BASE
|
# VITE FRONTEND KNOWLEDGE BASE
|
||||||
|
|
||||||
**Generated:** Thu Feb 19 2026
|
**Generated:** Thu Feb 26 2026
|
||||||
**Commit:** 137c34e
|
**Commit:** 21008cc
|
||||||
**Branch:** main
|
**Branch:** main
|
||||||
**Tag:** 2.1.4-rc2
|
**Tag:** 2.1.5-rc15
|
||||||
|
|
||||||
## OVERVIEW
|
## OVERVIEW
|
||||||
Web management console for FLVX.
|
Web management console for FLVX.
|
||||||
@@ -15,8 +15,8 @@ vite-frontend/
|
|||||||
├── src/
|
├── src/
|
||||||
│ ├── api/ # Axios wrapper + typed endpoint helpers
|
│ ├── api/ # Axios wrapper + typed endpoint helpers
|
||||||
│ ├── components/ui/ # shadcn/radix primitive components
|
│ ├── components/ui/ # shadcn/radix primitive components
|
||||||
│ ├── shadcn-bridge/heroui/ # HeroUI-compatible facade used by pages/layouts
|
│ ├── shadcn-bridge/heroui/ # HeroUI-compatible facade (23 components)
|
||||||
│ ├── pages/ # Route views + page modules (forward/node/tunnel split helpers)
|
│ ├── pages/ # Route views + page modules (forward/node/tunnel)
|
||||||
│ ├── hooks/ # H5/WebView/mobile hooks
|
│ ├── hooks/ # H5/WebView/mobile hooks
|
||||||
│ ├── styles/
|
│ ├── styles/
|
||||||
│ │ ├── globals.css # Base styles + imports tailwind-theme.pcss
|
│ │ ├── globals.css # Base styles + imports tailwind-theme.pcss
|
||||||
@@ -25,8 +25,8 @@ vite-frontend/
|
|||||||
│ ├── main.tsx # ReactDOM + BrowserRouter + Provider
|
│ ├── main.tsx # ReactDOM + BrowserRouter + Provider
|
||||||
│ └── provider.tsx # Toast/theme/provider composition
|
│ └── provider.tsx # Toast/theme/provider composition
|
||||||
├── components.json # shadcn/ui config
|
├── components.json # shadcn/ui config
|
||||||
├── tailwind.config.js # Compatibility config still used by migration scaffolding
|
├── tailwind.config.js # Compatibility config for migration scaffolding
|
||||||
├── vite.config.ts # base '/', host 0.0.0.0:3000; build minify/treeshake disabled
|
├── vite.config.ts # base '/', host 0.0.0.0:3000; minify/treeshake disabled
|
||||||
└── package.json
|
└── package.json
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -47,8 +47,8 @@ vite-frontend/
|
|||||||
- **API Envelope**: Responses follow `{code, msg, data, ts}`.
|
- **API Envelope**: Responses follow `{code, msg, data, ts}`.
|
||||||
- **UI Imports**: Use `src/shadcn-bridge/heroui/*` in app pages/layouts for compatibility.
|
- **UI Imports**: Use `src/shadcn-bridge/heroui/*` in app pages/layouts for compatibility.
|
||||||
- **Semantic Colors**: Keep `globals.css -> tailwind-theme.pcss` import intact or semantic classes break.
|
- **Semantic Colors**: Keep `globals.css -> tailwind-theme.pcss` import intact or semantic classes break.
|
||||||
- **Build profile**: `minify: false`, `treeshake: false` for easier debugging.
|
- **Build profile**: `minify: false`, `treeshake: false` for debugging.
|
||||||
- **Layout mode**: H5/mobile mode still controlled by existing route/query and hook logic.
|
- **Layout mode**: H5/mobile mode controlled by existing route/query and hook logic.
|
||||||
|
|
||||||
## ANTI-PATTERNS
|
## ANTI-PATTERNS
|
||||||
- **DO NOT ADD** `Bearer` to auth header in frontend requests.
|
- **DO NOT ADD** `Bearer` to auth header in frontend requests.
|
||||||
@@ -57,10 +57,9 @@ vite-frontend/
|
|||||||
- **DO NOT ADD** frontend tests; no Vitest/Jest setup exists.
|
- **DO NOT ADD** frontend tests; no Vitest/Jest setup exists.
|
||||||
|
|
||||||
## NOTES
|
## NOTES
|
||||||
- PR `#144` (shadcn migration) and PR `#142` (user-group binding) are merged in `main`.
|
|
||||||
- Release tag `2.1.4-rc2` points to commit `137c34e`.
|
|
||||||
- Button border/color parity depends on both bridge mapping and semantic Tailwind token export.
|
|
||||||
- Uses `rolldown-vite` (experimental Rust bundler) instead of standard Vite.
|
- Uses `rolldown-vite` (experimental Rust bundler) instead of standard Vite.
|
||||||
|
- Build outputs are non-minified (debugging mode).
|
||||||
|
- No test infrastructure exists (Vitest/Jest not configured).
|
||||||
|
|
||||||
## COMMANDS
|
## COMMANDS
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 114 KiB |
@@ -2,7 +2,12 @@
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
<link rel="icon" type="image/png" href="/pwa-192x192.png" />
|
||||||
|
<meta name="theme-color" content="#2563eb" />
|
||||||
|
<meta name="apple-mobile-web-app-capable" content="yes" />
|
||||||
|
<meta name="apple-mobile-web-app-status-bar-style" content="default" />
|
||||||
|
<meta name="apple-mobile-web-app-title" content="FLVX" />
|
||||||
|
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
|
||||||
|
|
||||||
<title>FLVX</title>
|
<title>FLVX</title>
|
||||||
<script>
|
<script>
|
||||||
|
|||||||
@@ -78,6 +78,7 @@
|
|||||||
"prettier": "3.5.3",
|
"prettier": "3.5.3",
|
||||||
"typescript": "5.6.3",
|
"typescript": "5.6.3",
|
||||||
"vite": "npm:rolldown-vite@^7.3.1",
|
"vite": "npm:rolldown-vite@^7.3.1",
|
||||||
|
"vite-plugin-pwa": "^1.1.0",
|
||||||
"vite-tsconfig-paths": "^6.0.5"
|
"vite-tsconfig-paths": "^6.0.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 60 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 68 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 421 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 421 KiB |
@@ -18,7 +18,7 @@ import AdminLayout from "@/layouts/admin";
|
|||||||
import H5Layout from "@/layouts/h5";
|
import H5Layout from "@/layouts/h5";
|
||||||
import H5SimpleLayout from "@/layouts/h5-simple";
|
import H5SimpleLayout from "@/layouts/h5-simple";
|
||||||
import { isLoggedIn } from "@/utils/auth";
|
import { isLoggedIn } from "@/utils/auth";
|
||||||
import { siteConfig } from "@/config/site";
|
import { siteConfig, updateSiteConfig } from "@/config/site";
|
||||||
import { useH5Mode } from "@/hooks/useH5Mode";
|
import { useH5Mode } from "@/hooks/useH5Mode";
|
||||||
|
|
||||||
// 简化的路由保护组件 - 使用 React Router 导航避免循环
|
// 简化的路由保护组件 - 使用 React Router 导航避免循环
|
||||||
@@ -90,23 +90,17 @@ function App() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
document.title = siteConfig.name;
|
document.title = siteConfig.name;
|
||||||
|
|
||||||
// 异步检查是否有配置更新
|
void updateSiteConfig();
|
||||||
const checkTitleUpdate = async () => {
|
|
||||||
try {
|
|
||||||
// 引入必要的函数
|
|
||||||
const { getCachedConfig } = await import("@/config/site");
|
|
||||||
const cachedAppName = await getCachedConfig("app_name");
|
|
||||||
|
|
||||||
if (cachedAppName && cachedAppName !== document.title) {
|
const handleConfigUpdate = () => {
|
||||||
document.title = cachedAppName;
|
void updateSiteConfig();
|
||||||
}
|
|
||||||
} catch {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 延迟检查,避免阻塞初始渲染
|
window.addEventListener("configUpdated", handleConfigUpdate);
|
||||||
const timer = setTimeout(checkTitleUpdate, 100);
|
|
||||||
|
|
||||||
return () => clearTimeout(timer);
|
return () => {
|
||||||
|
window.removeEventListener("configUpdated", handleConfigUpdate);
|
||||||
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ export interface ForwardApiItem {
|
|||||||
outFlow?: number;
|
outFlow?: number;
|
||||||
userId?: number;
|
userId?: number;
|
||||||
tunnelId?: number;
|
tunnelId?: number;
|
||||||
|
speedId?: number | null;
|
||||||
inx?: number;
|
inx?: number;
|
||||||
[key: string]: unknown;
|
[key: string]: unknown;
|
||||||
}
|
}
|
||||||
@@ -96,10 +97,10 @@ export interface StatisticsFlowApiItem {
|
|||||||
export interface SpeedLimitApiItem {
|
export interface SpeedLimitApiItem {
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
tunnelId: number;
|
tunnelId?: number | null;
|
||||||
speed: number;
|
speed: number;
|
||||||
status: number;
|
status: number;
|
||||||
tunnelName: string;
|
tunnelName?: string;
|
||||||
createdTime: string;
|
createdTime: string;
|
||||||
updatedTime: string;
|
updatedTime: string;
|
||||||
uploadSpeed?: number;
|
uploadSpeed?: number;
|
||||||
@@ -284,6 +285,7 @@ export interface ForwardMutationPayload {
|
|||||||
inPort?: number | null;
|
inPort?: number | null;
|
||||||
remoteAddr?: string;
|
remoteAddr?: string;
|
||||||
strategy?: string;
|
strategy?: string;
|
||||||
|
speedId?: number | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SpeedLimitMutationPayload {
|
export interface SpeedLimitMutationPayload {
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
|
import { siteConfig, getCachedConfigs } from "@/config/site";
|
||||||
|
import { Logo } from "@/components/icons";
|
||||||
|
|
||||||
|
type BrandLogoProps = {
|
||||||
|
size?: number;
|
||||||
|
className?: string;
|
||||||
|
alt?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const BrandLogo = ({
|
||||||
|
size = 24,
|
||||||
|
className,
|
||||||
|
alt = "brand logo",
|
||||||
|
}: BrandLogoProps) => {
|
||||||
|
const [logoUrl, setLogoUrl] = useState<string>(siteConfig.app_logo || "");
|
||||||
|
const [imageFailed, setImageFailed] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let mounted = true;
|
||||||
|
|
||||||
|
const syncLogo = async () => {
|
||||||
|
try {
|
||||||
|
const configMap = await getCachedConfigs();
|
||||||
|
const nextLogo = (
|
||||||
|
configMap.app_logo ||
|
||||||
|
siteConfig.app_logo ||
|
||||||
|
""
|
||||||
|
).trim();
|
||||||
|
|
||||||
|
if (!mounted) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
siteConfig.app_logo = nextLogo;
|
||||||
|
setLogoUrl(nextLogo);
|
||||||
|
setImageFailed(false);
|
||||||
|
} catch {
|
||||||
|
if (!mounted) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setLogoUrl((siteConfig.app_logo || "").trim());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void syncLogo();
|
||||||
|
|
||||||
|
const handleConfigUpdate = () => {
|
||||||
|
const nextLogo = (siteConfig.app_logo || "").trim();
|
||||||
|
|
||||||
|
setLogoUrl(nextLogo);
|
||||||
|
setImageFailed(false);
|
||||||
|
void syncLogo();
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener("configUpdated", handleConfigUpdate);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
mounted = false;
|
||||||
|
window.removeEventListener("configUpdated", handleConfigUpdate);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
if (!logoUrl || imageFailed) {
|
||||||
|
return <Logo className={className} size={size} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<img
|
||||||
|
alt={alt}
|
||||||
|
className={className}
|
||||||
|
height={size}
|
||||||
|
src={logoUrl}
|
||||||
|
width={size}
|
||||||
|
onError={() => setImageFailed(true)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -7,7 +7,7 @@ import {
|
|||||||
NavbarBrand,
|
NavbarBrand,
|
||||||
NavbarContent,
|
NavbarContent,
|
||||||
} from "@/shadcn-bridge/heroui/navbar";
|
} from "@/shadcn-bridge/heroui/navbar";
|
||||||
import { Logo } from "@/components/icons";
|
import { BrandLogo } from "@/components/brand-logo";
|
||||||
import { siteConfig, getCachedConfig } from "@/config/site";
|
import { siteConfig, getCachedConfig } from "@/config/site";
|
||||||
import { useWebViewMode } from "@/hooks/useWebViewMode";
|
import { useWebViewMode } from "@/hooks/useWebViewMode";
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ export const Navbar = () => {
|
|||||||
color="foreground"
|
color="foreground"
|
||||||
href="/"
|
href="/"
|
||||||
>
|
>
|
||||||
<Logo size={24} />
|
<BrandLogo size={24} />
|
||||||
<p className="font-bold text-inherit truncate">{appName}</p>
|
<p className="font-bold text-inherit truncate">{appName}</p>
|
||||||
</Link>
|
</Link>
|
||||||
</NavbarBrand>
|
</NavbarBrand>
|
||||||
|
|||||||
@@ -55,7 +55,6 @@ export function SearchBar({
|
|||||||
transition={{ duration: 0.18, ease: [0.25, 0.46, 0.45, 0.94] }}
|
transition={{ duration: 0.18, ease: [0.25, 0.46, 0.45, 0.94] }}
|
||||||
>
|
>
|
||||||
<Input
|
<Input
|
||||||
autoFocus
|
|
||||||
classNames={{
|
classNames={{
|
||||||
base: "bg-default-100",
|
base: "bg-default-100",
|
||||||
input:
|
input:
|
||||||
|
|||||||
@@ -38,13 +38,19 @@ function Alert({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function AlertTitle({ className, ...props }: React.ComponentProps<"h5">) {
|
function AlertTitle({
|
||||||
|
className,
|
||||||
|
children,
|
||||||
|
...props
|
||||||
|
}: React.ComponentProps<"h5">) {
|
||||||
return (
|
return (
|
||||||
<h5
|
<h5
|
||||||
className={cn("mb-1 font-medium leading-none tracking-tight", className)}
|
className={cn("mb-1 font-medium leading-none tracking-tight", className)}
|
||||||
data-slot="alert-title"
|
data-slot="alert-title"
|
||||||
{...props}
|
{...props}
|
||||||
/>
|
>
|
||||||
|
{children}
|
||||||
|
</h5>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,11 @@ function CardHeader({ className, ...props }: React.ComponentProps<"div">) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function CardTitle({ className, ...props }: React.ComponentProps<"h3">) {
|
function CardTitle({
|
||||||
|
className,
|
||||||
|
children,
|
||||||
|
...props
|
||||||
|
}: React.ComponentProps<"h3">) {
|
||||||
return (
|
return (
|
||||||
<h3
|
<h3
|
||||||
className={cn(
|
className={cn(
|
||||||
@@ -34,7 +38,9 @@ function CardTitle({ className, ...props }: React.ComponentProps<"h3">) {
|
|||||||
)}
|
)}
|
||||||
data-slot="card-title"
|
data-slot="card-title"
|
||||||
{...props}
|
{...props}
|
||||||
/>
|
>
|
||||||
|
{children}
|
||||||
|
</h3>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,110 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
|
import { siteConfig } from "@/config/site";
|
||||||
|
import {
|
||||||
|
UPDATE_CHANNEL_CHANGED_EVENT,
|
||||||
|
type UpdateReleaseChannel,
|
||||||
|
getLatestVersionByChannel,
|
||||||
|
getUpdateReleaseChannel,
|
||||||
|
hasVersionUpdate,
|
||||||
|
} from "@/utils/version-update";
|
||||||
|
|
||||||
|
const FALLBACK_GITHUB_REPO = "https://github.com/Sagit-chu/flux-panel";
|
||||||
|
|
||||||
|
interface VersionFooterProps {
|
||||||
|
version: string;
|
||||||
|
containerClassName?: string;
|
||||||
|
versionClassName?: string;
|
||||||
|
poweredClassName?: string;
|
||||||
|
updateBadgeClassName?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function VersionFooter({
|
||||||
|
version,
|
||||||
|
containerClassName,
|
||||||
|
versionClassName,
|
||||||
|
poweredClassName,
|
||||||
|
updateBadgeClassName,
|
||||||
|
}: VersionFooterProps) {
|
||||||
|
const [channel, setChannel] = useState<UpdateReleaseChannel>(
|
||||||
|
getUpdateReleaseChannel(),
|
||||||
|
);
|
||||||
|
const [updateAvailable, setUpdateAvailable] = useState(false);
|
||||||
|
const [latestUpdateVersion, setLatestUpdateVersion] = useState<string | null>(
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleChannelChange = () => {
|
||||||
|
setChannel(getUpdateReleaseChannel());
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener(UPDATE_CHANNEL_CHANGED_EVENT, handleChannelChange);
|
||||||
|
window.addEventListener("storage", handleChannelChange);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener(
|
||||||
|
UPDATE_CHANNEL_CHANGED_EVENT,
|
||||||
|
handleChannelChange,
|
||||||
|
);
|
||||||
|
window.removeEventListener("storage", handleChannelChange);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let active = true;
|
||||||
|
|
||||||
|
const checkUpdate = async () => {
|
||||||
|
const latestVersion = await getLatestVersionByChannel(
|
||||||
|
channel,
|
||||||
|
siteConfig.github_repo || FALLBACK_GITHUB_REPO,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!active) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!latestVersion) {
|
||||||
|
setUpdateAvailable(false);
|
||||||
|
setLatestUpdateVersion(null);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasUpdate = hasVersionUpdate(version, latestVersion);
|
||||||
|
|
||||||
|
setUpdateAvailable(hasUpdate);
|
||||||
|
setLatestUpdateVersion(hasUpdate ? latestVersion : null);
|
||||||
|
};
|
||||||
|
|
||||||
|
void checkUpdate();
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
active = false;
|
||||||
|
};
|
||||||
|
}, [channel, version]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={containerClassName}>
|
||||||
|
<p className={versionClassName}>
|
||||||
|
v{version}
|
||||||
|
{updateAvailable && latestUpdateVersion && (
|
||||||
|
<span className={updateBadgeClassName} role="status">
|
||||||
|
{latestUpdateVersion}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</p>
|
||||||
|
<p className={poweredClassName}>
|
||||||
|
Powered by{" "}
|
||||||
|
<a
|
||||||
|
className="text-gray-500 dark:text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 transition-colors"
|
||||||
|
href={siteConfig.github_repo}
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
target="_blank"
|
||||||
|
>
|
||||||
|
FLVX
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -16,10 +16,15 @@ const getInitialConfig = () => {
|
|||||||
version: VERSION,
|
version: VERSION,
|
||||||
app_version: APP_VERSION,
|
app_version: APP_VERSION,
|
||||||
github_repo: GITHUB_REPO,
|
github_repo: GITHUB_REPO,
|
||||||
|
app_logo: "",
|
||||||
|
app_favicon: "",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const cachedAppName = localStorage.getItem(CACHE_PREFIX + "app_name");
|
const cachedAppName = localStorage.getItem(CACHE_PREFIX + "app_name");
|
||||||
|
const cachedAppLogo = localStorage.getItem(CACHE_PREFIX + "app_logo") || "";
|
||||||
|
const cachedAppFavicon =
|
||||||
|
localStorage.getItem(CACHE_PREFIX + "app_favicon") || "";
|
||||||
|
|
||||||
if (cachedAppName) {
|
if (cachedAppName) {
|
||||||
return {
|
return {
|
||||||
@@ -27,6 +32,8 @@ const getInitialConfig = () => {
|
|||||||
version: VERSION,
|
version: VERSION,
|
||||||
app_version: APP_VERSION,
|
app_version: APP_VERSION,
|
||||||
github_repo: GITHUB_REPO,
|
github_repo: GITHUB_REPO,
|
||||||
|
app_logo: cachedAppLogo,
|
||||||
|
app_favicon: cachedAppFavicon,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,6 +41,9 @@ const getInitialConfig = () => {
|
|||||||
name: "FLVX",
|
name: "FLVX",
|
||||||
version: VERSION,
|
version: VERSION,
|
||||||
app_version: APP_VERSION,
|
app_version: APP_VERSION,
|
||||||
|
github_repo: GITHUB_REPO,
|
||||||
|
app_logo: cachedAppLogo,
|
||||||
|
app_favicon: cachedAppFavicon,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -99,7 +109,7 @@ export const getCachedConfig = async (key: string): Promise<string | null> => {
|
|||||||
// 获取所有配置(优先从缓存)
|
// 获取所有配置(优先从缓存)
|
||||||
export const getCachedConfigs = async (): Promise<Record<string, string>> => {
|
export const getCachedConfigs = async (): Promise<Record<string, string>> => {
|
||||||
// 尝试从缓存获取所有配置
|
// 尝试从缓存获取所有配置
|
||||||
const configKeys = ["app_name"];
|
const configKeys = ["app_name", "app_logo", "app_favicon"];
|
||||||
const cachedConfigs: Record<string, string> = {};
|
const cachedConfigs: Record<string, string> = {};
|
||||||
let hasCachedData = false;
|
let hasCachedData = false;
|
||||||
|
|
||||||
@@ -136,15 +146,48 @@ export const getCachedConfigs = async (): Promise<Record<string, string>> => {
|
|||||||
return {};
|
return {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const updateDocumentFavicon = (faviconUrl: string) => {
|
||||||
|
if (typeof document === "undefined") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalized = faviconUrl.trim() || "/favicon.ico";
|
||||||
|
const iconLinks = Array.from(
|
||||||
|
document.head.querySelectorAll<HTMLLinkElement>(
|
||||||
|
'link[rel="icon"], link[rel="shortcut icon"]',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (iconLinks.length === 0) {
|
||||||
|
const link = document.createElement("link");
|
||||||
|
|
||||||
|
link.rel = "icon";
|
||||||
|
link.href = normalized;
|
||||||
|
document.head.appendChild(link);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
iconLinks.forEach((link) => {
|
||||||
|
link.href = normalized;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// 动态更新网站配置
|
// 动态更新网站配置
|
||||||
export const updateSiteConfig = async () => {
|
export const updateSiteConfig = async () => {
|
||||||
const appName = await getCachedConfig("app_name");
|
const configMap = await getCachedConfigs();
|
||||||
|
const appName = (configMap.app_name || "").trim();
|
||||||
|
const appLogo = (configMap.app_logo || "").trim();
|
||||||
|
const appFavicon = (configMap.app_favicon || "").trim();
|
||||||
|
|
||||||
if (appName && appName !== siteConfig.name) {
|
if (appName && appName !== siteConfig.name) {
|
||||||
siteConfig.name = appName;
|
siteConfig.name = appName;
|
||||||
// 更新页面标题
|
|
||||||
document.title = appName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
siteConfig.app_logo = appLogo;
|
||||||
|
siteConfig.app_favicon = appFavicon;
|
||||||
|
document.title = siteConfig.name;
|
||||||
|
updateDocumentFavicon(siteConfig.app_favicon);
|
||||||
};
|
};
|
||||||
|
|
||||||
// 清除配置缓存的工具函数
|
// 清除配置缓存的工具函数
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ import {
|
|||||||
useDisclosure,
|
useDisclosure,
|
||||||
} from "@/shadcn-bridge/heroui/modal";
|
} from "@/shadcn-bridge/heroui/modal";
|
||||||
import { Input } from "@/shadcn-bridge/heroui/input";
|
import { Input } from "@/shadcn-bridge/heroui/input";
|
||||||
import { Logo } from "@/components/icons";
|
import { BrandLogo } from "@/components/brand-logo";
|
||||||
|
import { VersionFooter } from "@/components/version-footer";
|
||||||
import { updatePassword } from "@/api";
|
import { updatePassword } from "@/api";
|
||||||
import { safeLogout } from "@/utils/logout";
|
import { safeLogout } from "@/utils/logout";
|
||||||
import { siteConfig } from "@/config/site";
|
import { siteConfig } from "@/config/site";
|
||||||
@@ -50,7 +51,9 @@ export default function AdminLayout({
|
|||||||
const { isOpen, onOpen, onOpenChange } = useDisclosure();
|
const { isOpen, onOpen, onOpenChange } = useDisclosure();
|
||||||
|
|
||||||
const [mobileMenuVisible, setMobileMenuVisible] = useState(false);
|
const [mobileMenuVisible, setMobileMenuVisible] = useState(false);
|
||||||
const [isCollapsed, setIsCollapsed] = useState(() => localStorage.getItem("sidebar_collapsed") === "true");
|
const [isCollapsed, setIsCollapsed] = useState(
|
||||||
|
() => localStorage.getItem("sidebar_collapsed") === "true",
|
||||||
|
);
|
||||||
const [username, setUsername] = useState("");
|
const [username, setUsername] = useState("");
|
||||||
const [isAdmin, setIsAdmin] = useState(false);
|
const [isAdmin, setIsAdmin] = useState(false);
|
||||||
const [passwordLoading, setPasswordLoading] = useState(false);
|
const [passwordLoading, setPasswordLoading] = useState(false);
|
||||||
@@ -208,6 +211,7 @@ export default function AdminLayout({
|
|||||||
// 切换折叠状态
|
// 切换折叠状态
|
||||||
const toggleCollapse = () => {
|
const toggleCollapse = () => {
|
||||||
const newCollapsed = !isCollapsed;
|
const newCollapsed = !isCollapsed;
|
||||||
|
|
||||||
setIsCollapsed(newCollapsed);
|
setIsCollapsed(newCollapsed);
|
||||||
localStorage.setItem("sidebar_collapsed", newCollapsed.toString());
|
localStorage.setItem("sidebar_collapsed", newCollapsed.toString());
|
||||||
};
|
};
|
||||||
@@ -326,13 +330,14 @@ export default function AdminLayout({
|
|||||||
{/* Logo 区域 */}
|
{/* Logo 区域 */}
|
||||||
<div className="px-5 h-14 flex items-center overflow-hidden whitespace-nowrap box-border">
|
<div className="px-5 h-14 flex items-center overflow-hidden whitespace-nowrap box-border">
|
||||||
<div className="flex-shrink-0 flex items-center justify-center w-10">
|
<div className="flex-shrink-0 flex items-center justify-center w-10">
|
||||||
<Logo size={28} />
|
<BrandLogo size={28} />
|
||||||
</div>
|
</div>
|
||||||
<div className={`transition-all duration-300 overflow-hidden ${isCollapsed ? "max-w-0 opacity-0 ml-0" : "max-w-[180px] opacity-100 ml-2"}`}>
|
<div
|
||||||
|
className={`transition-all duration-300 overflow-hidden ${isCollapsed ? "max-w-0 opacity-0 ml-0" : "max-w-[180px] opacity-100 ml-2"}`}
|
||||||
|
>
|
||||||
<h1 className="text-sm font-bold text-foreground overflow-hidden whitespace-nowrap text-ellipsis">
|
<h1 className="text-sm font-bold text-foreground overflow-hidden whitespace-nowrap text-ellipsis">
|
||||||
{siteConfig.name}
|
{siteConfig.name}
|
||||||
</h1>
|
</h1>
|
||||||
<p className="text-xs text-default-500 overflow-hidden whitespace-nowrap text-ellipsis">v{siteConfig.version}</p>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -345,15 +350,16 @@ export default function AdminLayout({
|
|||||||
return (
|
return (
|
||||||
<li key={item.path}>
|
<li key={item.path}>
|
||||||
<motion.button
|
<motion.button
|
||||||
title={isCollapsed ? item.label : undefined}
|
|
||||||
className={`
|
className={`
|
||||||
w-full flex items-center p-2 rounded-lg text-left
|
w-full flex items-center p-2 rounded-lg text-left
|
||||||
relative min-h-[44px] overflow-hidden transition-colors
|
relative min-h-[44px] overflow-hidden transition-colors
|
||||||
${isActive
|
${
|
||||||
? "text-primary-600 dark:text-primary-300"
|
isActive
|
||||||
: "text-gray-700 dark:text-gray-200"
|
? "text-primary-600 dark:text-primary-300"
|
||||||
}
|
: "text-gray-700 dark:text-gray-200"
|
||||||
|
}
|
||||||
`}
|
`}
|
||||||
|
title={isCollapsed ? item.label : undefined}
|
||||||
transition={{ duration: 0.15 }}
|
transition={{ duration: 0.15 }}
|
||||||
onClick={() => handleMenuClick(item.path)}
|
onClick={() => handleMenuClick(item.path)}
|
||||||
>
|
>
|
||||||
@@ -378,7 +384,9 @@ export default function AdminLayout({
|
|||||||
<div className="flex-shrink-0 w-10 h-10 flex items-center justify-center relative z-10">
|
<div className="flex-shrink-0 w-10 h-10 flex items-center justify-center relative z-10">
|
||||||
{item.icon}
|
{item.icon}
|
||||||
</div>
|
</div>
|
||||||
<div className={`transition-all duration-300 overflow-hidden flex items-center ${isCollapsed ? "max-w-0 opacity-0 ml-0" : "max-w-[200px] opacity-100 ml-2"}`}>
|
<div
|
||||||
|
className={`transition-all duration-300 overflow-hidden flex items-center ${isCollapsed ? "max-w-0 opacity-0 ml-0" : "max-w-[200px] opacity-100 ml-2"}`}
|
||||||
|
>
|
||||||
<span className="font-medium text-sm relative z-10 whitespace-nowrap">
|
<span className="font-medium text-sm relative z-10 whitespace-nowrap">
|
||||||
{item.label}
|
{item.label}
|
||||||
</span>
|
</span>
|
||||||
@@ -392,38 +400,55 @@ export default function AdminLayout({
|
|||||||
|
|
||||||
{/* 底部版权信息和折叠按钮 */}
|
{/* 底部版权信息和折叠按钮 */}
|
||||||
<div className="px-5 py-2 pb-4 mt-auto flex-shrink-0 flex items-center justify-between overflow-hidden whitespace-nowrap box-border">
|
<div className="px-5 py-2 pb-4 mt-auto flex-shrink-0 flex items-center justify-between overflow-hidden whitespace-nowrap box-border">
|
||||||
<div className={`transition-all duration-300 overflow-hidden flex items-center ${isCollapsed ? "max-w-0 opacity-0" : "max-w-[200px] opacity-100"}`}>
|
<div
|
||||||
<p className="text-xs text-gray-400 dark:text-gray-500">
|
className={`transition-all duration-300 overflow-hidden flex items-center ${isCollapsed ? "max-w-0 opacity-0" : "max-w-[200px] opacity-100"}`}
|
||||||
Powered by{" "}
|
>
|
||||||
<a
|
<VersionFooter
|
||||||
className="text-gray-500 dark:text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 transition-colors"
|
poweredClassName="text-xs text-gray-400 dark:text-gray-500"
|
||||||
href={siteConfig.github_repo}
|
updateBadgeClassName="ml-2 inline-flex items-center rounded-full bg-rose-500/90 px-2 py-0.5 text-[10px] font-semibold tracking-wide text-white"
|
||||||
rel="noopener noreferrer"
|
version={siteConfig.version}
|
||||||
target="_blank"
|
versionClassName="text-xs text-gray-400 dark:text-gray-500"
|
||||||
>
|
/>
|
||||||
FLVX
|
|
||||||
</a>
|
|
||||||
</p>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* 桌面端折叠按钮 */}
|
{/* 桌面端折叠按钮 */}
|
||||||
{!isMobile && (
|
{!isMobile && (
|
||||||
<Button
|
<Button
|
||||||
isIconOnly
|
isIconOnly
|
||||||
variant="light"
|
|
||||||
size="sm"
|
|
||||||
className="flex-shrink-0 text-gray-400 hover:text-gray-700 dark:text-gray-500 dark:hover:text-gray-300 min-w-0 w-10 h-10 rounded-full ml-auto"
|
className="flex-shrink-0 text-gray-400 hover:text-gray-700 dark:text-gray-500 dark:hover:text-gray-300 min-w-0 w-10 h-10 rounded-full ml-auto"
|
||||||
|
size="sm"
|
||||||
|
variant="light"
|
||||||
onPress={toggleCollapse}
|
onPress={toggleCollapse}
|
||||||
>
|
>
|
||||||
{isCollapsed ? (
|
{isCollapsed ? (
|
||||||
// 向右扩展的提示
|
// 向右扩展的提示
|
||||||
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
<svg
|
||||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 5l7 7-7 7M5 5l7 7-7 7" />
|
className="w-5 h-5"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
d="M13 5l7 7-7 7M5 5l7 7-7 7"
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
/>
|
||||||
</svg>
|
</svg>
|
||||||
) : (
|
) : (
|
||||||
// 向左收起的提示
|
// 向左收起的提示
|
||||||
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
<svg
|
||||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M11 19l-7-7 7-7m8 14l-7-7 7-7" />
|
className="w-5 h-5"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
d="M11 19l-7-7 7-7m8 14l-7-7 7-7"
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
/>
|
||||||
</svg>
|
</svg>
|
||||||
)}
|
)}
|
||||||
</Button>
|
</Button>
|
||||||
@@ -532,9 +557,7 @@ export default function AdminLayout({
|
|||||||
</header>
|
</header>
|
||||||
|
|
||||||
{/* 主内容 */}
|
{/* 主内容 */}
|
||||||
<main
|
<main className="flex-1 bg-gray-100 dark:bg-black overflow-y-auto">
|
||||||
className={`flex-1 bg-gray-100 dark:bg-black overflow-hidden ${isMobile ? "" : "overflow-y-scroll"}`}
|
|
||||||
>
|
|
||||||
<AnimatePresence mode="wait">
|
<AnimatePresence mode="wait">
|
||||||
<motion.div
|
<motion.div
|
||||||
key={location.pathname}
|
key={location.pathname}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import React from "react";
|
|||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
import { Button } from "@/shadcn-bridge/heroui/button";
|
import { Button } from "@/shadcn-bridge/heroui/button";
|
||||||
import { Logo } from "@/components/icons";
|
import { BrandLogo } from "@/components/brand-logo";
|
||||||
import { siteConfig } from "@/config/site";
|
import { siteConfig } from "@/config/site";
|
||||||
import { useScrollTopOnPathChange } from "@/hooks/useScrollTopOnPathChange";
|
import { useScrollTopOnPathChange } from "@/hooks/useScrollTopOnPathChange";
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ export default function H5SimpleLayout({
|
|||||||
/>
|
/>
|
||||||
</svg>
|
</svg>
|
||||||
</Button>
|
</Button>
|
||||||
<Logo size={20} />
|
<BrandLogo size={20} />
|
||||||
<h1 className="text-sm font-bold text-foreground">
|
<h1 className="text-sm font-bold text-foreground">
|
||||||
{siteConfig.name}
|
{siteConfig.name}
|
||||||
</h1>
|
</h1>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { useState, useEffect } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
import { useNavigate, useLocation } from "react-router-dom";
|
import { useNavigate, useLocation } from "react-router-dom";
|
||||||
|
|
||||||
import { Logo } from "@/components/icons";
|
import { BrandLogo } from "@/components/brand-logo";
|
||||||
import { siteConfig } from "@/config/site";
|
import { siteConfig } from "@/config/site";
|
||||||
import { getAdminFlag } from "@/utils/session";
|
import { getAdminFlag } from "@/utils/session";
|
||||||
import { useScrollTopOnPathChange } from "@/hooks/useScrollTopOnPathChange";
|
import { useScrollTopOnPathChange } from "@/hooks/useScrollTopOnPathChange";
|
||||||
@@ -102,7 +102,7 @@ export default function H5Layout({ children }: { children: React.ReactNode }) {
|
|||||||
{/* 顶部导航栏 */}
|
{/* 顶部导航栏 */}
|
||||||
<header className="bg-white dark:bg-black shadow-sm border-b border-gray-200 dark:border-gray-600 h-14 safe-top flex-shrink-0 flex items-center justify-between px-4 relative z-10">
|
<header className="bg-white dark:bg-black shadow-sm border-b border-gray-200 dark:border-gray-600 h-14 safe-top flex-shrink-0 flex items-center justify-between px-4 relative z-10">
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<Logo size={20} />
|
<BrandLogo size={20} />
|
||||||
<h1 className="text-sm font-bold text-foreground">
|
<h1 className="text-sm font-bold text-foreground">
|
||||||
{siteConfig.name}
|
{siteConfig.name}
|
||||||
</h1>
|
</h1>
|
||||||
@@ -115,10 +115,10 @@ export default function H5Layout({ children }: { children: React.ReactNode }) {
|
|||||||
<main className="flex-1 bg-gray-100 dark:bg-black">{children}</main>
|
<main className="flex-1 bg-gray-100 dark:bg-black">{children}</main>
|
||||||
|
|
||||||
{/* 用于给固定 Tabbar 腾出空间的占位元素 */}
|
{/* 用于给固定 Tabbar 腾出空间的占位元素 */}
|
||||||
<div aria-hidden className="h-16 safe-bottom" />
|
<div aria-hidden className="h-[calc(4rem+var(--safe-area-bottom))]" />
|
||||||
|
|
||||||
{/* 底部Tabbar */}
|
{/* 底部Tabbar */}
|
||||||
<nav className="bg-white dark:bg-black border-t border-gray-200 dark:border-gray-600 h-16 safe-bottom flex-shrink-0 flex items-center justify-around px-2 fixed bottom-0 left-0 right-0 z-30">
|
<nav className="bg-white dark:bg-black border-t border-gray-200 dark:border-gray-600 h-[calc(4rem+var(--safe-area-bottom))] flex-shrink-0 flex items-center justify-around px-2 fixed bottom-0 left-0 right-0 z-30">
|
||||||
{filteredTabItems.map((item) => {
|
{filteredTabItems.map((item) => {
|
||||||
const isActive = location.pathname === item.path;
|
const isActive = location.pathname === item.path;
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ export default function H5Layout({ children }: { children: React.ReactNode }) {
|
|||||||
<button
|
<button
|
||||||
key={item.path}
|
key={item.path}
|
||||||
className={`
|
className={`
|
||||||
flex flex-col items-center justify-center flex-1 h-full
|
flex flex-col items-center justify-center flex-1 h-full pb-[var(--safe-area-bottom)]
|
||||||
transition-colors duration-200 min-h-[44px]
|
transition-colors duration-200 min-h-[44px]
|
||||||
${
|
${
|
||||||
isActive
|
isActive
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import ReactDOM from "react-dom/client";
|
import ReactDOM from "react-dom/client";
|
||||||
import { BrowserRouter } from "react-router-dom";
|
import { BrowserRouter } from "react-router-dom";
|
||||||
|
import { registerSW } from "virtual:pwa-register";
|
||||||
|
|
||||||
import App from "./App.tsx";
|
import App from "./App.tsx";
|
||||||
import { Provider } from "./provider.tsx";
|
import { Provider } from "./provider.tsx";
|
||||||
import "@/styles/globals.css";
|
import "@/styles/globals.css";
|
||||||
|
|
||||||
|
registerSW({ immediate: true });
|
||||||
|
|
||||||
ReactDOM.createRoot(document.getElementById("root")!).render(
|
ReactDOM.createRoot(document.getElementById("root")!).render(
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
<Provider>
|
<Provider>
|
||||||
|
|||||||
@@ -33,6 +33,11 @@ import {
|
|||||||
clearConfigCache,
|
clearConfigCache,
|
||||||
updateSiteConfig,
|
updateSiteConfig,
|
||||||
} from "@/config/site";
|
} from "@/config/site";
|
||||||
|
import {
|
||||||
|
type UpdateReleaseChannel,
|
||||||
|
getUpdateReleaseChannel,
|
||||||
|
setUpdateReleaseChannel,
|
||||||
|
} from "@/utils/version-update";
|
||||||
|
|
||||||
// 简单的保存图标组件
|
// 简单的保存图标组件
|
||||||
const SaveIcon = ({ className }: { className?: string }) => (
|
const SaveIcon = ({ className }: { className?: string }) => (
|
||||||
@@ -62,6 +67,13 @@ interface ConfigItem {
|
|||||||
dependsValue?: string; // 依赖的配置项值
|
dependsValue?: string; // 依赖的配置项值
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const BRAND_PREVIEW_KEYS = ["app_logo", "app_favicon"] as const;
|
||||||
|
|
||||||
|
type BrandPreviewKey = (typeof BRAND_PREVIEW_KEYS)[number];
|
||||||
|
|
||||||
|
const isBrandPreviewKey = (key: string): key is BrandPreviewKey =>
|
||||||
|
BRAND_PREVIEW_KEYS.includes(key as BrandPreviewKey);
|
||||||
|
|
||||||
// 网站配置项定义
|
// 网站配置项定义
|
||||||
const CONFIG_ITEMS: ConfigItem[] = [
|
const CONFIG_ITEMS: ConfigItem[] = [
|
||||||
{
|
{
|
||||||
@@ -86,6 +98,22 @@ const CONFIG_ITEMS: ConfigItem[] = [
|
|||||||
description: "在浏览器标签页和导航栏显示的应用名称",
|
description: "在浏览器标签页和导航栏显示的应用名称",
|
||||||
type: "input",
|
type: "input",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
key: "app_logo",
|
||||||
|
label: "网页角标 Logo",
|
||||||
|
placeholder: "请输入 Logo 图片 URL",
|
||||||
|
description:
|
||||||
|
"用于页面左上角导航角标,支持站内路径(如 /logo.png)或完整图片 URL",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "app_favicon",
|
||||||
|
label: "浏览器缩略图标",
|
||||||
|
placeholder: "请输入 Favicon 图片 URL",
|
||||||
|
description:
|
||||||
|
"用于浏览器标签页图标,支持站内路径(如 /favicon.ico)或完整图片 URL",
|
||||||
|
type: "input",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
key: "captcha_enabled",
|
key: "captcha_enabled",
|
||||||
label: "启用验证码",
|
label: "启用验证码",
|
||||||
@@ -138,6 +166,8 @@ const getInitialConfigs = (): Record<string, string> => {
|
|||||||
"cloudflare_secret_key",
|
"cloudflare_secret_key",
|
||||||
"ip",
|
"ip",
|
||||||
"panel_domain",
|
"panel_domain",
|
||||||
|
"app_logo",
|
||||||
|
"app_favicon",
|
||||||
];
|
];
|
||||||
const initialConfigs: Record<string, string> = {};
|
const initialConfigs: Record<string, string> = {};
|
||||||
|
|
||||||
@@ -182,6 +212,12 @@ export default function ConfigPage() {
|
|||||||
});
|
});
|
||||||
const [announcementLoading, setAnnouncementLoading] = useState(true);
|
const [announcementLoading, setAnnouncementLoading] = useState(true);
|
||||||
const [announcementSaving, setAnnouncementSaving] = useState(false);
|
const [announcementSaving, setAnnouncementSaving] = useState(false);
|
||||||
|
const [updateChannel, setUpdateChannel] = useState<UpdateReleaseChannel>(
|
||||||
|
getUpdateReleaseChannel(),
|
||||||
|
);
|
||||||
|
const [previewLoadFailed, setPreviewLoadFailed] = useState<
|
||||||
|
Partial<Record<BrandPreviewKey, boolean>>
|
||||||
|
>({});
|
||||||
|
|
||||||
// 权限检查
|
// 权限检查
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -267,11 +303,23 @@ export default function ConfigPage() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleUpdateChannelChange = (channel: UpdateReleaseChannel) => {
|
||||||
|
setUpdateChannel(channel);
|
||||||
|
setUpdateReleaseChannel(channel);
|
||||||
|
toast.success(
|
||||||
|
`更新通道已切换为${channel === "stable" ? "稳定版" : "开发版"}`,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const handleConfigChange = (key: string, value: string) => {
|
const handleConfigChange = (key: string, value: string) => {
|
||||||
const newConfigs = { ...configs, [key]: value };
|
const newConfigs = { ...configs, [key]: value };
|
||||||
|
|
||||||
setConfigs(newConfigs);
|
setConfigs(newConfigs);
|
||||||
|
|
||||||
|
if (isBrandPreviewKey(key)) {
|
||||||
|
setPreviewLoadFailed((prev) => ({ ...prev, [key]: false }));
|
||||||
|
}
|
||||||
|
|
||||||
const hasChangesNow =
|
const hasChangesNow =
|
||||||
Object.keys(newConfigs).some(
|
Object.keys(newConfigs).some(
|
||||||
(k) => newConfigs[k] !== originalConfigs[k],
|
(k) => newConfigs[k] !== originalConfigs[k],
|
||||||
@@ -303,8 +351,11 @@ export default function ConfigPage() {
|
|||||||
setOriginalConfigs({ ...configs });
|
setOriginalConfigs({ ...configs });
|
||||||
setHasChanges(false);
|
setHasChanges(false);
|
||||||
|
|
||||||
// 如果应用名称发生变化,立即更新网站配置
|
if (
|
||||||
if (changedKeys.includes("app_name")) {
|
changedKeys.some((key) =>
|
||||||
|
["app_name", "app_logo", "app_favicon"].includes(key),
|
||||||
|
)
|
||||||
|
) {
|
||||||
await updateSiteConfig();
|
await updateSiteConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -333,6 +384,78 @@ export default function ConfigPage() {
|
|||||||
return configs[item.dependsOn] === item.dependsValue;
|
return configs[item.dependsOn] === item.dependsValue;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const renderBrandPreview = (key: BrandPreviewKey) => {
|
||||||
|
const previewUrl = (configs[key] || "").trim();
|
||||||
|
const appNamePreview = (configs.app_name || "").trim() || "应用名称";
|
||||||
|
const failed = previewLoadFailed[key] === true;
|
||||||
|
const showImage = previewUrl.length > 0 && !failed;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="mt-3 rounded-lg border border-default-200 dark:border-default-100/30 bg-default-50/60 dark:bg-default-100/10 p-3">
|
||||||
|
<p className="text-xs text-default-500">实时预览</p>
|
||||||
|
<div className="mt-2 rounded-md border border-default-200 dark:border-default-100/30 bg-white dark:bg-black px-3 py-2">
|
||||||
|
{key === "app_logo" ? (
|
||||||
|
<div className="flex h-10 items-center gap-2">
|
||||||
|
{showImage ? (
|
||||||
|
<img
|
||||||
|
alt="logo preview"
|
||||||
|
className="h-7 w-7 rounded-sm border border-default-200 object-cover dark:border-default-100/30"
|
||||||
|
src={previewUrl}
|
||||||
|
onError={() =>
|
||||||
|
setPreviewLoadFailed((prev) => ({ ...prev, [key]: true }))
|
||||||
|
}
|
||||||
|
onLoad={() =>
|
||||||
|
setPreviewLoadFailed((prev) => ({ ...prev, [key]: false }))
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<div className="flex h-7 w-7 items-center justify-center rounded-sm bg-default-200 text-[10px] font-semibold text-default-600 dark:bg-default-700 dark:text-default-200">
|
||||||
|
LOGO
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<span className="truncate text-sm font-semibold text-foreground">
|
||||||
|
{appNamePreview}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="flex h-7 max-w-[260px] items-center gap-2 rounded border border-default-200 bg-default-100/70 px-2 dark:border-default-100/30 dark:bg-default-100/20">
|
||||||
|
{showImage ? (
|
||||||
|
<img
|
||||||
|
alt="favicon preview"
|
||||||
|
className="h-4 w-4 rounded-sm object-contain"
|
||||||
|
src={previewUrl}
|
||||||
|
onError={() =>
|
||||||
|
setPreviewLoadFailed((prev) => ({ ...prev, [key]: true }))
|
||||||
|
}
|
||||||
|
onLoad={() =>
|
||||||
|
setPreviewLoadFailed((prev) => ({ ...prev, [key]: false }))
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<div className="h-4 w-4 rounded-sm bg-default-300 dark:bg-default-600" />
|
||||||
|
)}
|
||||||
|
<span className="truncate text-xs text-default-700 dark:text-default-300">
|
||||||
|
{appNamePreview}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{previewUrl.length === 0 ? (
|
||||||
|
<p className="mt-2 text-xs text-default-500">
|
||||||
|
输入图片 URL 后会实时显示预览
|
||||||
|
</p>
|
||||||
|
) : null}
|
||||||
|
|
||||||
|
{previewUrl.length > 0 && failed ? (
|
||||||
|
<p className="mt-2 text-xs text-danger">
|
||||||
|
图片加载失败,请检查图片地址是否可访问
|
||||||
|
</p>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
// 渲染不同类型的配置项
|
// 渲染不同类型的配置项
|
||||||
const renderConfigItem = (item: ConfigItem) => {
|
const renderConfigItem = (item: ConfigItem) => {
|
||||||
const isChanged =
|
const isChanged =
|
||||||
@@ -341,19 +464,22 @@ export default function ConfigPage() {
|
|||||||
switch (item.type) {
|
switch (item.type) {
|
||||||
case "input":
|
case "input":
|
||||||
return (
|
return (
|
||||||
<Input
|
<>
|
||||||
classNames={{
|
<Input
|
||||||
input: "text-sm",
|
classNames={{
|
||||||
inputWrapper: isChanged
|
input: "text-sm",
|
||||||
? "border-warning-300 data-[hover=true]:border-warning-400"
|
inputWrapper: isChanged
|
||||||
: "",
|
? "border-warning-300 data-[hover=true]:border-warning-400"
|
||||||
}}
|
: "",
|
||||||
placeholder={item.placeholder}
|
}}
|
||||||
size="md"
|
placeholder={item.placeholder}
|
||||||
value={configs[item.key] || ""}
|
size="md"
|
||||||
variant="bordered"
|
value={configs[item.key] || ""}
|
||||||
onChange={(e) => handleConfigChange(item.key, e.target.value)}
|
variant="bordered"
|
||||||
/>
|
onChange={(e) => handleConfigChange(item.key, e.target.value)}
|
||||||
|
/>
|
||||||
|
{isBrandPreviewKey(item.key) ? renderBrandPreview(item.key) : null}
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
|
|
||||||
case "switch":
|
case "switch":
|
||||||
@@ -643,6 +769,42 @@ export default function ConfigPage() {
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
|
||||||
|
<Divider className="my-2" />
|
||||||
|
|
||||||
|
<div className="space-y-3">
|
||||||
|
<div className="flex flex-col gap-1">
|
||||||
|
<p className="text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||||
|
更新通道
|
||||||
|
</p>
|
||||||
|
<p className="text-xs text-gray-500 dark:text-gray-400">
|
||||||
|
稳定版仅匹配纯数字版本;开发版仅匹配包含 alpha / beta / rc
|
||||||
|
的版本。
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Select
|
||||||
|
selectedKeys={[updateChannel]}
|
||||||
|
size="md"
|
||||||
|
variant="bordered"
|
||||||
|
onSelectionChange={(keys) => {
|
||||||
|
const selected =
|
||||||
|
(Array.from(keys)[0] as UpdateReleaseChannel) || "stable";
|
||||||
|
|
||||||
|
handleUpdateChannelChange(selected);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<SelectItem key="stable" description="仅纯数字版本,如 2.1.4">
|
||||||
|
稳定版
|
||||||
|
</SelectItem>
|
||||||
|
<SelectItem
|
||||||
|
key="dev"
|
||||||
|
description="仅 alpha / beta / rc 关键字版本"
|
||||||
|
>
|
||||||
|
开发版
|
||||||
|
</SelectItem>
|
||||||
|
</Select>
|
||||||
|
</div>
|
||||||
</CardBody>
|
</CardBody>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
|
|||||||
+643
-296
File diff suppressed because it is too large
Load Diff
@@ -9,6 +9,7 @@ import { Input } from "@/shadcn-bridge/heroui/input";
|
|||||||
import { Button } from "@/shadcn-bridge/heroui/button";
|
import { Button } from "@/shadcn-bridge/heroui/button";
|
||||||
import { siteConfig } from "@/config/site";
|
import { siteConfig } from "@/config/site";
|
||||||
import { title } from "@/components/primitives";
|
import { title } from "@/components/primitives";
|
||||||
|
import { VersionFooter } from "@/components/version-footer";
|
||||||
import DefaultLayout from "@/layouts/default";
|
import DefaultLayout from "@/layouts/default";
|
||||||
import { login, LoginData, checkCaptcha, getConfigByName } from "@/api";
|
import { login, LoginData, checkCaptcha, getConfigByName } from "@/api";
|
||||||
import { writeLoginSession } from "@/utils/session";
|
import { writeLoginSession } from "@/utils/session";
|
||||||
@@ -213,22 +214,13 @@ export default function IndexPage() {
|
|||||||
|
|
||||||
{/* 版权信息 - 固定在底部,不占据布局空间 */}
|
{/* 版权信息 - 固定在底部,不占据布局空间 */}
|
||||||
|
|
||||||
<div className="fixed inset-x-0 bottom-4 text-center py-4">
|
<VersionFooter
|
||||||
<p className="text-xs text-gray-400 dark:text-gray-500">
|
containerClassName="fixed inset-x-0 bottom-4 text-center py-4"
|
||||||
Powered by{" "}
|
poweredClassName="text-xs text-gray-400 dark:text-gray-500"
|
||||||
<a
|
updateBadgeClassName="ml-2 inline-flex items-center rounded-full bg-rose-500/90 px-2 py-0.5 text-[10px] font-semibold tracking-wide text-white"
|
||||||
className="text-gray-500 dark:text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 transition-colors"
|
version={isWebView ? siteConfig.app_version : siteConfig.version}
|
||||||
href={siteConfig.github_repo}
|
versionClassName="text-xs text-gray-400 dark:text-gray-500 mt-1"
|
||||||
rel="noopener noreferrer"
|
/>
|
||||||
target="_blank"
|
|
||||||
>
|
|
||||||
FLVX
|
|
||||||
</a>
|
|
||||||
</p>
|
|
||||||
<p className="text-xs text-gray-400 dark:text-gray-500 mt-1">
|
|
||||||
v{isWebView ? siteConfig.app_version : siteConfig.version}
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* 验证码弹层 */}
|
{/* 验证码弹层 */}
|
||||||
{showCaptcha && siteKey && (
|
{showCaptcha && siteKey && (
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ interface SpeedLimitRule {
|
|||||||
name: string;
|
name: string;
|
||||||
speed: number;
|
speed: number;
|
||||||
status: number;
|
status: number;
|
||||||
tunnelId: number;
|
tunnelId?: number | null;
|
||||||
tunnelName: string;
|
tunnelName?: string;
|
||||||
createdTime: string;
|
createdTime: string;
|
||||||
updatedTime: string;
|
updatedTime: string;
|
||||||
}
|
}
|
||||||
@@ -139,9 +139,7 @@ export default function LimitPage() {
|
|||||||
newErrors.speed = "请输入有效的速度限制(≥1 Mbps)";
|
newErrors.speed = "请输入有效的速度限制(≥1 Mbps)";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!form.tunnelId) {
|
// tunnelId is optional - speed limits can be created without binding to a tunnel
|
||||||
newErrors.tunnelId = "请选择要绑定的隧道";
|
|
||||||
}
|
|
||||||
|
|
||||||
setErrors(newErrors);
|
setErrors(newErrors);
|
||||||
|
|
||||||
@@ -169,8 +167,8 @@ export default function LimitPage() {
|
|||||||
id: rule.id,
|
id: rule.id,
|
||||||
name: rule.name,
|
name: rule.name,
|
||||||
speed: rule.speed,
|
speed: rule.speed,
|
||||||
tunnelId: rule.tunnelId,
|
tunnelId: rule.tunnelId ?? null,
|
||||||
tunnelName: rule.tunnelName,
|
tunnelName: rule.tunnelName ?? "",
|
||||||
status: rule.status,
|
status: rule.status,
|
||||||
});
|
});
|
||||||
setErrors({});
|
setErrors({});
|
||||||
@@ -219,6 +217,8 @@ export default function LimitPage() {
|
|||||||
const createData = { ...form };
|
const createData = { ...form };
|
||||||
|
|
||||||
delete createData.id;
|
delete createData.id;
|
||||||
|
createData.tunnelId = null;
|
||||||
|
createData.tunnelName = "";
|
||||||
|
|
||||||
res = await createSpeedLimit(createData);
|
res = await createSpeedLimit(createData);
|
||||||
}
|
}
|
||||||
@@ -393,9 +393,7 @@ export default function LimitPage() {
|
|||||||
{isEdit ? "编辑限速规则" : "新增限速规则"}
|
{isEdit ? "编辑限速规则" : "新增限速规则"}
|
||||||
</h2>
|
</h2>
|
||||||
<p className="text-small text-default-500">
|
<p className="text-small text-default-500">
|
||||||
{isEdit
|
{isEdit ? "修改现有限速规则的配置信息" : "创建新的限速规则"}
|
||||||
? "修改现有限速规则的配置信息"
|
|
||||||
: "创建新的限速规则并绑定到隧道"}
|
|
||||||
</p>
|
</p>
|
||||||
</ModalHeader>
|
</ModalHeader>
|
||||||
<ModalBody>
|
<ModalBody>
|
||||||
@@ -435,43 +433,44 @@ export default function LimitPage() {
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Select
|
{isEdit && (
|
||||||
description={isEdit ? "编辑时无法修改绑定隧道" : undefined}
|
<Select
|
||||||
errorMessage={errors.tunnelId}
|
description="仅编辑时可调整绑定隧道"
|
||||||
isDisabled={isEdit}
|
errorMessage={errors.tunnelId}
|
||||||
isInvalid={!!errors.tunnelId}
|
isInvalid={!!errors.tunnelId}
|
||||||
label="绑定隧道"
|
label="绑定隧道"
|
||||||
placeholder="请选择要绑定的隧道"
|
placeholder="可选择要绑定的隧道(可选)"
|
||||||
selectedKeys={
|
selectedKeys={
|
||||||
form.tunnelId ? [form.tunnelId.toString()] : []
|
form.tunnelId ? [form.tunnelId.toString()] : []
|
||||||
}
|
|
||||||
variant="bordered"
|
|
||||||
onSelectionChange={(keys) => {
|
|
||||||
const selectedKey = Array.from(keys)[0] as string;
|
|
||||||
|
|
||||||
if (selectedKey) {
|
|
||||||
const selectedTunnel = tunnels.find(
|
|
||||||
(tunnel) => tunnel.id === parseInt(selectedKey),
|
|
||||||
);
|
|
||||||
|
|
||||||
setForm((prev) => ({
|
|
||||||
...prev,
|
|
||||||
tunnelId: parseInt(selectedKey),
|
|
||||||
tunnelName: selectedTunnel?.name || "",
|
|
||||||
}));
|
|
||||||
} else {
|
|
||||||
setForm((prev) => ({
|
|
||||||
...prev,
|
|
||||||
tunnelId: null,
|
|
||||||
tunnelName: "",
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
}}
|
variant="bordered"
|
||||||
>
|
onSelectionChange={(keys) => {
|
||||||
{tunnels.map((tunnel) => (
|
const selectedKey = Array.from(keys)[0] as string;
|
||||||
<SelectItem key={tunnel.id}>{tunnel.name}</SelectItem>
|
|
||||||
))}
|
if (selectedKey) {
|
||||||
</Select>
|
const selectedTunnel = tunnels.find(
|
||||||
|
(tunnel) => tunnel.id === parseInt(selectedKey),
|
||||||
|
);
|
||||||
|
|
||||||
|
setForm((prev) => ({
|
||||||
|
...prev,
|
||||||
|
tunnelId: parseInt(selectedKey),
|
||||||
|
tunnelName: selectedTunnel?.name || "",
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
setForm((prev) => ({
|
||||||
|
...prev,
|
||||||
|
tunnelId: null,
|
||||||
|
tunnelName: "",
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{tunnels.map((tunnel) => (
|
||||||
|
<SelectItem key={tunnel.id}>{tunnel.name}</SelectItem>
|
||||||
|
))}
|
||||||
|
</Select>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</ModalBody>
|
</ModalBody>
|
||||||
<ModalFooter>
|
<ModalFooter>
|
||||||
|
|||||||
@@ -1075,64 +1075,81 @@ export default function NodePage() {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex gap-2 items-center">
|
<div className="min-h-9 min-w-0 max-w-full overflow-x-auto touch-pan-x">
|
||||||
<Button
|
<div className="flex min-h-9 w-max min-w-full items-center justify-end gap-2 whitespace-nowrap [&>*]:shrink-0">
|
||||||
color={selectMode ? "secondary" : "default"}
|
{selectMode ? (
|
||||||
size="sm"
|
<>
|
||||||
variant={selectMode ? "solid" : "flat"}
|
<span className="text-sm text-default-600 shrink-0">
|
||||||
onPress={toggleSelectMode}
|
已选 {selectedIds.size} 项
|
||||||
>
|
</span>
|
||||||
{selectMode ? "退出" : "批量"}
|
<Button
|
||||||
</Button>
|
color="primary"
|
||||||
<Button color="primary" size="sm" variant="flat" onPress={handleAdd}>
|
size="sm"
|
||||||
新增
|
variant="flat"
|
||||||
</Button>
|
onPress={selectAll}
|
||||||
</div>
|
>
|
||||||
</div>
|
全选
|
||||||
|
</Button>
|
||||||
{/* 批量操作浮动工具栏 */}
|
<Button
|
||||||
{selectMode && selectedIds.size > 0 && (
|
color="secondary"
|
||||||
<div className="fixed bottom-7 left-1/2 z-50 w-[calc(100vw-1rem)] max-w-max -translate-x-1/2 overflow-x-auto rounded-lg border border-divider bg-content1 p-2 shadow-lg">
|
size="sm"
|
||||||
<div className="flex min-w-max items-center gap-2">
|
variant="flat"
|
||||||
<span className="text-sm font-medium shrink-0">
|
onPress={deselectAll}
|
||||||
已选 {selectedIds.size} 项
|
>
|
||||||
</span>
|
清空
|
||||||
<Button
|
</Button>
|
||||||
color="primary"
|
<Button
|
||||||
size="sm"
|
color="warning"
|
||||||
variant="flat"
|
isDisabled={selectedIds.size === 0}
|
||||||
onPress={selectAll}
|
isLoading={batchUpgradeLoading}
|
||||||
>
|
size="sm"
|
||||||
全选
|
variant="flat"
|
||||||
</Button>
|
onPress={() => openUpgradeModal("batch")}
|
||||||
<Button
|
>
|
||||||
color="secondary"
|
升级
|
||||||
size="sm"
|
</Button>
|
||||||
variant="flat"
|
<Button
|
||||||
onPress={deselectAll}
|
color="danger"
|
||||||
>
|
isDisabled={selectedIds.size === 0}
|
||||||
清空
|
size="sm"
|
||||||
</Button>
|
variant="flat"
|
||||||
<Button
|
onPress={() => setBatchDeleteModalOpen(true)}
|
||||||
color="warning"
|
>
|
||||||
isLoading={batchUpgradeLoading}
|
删除
|
||||||
size="sm"
|
</Button>
|
||||||
variant="flat"
|
<Button
|
||||||
onPress={() => openUpgradeModal("batch")}
|
color="secondary"
|
||||||
>
|
size="sm"
|
||||||
升级
|
variant="solid"
|
||||||
</Button>
|
onPress={toggleSelectMode}
|
||||||
<Button
|
>
|
||||||
color="danger"
|
退出
|
||||||
size="sm"
|
</Button>
|
||||||
variant="flat"
|
</>
|
||||||
onPress={() => setBatchDeleteModalOpen(true)}
|
) : (
|
||||||
>
|
<>
|
||||||
删除
|
<Button
|
||||||
</Button>
|
className="bg-sky-100 text-sky-700 hover:bg-sky-200 dark:bg-sky-900/30 dark:text-sky-300 dark:hover:bg-sky-900/45"
|
||||||
|
color="default"
|
||||||
|
size="sm"
|
||||||
|
variant="flat"
|
||||||
|
onPress={toggleSelectMode}
|
||||||
|
>
|
||||||
|
批量
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
color="primary"
|
||||||
|
size="sm"
|
||||||
|
variant="flat"
|
||||||
|
onPress={handleAdd}
|
||||||
|
>
|
||||||
|
新增
|
||||||
|
</Button>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
</div>
|
||||||
|
|
||||||
{!wsConnected && (
|
{!wsConnected && (
|
||||||
<Alert
|
<Alert
|
||||||
|
|||||||
@@ -375,6 +375,9 @@ export default function PanelSharingPage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const formatChainType = (chainType: number, hopInx: number) => {
|
const formatChainType = (chainType: number, hopInx: number) => {
|
||||||
|
if (chainType === 1) {
|
||||||
|
return "入口节点";
|
||||||
|
}
|
||||||
if (chainType === 2) {
|
if (chainType === 2) {
|
||||||
return `中继跳点 #${hopInx}`;
|
return `中继跳点 #${hopInx}`;
|
||||||
}
|
}
|
||||||
@@ -674,7 +677,11 @@ export default function PanelSharingPage() {
|
|||||||
</Tabs>
|
</Tabs>
|
||||||
|
|
||||||
{/* Create Share Modal */}
|
{/* Create Share Modal */}
|
||||||
<Modal isOpen={createShareOpen} onClose={() => setCreateShareOpen(false)}>
|
<Modal
|
||||||
|
isOpen={createShareOpen}
|
||||||
|
scrollBehavior="inside"
|
||||||
|
onClose={() => setCreateShareOpen(false)}
|
||||||
|
>
|
||||||
<ModalContent>
|
<ModalContent>
|
||||||
<ModalHeader>创建分享</ModalHeader>
|
<ModalHeader>创建分享</ModalHeader>
|
||||||
<ModalBody>
|
<ModalBody>
|
||||||
@@ -777,7 +784,11 @@ export default function PanelSharingPage() {
|
|||||||
</Modal>
|
</Modal>
|
||||||
|
|
||||||
{/* Edit Share Modal */}
|
{/* Edit Share Modal */}
|
||||||
<Modal isOpen={editShareOpen} onClose={() => setEditShareOpen(false)}>
|
<Modal
|
||||||
|
isOpen={editShareOpen}
|
||||||
|
scrollBehavior="inside"
|
||||||
|
onClose={() => setEditShareOpen(false)}
|
||||||
|
>
|
||||||
<ModalContent>
|
<ModalContent>
|
||||||
<ModalHeader>编辑分享</ModalHeader>
|
<ModalHeader>编辑分享</ModalHeader>
|
||||||
<ModalBody>
|
<ModalBody>
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import {
|
|||||||
import { Input } from "@/shadcn-bridge/heroui/input";
|
import { Input } from "@/shadcn-bridge/heroui/input";
|
||||||
import { isWebViewFunc } from "@/utils/panel";
|
import { isWebViewFunc } from "@/utils/panel";
|
||||||
import { siteConfig } from "@/config/site";
|
import { siteConfig } from "@/config/site";
|
||||||
|
import { VersionFooter } from "@/components/version-footer";
|
||||||
import { updatePassword } from "@/api";
|
import { updatePassword } from "@/api";
|
||||||
import { safeLogout } from "@/utils/logout";
|
import { safeLogout } from "@/utils/logout";
|
||||||
import { getAdminFlag, getSessionName } from "@/utils/session";
|
import { getAdminFlag, getSessionName } from "@/utils/session";
|
||||||
@@ -312,22 +313,15 @@ export default function ProfilePage() {
|
|||||||
</CardBody>
|
</CardBody>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
<div className="fixed inset-x-0 bottom-20 text-center py-4">
|
<VersionFooter
|
||||||
<p className="text-xs text-gray-400 dark:text-gray-500">
|
containerClassName="fixed inset-x-0 bottom-20 text-center py-4"
|
||||||
Powered by{" "}
|
poweredClassName="text-xs text-gray-400 dark:text-gray-500"
|
||||||
<a
|
updateBadgeClassName="ml-2 inline-flex items-center rounded-full bg-rose-500/90 px-2 py-0.5 text-[10px] font-semibold tracking-wide text-white"
|
||||||
className="text-gray-500 dark:text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 transition-colors"
|
version={
|
||||||
href={siteConfig.github_repo}
|
isWebViewFunc() ? siteConfig.app_version : siteConfig.version
|
||||||
rel="noopener noreferrer"
|
}
|
||||||
target="_blank"
|
versionClassName="text-xs text-gray-400 dark:text-gray-500 mt-1"
|
||||||
>
|
/>
|
||||||
FLVX
|
|
||||||
</a>
|
|
||||||
</p>
|
|
||||||
<p className="text-xs text-gray-400 dark:text-gray-500 mt-1">
|
|
||||||
v{isWebViewFunc() ? siteConfig.app_version : siteConfig.version}
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* 修改密码弹窗 */}
|
{/* 修改密码弹窗 */}
|
||||||
|
|||||||
@@ -5,7 +5,13 @@ import toast from "react-hot-toast";
|
|||||||
import { Input } from "@/shadcn-bridge/heroui/input";
|
import { Input } from "@/shadcn-bridge/heroui/input";
|
||||||
import { Button } from "@/shadcn-bridge/heroui/button";
|
import { Button } from "@/shadcn-bridge/heroui/button";
|
||||||
import { Card, CardBody } from "@/shadcn-bridge/heroui/card";
|
import { Card, CardBody } from "@/shadcn-bridge/heroui/card";
|
||||||
|
import { Select, SelectItem } from "@/shadcn-bridge/heroui/select";
|
||||||
import { reinitializeBaseURL } from "@/api/network";
|
import { reinitializeBaseURL } from "@/api/network";
|
||||||
|
import {
|
||||||
|
type UpdateReleaseChannel,
|
||||||
|
getUpdateReleaseChannel,
|
||||||
|
setUpdateReleaseChannel,
|
||||||
|
} from "@/utils/version-update";
|
||||||
import {
|
import {
|
||||||
getPanelAddresses,
|
getPanelAddresses,
|
||||||
savePanelAddress,
|
savePanelAddress,
|
||||||
@@ -25,6 +31,9 @@ export const SettingsPage = () => {
|
|||||||
const [panelAddresses, setPanelAddresses] = useState<PanelAddress[]>([]);
|
const [panelAddresses, setPanelAddresses] = useState<PanelAddress[]>([]);
|
||||||
const [newName, setNewName] = useState("");
|
const [newName, setNewName] = useState("");
|
||||||
const [newAddress, setNewAddress] = useState("");
|
const [newAddress, setNewAddress] = useState("");
|
||||||
|
const [updateChannel, setUpdateChannel] = useState<UpdateReleaseChannel>(
|
||||||
|
getUpdateReleaseChannel(),
|
||||||
|
);
|
||||||
|
|
||||||
const setPanelAddressesFunc = (newAddress: PanelAddress[]) => {
|
const setPanelAddressesFunc = (newAddress: PanelAddress[]) => {
|
||||||
setPanelAddresses(newAddress);
|
setPanelAddresses(newAddress);
|
||||||
@@ -79,6 +88,14 @@ export const SettingsPage = () => {
|
|||||||
loadPanelAddresses();
|
loadPanelAddresses();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const handleUpdateChannelChange = (channel: UpdateReleaseChannel) => {
|
||||||
|
setUpdateChannel(channel);
|
||||||
|
setUpdateReleaseChannel(channel);
|
||||||
|
toast.success(
|
||||||
|
`更新通道已切换为${channel === "stable" ? "稳定版" : "开发版"}`,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="min-h-screen bg-gray-50 dark:bg-black">
|
<div className="min-h-screen bg-gray-50 dark:bg-black">
|
||||||
{/* 顶部导航 */}
|
{/* 顶部导航 */}
|
||||||
@@ -117,6 +134,36 @@ export const SettingsPage = () => {
|
|||||||
{/* 内容区域 */}
|
{/* 内容区域 */}
|
||||||
<div className="max-w-4xl mx-auto px-4 py-6">
|
<div className="max-w-4xl mx-auto px-4 py-6">
|
||||||
<div className="space-y-6">
|
<div className="space-y-6">
|
||||||
|
<Card className="border border-gray-200 dark:border-gray-700">
|
||||||
|
<CardBody className="p-6">
|
||||||
|
<h2 className="text-lg font-medium text-gray-900 dark:text-white mb-4">
|
||||||
|
更新设置
|
||||||
|
</h2>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Select
|
||||||
|
label="更新通道"
|
||||||
|
selectedKeys={[updateChannel]}
|
||||||
|
onSelectionChange={(keys) => {
|
||||||
|
const selected =
|
||||||
|
(Array.from(keys)[0] as UpdateReleaseChannel) || "stable";
|
||||||
|
|
||||||
|
handleUpdateChannelChange(selected);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<SelectItem key="stable" textValue="stable">
|
||||||
|
稳定版(纯数字版本,如 2.1.4)
|
||||||
|
</SelectItem>
|
||||||
|
<SelectItem key="dev" textValue="dev">
|
||||||
|
开发版(含 alpha / beta / rc)
|
||||||
|
</SelectItem>
|
||||||
|
</Select>
|
||||||
|
<p className="text-xs text-gray-500 dark:text-gray-400">
|
||||||
|
版本提示会根据该通道检查最新版本。
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</CardBody>
|
||||||
|
</Card>
|
||||||
|
|
||||||
{/* 添加新地址 */}
|
{/* 添加新地址 */}
|
||||||
<Card className="border border-gray-200 dark:border-gray-700">
|
<Card className="border border-gray-200 dark:border-gray-700">
|
||||||
<CardBody className="p-6">
|
<CardBody className="p-6">
|
||||||
|
|||||||
+155
-162
@@ -293,37 +293,10 @@ export default function TunnelPage() {
|
|||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
// 添加节点到指定的转发链跳数
|
const toSelectedNodeIds = (keys: Iterable<unknown>): number[] => {
|
||||||
const addNodeToChain = (groupIndex: number, nodeId: number) => {
|
return Array.from(keys)
|
||||||
setForm((prev) => {
|
.map((key) => Number.parseInt(String(key), 10))
|
||||||
const chainNodes = [...(prev.chainNodes || [])];
|
.filter((nodeId) => Number.isFinite(nodeId));
|
||||||
const group = chainNodes[groupIndex] || [];
|
|
||||||
|
|
||||||
// 获取当前组的策略和协议
|
|
||||||
const strategy = group.length > 0 ? group[0].strategy : "round";
|
|
||||||
const protocol = group.length > 0 ? group[0].protocol : "tls";
|
|
||||||
|
|
||||||
// 添加节点到该组
|
|
||||||
chainNodes[groupIndex] = [
|
|
||||||
...group,
|
|
||||||
{ nodeId, chainType: 2, protocol, strategy },
|
|
||||||
];
|
|
||||||
|
|
||||||
return { ...prev, chainNodes };
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// 从某一跳删除指定节点
|
|
||||||
const removeNodeFromChain = (groupIndex: number, nodeId: number) => {
|
|
||||||
setForm((prev) => {
|
|
||||||
const chainNodes = [...(prev.chainNodes || [])];
|
|
||||||
|
|
||||||
chainNodes[groupIndex] = (chainNodes[groupIndex] || []).filter(
|
|
||||||
(node) => node.nodeId !== nodeId,
|
|
||||||
);
|
|
||||||
|
|
||||||
return { ...prev, chainNodes };
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 更新某一跳的所有节点的协议
|
// 更新某一跳的所有节点的协议
|
||||||
@@ -366,6 +339,51 @@ export default function TunnelPage() {
|
|||||||
return form.chainNodes || [];
|
return form.chainNodes || [];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const mergeOrderedNodes = (
|
||||||
|
currentNodes: ChainTunnel[],
|
||||||
|
selectedNodeIds: number[],
|
||||||
|
buildDefault: (nodeId: number) => ChainTunnel,
|
||||||
|
): ChainTunnel[] => {
|
||||||
|
const selectedSet = new Set(selectedNodeIds);
|
||||||
|
const kept = currentNodes.filter((node) => selectedSet.has(node.nodeId));
|
||||||
|
const keptIds = new Set(kept.map((node) => node.nodeId));
|
||||||
|
const added = selectedNodeIds
|
||||||
|
.filter((nodeId) => !keptIds.has(nodeId))
|
||||||
|
.map((nodeId) => buildDefault(nodeId));
|
||||||
|
|
||||||
|
return [...kept, ...added];
|
||||||
|
};
|
||||||
|
|
||||||
|
const syncChainGroupNodes = (
|
||||||
|
groupIndex: number,
|
||||||
|
selectedNodeIds: number[],
|
||||||
|
) => {
|
||||||
|
setForm((prev) => {
|
||||||
|
const chainNodes = [...(prev.chainNodes || [])];
|
||||||
|
const currentGroup = chainNodes[groupIndex] || [];
|
||||||
|
const protocol = currentGroup[0]?.protocol || "tls";
|
||||||
|
const strategy = currentGroup[0]?.strategy || "round";
|
||||||
|
const realNodes = currentGroup.filter((node) => node.nodeId !== -1);
|
||||||
|
const mergedNodes = mergeOrderedNodes(
|
||||||
|
realNodes,
|
||||||
|
selectedNodeIds,
|
||||||
|
(nodeId) => ({
|
||||||
|
nodeId,
|
||||||
|
chainType: 2,
|
||||||
|
protocol,
|
||||||
|
strategy,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
chainNodes[groupIndex] =
|
||||||
|
mergedNodes.length > 0
|
||||||
|
? mergedNodes
|
||||||
|
: [{ nodeId: -1, chainType: 2, protocol, strategy }];
|
||||||
|
|
||||||
|
return { ...prev, chainNodes };
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// 提交表单
|
// 提交表单
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
if (!validateForm()) return;
|
if (!validateForm()) return;
|
||||||
@@ -710,63 +728,81 @@ export default function TunnelPage() {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex items-center gap-2">
|
<div className="min-h-9 min-w-0 max-w-full overflow-x-auto touch-pan-x">
|
||||||
<Button
|
<div className="flex min-h-9 w-max min-w-full items-center justify-end gap-2 whitespace-nowrap [&>*]:shrink-0">
|
||||||
color={selectMode ? "secondary" : "default"}
|
{selectMode ? (
|
||||||
size="sm"
|
<>
|
||||||
variant={selectMode ? "solid" : "flat"}
|
<span className="text-sm text-default-600 shrink-0">
|
||||||
onPress={toggleSelectMode}
|
已选择 {selectedIds.size} 项
|
||||||
>
|
</span>
|
||||||
{selectMode ? "退出" : "批量"}
|
<Button
|
||||||
</Button>
|
color="primary"
|
||||||
<Button color="primary" size="sm" variant="flat" onPress={handleAdd}>
|
size="sm"
|
||||||
新增
|
variant="flat"
|
||||||
</Button>
|
onPress={selectAll}
|
||||||
</div>
|
>
|
||||||
</div>
|
全选
|
||||||
|
</Button>
|
||||||
{selectMode && selectedIds.size > 0 && (
|
<Button
|
||||||
<div className="fixed bottom-7 left-1/2 z-50 w-[calc(100vw-1rem)] max-w-max -translate-x-1/2 overflow-x-auto rounded-lg border border-divider bg-content1 p-2 shadow-lg">
|
color="secondary"
|
||||||
<div className="flex min-w-max items-center gap-2">
|
size="sm"
|
||||||
<span className="text-sm text-default-600 shrink-0">
|
variant="flat"
|
||||||
已选择 {selectedIds.size} 项
|
onPress={deselectAll}
|
||||||
</span>
|
>
|
||||||
<Button
|
清空
|
||||||
color="primary"
|
</Button>
|
||||||
size="sm"
|
<Button
|
||||||
variant="flat"
|
color="danger"
|
||||||
onPress={selectAll}
|
isDisabled={selectedIds.size === 0}
|
||||||
>
|
size="sm"
|
||||||
全选
|
variant="flat"
|
||||||
</Button>
|
onPress={() => setBatchDeleteModalOpen(true)}
|
||||||
<Button
|
>
|
||||||
color="secondary"
|
删除
|
||||||
size="sm"
|
</Button>
|
||||||
variant="flat"
|
<Button
|
||||||
onPress={deselectAll}
|
color="primary"
|
||||||
>
|
isDisabled={selectedIds.size === 0}
|
||||||
清空
|
isLoading={batchLoading}
|
||||||
</Button>
|
size="sm"
|
||||||
<Button
|
variant="flat"
|
||||||
color="danger"
|
onPress={handleBatchRedeploy}
|
||||||
size="sm"
|
>
|
||||||
variant="flat"
|
下发
|
||||||
onPress={() => setBatchDeleteModalOpen(true)}
|
</Button>
|
||||||
>
|
<Button
|
||||||
删除
|
color="secondary"
|
||||||
</Button>
|
size="sm"
|
||||||
<Button
|
variant="solid"
|
||||||
color="primary"
|
onPress={toggleSelectMode}
|
||||||
isLoading={batchLoading}
|
>
|
||||||
size="sm"
|
退出
|
||||||
variant="flat"
|
</Button>
|
||||||
onPress={handleBatchRedeploy}
|
</>
|
||||||
>
|
) : (
|
||||||
下发
|
<>
|
||||||
</Button>
|
<Button
|
||||||
|
className="bg-sky-100 text-sky-700 hover:bg-sky-200 dark:bg-sky-900/30 dark:text-sky-300 dark:hover:bg-sky-900/45"
|
||||||
|
color="default"
|
||||||
|
size="sm"
|
||||||
|
variant="flat"
|
||||||
|
onPress={toggleSelectMode}
|
||||||
|
>
|
||||||
|
批量
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
color="primary"
|
||||||
|
size="sm"
|
||||||
|
variant="flat"
|
||||||
|
onPress={handleAdd}
|
||||||
|
>
|
||||||
|
新增
|
||||||
|
</Button>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
</div>
|
||||||
|
|
||||||
{/* 隧道卡片网格 */}
|
{/* 隧道卡片网格 */}
|
||||||
{tunnels.length > 0 ? (
|
{tunnels.length > 0 ? (
|
||||||
@@ -1179,6 +1215,7 @@ export default function TunnelPage() {
|
|||||||
<Select
|
<Select
|
||||||
description="当节点同时拥有IPv4和IPv6地址时,选择隧道连接使用的地址类型"
|
description="当节点同时拥有IPv4和IPv6地址时,选择隧道连接使用的地址类型"
|
||||||
label="隧道连接地址偏好"
|
label="隧道连接地址偏好"
|
||||||
|
placeholder="自动选择"
|
||||||
selectedKeys={[form.ipPreference || ""]}
|
selectedKeys={[form.ipPreference || ""]}
|
||||||
variant="bordered"
|
variant="bordered"
|
||||||
onSelectionChange={(keys) => {
|
onSelectionChange={(keys) => {
|
||||||
@@ -1190,7 +1227,6 @@ export default function TunnelPage() {
|
|||||||
}));
|
}));
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<SelectItem key="">自动选择</SelectItem>
|
|
||||||
<SelectItem key="v4">优先IPv4</SelectItem>
|
<SelectItem key="v4">优先IPv4</SelectItem>
|
||||||
<SelectItem key="v6">优先IPv6</SelectItem>
|
<SelectItem key="v6">优先IPv6</SelectItem>
|
||||||
</Select>
|
</Select>
|
||||||
@@ -1220,21 +1256,16 @@ export default function TunnelPage() {
|
|||||||
selectionMode="multiple"
|
selectionMode="multiple"
|
||||||
variant="bordered"
|
variant="bordered"
|
||||||
onSelectionChange={(keys) => {
|
onSelectionChange={(keys) => {
|
||||||
const selectedIds = Array.from(keys).map((key) =>
|
const selectedIds = toSelectedNodeIds(keys);
|
||||||
parseInt(key as string),
|
|
||||||
);
|
|
||||||
const newInNodeId: ChainTunnel[] = selectedIds.map(
|
|
||||||
(nodeId) => {
|
|
||||||
// 保留已有的端口配置
|
|
||||||
const existing = form.inNodeId.find(
|
|
||||||
(ct) => ct.nodeId === nodeId,
|
|
||||||
);
|
|
||||||
|
|
||||||
return existing || { nodeId, chainType: 1 };
|
setForm((prev) => ({
|
||||||
},
|
...prev,
|
||||||
);
|
inNodeId: mergeOrderedNodes(
|
||||||
|
prev.inNodeId,
|
||||||
setForm((prev) => ({ ...prev, inNodeId: newInNodeId }));
|
selectedIds,
|
||||||
|
(nodeId) => ({ nodeId, chainType: 1 }),
|
||||||
|
),
|
||||||
|
}));
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{nodes.map((node) => (
|
{nodes.map((node) => (
|
||||||
@@ -1403,37 +1434,10 @@ export default function TunnelPage() {
|
|||||||
size="sm"
|
size="sm"
|
||||||
variant="bordered"
|
variant="bordered"
|
||||||
onSelectionChange={(keys) => {
|
onSelectionChange={(keys) => {
|
||||||
const selectedIds = Array.from(
|
syncChainGroupNodes(
|
||||||
keys,
|
groupIndex,
|
||||||
).map((key) => parseInt(key as string));
|
toSelectedNodeIds(keys),
|
||||||
const currentNodes = groupNodes.filter(
|
|
||||||
(ct) => ct.nodeId !== -1,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// 找出新增的节点
|
|
||||||
const currentNodeIds = currentNodes.map(
|
|
||||||
(ct) => ct.nodeId,
|
|
||||||
);
|
|
||||||
const addedIds = selectedIds.filter(
|
|
||||||
(id) => !currentNodeIds.includes(id),
|
|
||||||
);
|
|
||||||
const removedIds =
|
|
||||||
currentNodeIds.filter(
|
|
||||||
(id) => !selectedIds.includes(id),
|
|
||||||
);
|
|
||||||
|
|
||||||
// 添加新节点
|
|
||||||
addedIds.forEach((nodeId) => {
|
|
||||||
addNodeToChain(groupIndex, nodeId);
|
|
||||||
});
|
|
||||||
|
|
||||||
// 删除取消选择的节点
|
|
||||||
removedIds.forEach((nodeId) => {
|
|
||||||
removeNodeFromChain(
|
|
||||||
groupIndex,
|
|
||||||
nodeId,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{nodes.map((node) => (
|
{nodes.map((node) => (
|
||||||
@@ -1624,43 +1628,32 @@ export default function TunnelPage() {
|
|||||||
selectionMode="multiple"
|
selectionMode="multiple"
|
||||||
variant="bordered"
|
variant="bordered"
|
||||||
onSelectionChange={(keys) => {
|
onSelectionChange={(keys) => {
|
||||||
const selectedIds = Array.from(keys).map((key) =>
|
const selectedIds = toSelectedNodeIds(keys);
|
||||||
parseInt(key as string),
|
|
||||||
);
|
|
||||||
const currentOutNodes = form.outNodeId || [];
|
|
||||||
|
|
||||||
let protocol = "tls";
|
setForm((prev) => {
|
||||||
let strategy = "round";
|
const currentOutNodes = prev.outNodeId || [];
|
||||||
|
const protocol =
|
||||||
|
currentOutNodes[0]?.protocol || "tls";
|
||||||
|
const strategy =
|
||||||
|
currentOutNodes[0]?.strategy || "round";
|
||||||
|
const realNodes = currentOutNodes.filter(
|
||||||
|
(ct) => ct.nodeId !== -1,
|
||||||
|
);
|
||||||
|
|
||||||
if (currentOutNodes.length > 0) {
|
return {
|
||||||
protocol = currentOutNodes[0].protocol || "tls";
|
...prev,
|
||||||
strategy =
|
outNodeId: mergeOrderedNodes(
|
||||||
currentOutNodes[0].strategy || "round";
|
realNodes,
|
||||||
}
|
selectedIds,
|
||||||
|
(nodeId) => ({
|
||||||
const realNodes = currentOutNodes.filter(
|
|
||||||
(ct) => ct.nodeId !== -1,
|
|
||||||
);
|
|
||||||
const newOutNodeId: ChainTunnel[] =
|
|
||||||
selectedIds.map((nodeId) => {
|
|
||||||
const existing = realNodes.find(
|
|
||||||
(ct) => ct.nodeId === nodeId,
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
|
||||||
existing || {
|
|
||||||
nodeId,
|
nodeId,
|
||||||
chainType: 3,
|
chainType: 3,
|
||||||
protocol,
|
protocol,
|
||||||
strategy,
|
strategy,
|
||||||
}
|
}),
|
||||||
);
|
),
|
||||||
});
|
};
|
||||||
|
});
|
||||||
setForm((prev) => ({
|
|
||||||
...prev,
|
|
||||||
outNodeId: newOutNodeId,
|
|
||||||
}));
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{nodes.map((node) => (
|
{nodes.map((node) => (
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect, useMemo } from "react";
|
||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
import { parseDate } from "@internationalized/date";
|
import { parseDate } from "@internationalized/date";
|
||||||
|
|
||||||
@@ -219,6 +219,22 @@ export default function UserPage() {
|
|||||||
const [speedLimits, setSpeedLimits] = useState<SpeedLimit[]>([]);
|
const [speedLimits, setSpeedLimits] = useState<SpeedLimit[]>([]);
|
||||||
const [userGroups, setUserGroups] = useState<UserGroup[]>([]);
|
const [userGroups, setUserGroups] = useState<UserGroup[]>([]);
|
||||||
|
|
||||||
|
const noLimitSpeedLimitIds = useMemo(() => {
|
||||||
|
return new Set(
|
||||||
|
speedLimits
|
||||||
|
.filter((speedLimit) => speedLimit.name.trim() === "不限速")
|
||||||
|
.map((speedLimit) => speedLimit.id),
|
||||||
|
);
|
||||||
|
}, [speedLimits]);
|
||||||
|
|
||||||
|
const normalizeSpeedId = (speedId?: number | null): number | null => {
|
||||||
|
if (speedId === null || speedId === undefined) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return noLimitSpeedLimitIds.has(speedId) ? null : speedId;
|
||||||
|
};
|
||||||
|
|
||||||
// 生命周期
|
// 生命周期
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadUsers();
|
loadUsers();
|
||||||
@@ -432,7 +448,10 @@ export default function UserPage() {
|
|||||||
try {
|
try {
|
||||||
const tunnelsToAssign: TunnelAssignItem[] = Array.from(
|
const tunnelsToAssign: TunnelAssignItem[] = Array.from(
|
||||||
batchTunnelSelections.entries(),
|
batchTunnelSelections.entries(),
|
||||||
).map(([tunnelId, speedId]) => ({ tunnelId, speedId }));
|
).map(([tunnelId, speedId]) => ({
|
||||||
|
tunnelId,
|
||||||
|
speedId: normalizeSpeedId(speedId),
|
||||||
|
}));
|
||||||
|
|
||||||
const response = await batchAssignUserTunnel({
|
const response = await batchAssignUserTunnel({
|
||||||
userId: currentUser.id,
|
userId: currentUser.id,
|
||||||
@@ -456,6 +475,7 @@ export default function UserPage() {
|
|||||||
const handleEditTunnel = (userTunnel: UserTunnel) => {
|
const handleEditTunnel = (userTunnel: UserTunnel) => {
|
||||||
setEditTunnelForm({
|
setEditTunnelForm({
|
||||||
...userTunnel,
|
...userTunnel,
|
||||||
|
speedId: normalizeSpeedId(userTunnel.speedId),
|
||||||
expTime: userTunnel.expTime,
|
expTime: userTunnel.expTime,
|
||||||
});
|
});
|
||||||
onEditTunnelModalOpen();
|
onEditTunnelModalOpen();
|
||||||
@@ -472,7 +492,7 @@ export default function UserPage() {
|
|||||||
num: editTunnelForm.num,
|
num: editTunnelForm.num,
|
||||||
expTime: editTunnelForm.expTime,
|
expTime: editTunnelForm.expTime,
|
||||||
flowResetTime: editTunnelForm.flowResetTime,
|
flowResetTime: editTunnelForm.flowResetTime,
|
||||||
speedId: editTunnelForm.speedId,
|
speedId: normalizeSpeedId(editTunnelForm.speedId),
|
||||||
status: editTunnelForm.status,
|
status: editTunnelForm.status,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -583,13 +603,17 @@ export default function UserPage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const editAvailableSpeedLimits = speedLimits.filter(
|
const editAvailableSpeedLimits = speedLimits.filter(
|
||||||
(speedLimit) => speedLimit.tunnelId === editTunnelForm?.tunnelId,
|
(speedLimit) => !noLimitSpeedLimitIds.has(speedLimit.id),
|
||||||
);
|
);
|
||||||
|
|
||||||
const getSpeedLimitsForTunnel = (tunnelId: number) => {
|
const getSpeedLimitsForTunnel = (_tunnelId: number) => {
|
||||||
return speedLimits.filter((sl) => sl.tunnelId === tunnelId);
|
return speedLimits.filter(
|
||||||
|
(speedLimit) => !noLimitSpeedLimitIds.has(speedLimit.id),
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const editTunnelSelectedSpeedId = normalizeSpeedId(editTunnelForm?.speedId);
|
||||||
|
|
||||||
const toggleTunnelSelection = (tunnelId: number) => {
|
const toggleTunnelSelection = (tunnelId: number) => {
|
||||||
setBatchTunnelSelections((prev) => {
|
setBatchTunnelSelections((prev) => {
|
||||||
const newMap = new Map(prev);
|
const newMap = new Map(prev);
|
||||||
@@ -638,7 +662,6 @@ export default function UserPage() {
|
|||||||
) : (
|
) : (
|
||||||
<div className="flex w-full items-center gap-2 animate-appearance-in">
|
<div className="flex w-full items-center gap-2 animate-appearance-in">
|
||||||
<Input
|
<Input
|
||||||
autoFocus
|
|
||||||
classNames={{
|
classNames={{
|
||||||
base: "bg-default-100",
|
base: "bg-default-100",
|
||||||
input: "bg-transparent",
|
input: "bg-transparent",
|
||||||
@@ -1147,7 +1170,7 @@ export default function UserPage() {
|
|||||||
classNames={{
|
classNames={{
|
||||||
trigger: "min-h-10 h-10",
|
trigger: "min-h-10 h-10",
|
||||||
}}
|
}}
|
||||||
placeholder="限速规则"
|
placeholder="请选择限速规则"
|
||||||
selectedKeys={
|
selectedKeys={
|
||||||
batchTunnelSelections.get(tunnel.id) !==
|
batchTunnelSelections.get(tunnel.id) !==
|
||||||
null &&
|
null &&
|
||||||
@@ -1406,9 +1429,10 @@ export default function UserPage() {
|
|||||||
|
|
||||||
<Select
|
<Select
|
||||||
label="限速规则"
|
label="限速规则"
|
||||||
|
placeholder="请选择限速规则"
|
||||||
selectedKeys={
|
selectedKeys={
|
||||||
editTunnelForm.speedId
|
editTunnelSelectedSpeedId !== null
|
||||||
? [editTunnelForm.speedId.toString()]
|
? [editTunnelSelectedSpeedId.toString()]
|
||||||
: ["null"]
|
: ["null"]
|
||||||
}
|
}
|
||||||
onSelectionChange={(keys) => {
|
onSelectionChange={(keys) => {
|
||||||
|
|||||||
@@ -19,11 +19,18 @@ function mapColor(color: LinkColor) {
|
|||||||
return "text-default-600 hover:text-default-700";
|
return "text-default-600 hover:text-default-700";
|
||||||
}
|
}
|
||||||
|
|
||||||
export function Link({ className, color = "default", ...props }: LinkProps) {
|
export function Link({
|
||||||
|
className,
|
||||||
|
color = "default",
|
||||||
|
children,
|
||||||
|
...props
|
||||||
|
}: LinkProps) {
|
||||||
return (
|
return (
|
||||||
<a
|
<a
|
||||||
className={cn("transition-colors", mapColor(color), className)}
|
className={cn("transition-colors", mapColor(color), className)}
|
||||||
{...props}
|
{...props}
|
||||||
/>
|
>
|
||||||
|
{children}
|
||||||
|
</a>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -207,7 +207,7 @@ export function ModalFooter({
|
|||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn(
|
||||||
"mt-4 flex flex-col-reverse gap-2 sm:flex-row sm:justify-end",
|
"mt-4 flex flex-wrap justify-end gap-2",
|
||||||
context?.classNames?.footer,
|
context?.classNames?.footer,
|
||||||
className,
|
className,
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -228,11 +228,14 @@ export function Select<T>({
|
|||||||
|
|
||||||
const selectedArray = Array.from(selected);
|
const selectedArray = Array.from(selected);
|
||||||
const singleValue = selectedArray[0] ?? "";
|
const singleValue = selectedArray[0] ?? "";
|
||||||
const selectedLabels = options
|
const optionLabelMap = React.useMemo(() => {
|
||||||
.filter((option) => selected.has(option.key))
|
return new Map(options.map((option) => [option.key, option.label]));
|
||||||
.map((option) => option.label);
|
}, [options]);
|
||||||
const resolvedSelectedValues =
|
const resolvedSelectedValues = selectedArray.map((key) => {
|
||||||
selectedLabels.length > 0 ? selectedLabels : selectedArray;
|
const keyText = String(key);
|
||||||
|
|
||||||
|
return optionLabelMap.get(keyText) ?? keyText;
|
||||||
|
});
|
||||||
const selectedFullText = resolvedSelectedValues.join("、");
|
const selectedFullText = resolvedSelectedValues.join("、");
|
||||||
const selectedText =
|
const selectedText =
|
||||||
selectedArray.length > 0 ? selectedFullText : (placeholder ?? "请选择");
|
selectedArray.length > 0 ? selectedFullText : (placeholder ?? "请选择");
|
||||||
|
|||||||
@@ -88,7 +88,8 @@ export interface Tunnel {
|
|||||||
export interface SpeedLimit {
|
export interface SpeedLimit {
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
tunnelId: number;
|
tunnelId?: number | null;
|
||||||
|
speed?: number;
|
||||||
uploadSpeed: number;
|
uploadSpeed: number;
|
||||||
downloadSpeed: number;
|
downloadSpeed: number;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,218 @@
|
|||||||
|
export type UpdateReleaseChannel = "stable" | "dev";
|
||||||
|
|
||||||
|
export const UPDATE_CHANNEL_STORAGE_KEY = "update-release-channel";
|
||||||
|
export const UPDATE_CHANNEL_CHANGED_EVENT = "updateReleaseChannelChanged";
|
||||||
|
|
||||||
|
const CHANNEL_STABLE: UpdateReleaseChannel = "stable";
|
||||||
|
const CHANNEL_DEV: UpdateReleaseChannel = "dev";
|
||||||
|
|
||||||
|
const stableVersionPattern = /^\d+(?:\.\d+)+$/;
|
||||||
|
const testKeywordPattern = /(alpha|beta|rc)/i;
|
||||||
|
|
||||||
|
const VERSION_CACHE_TTL_MS = 10 * 60 * 1000;
|
||||||
|
|
||||||
|
type ReleaseItem = {
|
||||||
|
tag_name?: string;
|
||||||
|
draft?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
type LatestVersionCacheEntry = {
|
||||||
|
value: string | null;
|
||||||
|
expiresAt: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
const latestVersionCache: Record<
|
||||||
|
UpdateReleaseChannel,
|
||||||
|
LatestVersionCacheEntry
|
||||||
|
> = {
|
||||||
|
stable: { value: null, expiresAt: 0 },
|
||||||
|
dev: { value: null, expiresAt: 0 },
|
||||||
|
};
|
||||||
|
|
||||||
|
const normalizeChannel = (
|
||||||
|
value: string | null | undefined,
|
||||||
|
): UpdateReleaseChannel => {
|
||||||
|
return value === CHANNEL_DEV ? CHANNEL_DEV : CHANNEL_STABLE;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getUpdateReleaseChannel = (): UpdateReleaseChannel => {
|
||||||
|
if (typeof window === "undefined") {
|
||||||
|
return CHANNEL_STABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return normalizeChannel(localStorage.getItem(UPDATE_CHANNEL_STORAGE_KEY));
|
||||||
|
};
|
||||||
|
|
||||||
|
export const setUpdateReleaseChannel = (
|
||||||
|
channel: UpdateReleaseChannel,
|
||||||
|
): void => {
|
||||||
|
if (typeof window === "undefined") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
localStorage.setItem(UPDATE_CHANNEL_STORAGE_KEY, normalizeChannel(channel));
|
||||||
|
window.dispatchEvent(new Event(UPDATE_CHANNEL_CHANGED_EVENT));
|
||||||
|
};
|
||||||
|
|
||||||
|
const normalizeTag = (tag: string): string => {
|
||||||
|
return tag.trim().replace(/^v/i, "");
|
||||||
|
};
|
||||||
|
|
||||||
|
type ReleaseTagChannel = UpdateReleaseChannel | null;
|
||||||
|
|
||||||
|
const releaseChannelFromTag = (tag: string): ReleaseTagChannel => {
|
||||||
|
const normalizedTag = normalizeTag(tag).toLowerCase();
|
||||||
|
|
||||||
|
if (!normalizedTag) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stableVersionPattern.test(normalizedTag)) {
|
||||||
|
return CHANNEL_STABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (testKeywordPattern.test(normalizedTag)) {
|
||||||
|
return CHANNEL_DEV;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
type VersionParts = {
|
||||||
|
numbers: number[];
|
||||||
|
stageRank: number;
|
||||||
|
stageNumber: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
const parseVersionParts = (version: string): VersionParts => {
|
||||||
|
const normalized = normalizeTag(version).toLowerCase();
|
||||||
|
const numberMatches = normalized.match(/\d+/g) || [];
|
||||||
|
const numbers = numberMatches.map((item) => Number.parseInt(item, 10));
|
||||||
|
|
||||||
|
let stageRank = 0;
|
||||||
|
|
||||||
|
if (normalized.includes("rc")) {
|
||||||
|
stageRank = 3;
|
||||||
|
} else if (normalized.includes("beta")) {
|
||||||
|
stageRank = 2;
|
||||||
|
} else if (normalized.includes("alpha")) {
|
||||||
|
stageRank = 1;
|
||||||
|
} else if (stableVersionPattern.test(normalized)) {
|
||||||
|
stageRank = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
const stageNumberMatch = normalized.match(/(?:alpha|beta|rc)[.-]?(\d+)/);
|
||||||
|
const stageNumber = stageNumberMatch
|
||||||
|
? Number.parseInt(stageNumberMatch[1], 10)
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
return {
|
||||||
|
numbers,
|
||||||
|
stageRank,
|
||||||
|
stageNumber,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const compareVersions = (left: string, right: string): number => {
|
||||||
|
const a = parseVersionParts(left);
|
||||||
|
const b = parseVersionParts(right);
|
||||||
|
const maxLength = Math.max(a.numbers.length, b.numbers.length);
|
||||||
|
|
||||||
|
for (let i = 0; i < maxLength; i += 1) {
|
||||||
|
const aValue = a.numbers[i] || 0;
|
||||||
|
const bValue = b.numbers[i] || 0;
|
||||||
|
|
||||||
|
if (aValue !== bValue) {
|
||||||
|
return aValue - bValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (a.stageRank !== b.stageRank) {
|
||||||
|
return a.stageRank - b.stageRank;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (a.stageNumber !== b.stageNumber) {
|
||||||
|
return a.stageNumber - b.stageNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
const repoPathFromUrl = (repoUrl: string): string | null => {
|
||||||
|
try {
|
||||||
|
const parsed = new URL(repoUrl);
|
||||||
|
const segments = parsed.pathname
|
||||||
|
.replace(/\.git$/i, "")
|
||||||
|
.split("/")
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
|
if (segments.length < 2) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${segments[0]}/${segments[1]}`;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getLatestVersionByChannel = async (
|
||||||
|
channel: UpdateReleaseChannel,
|
||||||
|
repoUrl: string,
|
||||||
|
): Promise<string | null> => {
|
||||||
|
const normalizedChannel = normalizeChannel(channel);
|
||||||
|
const now = Date.now();
|
||||||
|
const cached = latestVersionCache[normalizedChannel];
|
||||||
|
|
||||||
|
if (cached.value && cached.expiresAt > now) {
|
||||||
|
return cached.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
const repoPath = repoPathFromUrl(repoUrl);
|
||||||
|
|
||||||
|
if (!repoPath) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(
|
||||||
|
`https://api.github.com/repos/${repoPath}/releases?per_page=50`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Accept: "application/vnd.github+json",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const releases = (await response.json()) as ReleaseItem[];
|
||||||
|
const candidateTags = releases
|
||||||
|
.filter((release) => !release.draft && typeof release.tag_name === "string")
|
||||||
|
.map((release) => (release.tag_name || "").trim())
|
||||||
|
.filter((tag) => releaseChannelFromTag(tag) === normalizedChannel);
|
||||||
|
|
||||||
|
if (candidateTags.length === 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const latest = candidateTags.sort((a, b) => compareVersions(b, a))[0];
|
||||||
|
|
||||||
|
latestVersionCache[normalizedChannel] = {
|
||||||
|
value: latest,
|
||||||
|
expiresAt: now + VERSION_CACHE_TTL_MS,
|
||||||
|
};
|
||||||
|
|
||||||
|
return latest;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const hasVersionUpdate = (
|
||||||
|
currentVersion: string,
|
||||||
|
latestVersion: string,
|
||||||
|
): boolean => {
|
||||||
|
return (
|
||||||
|
compareVersions(normalizeTag(currentVersion), normalizeTag(latestVersion)) <
|
||||||
|
0
|
||||||
|
);
|
||||||
|
};
|
||||||
Vendored
+9
@@ -1,4 +1,5 @@
|
|||||||
/// <reference types="vite/client" />
|
/// <reference types="vite/client" />
|
||||||
|
/// <reference types="vite-plugin-pwa/client" />
|
||||||
|
|
||||||
interface ImportMetaEnv {
|
interface ImportMetaEnv {
|
||||||
readonly VITE_APP_VERSION?: string;
|
readonly VITE_APP_VERSION?: string;
|
||||||
@@ -8,3 +9,11 @@ interface ImportMetaEnv {
|
|||||||
interface ImportMeta {
|
interface ImportMeta {
|
||||||
readonly env: ImportMetaEnv;
|
readonly env: ImportMetaEnv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare module "virtual:pwa-register" {
|
||||||
|
export function registerSW(options?: {
|
||||||
|
immediate?: boolean;
|
||||||
|
onNeedRefresh?: () => void;
|
||||||
|
onOfflineReady?: () => void;
|
||||||
|
}): (reloadPage?: boolean) => Promise<void>;
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,9 +2,50 @@ import path from "path";
|
|||||||
|
|
||||||
import { defineConfig } from "vite";
|
import { defineConfig } from "vite";
|
||||||
import react from "@vitejs/plugin-react";
|
import react from "@vitejs/plugin-react";
|
||||||
|
import { VitePWA } from "vite-plugin-pwa";
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [react()],
|
plugins: [
|
||||||
|
react(),
|
||||||
|
VitePWA({
|
||||||
|
registerType: "autoUpdate",
|
||||||
|
injectRegister: "auto",
|
||||||
|
includeAssets: ["favicon.ico", "apple-touch-icon.png"],
|
||||||
|
manifest: {
|
||||||
|
name: "FLVX",
|
||||||
|
short_name: "FLVX",
|
||||||
|
description: "FLVX forwarding management panel",
|
||||||
|
theme_color: "#2563eb",
|
||||||
|
background_color: "#f6f7fb",
|
||||||
|
display: "standalone",
|
||||||
|
start_url: "/",
|
||||||
|
scope: "/",
|
||||||
|
icons: [
|
||||||
|
{
|
||||||
|
src: "pwa-192x192.png",
|
||||||
|
sizes: "192x192",
|
||||||
|
type: "image/png",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
src: "pwa-512x512.png",
|
||||||
|
sizes: "512x512",
|
||||||
|
type: "image/png",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
src: "pwa-maskable-512x512.png",
|
||||||
|
sizes: "512x512",
|
||||||
|
type: "image/png",
|
||||||
|
purpose: "maskable",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
workbox: {
|
||||||
|
navigateFallback: "/index.html",
|
||||||
|
cleanupOutdatedCaches: true,
|
||||||
|
maximumFileSizeToCacheInBytes: 5 * 1024 * 1024,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
],
|
||||||
base: "/",
|
base: "/",
|
||||||
resolve: {
|
resolve: {
|
||||||
alias: {
|
alias: {
|
||||||
|
|||||||
Reference in New Issue
Block a user