Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
sfu
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alain Takoudjou
sfu
Commits
3240225d
Commit
3240225d
authored
May 28, 2020
by
Juliusz Chroboczek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make client generic.
parent
d8b98497
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
156 additions
and
99 deletions
+156
-99
group.go
group.go
+35
-88
webclient.go
webclient.go
+121
-11
No files found.
group.go
View file @
3240225d
...
...
@@ -21,6 +21,13 @@ import (
"github.com/pion/webrtc/v2"
)
type
client
interface
{
getGroup
()
*
group
getId
()
string
getUsername
()
string
pushConn
(
conn
*
upConnection
,
tracks
[]
*
upTrack
,
label
string
)
error
}
type
chatHistoryEntry
struct
{
id
string
user
string
...
...
@@ -41,7 +48,7 @@ type group struct {
locked
uint32
mu
sync
.
Mutex
clients
map
[
string
]
*
webC
lient
clients
map
[
string
]
c
lient
history
[]
chatHistoryEntry
}
...
...
@@ -64,7 +71,7 @@ type getUpAction struct {
}
type
pushConnsAction
struct
{
c
*
webC
lient
c
c
lient
}
type
connectionFailedAction
struct
{
...
...
@@ -122,7 +129,7 @@ func addGroup(name string, desc *groupDescription) (*group, error) {
g
=
&
group
{
name
:
name
,
description
:
desc
,
clients
:
make
(
map
[
string
]
*
webC
lient
),
clients
:
make
(
map
[
string
]
c
lient
),
}
groups
.
groups
[
name
]
=
g
}
else
if
desc
!=
nil
{
...
...
@@ -195,7 +202,7 @@ type userid struct {
username
string
}
func
addClient
(
name
string
,
client
*
webC
lient
,
user
,
pass
string
)
(
*
group
,
[]
userid
,
error
)
{
func
addClient
(
name
string
,
client
c
lient
,
user
,
pass
string
)
(
*
group
,
[]
userid
,
error
)
{
g
,
err
:=
addGroup
(
name
,
nil
)
if
err
!=
nil
{
return
nil
,
nil
,
err
...
...
@@ -205,7 +212,10 @@ func addClient(name string, client *webClient, user, pass string) (*group, []use
if
err
!=
nil
{
return
nil
,
nil
,
err
}
client
.
permissions
=
perms
c
,
ok
:=
client
.
(
*
webClient
)
if
ok
{
c
.
permissions
=
perms
}
if
!
perms
.
Op
&&
atomic
.
LoadUint32
(
&
g
.
locked
)
!=
0
{
return
nil
,
nil
,
userError
(
"group is locked"
)
...
...
@@ -219,34 +229,34 @@ func addClient(name string, client *webClient, user, pass string) (*group, []use
return
nil
,
nil
,
userError
(
"too many users"
)
}
}
if
g
.
clients
[
client
.
id
]
!=
nil
{
if
g
.
clients
[
client
.
getId
()
]
!=
nil
{
return
nil
,
nil
,
protocolError
(
"duplicate client id"
)
}
var
users
[]
userid
for
_
,
c
:=
range
g
.
clients
{
users
=
append
(
users
,
userid
{
c
.
id
,
c
.
username
})
users
=
append
(
users
,
userid
{
c
.
getId
(),
c
.
getUsername
()
})
}
g
.
clients
[
client
.
id
]
=
client
g
.
clients
[
client
.
getId
()
]
=
client
return
g
,
users
,
nil
}
func
delClient
(
c
*
webC
lient
)
{
c
.
group
.
mu
.
Lock
()
defer
c
.
group
.
mu
.
Unl
ock
()
g
:=
c
.
group
func
delClient
(
c
c
lient
)
{
g
:=
c
.
getGroup
()
g
.
mu
.
L
ock
()
defer
g
.
mu
.
Unlock
()
if
g
.
clients
[
c
.
id
]
!=
c
{
if
g
.
clients
[
c
.
getId
()
]
!=
c
{
log
.
Printf
(
"Deleting unknown client"
)
return
}
delete
(
g
.
clients
,
c
.
id
)
delete
(
g
.
clients
,
c
.
getId
()
)
}
func
(
g
*
group
)
getClients
(
except
*
webClient
)
[]
*
webC
lient
{
func
(
g
*
group
)
getClients
(
except
client
)
[]
c
lient
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
clients
:=
make
([]
*
webC
lient
,
0
,
len
(
g
.
clients
))
clients
:=
make
([]
c
lient
,
0
,
len
(
g
.
clients
))
for
_
,
c
:=
range
g
.
clients
{
if
c
!=
except
{
clients
=
append
(
clients
,
c
)
...
...
@@ -255,16 +265,16 @@ func (g *group) getClients(except *webClient) []*webClient {
return
clients
}
func
(
g
*
group
)
getClientUnlocked
(
id
string
)
*
webC
lient
{
for
_
,
c
:=
range
g
.
clients
{
if
c
.
i
d
==
id
{
func
(
g
*
group
)
getClientUnlocked
(
id
string
)
c
lient
{
for
idd
,
c
:=
range
g
.
clients
{
if
id
d
==
id
{
return
c
}
}
return
nil
}
func
(
g
*
group
)
Range
(
f
func
(
c
*
webC
lient
)
bool
)
{
func
(
g
*
group
)
Range
(
f
func
(
c
c
lient
)
bool
)
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
for
_
,
c
:=
range
g
.
clients
{
...
...
@@ -311,42 +321,12 @@ func (err writerDeadError) Error() string {
return
"client writer died"
}
func
(
c
*
webClient
)
write
(
m
clientMessage
)
error
{
select
{
case
c
.
writeCh
<-
m
:
return
nil
case
<-
c
.
writerDone
:
return
writerDeadError
(
0
)
}
}
func
(
c
*
webClient
)
error
(
err
error
)
error
{
switch
e
:=
err
.
(
type
)
{
case
userError
:
return
c
.
write
(
clientMessage
{
Type
:
"error"
,
Value
:
string
(
e
),
})
default
:
return
err
}
}
type
clientDeadError
int
func
(
err
clientDeadError
)
Error
()
string
{
return
"client dead"
}
func
(
c
*
webClient
)
action
(
m
interface
{})
error
{
select
{
case
c
.
actionCh
<-
m
:
return
nil
case
<-
c
.
done
:
return
clientDeadError
(
0
)
}
}
type
groupUser
struct
{
Username
string
`json:"username,omitempty"`
Password
string
`json:"password,omitempty"`
...
...
@@ -453,42 +433,6 @@ func getPermission(desc *groupDescription, user, pass string) (userPermission, e
return
p
,
userError
(
"not authorised"
)
}
func
setPermission
(
g
*
group
,
id
string
,
perm
string
)
error
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
c
:=
g
.
getClientUnlocked
(
id
)
if
c
==
nil
{
return
userError
(
"no such user"
)
}
switch
perm
{
case
"op"
:
c
.
permissions
.
Op
=
true
case
"unop"
:
c
.
permissions
.
Op
=
false
case
"present"
:
c
.
permissions
.
Present
=
true
case
"unpresent"
:
c
.
permissions
.
Present
=
false
default
:
return
userError
(
"unknown permission"
)
}
return
c
.
action
(
permissionsChangedAction
{})
}
func
kickClient
(
g
*
group
,
id
string
)
error
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
c
:=
g
.
getClientUnlocked
(
id
)
if
c
==
nil
{
return
userError
(
"no such user"
)
}
return
c
.
action
(
kickAction
{})
}
type
publicGroup
struct
{
Name
string
`json:"name"`
ClientCount
int
`json:"clientCount"`
...
...
@@ -580,9 +524,12 @@ func getGroupStats() []groupStats {
clients
:
make
([]
clientStats
,
0
,
len
(
clients
)),
}
for
_
,
c
:=
range
clients
{
c
,
ok
:=
c
.
(
*
webClient
)
if
ok
{
cs
:=
getClientStats
(
c
)
stats
.
clients
=
append
(
stats
.
clients
,
cs
)
}
}
sort
.
Slice
(
stats
.
clients
,
func
(
i
,
j
int
)
bool
{
return
stats
.
clients
[
i
]
.
id
<
stats
.
clients
[
j
]
.
id
})
...
...
webclient.go
View file @
3240225d
...
...
@@ -107,6 +107,18 @@ type webClient struct {
up
map
[
string
]
*
upConnection
}
func
(
c
*
webClient
)
getGroup
()
*
group
{
return
c
.
group
}
func
(
c
*
webClient
)
getId
()
string
{
return
c
.
id
}
func
(
c
*
webClient
)
getUsername
()
string
{
return
c
.
username
}
type
rateMap
map
[
string
]
uint32
func
(
v
*
rateMap
)
UnmarshalJSON
(
b
[]
byte
)
error
{
...
...
@@ -259,8 +271,11 @@ func startClient(conn *websocket.Conn) (err error) {
Username
:
c
.
username
,
}
for
_
,
c
:=
range
clients
{
c
,
ok
:=
c
.
(
*
webClient
)
if
ok
{
c
.
write
(
u
)
}
}
defer
func
()
{
clients
:=
g
.
getClients
(
c
)
...
...
@@ -271,8 +286,11 @@ func startClient(conn *websocket.Conn) (err error) {
Del
:
true
,
}
for
_
,
c
:=
range
clients
{
c
,
ok
:=
c
.
(
*
webClient
)
if
ok
{
c
.
write
(
u
)
}
}
}()
return
clientLoop
(
c
,
conn
)
...
...
@@ -403,7 +421,7 @@ func addUpConn(c *webClient, id string) (*upConnection, error) {
if
tracks
!=
nil
{
clients
:=
c
.
group
.
getClients
(
c
)
for
_
,
cc
:=
range
clients
{
pushConn
(
cc
,
u
,
tracks
,
u
.
label
)
cc
.
pushConn
(
u
,
tracks
,
u
.
label
)
}
}
})
...
...
@@ -1208,7 +1226,10 @@ func (c *webClient) setRequested(requested map[string]uint32) error {
go
func
()
{
clients
:=
c
.
group
.
getClients
(
c
)
for
_
,
cc
:=
range
clients
{
cc
.
action
(
pushConnsAction
{
c
})
ccc
,
ok
:=
cc
.
(
*
webClient
)
if
ok
{
ccc
.
action
(
pushConnsAction
{
c
})
}
}
}()
...
...
@@ -1250,11 +1271,18 @@ func addDownConnTracks(c *webClient, remote *upConnection, tracks []*upTrack) (*
return
down
,
nil
}
func
pushConn
(
c
*
webClient
,
conn
*
upConnection
,
tracks
[]
*
upTrack
,
label
string
)
{
c
.
action
(
addConnAction
{
conn
,
tracks
})
func
(
c
*
webClient
)
pushConn
(
conn
*
upConnection
,
tracks
[]
*
upTrack
,
label
string
)
error
{
err
:=
c
.
action
(
addConnAction
{
conn
,
tracks
})
if
err
!=
nil
{
return
err
}
if
label
!=
""
{
c
.
action
(
addLabelAction
{
conn
.
id
,
conn
.
label
})
err
:=
c
.
action
(
addLabelAction
{
conn
.
id
,
conn
.
label
})
if
err
!=
nil
{
return
err
}
}
return
nil
}
func
clientLoop
(
c
*
webClient
,
conn
*
websocket
.
Conn
)
error
{
...
...
@@ -1354,7 +1382,7 @@ func clientLoop(c *webClient, conn *websocket.Conn) error {
for
_
,
u
:=
range
c
.
up
{
tracks
:=
make
([]
*
upTrack
,
len
(
u
.
tracks
))
copy
(
tracks
,
u
.
tracks
)
go
pushConn
(
a
.
c
,
u
,
tracks
,
u
.
label
)
go
a
.
c
.
pushConn
(
u
,
tracks
,
u
.
label
)
}
case
connectionFailedAction
:
found
:=
delUpConn
(
c
,
a
.
id
)
...
...
@@ -1428,6 +1456,52 @@ func failConnection(c *webClient, id string, message string) error {
return
nil
}
func
setPermissions
(
g
*
group
,
id
string
,
perm
string
)
error
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
client
:=
g
.
getClientUnlocked
(
id
)
if
client
==
nil
{
return
userError
(
"no such user"
)
}
c
,
ok
:=
client
.
(
*
webClient
)
if
!
ok
{
return
userError
(
"this is not a real user"
)
}
switch
perm
{
case
"op"
:
c
.
permissions
.
Op
=
true
case
"unop"
:
c
.
permissions
.
Op
=
false
case
"present"
:
c
.
permissions
.
Present
=
true
case
"unpresent"
:
c
.
permissions
.
Present
=
false
default
:
return
userError
(
"unknown permission"
)
}
return
c
.
action
(
permissionsChangedAction
{})
}
func
kickClient
(
g
*
group
,
id
string
)
error
{
g
.
mu
.
Lock
()
defer
g
.
mu
.
Unlock
()
client
:=
g
.
getClientUnlocked
(
id
)
if
client
==
nil
{
return
userError
(
"no such user"
)
}
c
,
ok
:=
client
.
(
*
webClient
)
if
!
ok
{
return
userError
(
"this is not a real user"
)
}
return
c
.
action
(
kickAction
{})
}
func
handleClientMessage
(
c
*
webClient
,
m
clientMessage
)
error
{
switch
m
.
Type
{
case
"request"
:
...
...
@@ -1476,20 +1550,26 @@ func handleClientMessage(c *webClient, m clientMessage) error {
c
.
group
.
addToChatHistory
(
m
.
Id
,
m
.
Username
,
m
.
Value
,
m
.
Me
)
clients
:=
c
.
group
.
getClients
(
c
)
for
_
,
cc
:=
range
clients
{
cc
,
ok
:=
cc
.
(
*
webClient
)
if
ok
{
cc
.
write
(
m
)
}
}
case
"clearchat"
:
c
.
group
.
clearChatHistory
()
m
:=
clientMessage
{
Type
:
"clearchat"
}
clients
:=
c
.
group
.
getClients
(
nil
)
for
_
,
cc
:=
range
clients
{
cc
,
ok
:=
cc
.
(
*
webClient
)
if
ok
{
cc
.
write
(
m
)
}
}
case
"op"
,
"unop"
,
"present"
,
"unpresent"
:
if
!
c
.
permissions
.
Op
{
return
c
.
error
(
userError
(
"not authorised"
))
}
err
:=
setPermission
(
c
.
group
,
m
.
Id
,
m
.
Type
)
err
:=
setPermission
s
(
c
.
group
,
m
.
Id
,
m
.
Type
)
if
err
!=
nil
{
return
c
.
error
(
err
)
}
...
...
@@ -1619,3 +1699,33 @@ func clientWriter(conn *websocket.Conn, ch <-chan interface{}, done chan<- struc
}
}
}
func
(
c
*
webClient
)
action
(
m
interface
{})
error
{
select
{
case
c
.
actionCh
<-
m
:
return
nil
case
<-
c
.
done
:
return
clientDeadError
(
0
)
}
}
func
(
c
*
webClient
)
write
(
m
clientMessage
)
error
{
select
{
case
c
.
writeCh
<-
m
:
return
nil
case
<-
c
.
writerDone
:
return
writerDeadError
(
0
)
}
}
func
(
c
*
webClient
)
error
(
err
error
)
error
{
switch
e
:=
err
.
(
type
)
{
case
userError
:
return
c
.
write
(
clientMessage
{
Type
:
"error"
,
Value
:
string
(
e
),
})
default
:
return
err
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment