-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
206 lines (196 loc) · 5.58 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package room // import "m7s.live/plugin/room/v4"
import (
_ "embed"
"encoding/json"
"fmt"
"net"
"net/http"
"strings"
"sync"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"github.com/google/uuid"
"go.uber.org/zap"
. "m7s.live/engine/v4"
"m7s.live/engine/v4/common"
"m7s.live/engine/v4/config"
"m7s.live/engine/v4/track"
"m7s.live/engine/v4/util"
)
type User struct {
Subscriber
StreamPath string
Token string `json:"-" yaml:"-"`
Room *Room `json:"-" yaml:"-"`
net.Conn `json:"-" yaml:"-"`
writeLock sync.Mutex
}
func (u *User) OnEvent(event any) {
switch v := event.(type) {
case *track.Data[[]byte]:
go v.Play(u.IO, func(data *common.DataFrame[[]byte]) error {
u.writeLock.Lock()
defer u.writeLock.Unlock()
return wsutil.WriteServerText(u.Conn, data.Data)
})
default:
u.Subscriber.OnEvent(event)
}
}
func (u *User) Send(event string, data any) {
if u.Conn != nil {
u.writeLock.Lock()
defer u.writeLock.Unlock()
j, err := json.Marshal(map[string]any{"event": event, "data": data})
if err == nil {
wsutil.WriteServerText(u.Conn, j)
}
}
}
type Room struct {
Publisher `json:"-" yaml:"-"`
Users util.Map[string, *User]
track *track.Data[[]byte]
}
var Rooms util.Map[string, *Room]
//go:embed default.yaml
var defaultYaml DefaultYaml
type RoomConfig struct {
config.Subscribe
config.HTTP
AppName string `default:"room" desc:"用于订阅房间消息的应用名(streamPath第一段)"`
Size int `default:"20" desc:"房间大小"` //房间大小
Private map[string]string `desc:"私密房间" key:"房间号" value:"密码"` //私密房间 key房间号,value密码
Verify struct {
URL string `desc:"验证用户身份的URL"`
Method string `desc:"验证用户身份的HTTP方法"`
Header map[string]string `desc:"验证用户身份的HTTP头" key:"名称" value:"值"`
} `desc:"验证用户身份"`
lock sync.RWMutex
Ping string `default:"ping" desc:"用于客户端与服务器保持心跳时客户端发送的特殊字符串"`
Pong string `default:"pong" desc:"用于客户端与服务器保持心跳时服务器响应的特殊字符串"`
}
var plugin = InstallPlugin(&RoomConfig{}, defaultYaml)
func (rc *RoomConfig) OnEvent(event any) {
switch v := event.(type) {
case SEpublish:
args := v.Target.Publisher.GetPublisher().Args
token := args.Get("token")
ss := strings.Split(token, ":")
if len(ss) != 3 {
return
}
roomId := ss[0]
userId := ss[1]
if roomId != "" && Rooms.Has(roomId) {
room := Rooms.Get(roomId)
if room.Users.Has(userId) {
user := room.Users.Get(userId)
if user.Token == token {
user.StreamPath = v.Target.Path
data, _ := json.Marshal(map[string]any{"event": "publish", "data": v.Target.Path, "args": user.Args, "userId": user.ID})
room.track.Push(data)
}
}
}
}
}
func (rc *RoomConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ss := strings.Split(r.URL.Path, "/")[1:]
var roomId, userId, token string
if len(ss) == 2 {
roomId = ss[0]
userId = ss[1]
} else {
http.Error(w, "invalid url", http.StatusBadRequest)
return
}
if rc.Verify.URL != "" {
req, _ := http.NewRequest(rc.Verify.Method, rc.Verify.URL, nil)
req.Header = r.Header
res, _ := http.DefaultClient.Do(req)
if res.StatusCode != 200 {
http.Error(w, "verify failed", http.StatusForbidden)
}
}
if rc.Private != nil {
rc.lock.RLock()
pass, ok := rc.Private[roomId]
rc.lock.Unlock()
if ok {
if pass != r.URL.Query().Get("password") {
http.Error(w, "password wrong", http.StatusForbidden)
return
}
}
}
var room *Room
if !Rooms.Has(roomId) {
room = &Room{}
room.ID = roomId
if plugin.Publish(rc.AppName+"/"+roomId, room) == nil {
Rooms.Add(roomId, room)
if actual, loaded := room.Stream.Tracks.Map.Load("data"); !loaded {
room.track = &track.Data[[]byte]{}
room.track.Init(100)
room.track.SetStuff("data")
room.track.Locker = &sync.Mutex{}
room.track.Attach(room.Stream)
} else {
room.track = actual.(*track.Data[[]byte])
room.track.SetStuff(room)
}
} else {
http.Error(w, "room already exist", http.StatusBadRequest)
return
}
} else {
room = Rooms.Get(roomId)
}
if room.Users.Has(userId) {
http.Error(w, "user exist", http.StatusBadRequest)
return
}
conn, _, _, err := ws.UpgradeHTTP(r, w)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer conn.Close()
token = fmt.Sprintf("%s:%s:%s", roomId, userId, uuid.NewString())
user := &User{Room: room, Conn: conn, Token: token}
user.ID = userId
if err = plugin.Subscribe(rc.AppName+"/"+room.ID+util.Conditoinal(r.URL.RawQuery == "", "", "?"+r.URL.RawQuery), user); err == nil {
data, _ := json.Marshal(map[string]any{"event": "userjoin", "data": user})
room.track.Push(data)
room.Users.Add(userId, user)
user.Send("joined", map[string]any{"token": token, "userList": room.Users.ToList()})
defer func() {
user.Stop(zap.Error(err))
room.Users.Delete(userId)
if room.Users.Len() == 0 {
room.track.Dispose()
room.Stop()
Rooms.Delete(roomId)
}
}()
} else {
return
}
var msg []byte
var op ws.OpCode
for {
msg, op, err = wsutil.ReadClientData(conn)
if string(msg) == rc.Ping {
wsutil.WriteServerText(conn, []byte(rc.Pong))
} else {
data, _ := json.Marshal(map[string]any{"event": "msg", "data": string(msg), "userId": userId})
room.track.Push(data)
}
if op == ws.OpClose || err != nil {
data, _ := json.Marshal(map[string]any{"event": "userleave", "userId": userId, "data": user})
room.track.Push(data)
return
}
}
}