Files
16gagent/game-accelerator-tech-plan/13-源码级设计.md
T

20 KiB

第十三部分:源码级设计

13.1 项目目录结构

game-accelerator/
├── README.md
├── LICENSE
├── Makefile
├── docker-compose.yml
├── .github/
│   └── workflows/
│       ├── ci.yml
│       ├── release.yml
│       └── deploy.yml
│
├── docs/                          # 文档
│   ├── architecture.md
│   ├── api.md
│   ├── deployment.md
│   └── contributing.md
│
├── proto/                         # Protobuf定义
│   ├── user.proto
│   ├── node.proto
│   ├── dispatch.proto
│   ├── game.proto
│   └── common.proto
│
├── client/                        # 客户端
│   ├── windows/                   # Windows客户端
│   │   ├── CMakeLists.txt
│   │   ├── src/
│   │   │   ├── main.cpp
│   │   │   ├── core/
│   │   │   │   ├── tunnel.h/cpp         # 隧道模块
│   │   │   │   ├── protocol.h/cpp       # 协议处理
│   │   │   │   ├── crypto.h/cpp         # 加密模块
│   │   │   │   ├── session.h/cpp        # 会话管理
│   │   │   │   └── config.h/cpp         # 配置管理
│   │   │   ├── capture/
│   │   │   │   ├── wfp_driver.h/cpp     # WFP驱动交互
│   │   │   │   ├── traffic_filter.h/cpp # 流量过滤
│   │   │   │   └── game_detect.h/cpp    # 游戏检测
│   │   │   ├── network/
│   │   │   │   ├── udp_relay.h/cpp      # UDP转发
│   │   │   │   ├── quic_tunnel.h/cpp    # QUIC隧道
│   │   │   │   ├── fec.h/cpp            # 前向纠错
│   │   │   │   └── multipath.h/cpp      # 多路径
│   │   │   ├── scheduler/
│   │   │   │   ├── node_selector.h/cpp  # 节点选择
│   │   │   │   ├── speed_test.h/cpp     # 测速
│   │   │   │   └── auto_switch.h/cpp    # 自动切换
│   │   │   └── ui/
│   │   │       ├── main_window.h/cpp    # 主窗口
│   │   │       ├── login_dialog.h/cpp   # 登录对话框
│   │   │       ├── game_list.h/cpp      # 游戏列表
│   │   │       └── settings.h/cpp       # 设置页面
│   │   ├── driver/                  # WFP驱动
│   │   │   ├── driver.c
│   │   │   ├── callout.c
│   │   │   ├── filter.c
│   │   │   └── utils.c
│   │   └── resources/
│   │       ├── icons/
│   │       └── strings/
│   │
│   ├── macos/                     # macOS客户端
│   │   ├── Package.swift
│   │   ├── Sources/
│   │   │   ├── Accelerator/
│   │   │   │   ├── Core/
│   │   │   │   ├── Network/
│   │   │   │   └── UI/
│   │   │   └── NetworkExtension/
│   │   └── Tests/
│   │
│   ├── android/                   # Android客户端
│   │   ├── build.gradle.kts
│   │   ├── app/
│   │   │   └── src/
│   │   │       └── main/
│   │   │           ├── java/
│   │   │           │   └── com/accelerator/
│   │   │           │       ├── core/
│   │   │           │       ├── vpn/
│   │   │           │       ├── ui/
│   │   │           │       └── service/
│   │   │           └── res/
│   │   └── build.gradle.kts
│   │
│   └── ios/                       # iOS客户端
│       ├── Accelerator.xcodeproj
│       ├── Accelerator/
│       │   ├── Core/
│       │   ├── Network/
│       │   └── UI/
│       └── PacketTunnel/
│
├── server/                        # 服务端
│   ├── go.mod
│   ├── go.sum
│   ├── cmd/                       # 入口
│   │   ├── api-gateway/           # API网关
│   │   │   └── main.go
│   │   ├── user-service/          # 用户服务
│   │   │   └── main.go
│   │   ├── order-service/         # 订单服务
│   │   │   └── main.go
│   │   ├── node-service/          # 节点服务
│   │   │   └── main.go
│   │   ├── dispatch-service/      # 调度服务
│   │   │   └── main.go
│   │   ├── game-service/          # 游戏服务
│   │   │   └── main.go
│   │   ├── relay-service/         # 转发服务
│   │   │   └── main.go
│   │   └── monitor-service/       # 监控服务
│   │       └── main.go
│   │
│   ├── internal/                  # 内部包
│   │   ├── model/                 # 数据模型
│   │   │   ├── user.go
│   │   │   ├── node.go
│   │   │   ├── order.go
│   │   │   ├── game.go
│   │   │   └── session.go
│   │   ├── repository/            # 数据访问层
│   │   │   ├── mysql/
│   │   │   │   ├── user_repo.go
│   │   │   │   ├── node_repo.go
│   │   │   │   └── order_repo.go
│   │   │   └── redis/
│   │   │       ├── cache.go
│   │   │       └── session.go
│   │   ├── service/               # 业务逻辑层
│   │   │   ├── auth/
│   │   │   │   ├── auth.go
│   │   │   │   ├── jwt.go
│   │   │   │   └── password.go
│   │   │   ├── user/
│   │   │   │   ├── user.go
│   │   │   │   └── device.go
│   │   │   ├── node/
│   │   │   │   ├── node.go
│   │   │   │   ├── health.go
│   │   │   │   └── discovery.go
│   │   │   ├── dispatch/
│   │   │   │   ├── scheduler.go
│   │   │   │   ├── scorer.go
│   │   │   │   └── router.go
│   │   │   ├── order/
│   │   │   │   ├── order.go
│   │   │   │   └── payment.go
│   │   │   └── relay/
│   │   │       ├── udp_relay.go
│   │   │       ├── quic_tunnel.go
│   │   │       └── fec.go
│   │   ├── handler/               # HTTP/RPC处理器
│   │   │   ├── http/
│   │   │   │   ├── user_handler.go
│   │   │   │   ├── node_handler.go
│   │   │   │   └── order_handler.go
│   │   │   └── grpc/
│   │   │       ├── dispatch_handler.go
│   │   │       └── node_handler.go
│   │   ├── middleware/            # 中间件
│   │   │   ├── auth.go
│   │   │   ├── rate_limit.go
│   │   │   ├── cors.go
│   │   │   └── logger.go
│   │   ├── config/                # 配置
│   │   │   └── config.go
│   │   └── pkg/                   # 公共包
│   │       ├── crypto/
│   │       │   ├── aes.go
│   │       │   └── ecdh.go
│   │       ├── network/
│   │       │   ├── bbr.go
│   │       │   └── fec.go
│   │       └── utils/
│   │           ├── ip.go
│   │           └── geo.go
│   │
│   └── migrations/                # 数据库迁移
│       ├── 001_users.up.sql
│       ├── 001_users.down.sql
│       ├── 002_nodes.up.sql
│       └── ...
│
├── admin/                         # 管理后台
│   ├── package.json
│   ├── tsconfig.json
│   ├── vite.config.ts
│   ├── src/
│   │   ├── App.tsx
│   │   ├── main.tsx
│   │   ├── pages/
│   │   │   ├── Dashboard/
│   │   │   ├── Users/
│   │   │   ├── Nodes/
│   │   │   ├── Games/
│   │   │   ├── Orders/
│   │   │   ├── Monitoring/
│   │   │   └── Settings/
│   │   ├── components/
│   │   │   ├── Layout/
│   │   │   ├── Charts/
│   │   │   └── Common/
│   │   ├── services/
│   │   │   ├── api.ts
│   │   │   ├── user.ts
│   │   │   ├── node.ts
│   │   │   └── order.ts
│   │   ├── stores/
│   │   │   ├── auth.ts
│   │   │   └── app.ts
│   │   └── utils/
│   │       ├── request.ts
│   │       └── auth.ts
│   └── public/
│
├── deploy/                        # 部署配置
│   ├── docker/
│   │   ├── Dockerfile.api-gateway
│   │   ├── Dockerfile.user-service
│   │   ├── Dockerfile.relay-service
│   │   └── ...
│   ├── k8s/
│   │   ├── namespace.yaml
│   │   ├── configmap.yaml
│   │   ├── secrets.yaml
│   │   ├── deployments/
│   │   │   ├── api-gateway.yaml
│   │   │   ├── user-service.yaml
│   │   │   └── ...
│   │   ├── services/
│   │   └── ingress/
│   ├── terraform/
│   │   ├── main.tf
│   │   ├── variables.tf
│   │   ├── outputs.tf
│   │   └── modules/
│   │       ├── alicloud/
│   │       ├── aws/
│   │       └── gcp/
│   └── ansible/
│       ├── playbooks/
│       └── roles/
│
├── scripts/                       # 脚本
│   ├── build.sh
│   ├── deploy.sh
│   ├── test.sh
│   └── seed_data.sh
│
└── test/                          # 测试
    ├── unit/
    ├── integration/
    ├── e2e/
    └── performance/

