-
Notifications
You must be signed in to change notification settings - Fork 1
/
WebsocketServer.jl
272 lines (254 loc) · 8.85 KB
/
WebsocketServer.jl
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"""
WebsocketServer([; options...])
Constructs a new WebsocketServer, overriding [`serverConfig`](@ref) with the passed options.
# Example
```julia
using SimpleWebsockets
server = WebsocketServer([; options...])
```
"""
struct WebsocketServer
config::NamedTuple
callbacks::Dict{Symbol, Union{Bool, Function}}
flags::Dict{Symbol, Bool}
server::Dict{Symbol, Union{Sockets.TCPServer, Array{WebsocketConnection, 1}, Nothing}}
location::Dict{Symbol, Union{String, Nothing, Integer}}
function WebsocketServer(; config...)
@debug "WebsocketServer initiate"
config = merge(serverConfig, (; config...))
config = merge(config, (; maskOutgoingPackets = false, type = "server",))
self = new(
config,
Dict{Symbol, Union{Bool, Function}}(
:client => false,
:connectError => false,
:peerError => false,
:closed => false,
:listening => false
),
Dict{Symbol, Bool}(
:isopen => false
),
Dict{Symbol, Union{Sockets.TCPServer, Array{WebsocketConnection, 1}, Nothing}}(
:clients => Array{WebsocketConnection, 1}(),
:socket => nothing
),
Dict{Symbol, Union{String, Nothing, Integer}}(
:host => nothing,
:port => nothing
)
)
end
end
"""
listen(callback::Function, server::WebsocketServer, event::Symbol)
Register event callbacks onto a server. The callback must be a function with exactly one argument.
Valid events are:
- :listening
- :client
- :connectError
- :peerError
- :closed
# Example
```julia
listen(server, :client) do client
#...
end
```
"""
function listen(
cb::Function,
self::WebsocketServer,
event::Symbol
)
if !haskey(self.callbacks, event)
return @warn "WebsocketServer has no listener for :$event."
end
if haskey(self.callbacks, event) && !(self.callbacks[event] isa Function)
self.callbacks[event] = data -> (
@async try
cb(data)
catch err
err = CallbackError(err, catch_backtrace())
err.log()
exit()
end
)
end
end
function validateAuth(server::WebsocketServer, headers::HTTP.Messages.Request, queries::Dict{String,String})
if server.config.authfunction isa Function
headers = map(headers.headers) do header
(Symbol(first(header)), last(header))
end
queries = map(collect(queries)) do query
(Symbol(first(query)), last(query))
end
return server.config.authfunction(RequestDetails((; headers...),(; queries...)))
end
return true
end
function validateUpgrade(headers::HTTP.Messages.Request)
if !HTTP.hasheader(headers, "Upgrade", "websocket")
throw(error("""did not receive "Upgrade: websocket" """))
end
if !HTTP.hasheader(headers, "Connection", "Upgrade") && !HTTP.hasheader(headers, "Connection", "keep-alive, upgrade")
throw(error("""did not receive "Connection: Upgrade" """))
end
if !HTTP.hasheader(headers, "Sec-WebSocket-Version", "13") && !HTTP.hasheader(headers, "Sec-WebSocket-Version", "8")
throw(error("""did not receive "Sec-WebSocket-Version: [13 or 8]" """))
end
if !HTTP.hasheader(headers, "Sec-WebSocket-Key")
throw(error("""did not receive "Sec-WebSocket-Key" header."""))
end
end
"""
serve(server::WebsocketServer, [port::Int, host::String; options...])
Opens up a TCP connection listener. Blocks while the server is listening.
Defaults:
- port: 8080
- host: "localhost"
`; options...` are passed to the underlying [HTTP.servers.listen](https://juliaweb.github.io/HTTP.jl/v1.0.2/server/#HTTP.listen)
# Example
```julia
closed = Condition()
#...
@async serve(server, 8081, "localhost")
#...
wait(closed)
```
"""
function serve(self::WebsocketServer, port::Int = 8080, host::String = "localhost"; options...)
@debug "WebsocketServer.listen"
config = self.config
options = merge(serverOptions, (; options...))
self.location[:port] = port
self.location[:host] = host
try
host = getaddrinfo(host)
if config.ssl
tlsconfig = HTTP.Servers.SSLConfig(config.sslcert, config.sslkey)
options = merge(options, (; sslconfig = tlsconfig))
end
clientcallback = self.callbacks[:client]
clientcallback === false && throw(error("tried to bind the server before registering \":client\" handler"))
self.server[:server] = Sockets.listen(host, port)
Sockets.nagle(self.server[:server], config.useNagleAlgorithm)
self.flags[:isopen] = true
if self.callbacks[:listening] isa Function
self.callbacks[:listening]((; port = port, host = host))
end
HTTP.listen(self.server[:server]; options...) do io
try
headers = io.message
validateUpgrade(headers)
queries = HTTP.queryparams(parse(HTTP.URI, io.message.target))
length(queries) === 0 && (queries = Dict{String, String}())
if !validateAuth(self, headers, queries)
try
throw(error("Invalid authorization"))
catch err
err = PeerConnectError(err, catch_backtrace())
errcallback = self.callbacks[:peerError]
if errcallback isa Function
errcallback(err)
end
end
HTTP.setstatus(io, 401)
startwrite(io)
return
end
HTTP.setstatus(io, 101)
key = string(HTTP.header(headers, "Sec-WebSocket-Key"))
HTTP.setheader(io, "Sec-WebSocket-Accept" => acceptHash(key))
HTTP.setheader(io, "Upgrade" => "websocket")
HTTP.setheader(io, "Connection" => "Upgrade")
startwrite(io)
client = WebsocketConnection(io.stream, config, self.server[:clients])
push!(self.server[:clients], client)
clientcallback(client)
if HTTP.hasheader(headers, "Sec-WebSocket-Extensions")
@warn "Websocket extensions not implemented" header = HTTP.header(headers, "Sec-WebSocket-Extensions")
end
startConnection(client, io)
catch err
err = PeerConnectError(err, catch_backtrace())
errcallback = self.callbacks[:peerError]
if errcallback isa Function
errcallback(err)
end
HTTP.setstatus(io, 400)
startwrite(io)
end
end
close(self)
catch err
self.flags[:isopen] = false
if typeof(err) === Base.IOError && occursin("software caused connection abort", err.msg)
close(self)
end
err = ConnectError(err, catch_backtrace())
errcallback = self.callbacks[:connectError]
if errcallback isa Function
errcallback(err)
else
err.log()
exit()
end
end
end
"""
emit(server::WebsocketServer, data::Union{Array{UInt8,1}, String, Number})
Sends the given `data` as a message to all clients connected to the server.
# Example
```julia
emit(server, "Hello everybody from your loving server.")
```
"""
function emit(self::WebsocketServer, data::Union{Array{UInt8,1}, String, Number})
for client in self.server[:clients]
client.validate["valid"] !== true && continue
send(client, data)
end
end
"""
close(server::WebsocketServer)
Gracefully disconnects all connected clients, then closes the TCP socket listener.
"""
function Base.close(self::WebsocketServer)
host = self.location[:host]
port = self.location[:port]
@sync begin
for client in self.server[:clients]
close(client, CLOSE_REASON_GOING_AWAY)
@async wait(client.closed)
end
end
close(self.server[:server])
while isopen(self.server[:server])
@warn isopen(self.server[:server])
sleep(0.1)
end
self.flags[:isopen] = false
closecallback = self.callbacks[:closed]
if closecallback isa Function
closecallback((; host = host, port = port))
else
@info "The websocket server was closed cleanly:" host = host port = port
end
return
end
"""
isopen(server::WebsocketServer)::Bool
Returns a Bool indication if the server TCP listener is open.
"""
function Base.isopen(server::WebsocketServer)
server.flags[:isopen]
end
"""
length(server::WebsocketServer)::Int
Returns the number of clients connected to the server.
"""
function Base.length(self::WebsocketServer)
length(self.server[:clients])
end