Skip to content

Commit 390675e

Browse files
committed
Add a Value::WebSocket class
1 parent d862526 commit 390675e

File tree

2 files changed

+556
-0
lines changed

2 files changed

+556
-0
lines changed

lib/ronin/recon/values/web_socket.rb

+240
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
# frozen_string_literal: true
2+
#
3+
# ronin-recon - A micro-framework and tool for performing reconnaissance.
4+
#
5+
# Copyright (c) 2023-2024 Hal Brodigan ([email protected])
6+
#
7+
# ronin-recon 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-recon 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-recon. If not, see <https://www.gnu.org/licenses/>.
19+
#
20+
21+
require_relative '../value'
22+
23+
require 'uri'
24+
25+
module Ronin
26+
module Recon
27+
module Values
28+
#
29+
# Represents a WebSocket.
30+
#
31+
# @api public
32+
#
33+
# @since 0.2.0
34+
#
35+
class WebSocket < Value
36+
37+
# The parsed URI.
38+
#
39+
# @return [URI::WS, URI::WSS]
40+
attr_reader :uri
41+
42+
#
43+
# Initializes the WebSocket value.
44+
#
45+
# @param [URI::WS, URI::WSS, String] url
46+
#
47+
def initialize(url)
48+
@uri = URI(url)
49+
end
50+
51+
#
52+
# Indicates whether the WebSocket uses `ws://` or `wss://`.
53+
#
54+
# @return ['ws', 'wss']
55+
#
56+
def scheme
57+
@uri.scheme
58+
end
59+
60+
#
61+
# The WebSocket's hostname.
62+
#
63+
# @return [String]
64+
#
65+
def host
66+
@uri.host
67+
end
68+
69+
#
70+
# The WebSocket's port number.
71+
#
72+
# @return [Integer]
73+
#
74+
def port
75+
@uri.port
76+
end
77+
78+
#
79+
# The WebSocket's path.
80+
#
81+
# @return [String]
82+
#
83+
def path
84+
@uri.path
85+
end
86+
87+
#
88+
# The WebSocket's query
89+
#
90+
# @return [String]
91+
#
92+
def query
93+
@uri.query
94+
end
95+
96+
#
97+
# Initializes the 'ws://' WebSocket.
98+
#
99+
# @param [String] host
100+
# The WebSocket's host.
101+
#
102+
# @param [Integer] port
103+
# The WebSocket's port.
104+
#
105+
# @param [String] path
106+
# The WebSocket's path.
107+
#
108+
# @param [String] query
109+
# The WebSocket's query.
110+
#
111+
def self.ws(host,port=80,path=nil,query=nil)
112+
new(URI::WS.build(host: host, port: port, path: path, query: query))
113+
end
114+
115+
#
116+
# Initializes the 'wss://' WebSocket.
117+
#
118+
# @param [String] host
119+
# The WebSocket's host.
120+
#
121+
# @param [Integer] port
122+
# The WebSocket's port.
123+
#
124+
# @param [String] path
125+
# The WebSocket's path.
126+
#
127+
# @param [String] query
128+
# The WebSocket's query.
129+
#
130+
def self.wss(host,port=443,path=nil,query=nil)
131+
new(URI::WSS.build(host: host, port: port, path: path, query: query))
132+
end
133+
134+
#
135+
# Compares the WebSocket to another value.
136+
#
137+
# @param [Value] other
138+
#
139+
# @return [Boolean]
140+
#
141+
def eql?(other)
142+
self.class == other.class &&
143+
scheme == other.scheme &&
144+
host == other.host &&
145+
port == other.port &&
146+
path == other.path &&
147+
query == other.query
148+
end
149+
150+
#
151+
# Case equality method used for fuzzy matching.
152+
#
153+
# @param [Value] other
154+
# The other value to compare.
155+
#
156+
# @return [Boolean]
157+
# Imdicates whether the other value same as {WebSocket}
158+
#
159+
def ===(other)
160+
case other
161+
when WebSocket
162+
eql?(other)
163+
else
164+
false
165+
end
166+
end
167+
168+
#
169+
# The "hash" value of the WebSocket.
170+
#
171+
# @return [Integer]
172+
# The hash value of {#scheme}, {#host}, {#port}, {#path} and {#query}.
173+
#
174+
def hash
175+
[self.class, scheme, host, port, path, query].hash
176+
end
177+
178+
# Mapping of {#scheme} values to URI classes.
179+
#
180+
# @api private
181+
URI_CLASSES = {
182+
'wss' => URI::WSS,
183+
'ws' => URI::WS
184+
}
185+
186+
#
187+
# Converts the WebSocket to URI.
188+
#
189+
# @return [URI::WS, URI::WSS]
190+
# The URI object for the website.
191+
#
192+
def to_uri
193+
@uri
194+
end
195+
196+
#
197+
# Converts the WebSocket to a String.
198+
#
199+
# @return [String]
200+
# The base URL value for the WebSocket.
201+
#
202+
def to_s
203+
@uri.to_s
204+
end
205+
206+
#
207+
# Coerces the WebSocket value into JSON.
208+
#
209+
# @return [Hash{Symbol => Object}]
210+
# The Ruby Hash that will be converted into JSON.
211+
#
212+
def as_json
213+
{
214+
type: :web_socket,
215+
scheme: scheme,
216+
host: host,
217+
port: port,
218+
path: path,
219+
query: query
220+
}
221+
end
222+
223+
#
224+
# Returns the type or kind of recon value.
225+
#
226+
# @return [:web_socket]
227+
#
228+
# @note
229+
# This is used internally to map a recon value class to a printable
230+
# type.
231+
#
232+
# @api private
233+
#
234+
def self.value_type
235+
:web_socket
236+
end
237+
end
238+
end
239+
end
240+
end

0 commit comments

Comments
 (0)