13.2 核心模块接口定义

13.2.1 用户服务接口

// server/internal/service/user/user.go
package user

import (
    "context"
    "time"
)

// Service 用户服务接口
type Service interface {
    // Register 用户注册
    Register(ctx context.Context, req *RegisterRequest) (*RegisterResponse, error)
    
    // Login 用户登录
    Login(ctx context.Context, req *LoginRequest) (*LoginResponse, error)
    
    // Logout 用户登出
    Logout(ctx context.Context, userID uint64) error
    
    // RefreshToken 刷新Token
    RefreshToken(ctx context.Context, refreshToken string) (*TokenPair, error)
    
    // GetProfile 获取用户信息
    GetProfile(ctx context.Context, userID uint64) (*UserProfile, error)
    
    // UpdateProfile 更新用户信息
    UpdateProfile(ctx context.Context, userID uint64, req *UpdateProfileRequest) error
    
    // ChangePassword 修改密码
    ChangePassword(ctx context.Context, userID uint64, req *ChangePasswordRequest) error
    
    // BindDevice 绑定设备
    BindDevice(ctx context.Context, userID uint64, device *DeviceInfo) error
    
    // UnbindDevice 解绑设备
    UnbindDevice(ctx context.Context, userID uint64, deviceID string) error
    
    // GetDevices 获取设备列表
    GetDevices(ctx context.Context, userID uint64) ([]*DeviceInfo, error)
}

// RegisterRequest 注册请求
type RegisterRequest struct {
    Phone    string `json:"phone" validate:"required"`
    Password string `json:"password" validate:"required,min=8"`
    Code     string `json:"code" validate:"required"` // 验证码
}

// LoginRequest 登录请求
type LoginRequest struct {
    Phone    string `json:"phone"`
    Email    string `json:"email"`
    Password string `json:"password" validate:"required"`
    DeviceID string `json:"device_id" validate:"required"`
}

// LoginResponse 登录响应
type LoginResponse struct {
    AccessToken  string       `json:"access_token"`
    RefreshToken string       `json:"refresh_token"`
    ExpiresIn    int64        `json:"expires_in"`
    User         *UserProfile `json:"user"`
}

// TokenPair Token对
type TokenPair struct {
    AccessToken  string `json:"access_token"`
    RefreshToken string `json:"refresh_token"`
    ExpiresIn    int64  `json:"expires_in"`
}

// UserProfile 用户信息
type UserProfile struct {
    ID        uint64    `json:"id"`
    UUID      string    `json:"uuid"`
    Phone     string    `json:"phone"`
    Email     string    `json:"email"`
    Nickname  string    `json:"nickname"`
    Avatar    string    `json:"avatar"`
    Status    int       `json:"status"`
    CreatedAt time.Time `json:"created_at"`
}

// DeviceInfo 设备信息
type DeviceInfo struct {
    DeviceID   string    `json:"device_id"`
    DeviceName string    `json:"device_name"`
    DeviceType string    `json:"device_type"`
    OSVersion  string    `json:"os_version"`
    LastActive time.Time `json:"last_active"`
}

13.2.2 调度服务接口

// server/internal/service/dispatch/dispatch.go
package dispatch

import (
    "context"
)

// Service 调度服务接口
type Service interface {
    // GetBestNode 获取最优节点
    GetBestNode(ctx context.Context, req *DispatchRequest) (*NodeInfo, error)
    
    // GetNodeList 获取节点列表
    GetNodeList(ctx context.Context, req *NodeListRequest) (*NodeListResponse, error)
    
    // ReportQuality 上报链路质量
    ReportQuality(ctx context.Context, report *QualityReport) error
    
    // GetRoute 获取路由路径
    GetRoute(ctx context.Context, req *RouteRequest) (*RouteInfo, error)
    
    // SwitchNode 切换节点
    SwitchNode(ctx context.Context, sessionID string, newNodeID string) error
}

// DispatchRequest 调度请求
type DispatchRequest struct {
    UserID    uint64  `json:"user_id"`
    GameID    string  `json:"game_id"`
    ServerIP  string  `json:"server_ip"`
    UserIP    string  `json:"user_ip"`
    UserISP   string  `json:"user_isp"`
    UserCity  string  `json:"user_city"`
    Protocol  string  `json:"protocol"`
}

// NodeInfo 节点信息
type NodeInfo struct {
    NodeID      string  `json:"node_id"`
    Name        string  `json:"name"`
    IP          string  `json:"ip"`
    Port        int     `json:"port"`
    Region      string  `json:"region"`
    City        string  `json:"city"`
    ISP         string  `json:"isp"`
    Score       float64 `json:"score"`
    RTT         float64 `json:"rtt"`
    LossRate    float64 `json:"loss_rate"`
    LoadRate    float64 `json:"load_rate"`
    Protocol    string  `json:"protocol"`
}

