ronin-support-web 0.1.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,888 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # ronin-support-web - A web support library for ronin-rb.
4
+ #
5
+ # Copyright (c) 2023-2024 Hal Brodigan (postmodern.mod3@gmail.com)
6
+ #
7
+ # ronin-support-web is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Lesser General Public License as published
9
+ # by the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # ronin-support-web is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with ronin-support-web. If not, see <https://www.gnu.org/licenses/>.
19
+ #
20
+
21
+ require 'ronin/support/web/websocket'
22
+
23
+ require 'uri/ws'
24
+ require 'uri/wss'
25
+
26
+ module Ronin
27
+ module Support
28
+ module Web
29
+ module WebSocket
30
+ #
31
+ # Adds helper methods for working with WebSockets.
32
+ #
33
+ module Mixin
34
+ # @!macro [new] ssl_kwargs
35
+ # @option ssl [1, 1.1, 1.2, String, Symbol, nil] :version
36
+ # The SSL version to use.
37
+ #
38
+ # @option ssl [Symbol, Boolean] :verify
39
+ # Specifies whether to verify the SSL certificate.
40
+ # May be one of the following:
41
+ #
42
+ # * `:none`
43
+ # * `:peer`
44
+ # * `:fail_if_no_peer_cert`
45
+ # * `:client_once`
46
+ #
47
+ # @option ssl [Crypto::Key::RSA, OpenSSL::PKey::RSA, nil] :key
48
+ # The RSA key to use for the SSL context.
49
+ #
50
+ # @option ssl [String] :key_file
51
+ # The path to the SSL `.key` file.
52
+ #
53
+ # @option ssl [Crypto::Cert, OpenSSL::X509::Certificate, nil] :cert
54
+ # The X509 certificate to use for the SSL context.
55
+ #
56
+ # @option ssl [String] :cert_file
57
+ # The path to the SSL `.crt` file.
58
+ #
59
+ # @option ssl [String] :ca_bundle
60
+ # Path to the CA certificate file or directory.
61
+
62
+ # @!macro [new] client_kwargs
63
+ # @option kwargs [String, nil] :bind_host
64
+ # The optional host to bind the server socket to.
65
+ #
66
+ # @option kwargs [Integer, nil] :bind_port
67
+ # The optioanl port to bind the server socket to. If not
68
+ # specified, it will default to the port of the URL.
69
+ #
70
+ # @!macro ssl_kwargs
71
+
72
+ #
73
+ # Tests whether the WebSocket is open.
74
+ #
75
+ # @param [String, URI::WS, URI::WSS] url
76
+ # The `ws://` or `wss://` URL to connect to.
77
+ #
78
+ # @param [Hash{Symbol => Object}] ssl
79
+ # Additional keyword arguments for
80
+ # `Ronin::Support::Network::SSL.connect`.
81
+ #
82
+ # @param [Hash{Symbol => Object}] kwargs
83
+ # Additional keyword arguments for {Client#initialize}.
84
+ #
85
+ # @!macro client_kwargs
86
+ #
87
+ # @return [Boolean, nil]
88
+ # Specifies whether the WebSocket is open.
89
+ # If the connection was not accepted, `nil` will be returned.
90
+ #
91
+ # @api public
92
+ #
93
+ # @see WebSocket.open?
94
+ #
95
+ def websocket_open?(url, ssl: {}, **kwargs)
96
+ WebSocket.open?(url, ssl: ssl, **kwargs)
97
+ end
98
+
99
+ #
100
+ # Connects to a websocket.
101
+ #
102
+ # @param [String, URI::WS, URI::WSS] url
103
+ # The `ws://` or `wss://` URL to connect to.
104
+ #
105
+ # @param [Hash{Symbol => Object}] ssl
106
+ # Additional keyword arguments for
107
+ # `Ronin::Support::Network::SSL.connect`.
108
+ #
109
+ # @param [Hash{Symbol => Object}] kwargs
110
+ # Additional keyword arguments for {Client#initialize}.
111
+ #
112
+ # @!macro client_kwargs
113
+ #
114
+ # @yield [websocket]
115
+ # If a block is given, then it will be passed the WebSocket
116
+ # connection. Once the block has returned, the WebSocket connection
117
+ # will be closed.
118
+ #
119
+ # @yieldparam [Client] websocket
120
+ # The WebSocket connection.
121
+ #
122
+ # @return [Client]
123
+ # The WebSocket connection.
124
+ #
125
+ # @example Connecting to a WebSocket server:
126
+ # websocket_connect('ws://websocket-echo.com')
127
+ # # => #<Ronin::Support::Web::WebSocket::Client: ...>
128
+ #
129
+ # @example Creating a temporary WebSocket connection:
130
+ # websocket_connect('ws://websocket-echo.com') do |websocket|
131
+ # # ...
132
+ # end
133
+ #
134
+ # @api public
135
+ #
136
+ # @see WebSocket.connect
137
+ #
138
+ def websocket_connect(url, ssl: {}, **kwargs, &block)
139
+ WebSocket.connect(url, ssl: ssl, **kwargs, &block)
140
+ end
141
+
142
+ #
143
+ # Connects to the WebSocket and sends the data.
144
+ #
145
+ # @param [String] data
146
+ # The data to send to the WebSocket.
147
+ #
148
+ # @param [String, URI::WS, URI::WSS] url
149
+ # The `ws://` or `wss://` URL to connect to.
150
+ #
151
+ # @param [:text, :binary, :ping, :pong, :close] type
152
+ # The data frame type.
153
+ #
154
+ # @param [Hash{Symbol => Object}] ssl
155
+ # Additional keyword arguments for
156
+ # `Ronin::Support::Network::SSL.connect`.
157
+ #
158
+ # @param [Hash{Symbol => Object}] kwargs
159
+ # Additional keyword arguments for {Client#initialize}.
160
+ #
161
+ # @!macro client_kwargs
162
+ #
163
+ # @yield [websocket]
164
+ # If a block is given, then it will be passed the WebSocket
165
+ # connection. Once the block has returned, the WebSocket connection
166
+ # will be closed.
167
+ #
168
+ # @yieldparam [Client] websocket
169
+ # The WebSocket connection.
170
+ #
171
+ # @return [Client]
172
+ # The WebSocket connection.
173
+ #
174
+ # @api public
175
+ #
176
+ # @see WebSocket.connect_and_send
177
+ #
178
+ def websocket_connect_and_send(data,url, type: :text, ssl: {}, **kwargs,&block)
179
+ WebSocket.connect_and_send(data,url, type: type, ssl: ssl, **kwargs,&block)
180
+ end
181
+
182
+ #
183
+ # Connects to the WebSocket, sends the data, and closes the
184
+ # connection.
185
+ #
186
+ # @param [String] data
187
+ # The data to send to the WebSocket.
188
+ #
189
+ # @param [String, URI::WS, URI::WSS] url
190
+ # The `ws://` or `wss://` URL to connect to.
191
+ #
192
+ # @param [:text, :binary, :ping, :pong, :close] type
193
+ # The data frame type.
194
+ #
195
+ # @param [Hash{Symbol => Object}] ssl
196
+ # Additional keyword arguments for
197
+ # `Ronin::Support::Network::SSL.connect`.
198
+ #
199
+ # @param [Hash{Symbol => Object}] kwargs
200
+ # Additional keyword arguments for {Client#initialize}.
201
+ #
202
+ # @!macro client_kwargs
203
+ #
204
+ # @return [true]
205
+ #
206
+ # @api public
207
+ #
208
+ # @see WebSocket.send
209
+ #
210
+ def websocket_send(data,url, type: :text, ssl: {}, **kwargs)
211
+ WebSocket.send(data,url, type: type, ssl: ssl, **kwargs)
212
+ end
213
+
214
+ # @!macro [new] server_kwargs
215
+ # @option kwargs [String, nil] :bind_host
216
+ # The optional host to bind the server socket to.
217
+ #
218
+ # @option kwargs [Integer, nil] :bind_port
219
+ # The optioanl port to bind the server socket to. If not
220
+ # specified, it will default to the port of the URL.
221
+ #
222
+ # @option kwargs [Integer] :backlog (5)
223
+ # The maximum backlog of pending connections.
224
+ #
225
+ # @!macro ssl_kwargs
226
+
227
+ #
228
+ # Starts a WebSocket server.
229
+ #
230
+ # @param [String, URI::WS, URI::WSS] url
231
+ # The `ws://` or `wss://` URL to connect to.
232
+ #
233
+ # @param [Hash{Symbol => Object}] ssl
234
+ # Additional keyword arguments for
235
+ # `Ronin::Support::Network::SSL.server`.
236
+ #
237
+ # @param [Hash{Symbol => Object}] kwargs
238
+ # Additional keyword arguments for {Server#initialize}.
239
+ #
240
+ # @!macro server_kwargs
241
+ #
242
+ # @yield [server]
243
+ # If a block is given, then it will be passed the WebSocket server.
244
+ # Once the block has returned, the WebSocket server will be closed.
245
+ #
246
+ # @yieldparam [Server] server
247
+ # The WebSocket server.
248
+ #
249
+ # @return [Server]
250
+ # The WebSocket server.
251
+ #
252
+ # @api public
253
+ #
254
+ # @see WebSocket.server
255
+ #
256
+ def websocket_server(url, ssl: {}, **kwargs, &block)
257
+ WebSocket.server(url, ssl: ssl, **kwargs, &block)
258
+ end
259
+
260
+ #
261
+ # Creates a new WebSocket server listening on a given host and port,
262
+ # accepting clients in a loop.
263
+ #
264
+ # @param [String, URI::WS, URI::WSS] url
265
+ # The `ws://` or `wss://` URL to connect to.
266
+ #
267
+ # @param [Hash{Symbol => Object}] ssl
268
+ # Additional keyword arguments for
269
+ # `Ronin::Support::Network::SSL.server`.
270
+ #
271
+ # @param [Hash{Symbol => Object}] kwargs
272
+ # Additional keyword arguments for {Server#initialize}.
273
+ #
274
+ # @!macro server_kwargs
275
+ #
276
+ # @yield [client]
277
+ # The given block will be passed the newly connected WebSocket
278
+ # client. After the block has finished, the WebSocket client will be
279
+ # closed.
280
+ #
281
+ # @yieldparam [Server::Client] client
282
+ # A newly connected WebSocket client.
283
+ #
284
+ # @return [nil]
285
+ #
286
+ # @api public
287
+ #
288
+ # @see WebSocket.server_loop
289
+ #
290
+ def websocket_server_loop(url, ssl: {}, **kwargs,&block)
291
+ WebSocket.server_loop(url, ssl: ssl, **kwargs,&block)
292
+ end
293
+
294
+ #
295
+ # Opens a WebSocket server, accepts a single connection, yields it to
296
+ # the given block, then closes both the connection and the server.
297
+ #
298
+ # @param [String, URI::WS, URI::WSS] url
299
+ # The `ws://` or `wss://` URL to connect to.
300
+ #
301
+ # @param [Hash{Symbol => Object}] ssl
302
+ # Additional keyword arguments for
303
+ # `Ronin::Support::Network::SSL.server`.
304
+ #
305
+ # @param [Hash{Symbol => Object}] kwargs
306
+ # Additional keyword arguments for {Server#initialize}.
307
+ #
308
+ # @!macro server_kwargs
309
+ #
310
+ # @yield [client]
311
+ # The given block will be passed the newly connected WebSocket
312
+ # client. After the block has finished, the WebSocket client will be
313
+ # closed.
314
+ #
315
+ # @yieldparam [Server::Client] client
316
+ # A newly connected WebSocket client.
317
+ #
318
+ # @return [nil]
319
+ #
320
+ # @api public
321
+ #
322
+ # @see WebSocket.accept
323
+ #
324
+ def websocket_accept(url, ssl: {}, **kwargs,&block)
325
+ WebSocket.accept(url, ssl: ssl, **kwargs,&block)
326
+ end
327
+
328
+ #
329
+ # Tests whether the WebSocket is open.
330
+ #
331
+ # @param [String] host
332
+ # The WebSocket host.
333
+ #
334
+ # @param [Integer] port
335
+ # The WebSocket port.
336
+ #
337
+ # @param [Hash{Symbol => Object}] ssl
338
+ # Additional keyword arguments for
339
+ # `Ronin::Support::Network::SSL.connect`.
340
+ #
341
+ # @param [Hash{Symbol => Object}] kwargs
342
+ # Additional keyword arguments for {Client#initialize}.
343
+ #
344
+ # @!macro client_kwargs
345
+ #
346
+ # @return [Boolean, nil]
347
+ # Specifies whether the WebSocket is open.
348
+ # If the connection was not accepted, `nil` will be returned.
349
+ #
350
+ # @api public
351
+ #
352
+ # @see WebSocket.open?
353
+ #
354
+ def ws_open?(host,port=80, ssl: {}, **kwargs)
355
+ uri = URI::WS.build(host: host, port: port)
356
+
357
+ WebSocket.open?(uri, ssl: ssl, **kwargs)
358
+ end
359
+
360
+ #
361
+ # Connects to a WebSocket.
362
+ #
363
+ # @param [String] host
364
+ # The WebSocket host.
365
+ #
366
+ # @param [Integer] port
367
+ # The WebSocket port.
368
+ #
369
+ # @param [Hash{Symbol => Object}] ssl
370
+ # Additional keyword arguments for
371
+ # `Ronin::Support::Network::SSL.connect`.
372
+ #
373
+ # @param [Hash{Symbol => Object}] kwargs
374
+ # Additional keyword arguments for {Client#initialize}.
375
+ #
376
+ # @!macro client_kwargs
377
+ #
378
+ # @yield [websocket]
379
+ # If a block is given, then it will be passed the WebSocket
380
+ # connection. Once the block has returned, the WebSocket connection
381
+ # will be closed.
382
+ #
383
+ # @yieldparam [Client] websocket
384
+ # The WebSocket connection.
385
+ #
386
+ # @return [Client]
387
+ # The WebSocket connection.
388
+ #
389
+ # @example Connecting to a WebSocket server:
390
+ # ws_connect('ws://websocket-echo.com')
391
+ # # => #<Ronin::Support::Web::WebSocket::Client: ...>
392
+ #
393
+ # @example Creating a temporary WebSocket connection:
394
+ # ws_connect('ws://websocket-echo.com') do |websocket|
395
+ # # ...
396
+ # end
397
+ #
398
+ # @api public
399
+ #
400
+ # @see WebSocket.connect
401
+ #
402
+ def ws_connect(host,port=80, ssl: {}, **kwargs,&block)
403
+ uri = URI::WS.build(host: host, port: port)
404
+
405
+ WebSocket.connect(uri, ssl: ssl, **kwargs, &block)
406
+ end
407
+
408
+ #
409
+ # Connects to the WebSocket and sends the data.
410
+ #
411
+ # @param [String] data
412
+ # The data to send to the WebSocket.
413
+ #
414
+ # @param [String] host
415
+ # The WebSocket host.
416
+ #
417
+ # @param [Integer] port
418
+ # The WebSocket port.
419
+ #
420
+ # @param [:text, :binary, :ping, :pong, :close] type
421
+ # The data frame type.
422
+ #
423
+ # @param [Hash{Symbol => Object}] ssl
424
+ # Additional keyword arguments for
425
+ # `Ronin::Support::Network::SSL.connect`.
426
+ #
427
+ # @param [Hash{Symbol => Object}] kwargs
428
+ # Additional keyword arguments for {Client#initialize}.
429
+ #
430
+ # @!macro client_kwargs
431
+ #
432
+ # @yield [websocket]
433
+ # If a block is given, then it will be passed the WebSocket
434
+ # connection. Once the block has returned, the WebSocket connection
435
+ # will be closed.
436
+ #
437
+ # @yieldparam [Client] websocket
438
+ # The WebSocket connection.
439
+ #
440
+ # @return [Client]
441
+ # The WebSocket connection.
442
+ #
443
+ # @api public
444
+ #
445
+ # @see WebSocket.connect_and_send
446
+ #
447
+ def ws_connect_and_send(data,host,port=80, type: :text, ssl: {}, **kwargs,&block)
448
+ uri = URI::WS.build(host: host, port: port)
449
+
450
+ WebSocket.connect_and_send(data,uri, type: type, ssl: ssl, **kwargs,&block)
451
+ end
452
+
453
+ #
454
+ # Connects to the WebSocket, sends the data, and closes the
455
+ # connection.
456
+ #
457
+ # @param [String] data
458
+ # The data to send to the WebSocket.
459
+ #
460
+ # @param [String] host
461
+ # The WebSocket host.
462
+ #
463
+ # @param [Integer] port
464
+ # The WebSocket port.
465
+ #
466
+ # @param [:text, :binary, :ping, :pong, :close] type
467
+ # The data frame type.
468
+ #
469
+ # @param [Hash{Symbol => Object}] ssl
470
+ # Additional keyword arguments for
471
+ # `Ronin::Support::Network::SSL.connect`.
472
+ #
473
+ # @param [Hash{Symbol => Object}] kwargs
474
+ # Additional keyword arguments for {Client#initialize}.
475
+ #
476
+ # @!macro client_kwargs
477
+ #
478
+ # @return [true]
479
+ #
480
+ # @api public
481
+ #
482
+ # @see WebSocket.send
483
+ #
484
+ def ws_send(data,host,port=80, type: :text, ssl: {}, **kwargs)
485
+ uri = URI::WS.build(host: host, port: port)
486
+
487
+ WebSocket.send(data,uri, type: type, ssl: ssl, **kwargs)
488
+ end
489
+
490
+ #
491
+ # Tests whether the SSL/TLS WebSocket is open.
492
+ #
493
+ # @param [String] host
494
+ # The WebSocket host.
495
+ #
496
+ # @param [Integer] port
497
+ # The WebSocket port.
498
+ #
499
+ # @param [Hash{Symbol => Object}] ssl
500
+ # Additional keyword arguments for
501
+ # `Ronin::Support::Network::SSL.connect`.
502
+ #
503
+ # @param [Hash{Symbol => Object}] kwargs
504
+ # Additional keyword arguments for {Client#initialize}.
505
+ #
506
+ # @!macro client_kwargs
507
+ #
508
+ # @return [Boolean, nil]
509
+ # Specifies whether the WebSocket is open.
510
+ # If the connection was not accepted, `nil` will be returned.
511
+ #
512
+ # @api public
513
+ #
514
+ # @see WebSocket.open?
515
+ #
516
+ def wss_open?(host,port=443, ssl: {}, **kwargs)
517
+ uri = URI::WSS.build(host: host, port: port)
518
+
519
+ WebSocket.open?(uri, ssl: ssl, **kwargs)
520
+ end
521
+
522
+ #
523
+ # Connects to a SSL/TLS WebSocket.
524
+ #
525
+ # @param [String] host
526
+ # The WebSocket host.
527
+ #
528
+ # @param [Integer] port
529
+ # The WebSocket port.
530
+ #
531
+ # @param [Hash{Symbol => Object}] ssl
532
+ # Additional keyword arguments for
533
+ # `Ronin::Support::Network::SSL.connect`.
534
+ #
535
+ # @param [Hash{Symbol => Object}] kwargs
536
+ # Additional keyword arguments for {Client#initialize}.
537
+ #
538
+ # @!macro client_kwargs
539
+ #
540
+ # @yield [websocket]
541
+ # If a block is given, then it will be passed the WebSocket
542
+ # connection. Once the block has returned, the WebSocket connection
543
+ # will be closed.
544
+ #
545
+ # @yieldparam [Client] websocket
546
+ # The WebSocket connection.
547
+ #
548
+ # @return [Client]
549
+ # The WebSocket connection.
550
+ #
551
+ # @example Connecting to a WebSocket server:
552
+ # wss_connect('websocket-echo.com')
553
+ # # => #<Ronin::Support::Web::WebSocket::Client: ...>
554
+ #
555
+ # @example Creating a temporary WebSocket connection:
556
+ # wss_connect('websocket-echo.com') do |websocket|
557
+ # # ...
558
+ # end
559
+ #
560
+ # @api public
561
+ #
562
+ # @see WebSocket.connect
563
+ #
564
+ def wss_connect(host,port=443, ssl: {}, **kwargs,&block)
565
+ uri = URI::WSS.build(host: host, port: port)
566
+
567
+ WebSocket.connect(uri, ssl: ssl, **kwargs, &block)
568
+ end
569
+
570
+ #
571
+ # Connects to the SSL/TLS WebSocket and sends the data.
572
+ #
573
+ # @param [String] data
574
+ # The data to send to the WebSocket.
575
+ #
576
+ # @param [String] host
577
+ # The WebSocket host.
578
+ #
579
+ # @param [Integer] port
580
+ # The WebSocket port.
581
+ #
582
+ # @param [:text, :binary, :ping, :pong, :close] type
583
+ # The data frame type.
584
+ #
585
+ # @param [Hash{Symbol => Object}] ssl
586
+ # Additional keyword arguments for
587
+ # `Ronin::Support::Network::SSL.connect`.
588
+ #
589
+ # @param [Hash{Symbol => Object}] kwargs
590
+ # Additional keyword arguments for {Client#initialize}.
591
+ #
592
+ # @!macro client_kwargs
593
+ #
594
+ # @yield [websocket]
595
+ # If a block is given, then it will be passed the WebSocket
596
+ # connection. Once the block has returned, the WebSocket connection
597
+ # will be closed.
598
+ #
599
+ # @yieldparam [Client] websocket
600
+ # The WebSocket connection.
601
+ #
602
+ # @return [Client]
603
+ # The WebSocket connection.
604
+ #
605
+ # @api public
606
+ #
607
+ # @see WebSocket.connect_and_send
608
+ #
609
+ def wss_connect_and_send(data,host,port=443, type: :text, ssl: {}, **kwargs,&block)
610
+ uri = URI::WSS.build(host: host, port: port)
611
+
612
+ WebSocket.connect_and_send(data,uri, type: type, ssl: ssl, **kwargs, &block)
613
+ end
614
+
615
+ #
616
+ # Connects to the SSL/TLS WebSocket, sends the data, and closes the
617
+ # connection.
618
+ #
619
+ # @param [String] data
620
+ # The data to send to the WebSocket.
621
+ #
622
+ # @param [String] host
623
+ # The WebSocket host.
624
+ #
625
+ # @param [Integer] port
626
+ # The WebSocket port.
627
+ #
628
+ # @param [:text, :binary, :ping, :pong, :close] type
629
+ # The data frame type.
630
+ #
631
+ # @param [Hash{Symbol => Object}] ssl
632
+ # Additional keyword arguments for
633
+ # `Ronin::Support::Network::SSL.connect`.
634
+ #
635
+ # @param [Hash{Symbol => Object}] kwargs
636
+ # Additional keyword arguments for {Client#initialize}.
637
+ #
638
+ # @!macro client_kwargs
639
+ #
640
+ # @return [true]
641
+ #
642
+ # @api public
643
+ #
644
+ # @see WebSocket.send
645
+ #
646
+ def wss_send(data,host,port=443, type: :text, ssl: {}, **kwargs)
647
+ uri = URI::WSS.build(host: host, port: port)
648
+
649
+ WebSocket.send(data,uri, type: type, ssl: ssl, **kwargs)
650
+ end
651
+
652
+ #
653
+ # Starts a WebSocket server.
654
+ #
655
+ # @param [String, nil] host
656
+ # The optional host that the WebSocket server will listen on.
657
+ #
658
+ # @param [Integer, nil] port
659
+ # The optional port that the WebSocket server will listen on.
660
+ #
661
+ # @param [Hash{Symbol => Object}] kwargs
662
+ # Additional keyword arguments for {Server#initialize}.
663
+ #
664
+ # @param [Hash{Symbol => Object}] ssl
665
+ # Additional keyword arguments for
666
+ # `Ronin::Support::Network::SSL.server`.
667
+ #
668
+ # @!macro server_kwargs
669
+ #
670
+ # @yield [server]
671
+ # If a block is given, then it will be passed the WebSocket server.
672
+ # Once the block has returned, the WebSocket server will be closed.
673
+ #
674
+ # @yieldparam [Server] server
675
+ # The WebSocket server.
676
+ #
677
+ # @return [Server]
678
+ # The WebSocket server.
679
+ #
680
+ # @api public
681
+ #
682
+ # @see WebSocker.server
683
+ #
684
+ def ws_server(host,port=80, ssl: {}, **kwargs,&block)
685
+ uri = URI::WS.build(host: host, port: port)
686
+
687
+ WebSocket.server(uri, ssl: ssl, **kwargs,&block)
688
+ end
689
+
690
+ #
691
+ # Creates a new WebSocket server listening on a given host and port,
692
+ # accepting clients in a loop.
693
+ #
694
+ # @param [String, nil] host
695
+ # The optional host that the WebSocket server will listen on.
696
+ #
697
+ # @param [Integer, nil] port
698
+ # The optional port that the WebSocket server will listen on.
699
+ #
700
+ # @param [Hash{Symbol => Object}] ssl
701
+ # Additional keyword arguments for
702
+ # `Ronin::Support::Network::SSL.server`.
703
+ #
704
+ # @param [Hash{Symbol => Object}] kwargs
705
+ # Additional keyword arguments for {Server#initialize}.
706
+ #
707
+ # @!macro server_kwargs
708
+ #
709
+ # @yield [client]
710
+ # The given block will be passed the newly connected WebSocket
711
+ # client. After the block has finished, the WebSocket client will be
712
+ # closed.
713
+ #
714
+ # @yieldparam [Server::Client] client
715
+ # A newly connected WebSocket client.
716
+ #
717
+ # @return [nil]
718
+ #
719
+ # @api public
720
+ #
721
+ # @see WebSocket.server_loop
722
+ #
723
+ def ws_server_loop(host,port=80, ssl: {}, **kwargs,&block)
724
+ uri = URI::WS.build(host: host, port: port)
725
+
726
+ WebSocket.server_loop(uri, ssl: ssl, **kwargs,&block)
727
+ end
728
+
729
+ #
730
+ # Opens a WebSocket server, accepts a single connection, yields it to
731
+ # the given block, then closes both the connection and the server.
732
+ #
733
+ # @param [String, nil] host
734
+ # The optional host that the WebSocket server will listen on.
735
+ #
736
+ # @param [Integer, nil] port
737
+ # The optional port that the WebSocket server will listen on.
738
+ #
739
+ # @param [Hash{Symbol => Object}] ssl
740
+ # Additional keyword arguments for
741
+ # `Ronin::Support::Network::SSL.server`.
742
+ #
743
+ # @param [Hash{Symbol => Object}] kwargs
744
+ # Additional keyword arguments for {Server#initialize}.
745
+ #
746
+ # @!macro server_kwargs
747
+ #
748
+ # @yield [client]
749
+ # The given block will be passed the newly connected WebSocket
750
+ # client. After the block has finished, the WebSocket client will be
751
+ # closed.
752
+ #
753
+ # @yieldparam [Server::Client] client
754
+ # A newly connected WebSocket client.
755
+ #
756
+ # @return [nil]
757
+ #
758
+ # @api public
759
+ #
760
+ # @see WebSocket.accept
761
+ #
762
+ def ws_accept(host,port=80, ssl: {}, **kwargs,&block)
763
+ uri = URI::WS.build(host: host, port: port)
764
+
765
+ WebSocket.accept(uri, ssl: ssl, **kwargs,&block)
766
+ end
767
+
768
+ #
769
+ # Starts a SSL/TLS WebSocket server.
770
+ #
771
+ # @param [String, nil] host
772
+ # The optional host that the WebSocket server will listen on.
773
+ #
774
+ # @param [Integer, nil] port
775
+ # The optional port that the WebSocket server will listen on.
776
+ #
777
+ # @param [Hash{Symbol => Object}] kwargs
778
+ # Additional keyword arguments for {Server#initialize}.
779
+ #
780
+ # @param [Hash{Symbol => Object}] ssl
781
+ # Additional keyword arguments for
782
+ # `Ronin::Support::Network::SSL.server`.
783
+ #
784
+ # @!macro server_kwargs
785
+ #
786
+ # @yield [server]
787
+ # If a block is given, then it will be passed the WebSocket server.
788
+ # Once the block has returned, the WebSocket server will be closed.
789
+ #
790
+ # @yieldparam [Server] server
791
+ # The WebSocket server.
792
+ #
793
+ # @return [Server]
794
+ # The WebSocket server.
795
+ #
796
+ # @api public
797
+ #
798
+ # @see WebSocket.server
799
+ #
800
+ def wss_server(host,port=443, ssl: {}, **kwargs,&block)
801
+ uri = URI::WSS.build(host: host, port: port)
802
+
803
+ WebSocket.server(uri, ssl: ssl, **kwargs,&block)
804
+ end
805
+
806
+ #
807
+ # Creates a new SSL/TLS WebSocket server listening on a given host
808
+ # and port, accepting clients in a loop.
809
+ #
810
+ # @param [String, nil] host
811
+ # The optional host that the WebSocket server will listen on.
812
+ #
813
+ # @param [Integer, nil] port
814
+ # The optional port that the WebSocket server will listen on.
815
+ #
816
+ # @param [Hash{Symbol => Object}] ssl
817
+ # Additional keyword arguments for
818
+ # `Ronin::Support::Network::SSL.server`.
819
+ #
820
+ # @param [Hash{Symbol => Object}] kwargs
821
+ # Additional keyword arguments for {Server#initialize}.
822
+ #
823
+ # @!macro server_kwargs
824
+ #
825
+ # @yield [client]
826
+ # The given block will be passed the newly connected WebSocket
827
+ # client. After the block has finished, the WebSocket client will be
828
+ # closed.
829
+ #
830
+ # @yieldparam [Server::Client] client
831
+ # A newly connected WebSocket client.
832
+ #
833
+ # @return [nil]
834
+ #
835
+ # @api public
836
+ #
837
+ # @see WebSocket.server_loop
838
+ #
839
+ def wss_server_loop(host,port=443, ssl: {}, **kwargs,&block)
840
+ uri = URI::WSS.build(host: host, port: port)
841
+
842
+ WebSocket.server_loop(uri, ssl: ssl, **kwargs,&block)
843
+ end
844
+
845
+ #
846
+ # Opens a SSL/TLS WebSocket server, accepts a single connection,
847
+ # yields it to the given block, then closes both the connection and
848
+ # the server.
849
+ #
850
+ # @param [String, nil] host
851
+ # The optional host that the WebSocket server will listen on.
852
+ #
853
+ # @param [Integer, nil] port
854
+ # The optional port that the WebSocket server will listen on.
855
+ #
856
+ # @param [Hash{Symbol => Object}] ssl
857
+ # Additional keyword arguments for
858
+ # `Ronin::Support::Network::SSL.server`.
859
+ #
860
+ # @param [Hash{Symbol => Object}] kwargs
861
+ # Additional keyword arguments for {Server#initialize}.
862
+ #
863
+ # @!macro server_kwargs
864
+ #
865
+ # @yield [client]
866
+ # The given block will be passed the newly connected WebSocket
867
+ # client. After the block has finished, the WebSocket client will be
868
+ # closed.
869
+ #
870
+ # @yieldparam [Server::Client] client
871
+ # A newly connected WebSocket client.
872
+ #
873
+ # @return [nil]
874
+ #
875
+ # @api public
876
+ #
877
+ # @see WebSocket.accept
878
+ #
879
+ def wss_accept(host,port=443, ssl: {}, **kwargs,&block)
880
+ uri = URI::WSS.build(host: host, port: port)
881
+
882
+ WebSocket.accept(uri, ssl: ssl, **kwargs,&block)
883
+ end
884
+ end
885
+ end
886
+ end
887
+ end
888
+ end