Skip to content

Commit 03d9f37

Browse files
author
shijiashuai
committed
feat: update README, signal hub, and add project infrastructure
1 parent 31f28d8 commit 03d9f37

6 files changed

Lines changed: 100 additions & 23 deletions

File tree

CONTRIBUTING.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Contributing
2+
3+
感谢你对本项目的关注!欢迎通过 Issue 和 Pull Request 参与贡献。
4+
5+
## 开发流程
6+
7+
1. Fork 本仓库
8+
2. 创建特性分支:`git checkout -b feature/your-feature`
9+
3. 提交更改:`git commit -m "feat: add your feature"`
10+
4. 推送分支:`git push origin feature/your-feature`
11+
5. 创建 Pull Request
12+
13+
## 开发与测试
14+
15+
```bash
16+
go mod tidy
17+
go run ./cmd/server
18+
```
19+
20+
## 代码规范
21+
22+
- Go 代码遵循 `gofmt``go vet` 规范
23+
- 使用 `.editorconfig` 中定义的缩进和格式规则
24+
- 前端代码保持简洁,避免引入重型框架
25+
- 确保构建无错误
26+
27+
## 提交信息格式
28+
29+
推荐使用 [Conventional Commits](https://www.conventionalcommits.org/)
30+
31+
- `feat:` 新功能
32+
- `fix:` 修复 Bug
33+
- `docs:` 文档更新
34+
- `refactor:` 重构
35+
- `chore:` 构建/工具链

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# WebRTC
22

3+
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
4+
![Go](https://img.shields.io/badge/Go-1.22+-00ADD8?logo=go&logoColor=white)
5+
![WebRTC](https://img.shields.io/badge/WebRTC-Enabled-333333?logo=webrtc&logoColor=white)
6+
![Docker](https://img.shields.io/badge/Docker-Ready-2496ED?logo=docker&logoColor=white)
7+
38
WebRTC 是一个基于 Go 的最小可用 WebRTC 示例项目,提供 WebSocket 信令服务器与浏览器端 Demo,适合作为实时音视频应用的入门模板。
49

510
## 特性
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 2025-02-13 项目基础设施优化
2+
3+
## 新增
4+
- README 添加标准化 badges(License、Go、WebRTC、Docker)

cmd/server/main.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,30 @@ import (
1313
sig "lessup/webrtc/internal/signal"
1414
)
1515

16+
func parseOrigins(raw string) (origins []string, allowAll bool) {
17+
if raw == "" {
18+
return nil, false
19+
}
20+
if raw == "*" {
21+
return nil, true
22+
}
23+
parts := strings.Split(raw, ",")
24+
origins = make([]string, 0, len(parts))
25+
for _, p := range parts {
26+
if p = strings.TrimSpace(p); p != "" {
27+
origins = append(origins, p)
28+
}
29+
}
30+
return origins, false
31+
}
32+
1633
func main() {
1734
addr := ":8080"
1835
if v := os.Getenv("ADDR"); v != "" {
1936
addr = v
2037
}
2138

22-
var wsAllowAll bool
23-
var wsAllowed []string
24-
if v := os.Getenv("WS_ALLOWED_ORIGINS"); v != "" {
25-
if v == "*" {
26-
wsAllowAll = true
27-
} else {
28-
parts := strings.Split(v, ",")
29-
wsAllowed = make([]string, 0, len(parts))
30-
for _, p := range parts {
31-
p = strings.TrimSpace(p)
32-
if p != "" {
33-
wsAllowed = append(wsAllowed, p)
34-
}
35-
}
36-
}
37-
}
39+
wsAllowed, wsAllowAll := parseOrigins(os.Getenv("WS_ALLOWED_ORIGINS"))
3840

3941
hub := sig.NewHubWithOptions(sig.Options{
4042
AllowedOrigins: wsAllowed,

internal/signal/hub.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ func (h *Hub) isOriginAllowed(r *http.Request) bool {
5656
origin := r.Header.Get("Origin")
5757
if origin == "" {
5858
host := r.Host
59-
if host == "localhost:8080" || strings.HasPrefix(host, "127.0.0.1:") || strings.HasPrefix(host, "localhost:") {
60-
return true
61-
}
62-
return false
59+
return strings.HasPrefix(host, "localhost:") || strings.HasPrefix(host, "127.0.0.1:")
6360
}
6461
if len(h.allowedOrigins) == 0 {
6562
u, err := url.Parse(origin)
@@ -133,7 +130,7 @@ func (h *Hub) addClient(c *Client) {
133130
}
134131
m[c.id] = c
135132
log.Printf("signal: join room=%s id=%s", c.room, c.id)
136-
h.broadcastMembers(c.room, m)
133+
broadcastMembers(c.room, m)
137134
}
138135

139136
func (h *Hub) removeClient(c *Client) {
@@ -149,19 +146,20 @@ func (h *Hub) removeClient(c *Client) {
149146
return
150147
}
151148
delete(m, c.id)
149+
log.Printf("signal: leave room=%s id=%s", room, c.id)
152150
c.room = ""
153151
if len(m) == 0 {
154152
delete(h.rooms, room)
155153
log.Printf("signal: room %s closed", room)
156154
return
157155
}
158-
h.broadcastMembers(room, m)
156+
broadcastMembers(room, m)
159157
}
160158
}
161159

162160
// broadcastMembers sends the current member list to all clients in a room.
163161
// Must be called with h.mu held.
164-
func (h *Hub) broadcastMembers(room string, m map[string]*Client) {
162+
func broadcastMembers(room string, m map[string]*Client) {
165163
members := make([]string, 0, len(m))
166164
for id := range m {
167165
members = append(members, id)

internal/signal/hub_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,44 @@
11
package signal
22

33
import (
4+
"net/http"
45
"testing"
56

67
"github.com/gorilla/websocket"
78
)
89

10+
func TestIsOriginAllowed(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
opts Options
14+
host string
15+
origin string
16+
allowed bool
17+
}{
18+
{"no origin localhost", Options{}, "localhost:8080", "", true},
19+
{"no origin 127.0.0.1", Options{}, "127.0.0.1:8080", "", true},
20+
{"no origin external", Options{}, "example.com:8080", "", false},
21+
{"origin localhost", Options{}, "localhost:8080", "http://localhost:8080", true},
22+
{"origin 127.0.0.1", Options{}, "127.0.0.1:8080", "http://127.0.0.1:8080", true},
23+
{"origin external blocked", Options{}, "example.com", "https://example.com", false},
24+
{"allow all", Options{AllowAllOrigins: true}, "example.com", "https://evil.com", true},
25+
{"whitelist match", Options{AllowedOrigins: []string{"https://example.com"}}, "", "https://example.com", true},
26+
{"whitelist miss", Options{AllowedOrigins: []string{"https://example.com"}}, "", "https://evil.com", false},
27+
}
28+
for _, tt := range tests {
29+
t.Run(tt.name, func(t *testing.T) {
30+
h := NewHubWithOptions(tt.opts)
31+
r := &http.Request{Host: tt.host, Header: http.Header{}}
32+
if tt.origin != "" {
33+
r.Header.Set("Origin", tt.origin)
34+
}
35+
if got := h.isOriginAllowed(r); got != tt.allowed {
36+
t.Errorf("isOriginAllowed() = %v, want %v", got, tt.allowed)
37+
}
38+
})
39+
}
40+
}
41+
942
func drainMessages(ch <-chan Message) {
1043
for {
1144
select {

0 commit comments

Comments
 (0)