// QualityReport 质量报告
type QualityReport struct {
    UserID    uint64  `json:"user_id"`
    SessionID string  `json:"session_id"`
    NodeID    string  `json:"node_id"`
    RTT       float64 `json:"rtt"`
    LossRate  float64 `json:"loss_rate"`
    Jitter    float64 `json:"jitter"`
    Timestamp int64   `json:"timestamp"`
}

// RouteInfo 路由信息
type RouteInfo struct {
    AccessNode  *NodeInfo   `json:"access_node"`
    RelayNodes  []*NodeInfo `json:"relay_nodes"`
    ExitNode    *NodeInfo   `json:"exit_node"`
    Protocol    string      `json:"protocol"`
    TotalRTT    float64     `json:"total_rtt"`
}

13.2.3 RPC定义

// proto/dispatch.proto
syntax = "proto3";

package dispatch;

option go_package = "github.com/accelerator/proto/dispatch";

// 调度服务
service DispatchService {
  // 获取最优节点
  rpc GetBestNode (DispatchRequest) returns (DispatchResponse);
  
  // 获取节点列表
  rpc GetNodeList (NodeListRequest) returns (NodeListResponse);
  
  // 上报质量
  rpc ReportQuality (QualityReport) returns (QualityReportResponse);
  
  // 获取路由
  rpc GetRoute (RouteRequest) returns (RouteResponse);
  
  // 切换节点
  rpc SwitchNode (SwitchNodeRequest) returns (SwitchNodeResponse);
}

message DispatchRequest {
  uint64 user_id = 1;
  string game_id = 2;
  string server_ip = 3;
  string user_ip = 4;
  string user_isp = 5;
  string user_city = 6;
  string protocol = 7;
}

message DispatchResponse {
  NodeInfo node = 1;
  repeated NodeInfo backup_nodes = 2;
  RouteInfo route = 3;
}

message NodeInfo {
  string node_id = 1;
  string name = 2;
  string ip = 3;
  int32 port = 4;
  string region = 5;
  string city = 6;
  string isp = 7;
  double score = 8;
  double rtt = 9;
  double loss_rate = 10;
  double load_rate = 11;
}

message RouteInfo {
  NodeInfo access_node = 1;
  repeated NodeInfo relay_nodes = 2;
  NodeInfo exit_node = 3;
  string protocol = 4;
  double total_rtt = 5;
}

message QualityReport {
  uint64 user_id = 1;
  string session_id = 2;
  string node_id = 3;
  double rtt = 4;
  double loss_rate = 5;
  double jitter = 6;
  int64 timestamp = 7;
}

message QualityReportResponse {
  bool success = 1;
}

message NodeListRequest {
  string region = 1;
  string game_id = 2;
  int32 page = 3;
  int32 page_size = 4;
}

message NodeListResponse {
  repeated NodeInfo nodes = 1;
  int32 total = 2;
}

message SwitchNodeRequest {
  string session_id = 1;
  string new_node_id = 2;
}

message SwitchNodeResponse {
  bool success = 1;
  NodeInfo new_node = 2;
}

13.3 消息队列设计

┌──────────────────────────────────────────────────────────────────┐
│                    Kafka Topic 设计                                │
├──────────────────────────────────────────────────────────────────┤
│                                                                  │
│  Topic列表:                                                     │
│  ┌────────────────────┬──────────┬──────────────────────────┐   │
│  │ Topic              │ 分区数   │ 说明                      │   │
│  ├────────────────────┼──────────┼──────────────────────────┤   │
│  │ user.events        │ 6        │ 用户事件(登录/注册等)   │   │
│  │ order.events       │ 6        │ 订单事件(创建/支付等)   │   │
│  │ session.events     │ 12       │ 会话事件(开始/结束等)   │   │
│  │ quality.reports    │ 12       │ 链路质量报告              │   │
│  │ node.events        │ 6        │ 节点事件(上线/下线等)   │   │
│  │ risk.events        │ 3        │ 风控事件                  │   │
│  │ log.events         │ 12       │ 日志事件                  │   │
│  └────────────────────┴──────────┴──────────────────────────┘   │
│                                                                  │
│  消息格式(JSON):                                              │
│  {                                                               │
│    "event_id": "uuid",                                          │
│    "event_type": "user.login",                                  │
│    "timestamp": 1717651200,                                     │
│    "source": "user-service",                                    │
│    "data": { ... }                                              │
│  }                                                               │
│                                                                  │
│  消费者组:                                                      │
│  - user-service-group: 消费user.events                          │
│  - order-service-group: 消费order.events                        │
│  - monitor-group: 消费所有事件(监控用)                         │
│  - analytics-group: 消费所有事件(分析用)                       │
│                                                                  │
└──────────────────────────────────────────────────────────────────┘