@@ -17,20 +17,18 @@ def initialize(url, protocols = nil, options = {})
17
17
super ( options ) { ::WebSocket ::Driver . client ( self , :max_length => options [ :max_length ] , :protocols => protocols ) }
18
18
19
19
proxy = options . fetch ( :proxy , { } )
20
- endpoint = URI . parse ( proxy [ :origin ] || @url )
21
- port = endpoint . port || DEFAULT_PORTS [ endpoint . scheme ]
22
- @secure = SECURE_PROTOCOLS . include? ( endpoint . scheme )
20
+ @endpoint = URI . parse ( proxy [ :origin ] || @url )
21
+ port = @endpoint . port || DEFAULT_PORTS [ @endpoint . scheme ]
23
22
@origin_tls = options . fetch ( :tls , { } )
24
23
@socket_tls = proxy [ :origin ] ? proxy . fetch ( :tls , { } ) : @origin_tls
25
24
26
25
configure_proxy ( proxy )
27
26
28
- EventMachine . connect ( endpoint . host , port , Connection ) do |conn |
27
+ EventMachine . connect ( @ endpoint. host , port , Connection ) do |conn |
29
28
conn . parent = self
30
29
end
31
30
rescue => error
32
- emit_error ( "Network error: #{ url } : #{ error . message } " )
33
- finalize_close
31
+ on_network_error ( error )
34
32
end
35
33
36
34
private
@@ -46,38 +44,60 @@ def configure_proxy(proxy)
46
44
end
47
45
48
46
@proxy . on ( :connect ) do
49
- uri = URI . parse ( @url )
50
- secure = SECURE_PROTOCOLS . include? ( uri . scheme )
51
47
@proxy = nil
52
-
53
- if secure
54
- origin_tls = { :sni_hostname => uri . host } . merge ( @origin_tls )
55
- @stream . start_tls ( origin_tls )
56
- end
57
-
48
+ start_tls ( URI . parse ( @url ) , @origin_tls )
58
49
@driver . start
59
50
end
60
51
end
61
52
53
+ def start_tls ( uri , options )
54
+ return unless SECURE_PROTOCOLS . include? ( uri . scheme )
55
+
56
+ tls_options = { :sni_hostname => uri . host , :verify_peer => true } . merge ( options )
57
+ @ssl_verifier = SslVerifier . new ( uri . host , tls_options )
58
+ @stream . start_tls ( tls_options )
59
+ end
60
+
62
61
def on_connect ( stream )
63
62
@stream = stream
64
-
65
- if @secure
66
- socket_tls = { :sni_hostname => URI . parse ( @url ) . host } . merge ( @socket_tls )
67
- @stream . start_tls ( socket_tls )
68
- end
63
+ start_tls ( @endpoint , @socket_tls )
69
64
70
65
worker = @proxy || @driver
71
66
worker . start
72
67
end
73
68
69
+ def on_network_error ( error )
70
+ emit_error ( "Network error: #{ @url } : #{ error . message } " )
71
+ finalize_close
72
+ end
73
+
74
+ def ssl_verify_peer ( cert )
75
+ @ssl_verifier . ssl_verify_peer ( cert )
76
+ rescue => error
77
+ on_network_error ( error )
78
+ end
79
+
80
+ def ssl_handshake_completed
81
+ @ssl_verifier . ssl_handshake_completed
82
+ rescue => error
83
+ on_network_error ( error )
84
+ end
85
+
74
86
module Connection
75
87
attr_accessor :parent
76
88
77
89
def connection_completed
78
90
parent . __send__ ( :on_connect , self )
79
91
end
80
92
93
+ def ssl_verify_peer ( cert )
94
+ parent . __send__ ( :ssl_verify_peer , cert )
95
+ end
96
+
97
+ def ssl_handshake_completed
98
+ parent . __send__ ( :ssl_handshake_completed )
99
+ end
100
+
81
101
def receive_data ( data )
82
102
parent . __send__ ( :parse , data )
83
103
end
0 commit comments