FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
adminforth/adminforth/modules/socketBroker.ts at validationtrfix · devforth/adminforth · GitHub
devforth
adminforth
Repository navigation
Code
Issues
16
(16)
Pull requests
4
(4)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
adminforth
/
adminforth
/
modules
/
socketBroker.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
138 lines (121 loc) · 4.18 KB
Breadcrumbs
adminforth
/
adminforth
/
modules
/
socketBroker.ts
Copy path
File metadata and controls
138 lines (121 loc) · 4.18 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
{
IAdminForth
,
IWebSocketBroker
,
IWebSocketClient
}
from
"../types/Back.js"
;
import
{
AdminUser
}
from
"../types/Common.js"
;
export
default
class
SocketBroker
implements
IWebSocketBroker
{
clients
:
IWebSocketClient
[
]
=
[
]
;
topics
:
{
[
key
:
string
]
:
IWebSocketClient
[
]
}
=
{
}
;
adminforth
:
IAdminForth
;
deadCheckerRunning
=
false
;
constructor
(
adminforth
:
IAdminForth
)
{
this
.
adminforth
=
adminforth
;
}
async
startChecker
(
)
{
if
(
this
.
deadCheckerRunning
)
{
return
;
}
this
.
deadCheckerRunning
=
true
;
while
(
true
)
{
await
this
.
checkDeadClients
(
)
;
await
new
Promise
(
(
resolve
)
=>
setTimeout
(
resolve
,
10_000
)
)
;
}
}
async
checkDeadClients
(
)
{
const
now
=
Date
.
now
(
)
;
const
deadClients
=
[
]
;
for
(
const
client
of
this
.
clients
)
{
if
(
now
-
client
.
lastPing
>
30_000
)
{
deadClients
.
push
(
client
)
;
}
}
deadClients
.
forEach
(
client
=>
{
client
.
close
(
)
;
delete
this
.
clients
[
client
.
id
]
;
}
)
;
}
deleteClientFromTopic
(
client
:
IWebSocketClient
,
topic
:
string
)
{
if
(
!
this
.
topics
[
topic
]
)
{
return
;
}
this
.
topics
[
topic
]
=
this
.
topics
[
topic
]
.
filter
(
c
=>
c
!==
client
)
;
}
cleanupTopicIfEmpty
(
topic
:
string
)
{
if
(
!
this
.
topics
[
topic
]
)
{
return
;
}
if
(
this
.
topics
[
topic
]
.
length
===
0
)
{
delete
this
.
topics
[
topic
]
;
}
}
registerWsClient
(
client
:
IWebSocketClient
)
:
void
{
this
.
startChecker
(
)
;
if
(
!
this
.
clients
[
client
.
id
]
)
{
this
.
clients
[
client
.
id
]
=
client
;
}
client
.
onMessage
(
async
(
message
)
=>
{
if
(
message
.
toString
(
)
===
'ping'
)
{
client
.
send
(
'pong'
)
;
client
.
lastPing
=
Date
.
now
(
)
;
}
else
{
const
data
=
JSON
.
parse
(
message
)
;
if
(
data
.
type
===
'subscribe'
)
{
if
(
!
data
.
topic
.
startsWith
(
'/opentopic/'
)
)
{
if
(
this
.
adminforth
.
config
.
auth
.
websocketTopicAuth
)
{
let
authResult
=
false
;
try
{
authResult
=
await
this
.
adminforth
.
config
.
auth
.
websocketTopicAuth
(
data
.
topic
,
client
.
adminUser
)
;
}
catch
(
e
)
{
console
.
error
(
'Error in websocketTopicAuth, assuming connection not allowed'
,
e
)
;
}
if
(
!
authResult
)
{
client
.
send
(
JSON
.
stringify
(
{
type
:
'error'
,
message
:
'Unauthorized'
}
)
)
;
return
;
}
}
}
if
(
!
data
.
topic
)
{
client
.
send
(
JSON
.
stringify
(
{
type
:
'error'
,
message
:
'No topic provided'
}
)
)
;
}
if
(
!
this
.
topics
[
data
.
topic
]
)
{
this
.
topics
[
data
.
topic
]
=
[
]
;
}
this
.
topics
[
data
.
topic
]
.
push
(
client
)
;
client
.
topics
.
add
(
data
.
topic
)
;
if
(
this
.
adminforth
.
config
.
auth
.
websocketSubscribed
)
{
this
.
adminforth
.
config
.
auth
.
websocketSubscribed
(
data
.
topic
,
client
.
adminUser
)
;
}
}
else
if
(
data
.
type
===
'unsubscribe'
)
{
if
(
!
data
.
topic
)
{
client
.
send
(
JSON
.
stringify
(
{
type
:
'error'
,
message
:
'No topic provided'
}
)
)
;
}
this
.
deleteClientFromTopic
(
client
,
data
.
topic
)
;
this
.
cleanupTopicIfEmpty
(
data
.
topic
)
;
client
.
topics
.
delete
(
data
.
topic
)
;
}
}
}
)
;
client
.
onClose
(
(
)
=>
{
for
(
const
topic
of
client
.
topics
)
{
this
.
deleteClientFromTopic
(
client
,
topic
)
;
this
.
cleanupTopicIfEmpty
(
topic
)
;
}
delete
this
.
clients
[
client
.
id
]
;
}
)
;
// send ready message
client
.
send
(
JSON
.
stringify
(
{
type
:
'ready'
}
)
)
;
}
async
publish
(
topic
:
string
,
data
:
any
,
filterUsers
?:
(
adminUser
:
AdminUser
)
=>
Promise
<
boolean
>
)
:
Promise
<
void
>
{
if
(
!
this
.
topics
[
topic
]
)
{
process
.
env
.
HEAVY_DEBUG
&&
console
.
log
(
'No clients subscribed to topic'
,
topic
)
;
return
;
}
for
(
const
client
of
this
.
topics
[
topic
]
)
{
if
(
filterUsers
)
{
if
(
!
(
await
filterUsers
(
client
.
adminUser
)
)
)
{
process
.
env
.
HEAVY_DEBUG
&&
console
.
log
(
'Client not authorized to receive message'
,
topic
,
client
.
adminUser
)
;
continue
;
}
}
process
.
env
.
HEAVY_DEBUG
&&
console
.
log
(
'Sending data to soket'
,
topic
,
data
)
;
client
.
send
(
JSON
.
stringify
(
{
type
:
'message'
,
topic
,
data
}
)
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL