-
Notifications
You must be signed in to change notification settings - Fork 122
/
web_socket_client.rb
198 lines (162 loc) · 4.91 KB
/
web_socket_client.rb
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
# frozen_string_literal: true
## This code is based on github.com/shokai/websocket-client-simple (MIT License)
require 'event_emitter'
require 'websocket'
require 'resolv'
require 'openssl'
require 'uri'
# Utility class for processing WebSocket messages.
#
# :reek:InstanceVariableAssumption for @frame and @handshake_finished.
# :reek:RepeatedConditional for secure? checks.
module Authentication
module AuthnK8s
class WebSocketClient
include EventEmitter
def self.connect(url, **options)
client = WebSocketClient.new(url, **options)
yield(client) if block_given?
client.connect
client
end
# Used by WebSocketClientEventHandler
attr_reader :handshake
def initialize(
url,
# Optional keyword arguments to configure the secure socket behavior
hostname: nil,
headers: nil,
cert_store: SecureTcpSocket.default_cert_store,
verify_mode: OpenSSL::SSL::VERIFY_PEER
)
# Parse the given url to ensure it's valid
@uri = URI.parse(url)
# Use the provided port or default to the standard ports
@port = @uri.port || (secure? ? 443 : 80)
@hostname = hostname
@headers = headers
@cert_store = cert_store
@verify_mode = verify_mode
end
def connect
# Do nothing if already connected
return if @socket
# Establish initial connection to server
open_socket
# If the connection uses TLS, establish the secure context
secure_socket if secure?
# Begin websocket IO loop in a separate thread
begin_event_loop
# Send initial websocket handshake
@handshake ||= WebSocket::Handshake::Client.new(
url: @uri.to_s,
headers: @headers
)
@socket.write(@handshake.to_s)
end
def send(data, opt = { type: :text })
return if !@handshake_finished || @closed
type = opt[:type]
frame = ::WebSocket::Frame::Outgoing::Client.new(
data: data,
type: type, version: @handshake.version
)
begin
@socket.write(frame.to_s)
rescue Errno::EPIPE => e
@pipe_broken = true
emit(:__close, e)
end
end
def close
return if @closed
unless @pipe_broken
send(nil, type: :close)
end
@closed = true
@socket&.close
@socket = nil
emit(:__close)
Thread.kill(@thread) if @thread
end
def open?
@handshake.finished? && !@closed
end
protected
# :reek:DuplicateMethodCall for @uri.host
def open_socket
@socket = if proxy_uri
ProxiedTcpSocket.new(
proxy_uri: proxy_uri,
destination_host: @uri.host,
destination_port: @port
).tcp_socket
else
TCPSocket.new(@uri.host, @port)
end
end
# Wrap the given tcp_socket in an SSL socket to secure the connection
def secure_socket
@socket = SecureTcpSocket.new(
socket: @socket,
hostname: @hostname || @uri.host,
cert_store: @cert_store,
verify_mode: @verify_mode
)
end
# This returns the proxy url relevant to the connection from the
# environment. If the server connection uses TLS, then use the
# https_proxy value, otherwise use the http_proxy value.
def proxy_uri
@proxy_uri ||= @uri.find_proxy
end
def secure?
%w[https wss].include?(@uri.scheme)
end
# :reek:TooManyStatements
def begin_event_loop
@handshake_finished = false
@pipe_broken = false
@closed = false
# Set up event handler with the websocket is closed
once(:__close) do |err|
close
emit(:close, err)
end
@thread = Thread.new do
until @closed
begin
unless recv_data = @socket.getc
sleep(1)
next
end
if @handshake_finished
process_incoming_data(recv_data)
else
process_handshake_data(recv_data)
end
rescue => e
emit(:error, e)
end
end
end
end
def process_incoming_data(recv_data)
# Initialize the frame if this is the first data received
@frame ||= WebSocket::Frame::Incoming::Client.new
# Add data to the frame and handle websocket events
@frame << recv_data
while msg = @frame.next
emit(:message, msg)
end
end
def process_handshake_data(recv_data)
@handshake << recv_data
# Continue waiting if the websocket handshake isn't finished
return unless @handshake.finished?
@handshake_finished = true
emit(:open)
end
end
end
end