commit c84dd6ea36768d1981d79d6fcebd85a16ef6e8a2 Author: CGH0S7 <776459475@qq.com> Date: Mon Mar 16 15:54:41 2026 +0800 Phase 2 completed: Stream Server Auth, Database and Business API diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c41608 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +# 编译产物 +/server +*.exe +*.out + +# 依赖库 +/vendor/ + +# 编辑器与系统生成 +.idea/ +.vscode/ +.DS_Store \ No newline at end of file diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 0000000..51f00f1 --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "log" + + "hightube/internal/api" + "hightube/internal/db" + "hightube/internal/stream" +) + +func main() { + log.Println("Starting Hightube Server Version-1.0.2 ...") + + // Initialize Database and run auto-migrations + db.InitDB() + + // Start the API server in a goroutine so it doesn't block the RTMP server + go func() { + r := api.SetupRouter() + log.Println("[INFO] API Server is listening on :8080...") + if err := r.Run(":8080"); err != nil { + log.Fatalf("Failed to start API server: %v", err) + } + }() + + // Setup and start the RTMP server + log.Println("[INFO] Ready to receive RTMP streams from OBS.") + srv := stream.NewRTMPServer() + if err := srv.Start(":1935"); err != nil { + log.Fatalf("Failed to start RTMP server: %v", err) + } +} diff --git a/docs/PROGRESS.md b/docs/PROGRESS.md new file mode 100644 index 0000000..4d1bbd2 --- /dev/null +++ b/docs/PROGRESS.md @@ -0,0 +1,23 @@ +# Hightube 开发进度记录 + +## 2026-03-16: Phase 2 核心后端与鉴权完成 + +### 已实现功能: +1. **RTMP 流媒体服务 (Phase 1 & 2)**: + - 支持 OBS 推流鉴权(基于数据库中的 `stream_key`)。 + - 支持观众通过公开的 `room_id` 进行拉流(实现推拉路径隔离,保护主播私钥)。 + - 优化了连接断开时的日志处理。 +2. **数据库集成**: + - 使用 SQLite + GORM。 + - 实现了 `User` 和 `Room` 的数据模型与自动迁移。 +3. **业务 API (Gin)**: + - `POST /api/register`: 用户注册并自动创建直播间。 + - `POST /api/login`: JWT 鉴权登录。 + - `GET /api/room/my`: 获取个人直播间推流码。 + - `GET /api/rooms/active`: 发现正在直播的房间。 +4. **工程化**: + - 标准的 Go `cmd/internal` 目录结构。 + - 接入了 JWT 中间件进行接口保护。 + +### 待验证/待办: +- 进入 Phase 3: 开始构建 Flutter 跨平台客户端。 diff --git a/docs/PROJECT_PLAN.md b/docs/PROJECT_PLAN.md new file mode 100644 index 0000000..b8f9f28 --- /dev/null +++ b/docs/PROJECT_PLAN.md @@ -0,0 +1,74 @@ +# Hightube - 开源直播平台项目文档与实现方针 + +## 1. 项目概述 +Hightube 是一个针对网络应用开发课程大作业的跨平台开源个人直播平台。旨在提供用户注册、登录、开启个人直播室以及实时评论(后续迭代弹幕)等互动功能。 + +## 2. 技术栈架构与评估 + +### 2.1 客户端 (Flutter) +- **目标平台**: Windows, Linux, Android, Web +- **优势**: 一套代码编译多端运行,UI 表现力强且一致性好。 +- **挑战**: 桌面端和 Web 端的音视频采集(推流)和硬件加速播放(拉流)生态可能略逊于移动端,需仔细调研第三方插件的跨平台支持度(例如 `video_player`, `fvp` 等拉流插件;推流端前期可借用 OBS,后期再实现原生/插件推流)。 + +### 2.2 服务端 (Go 或 Rust) +- **推荐选型**: **Go** (Golang)。 +- **理由**: 直播服务本质是一个处理大量高并发连接和音视频协议的网络应用。Go 的 Goroutine 并发模型非常适合处理长连接(如 WebSocket 和 RTMP 推拉流)。且 Go 拥有完善的流媒体和网络编程库,可以让你专注于业务逻辑而非底层内存管理,开发周期更贴合大作业的进度。 +- **备选**: Rust。性能卓越且内存安全,是极佳的加分项,但处理复杂协议的学习曲线和开发成本较高。 + +### 2.3 数据库与存储 +- **关系型数据库**: **SQLite** +- **理由**: 作为课程大作业初期/MVP版本,SQLite 轻量且无需额外配置服务端即可运行。 +- **扩展性设计**: 直播和评论的并发写入特性,如果后续升级或实际部署,SQLite 的并发写性能可能是瓶颈。**建议在后端使用 ORM 框架(如 Gorm/SeaORM)**,这样可以利用同一套代码在未来无缝迁移至 PostgreSQL 或 MySQL。 + +## 3. 核心架构设计 + +系统将分为三个主要逻辑模块: +1. **API 服务 (业务后端)**: 处理用户认证(注册/登录/JWT)、直播间创建/配置管理、获取推流凭证(Stream Key)、以及持久化历史评论。 +2. **Media 服务 (流媒体中心)**: 核心服务。负责接收主播的音视频流(推荐使用 RTMP 协议接入),并将其分发(转封装/转码)给观众端(例如 HTTP-FLV, HLS 或 WebRTC)。初期可以将该服务与 API 服务跑在同一个进程内。 +3. **互动服务 (WebSocket)**: 为当前活跃的直播间提供低延迟的实时评论广播和弹幕分发通道。 + +--- + +## 4. 阶段性迭代计划 (Roadmap) + +本项目采用敏捷开发、逐步迭代的策略,从核心底层做起,直至完善前端交互。 + +### Phase 1: 核心流媒体服务构建 (推拉流基石) +*目标:跑通纯流媒体的“推、拉”数据通路,这是整个项目的核心难点。* +1. **技术确认**: 确定后端的开发语言与流媒体协议(首选 RTMP 接收,HTTP-FLV / HLS 分发)。 +2. **服务端搭建**: 实现 TCP 监听,能够解析基础的推流握手和音视频数据包包头(可手写核心逻辑或引入开源基础库辅助)。 +3. **推流测试**: 不写前端推流代码,直接使用第三方工具(如 OBS Studio)配置推流地址向我们的本地服务器推流。 +4. **拉流测试**: 不写前端播放器,直接使用本地播放器(如 VLC)或基于网页的测试工具(flv.js)测试拉取直播流。 + +### Phase 2: 基础业务 API 与数据库搭建 +*目标:建立平台的基本业务逻辑和数据存储,实现账号与权限控制。* +1. **数据库设计**: 引入 ORM,设计基本的 `users` (用户表) 和 `rooms` (直播间表)。 +2. **API 开发**: + - 用户注册与登录,颁发 JWT Token。 + - 获取/重置个人专属推流密钥(Stream Key)。 + - 获取当前活跃状态的直播间列表。 +3. **安全鉴权**: 改造 Phase 1 的流媒体服务,在接收推流时校验推流地址中的 Stream Key 是否合法并对应正确的用户房间。 + +### Phase 3: 客户端 MVP 版本构建 (观看端) +*目标:用户可以使用我们自己的 Flutter 客户端进行注册并观看直播。* +1. **Flutter 框架初始化**: 搭建跨端项目架构,配置网络请求拦截器(携带 Token)。 +2. **基础 UI 开发**: + - 登录与注册页面。 + - 平台首页(大厅),展示通过 API 获取的直播列表卡片。 + - 直播间详情页。 +3. **播放器集成**: 引入并在四个平台上跑通 Flutter 视频播放插件,实现在直播间页面动态加载 HTTP-FLV 或 HLS 流媒体链接。 + +### Phase 4: 实时互动服务 (评论系统) +*目标:增加平台的社交互动性。* +1. **后端 WebSocket 服务**: 建立基于 `room_id` 的 WebSocket 广播群组(Hub 模式)。 +2. **前后端联调**: 客户端进入直播间后自动连接 WebSocket 服务,实现收发消息。 +3. **弹幕预演**: 客户端除了在底部展示聊天列表外,开始尝试利用 Flutter 的 `Stack` 布局机制,实现简单的文字横向漂移弹幕层。 + +### Phase 5: 客户端开播体验 (完全体) +*目标:摆脱 OBS,使用我们的应用自行调用软硬件资源进行推流开播。* +1. **开播 UI**: 客户端新增“主播控制台”界面。 +2. **多端推流探究**: 调研 Flutter 中的音视频采集和推流插件(如 `flutter_webrtc` 或相机结合底层编码推流库)。 +3. **整体整合与测试**: 对四大平台进行兼容性测试、性能调优和 UI 美化,准备课程答辩演示。 + +--- +*本文档将作为整个大作业项目的开发基准与进度追踪参考。* \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9054442 --- /dev/null +++ b/go.mod @@ -0,0 +1,44 @@ +module hightube + +go 1.26.1 + +require github.com/nareix/joy4 v0.0.0-20200507095837-05a4ffbb5369 + +require ( + github.com/bytedance/gopkg v0.1.3 // indirect + github.com/bytedance/sonic v1.15.0 // indirect + github.com/bytedance/sonic/loader v0.5.0 // indirect + github.com/cloudwego/base64x v0.1.6 // indirect + github.com/gabriel-vasile/mimetype v1.4.12 // indirect + github.com/gin-contrib/sse v1.1.0 // indirect + github.com/gin-gonic/gin v1.12.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.30.1 // indirect + github.com/goccy/go-json v0.10.5 // indirect + github.com/goccy/go-yaml v1.19.2 // indirect + github.com/golang-jwt/jwt/v5 v5.3.1 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.3.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-sqlite3 v1.14.22 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.4 // indirect + github.com/quic-go/qpack v0.6.0 // indirect + github.com/quic-go/quic-go v0.59.0 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.3.1 // indirect + go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect + golang.org/x/arch v0.22.0 // indirect + golang.org/x/crypto v0.48.0 // indirect + golang.org/x/net v0.51.0 // indirect + golang.org/x/sys v0.41.0 // indirect + golang.org/x/text v0.34.0 // indirect + google.golang.org/protobuf v1.36.10 // indirect + gorm.io/driver/sqlite v1.6.0 // indirect + gorm.io/gorm v1.30.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..37eebae --- /dev/null +++ b/go.sum @@ -0,0 +1,92 @@ +github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M= +github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM= +github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE= +github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k= +github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE= +github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo= +github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M= +github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= +github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= +github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= +github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= +github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8= +github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.30.1 h1:f3zDSN/zOma+w6+1Wswgd9fLkdwy06ntQJp0BBvFG0w= +github.com/go-playground/validator/v10 v10.30.1/go.mod h1:oSuBIQzuJxL//3MelwSLD5hc2Tu889bF0Idm9Dg26cM= +github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM= +github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= +github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY= +github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= +github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/nareix/joy4 v0.0.0-20200507095837-05a4ffbb5369 h1:Yp0zFEufLz0H7jzffb4UPXijavlyqlYeOg7dcyVUNnQ= +github.com/nareix/joy4 v0.0.0-20200507095837-05a4ffbb5369/go.mod h1:aFJ1ZwLjvHN4yEzE5Bkz8rD8/d8Vlj3UIuvz2yfET7I= +github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= +github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= +github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII= +github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw= +github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY= +github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= +go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE= +go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0= +golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI= +golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A= +golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts= +golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos= +golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo= +golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k= +golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk= +golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA= +google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= +google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ= +gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8= +gorm.io/gorm v1.30.0 h1:qbT5aPv1UH8gI99OsRlvDToLxW5zR7FzS9acZDOZcgs= +gorm.io/gorm v1.30.0/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE= diff --git a/hightube.db b/hightube.db new file mode 100644 index 0000000..3d621ff Binary files /dev/null and b/hightube.db differ diff --git a/internal/api/auth.go b/internal/api/auth.go new file mode 100644 index 0000000..5aeea03 --- /dev/null +++ b/internal/api/auth.go @@ -0,0 +1,96 @@ +package api + +import ( + "net/http" + + "github.com/gin-gonic/gin" + + "hightube/internal/db" + "hightube/internal/model" + "hightube/internal/utils" +) + +type RegisterRequest struct { + Username string `json:"username" binding:"required"` + Password string `json:"password" binding:"required"` +} + +type LoginRequest struct { + Username string `json:"username" binding:"required"` + Password string `json:"password" binding:"required"` +} + +func Register(c *gin.Context) { + var req RegisterRequest + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + // Check if user exists + var existingUser model.User + if err := db.DB.Where("username = ?", req.Username).First(&existingUser).Error; err == nil { + c.JSON(http.StatusConflict, gin.H{"error": "Username already exists"}) + return + } + + // Hash password + hashedPassword, err := utils.HashPassword(req.Password) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to hash password"}) + return + } + + // Create user + user := model.User{ + Username: req.Username, + Password: hashedPassword, + } + if err := db.DB.Create(&user).Error; err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create user"}) + return + } + + // Create a default live room for the new user + room := model.Room{ + UserID: user.ID, + Title: user.Username + "'s Live Room", + StreamKey: utils.GenerateStreamKey(), + IsActive: false, + } + if err := db.DB.Create(&room).Error; err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create room for user"}) + return + } + + c.JSON(http.StatusCreated, gin.H{"message": "User registered successfully", "user_id": user.ID}) +} + +func Login(c *gin.Context) { + var req LoginRequest + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + var user model.User + if err := db.DB.Where("username = ?", req.Username).First(&user).Error; err != nil { + c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid username or password"}) + return + } + + if !utils.CheckPasswordHash(req.Password, user.Password) { + c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid username or password"}) + return + } + + token, err := utils.GenerateToken(user.ID) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate token"}) + return + } + + c.JSON(http.StatusOK, gin.H{ + "token": token, + }) +} diff --git a/internal/api/middleware.go b/internal/api/middleware.go new file mode 100644 index 0000000..e0b3791 --- /dev/null +++ b/internal/api/middleware.go @@ -0,0 +1,42 @@ +package api + +import ( + "net/http" + "strconv" + "strings" + + "github.com/gin-gonic/gin" + + "hightube/internal/utils" +) + +// AuthMiddleware intercepts requests, validates JWT, and injects user_id into context +func AuthMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + authHeader := c.GetHeader("Authorization") + if authHeader == "" { + c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header is required"}) + c.Abort() + return + } + + parts := strings.Split(authHeader, " ") + if len(parts) != 2 || parts[0] != "Bearer" { + c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header format must be Bearer {token}"}) + c.Abort() + return + } + + tokenStr := parts[1] + userIDStr, err := utils.ParseToken(tokenStr) + if err != nil { + c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid or expired token"}) + c.Abort() + return + } + + userID, _ := strconv.ParseUint(userIDStr, 10, 32) + c.Set("user_id", uint(userID)) + c.Next() + } +} diff --git a/internal/api/room.go b/internal/api/room.go new file mode 100644 index 0000000..0a3ce92 --- /dev/null +++ b/internal/api/room.go @@ -0,0 +1,50 @@ +package api + +import ( + "net/http" + + "github.com/gin-gonic/gin" + + "hightube/internal/db" + "hightube/internal/model" +) + +// GetMyRoom returns the room details for the currently authenticated user +func GetMyRoom(c *gin.Context) { + userID, _ := c.Get("user_id") + + var room model.Room + if err := db.DB.Where("user_id = ?", userID).First(&room).Error; err != nil { + c.JSON(http.StatusNotFound, gin.H{"error": "Room not found"}) + return + } + + c.JSON(http.StatusOK, gin.H{ + "room_id": room.ID, + "title": room.Title, + "stream_key": room.StreamKey, + "is_active": room.IsActive, + }) +} + +// GetActiveRooms returns a list of all currently active live rooms +func GetActiveRooms(c *gin.Context) { + var rooms []model.Room + // Fetch rooms where is_active is true + if err := db.DB.Where("is_active = ?", true).Find(&rooms).Error; err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch active rooms"}) + return + } + + // Return safe information (do not leak stream keys) + var result []map[string]interface{} + for _, r := range rooms { + result = append(result, map[string]interface{}{ + "room_id": r.ID, + "title": r.Title, + "user_id": r.UserID, + }) + } + + c.JSON(http.StatusOK, gin.H{"active_rooms": result}) +} diff --git a/internal/api/router.go b/internal/api/router.go new file mode 100644 index 0000000..e2cda90 --- /dev/null +++ b/internal/api/router.go @@ -0,0 +1,30 @@ +package api + +import ( + "github.com/gin-gonic/gin" +) + +// SetupRouter configures the Gin router and defines API endpoints +func SetupRouter() *gin.Engine { + // 设置为发布模式,消除 "[WARNING] Running in debug mode" 警告 + gin.SetMode(gin.ReleaseMode) + + r := gin.Default() + + // 清除代理信任警告 "[WARNING] You trusted all proxies" + r.SetTrustedProxies(nil) + + // Public routes + r.POST("/api/register", Register) + r.POST("/api/login", Login) + r.GET("/api/rooms/active", GetActiveRooms) + + // Protected routes (require JWT) + authGroup := r.Group("/api") + authGroup.Use(AuthMiddleware()) + { + authGroup.GET("/room/my", GetMyRoom) + } + + return r +} diff --git a/internal/db/db.go b/internal/db/db.go new file mode 100644 index 0000000..17c69b9 --- /dev/null +++ b/internal/db/db.go @@ -0,0 +1,45 @@ +package db + +import ( + "log" + "os" + "time" + + "gorm.io/driver/sqlite" + "gorm.io/gorm" + "gorm.io/gorm/logger" + + "hightube/internal/model" +) + +var DB *gorm.DB + +// InitDB initializes the SQLite database connection and auto-migrates models. +func InitDB() { + newLogger := logger.New( + log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer + logger.Config{ + SlowThreshold: time.Second, // Slow SQL threshold + LogLevel: logger.Warn, // Log level + IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger + Colorful: true, // Disable color + }, + ) + + var err error + // Use SQLite database stored in a local file named "hightube.db" + DB, err = gorm.Open(sqlite.Open("hightube.db"), &gorm.Config{ + Logger: newLogger, + }) + if err != nil { + log.Fatalf("Failed to connect database: %v", err) + } + + // Auto-migrate the schema + err = DB.AutoMigrate(&model.User{}, &model.Room{}) + if err != nil { + log.Fatalf("Failed to migrate database: %v", err) + } + + log.Println("Database initialized successfully.") +} diff --git a/internal/model/room.go b/internal/model/room.go new file mode 100644 index 0000000..51cdbb8 --- /dev/null +++ b/internal/model/room.go @@ -0,0 +1,14 @@ +package model + +import ( + "gorm.io/gorm" +) + +// Room represents a user's personal live streaming room. +type Room struct { + gorm.Model + UserID uint `gorm:"uniqueIndex;not null"` + Title string `gorm:"default:'My Live Room'"` + StreamKey string `gorm:"uniqueIndex;not null"` // Secret key for OBS streaming + IsActive bool `gorm:"default:false"` // Whether the stream is currently active +} diff --git a/internal/model/user.go b/internal/model/user.go new file mode 100644 index 0000000..73b50fa --- /dev/null +++ b/internal/model/user.go @@ -0,0 +1,12 @@ +package model + +import ( + "gorm.io/gorm" +) + +// User represents a registered user in the system. +type User struct { + gorm.Model + Username string `gorm:"uniqueIndex;not null"` + Password string `gorm:"not null"` // Hashed password +} diff --git a/internal/stream/server.go b/internal/stream/server.go new file mode 100644 index 0000000..4e0a2b7 --- /dev/null +++ b/internal/stream/server.go @@ -0,0 +1,136 @@ +package stream + +import ( + "fmt" + "io" + "strings" + "sync" + + "github.com/nareix/joy4/av/avutil" + "github.com/nareix/joy4/av/pubsub" + "github.com/nareix/joy4/format" + "github.com/nareix/joy4/format/rtmp" + + "hightube/internal/db" + "hightube/internal/model" +) + +func init() { + // Register all supported audio/video formats + format.RegisterAll() +} + +// RTMPServer manages all active live streams +type RTMPServer struct { + server *rtmp.Server + channels map[string]*pubsub.Queue + mutex sync.RWMutex +} + +// NewRTMPServer creates and initializes a new media server +func NewRTMPServer() *RTMPServer { + s := &RTMPServer{ + channels: make(map[string]*pubsub.Queue), + server: &rtmp.Server{}, + } + + // Triggered when a broadcaster (e.g., OBS) starts publishing + s.server.HandlePublish = func(conn *rtmp.Conn) { + streamPath := conn.URL.Path // Expected format: /live/{stream_key} + fmt.Printf("[INFO] OBS is attempting to publish to: %s\n", streamPath) + + // Extract stream key from path + parts := strings.Split(streamPath, "/") + if len(parts) < 3 || parts[1] != "live" { + fmt.Printf("[WARN] Invalid publish path format: %s\n", streamPath) + return + } + streamKey := parts[2] + + // Authenticate stream key + var room model.Room + if err := db.DB.Where("stream_key = ?", streamKey).First(&room).Error; err != nil { + fmt.Printf("[WARN] Authentication failed, invalid stream key: %s\n", streamKey) + return // Reject connection + } + + fmt.Printf("[INFO] Stream authenticated for Room ID: %d\n", room.ID) + + // 1. Get audio/video stream metadata + streams, err := conn.Streams() + if err != nil { + fmt.Printf("[ERROR] Failed to parse stream headers: %v\n", err) + return + } + + // 2. Map the active stream by Room ID so viewers can use /live/{room_id} + roomLivePath := fmt.Sprintf("/live/%d", room.ID) + + s.mutex.Lock() + q := pubsub.NewQueue() + q.WriteHeader(streams) + s.channels[roomLivePath] = q + s.mutex.Unlock() + + // Mark room as active in DB + db.DB.Model(&room).Update("is_active", true) + + // 3. Cleanup on end + defer func() { + s.mutex.Lock() + delete(s.channels, roomLivePath) + s.mutex.Unlock() + q.Close() + db.DB.Model(&room).Update("is_active", false) // Mark room as inactive + fmt.Printf("[INFO] Publishing ended for Room ID: %d\n", room.ID) + }() + + // 4. Continuously copy data packets to our broadcast queue + avutil.CopyPackets(q, conn) + } + + // Triggered when a viewer (e.g., VLC) requests playback + s.server.HandlePlay = func(conn *rtmp.Conn) { + streamPath := conn.URL.Path // Expected format: /live/{room_id} + fmt.Printf("[INFO] VLC is pulling stream from: %s\n", streamPath) + + // 1. Look for the requested room's data queue + s.mutex.RLock() + q, ok := s.channels[streamPath] + s.mutex.RUnlock() + + if !ok { + fmt.Printf("[WARN] Stream not found or inactive: %s\n", streamPath) + return + } + + // 2. Get the cursor from the latest position and notify client of stream format + cursor := q.Latest() + streams, _ := cursor.Streams() + conn.WriteHeader(streams) + + // 3. Cleanup on end + defer fmt.Printf("[INFO] Playback ended: %s\n", streamPath) + + // 4. Continuously copy data packets to the viewer + err := avutil.CopyPackets(conn, cursor) + if err != nil && err != io.EOF { + // 如果是客户端主动断开连接引起的错误,不将其作为严重错误打印 + errStr := err.Error() + if strings.Contains(errStr, "broken pipe") || strings.Contains(errStr, "connection reset by peer") { + fmt.Printf("[INFO] Viewer disconnected normally: %s\n", streamPath) + } else { + fmt.Printf("[ERROR] Error occurred during playback: %v\n", err) + } + } + } + + return s +} + +// Start launches the RTMP server +func (s *RTMPServer) Start(addr string) error { + s.server.Addr = addr + fmt.Printf("[INFO] RTMP Server is listening on %s...\n", addr) + return s.server.ListenAndServe() +} diff --git a/internal/utils/utils.go b/internal/utils/utils.go new file mode 100644 index 0000000..f6f8bd5 --- /dev/null +++ b/internal/utils/utils.go @@ -0,0 +1,55 @@ +package utils + +import ( + "crypto/rand" + "encoding/hex" + "fmt" + "time" + + "github.com/golang-jwt/jwt/v5" + "golang.org/x/crypto/bcrypt" +) + +// In production, load this from environment variables +var jwtKey = []byte("hightube_super_secret_key_MVP_only") + +// GenerateToken generates a JWT token for a given user ID +func GenerateToken(userID uint) (string, error) { + claims := &jwt.RegisteredClaims{ + Subject: fmt.Sprintf("%d", userID), + ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)), + } + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) + return token.SignedString(jwtKey) +} + +// ParseToken parses the JWT string and returns the user ID (Subject) +func ParseToken(tokenStr string) (string, error) { + claims := &jwt.RegisteredClaims{} + token, err := jwt.ParseWithClaims(tokenStr, claims, func(t *jwt.Token) (interface{}, error) { + return jwtKey, nil + }) + if err != nil || !token.Valid { + return "", err + } + return claims.Subject, nil +} + +// HashPassword creates a bcrypt hash of the password +func HashPassword(password string) (string, error) { + bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14) + return string(bytes), err +} + +// CheckPasswordHash compares a bcrypt hashed password with its possible plaintext equivalent +func CheckPasswordHash(password, hash string) bool { + err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) + return err == nil +} + +// GenerateStreamKey generates a random string to be used as a stream key +func GenerateStreamKey() string { + bytes := make([]byte, 16) + rand.Read(bytes) + return hex.EncodeToString(bytes) +}