Module: PryRemoteEm::Client
- Includes:
- EM::Deferrable, Pry::Helpers::BaseHelpers, Generic
- Defined in:
- lib/pry-remote-em/client.rb,
lib/pry-remote-em/client/proxy.rb,
lib/pry-remote-em/client/broker.rb,
lib/pry-remote-em/client/generic.rb,
lib/pry-remote-em/client/keyboard.rb
Defined Under Namespace
Modules: Broker, Generic, Keyboard, Proxy
Constant Summary
Constants included
from Proto
Proto::PREAMBLE, Proto::PREAMBLE_LEN, Proto::SEPERATOR, Proto::SEPERATOR_LEN
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Generic
#connection_completed, #log, #start_tls
Methods included from Proto
#receive_data, #receive_json, #receive_proxy_connection, #receive_register_server, #receive_shell_data, #receive_shell_sig, #receive_start_tls, #receive_unregister_server, #send_auth, #send_banner, #send_completion, #send_data, #send_heatbeat, #send_json, #send_msg, #send_msg_bcast, #send_prompt, #send_proxy_connection, #send_raw, #send_register_server, #send_server_list, #send_shell_cmd, #send_shell_result, #send_start_tls, #send_unregister_server
Instance Attribute Details
31
32
33
|
# File 'lib/pry-remote-em/client.rb', line 31
def opts
@opts
end
|
Class Method Details
.start(host = PryRemoteEm::DEFHOST, port = PryRemoteEM::DEFPORT, opts = {:tls => false}) ⇒ Object
20
21
22
23
24
25
26
27
28
|
# File 'lib/pry-remote-em/client.rb', line 20
def start(host = PryRemoteEm::DEFHOST, port = PryRemoteEM::DEFPORT, opts = {:tls => false})
EM.connect(host || PryRemoteEm::DEFHOST, port || PryRemoteEm::DEFPORT, PryRemoteEm::Client, opts) do |c|
c.callback { yield if block_given? }
c.errback do |e|
Kernel.puts "[pry-remote-em] connection failed\n#{e}"
yield(e) if block_given?
end
end
end
|
Instance Method Details
#authenticate ⇒ Object
218
219
220
221
222
223
224
|
# File 'lib/pry-remote-em/client.rb', line 218
def authenticate
return fail("[pry-remote-em] authentication required") unless @auth
return fail("[pry-remote-em] can't authenticate before negotiation complete") unless @negotiated
user, pass = @auth.call
return fail("[pry-remote-em] expected #{@auth} to return a user and password") unless user && pass
send_auth([user, pass])
end
|
#auto_complete(word) ⇒ Object
226
227
228
229
230
231
232
233
|
# File 'lib/pry-remote-em/client.rb', line 226
def auto_complete(word)
@waiting = Thread.current
EM.next_tick { send_completion(word) }
sleep
c = Thread.current[:completion]
Thread.current[:completion] = nil
c
end
|
#choose_server(list) ⇒ Object
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
|
# File 'lib/pry-remote-em/client.rb', line 95
def choose_server(list)
highline = HighLine.new
proxy = false
choice = nil
nm_col_len = list.values.map(&:length).sort[-1] + 5
ur_col_len = list.keys.map(&:length).sort[-1] + 5
= sprintf("| %-3s | %-#{nm_col_len}s | %-#{ur_col_len}s |", "", "name", "url")
border = ("-" * .length)
table = [border, , border]
list = list.to_a.map{|url, name| [URI.parse(url), name]}
list = filter_server_list(list)
list = sort_server_list(list)
list.each_with_index do |(url, name), idx|
table << sprintf("| %-2d | %-#{nm_col_len}s | %-#{ur_col_len}s |", idx + 1, name, url)
end
table << border
table = table.join("\n")
Kernel.puts table
while choice.nil?
if proxy
question = "(q) to quit; (r) to refresh (c) to connect\nproxy to: "
else
question = "(q) to quit; (r) to refresh (p) to proxy\nconnect to: "
end
if (choice = opts.delete(:proxy))
proxy = true
else
choice = opts.delete(:connect) || highline.ask(question)
proxy = false
end
return close_connection if ['q', 'quit', 'exit'].include?(choice.downcase)
if ['r', 'reload', 'refresh'].include?(choice.downcase)
send_server_list
return nil
end
if ['c', 'connect'].include?(choice.downcase)
proxy = false
choice = nil
next
end
if ['p', 'proxy'].include?(choice.downcase)
proxy = true
choice = nil
next
end
choice = choice.to_i > 0 ?
list[choice.to_i - 1] :
list.find{|(url, name)| url.to_s == choice || name == choice }
log.error("\033[31mserver not found\033[0m") unless choice
end
[choice, proxy]
end
|
#filter_server_list(list) ⇒ Object
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/pry-remote-em/client.rb', line 165
def filter_server_list(list)
if opts[:filter_host]
list = list.select { |url, name| url.host =~ opts[:filter_host] }
end
if opts[:filter_name]
list = list.select { |url, name| name =~ opts[:filter_name] }
end
if opts.include?(:filter_ssl)
list = opts[:filter_ssl] ?
list.select{|url, name| url.scheme == 'pryems' } :
list.select{|url, name| url.scheme == 'pryem' }
end
if list.empty?
log.info("\033[33m[pry-remote-em] no registered servers match the given filter\033[0m")
Process.exit
end
list
end
|
#initialize(opts = {}) ⇒ Object
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/pry-remote-em/client.rb', line 33
def initialize(opts = {})
@opts = opts
if (a = opts[:auth])
if a.respond_to?(:call)
@auth = a
else
@auth = lambda { a }
end
end
end
|
#post_init ⇒ Object
44
45
46
|
# File 'lib/pry-remote-em/client.rb', line 44
def post_init
Readline.completion_proc = method(:auto_complete)
end
|
#readline(prompt) ⇒ Object
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
273
274
275
276
277
|
# File 'lib/pry-remote-em/client.rb', line 246
def readline(prompt)
if @negotiated && !@unbound
op = proc { Readline.readline(prompt, !prompt.nil?) }
callback = proc do |l|
if '!!' == l[0..1]
send_msg_bcast(l[2..-1])
elsif '!' == l[0]
send_msg(l[1..-1])
elsif '.' == l[0]
if !Client.const_defined?(:Keyboard)
Kernel.puts "\033[31minteractive shell commands are not supported without termios\033[0m"
readline(prompt)
else
send_shell_cmd(l[1..-1])
if Gem.loaded_specs["eventmachine"].version < Gem::Version.new("1.0.0.beta4")
Kernel.puts "\033[1minteractive shell commands are not well supported when running on EventMachine prior to 1.0.0.beta4\033[0m"
else
@keyboard = EM.open_keyboard(Keyboard, self)
end
end
elsif 'reset' == l.strip
Kernel.puts "\033[1m#{$0} #{ARGV.join(' ')}\033[0m"
exec("#{$0} #{ARGV.join(' ')}")
else
send_raw(l)
end end
EM.defer(op, callback)
end
end
|
#receive_auth(a) ⇒ Object
184
185
186
187
188
|
# File 'lib/pry-remote-em/client.rb', line 184
def receive_auth(a)
return fail a if a.is_a?(String)
return authenticate if a == false
@authenticated = true if a == true
end
|
#receive_banner(name, version, scheme) ⇒ Object
70
71
72
73
74
75
|
# File 'lib/pry-remote-em/client.rb', line 70
def receive_banner(name, version, scheme)
if super(name, version, scheme)
start_tls if @opts[:tls]
end
end
|
#receive_completion(c) ⇒ Object
235
236
237
238
239
240
|
# File 'lib/pry-remote-em/client.rb', line 235
def receive_completion(c)
return unless @waiting
@waiting[:completion] = c
@waiting, t = nil, @waiting
t.run
end
|
#receive_msg(m) ⇒ Object
190
191
192
|
# File 'lib/pry-remote-em/client.rb', line 190
def receive_msg(m)
Kernel.puts "\033[1m! msg: " + m + "\033[0m"
end
|
#receive_msg_bcast(mb) ⇒ Object
194
195
196
|
# File 'lib/pry-remote-em/client.rb', line 194
def receive_msg_bcast(mb)
Kernel.puts "\033[1m!! msg: " + mb + "\033[0m"
end
|
#receive_prompt(p) ⇒ Object
242
243
244
|
# File 'lib/pry-remote-em/client.rb', line 242
def receive_prompt(p)
readline(p)
end
|
#receive_raw(r) ⇒ Object
209
210
211
212
|
# File 'lib/pry-remote-em/client.rb', line 209
def receive_raw(r)
stagger_output(r, $stdout)
end
|
#receive_server_list(list) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/pry-remote-em/client.rb', line 77
def receive_server_list(list)
if list.empty?
log.info("\033[33m[pry-remote-em] no servers are registered with the broker\033[0m")
Process.exit
end
choice, proxy = choose_server(list)
return unless choice
uri, name = choice[0], choice[1]
if proxy
@opts[:tls] = uri.scheme == 'pryems'
@negotiated = false
@tls_started = false
return send_proxy_connection(uri)
end
@reconnect_to = uri
close_connection
end
|
#receive_shell_cmd(c) ⇒ Object
198
199
200
|
# File 'lib/pry-remote-em/client.rb', line 198
def receive_shell_cmd(c)
Kernel.puts c
end
|
#receive_shell_result(c) ⇒ Object
202
203
204
205
206
207
|
# File 'lib/pry-remote-em/client.rb', line 202
def receive_shell_result(c)
if @keyboard
@keyboard.bufferio(true)
@keyboard.close_connection
end
end
|
#receive_unknown(j) ⇒ Object
214
215
216
|
# File 'lib/pry-remote-em/client.rb', line 214
def receive_unknown(j)
warn "[pry-remote-em] received unexpected data: #{j.inspect}"
end
|
#sort_server_list(list) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/pry-remote-em/client.rb', line 149
def sort_server_list(list)
type = opts[:sort] || :host
case type
when :name
list.sort { |a,b| a[1] <=> b[1] }
when :host
list.sort { |a,b| a[0].host <=> b[0].host }
when :ssl
list.sort { |a,b| b[0].scheme <=> a[0].scheme }
when :port
list.sort { |a,b| a[0].port <=> b[0].port }
else
list.sort { |a,b| a[0].host <=> b[0].host }
end
end
|
#ssl_handshake_completed ⇒ Object
48
49
50
51
|
# File 'lib/pry-remote-em/client.rb', line 48
def ssl_handshake_completed
log.info("[pry-remote-em] TLS connection established")
@opts[:tls] = true
end
|
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/pry-remote-em/client.rb', line 53
def unbind
if (uri = @reconnect_to)
@reconnect_to = nil
tls = uri.scheme == 'pryems'
log.info("\033[35m[pry-remote-em] connection will not be encrypted\033[0m") if @opts[:tls] && !tls
@opts[:tls] = tls
@tls_started = false
reconnect(uri.host, uri.port)
else
@unbound = true
log.info("[pry-remote-em] session terminated")
return succeed if Gem.loaded_specs["eventmachine"].version < Gem::Version.new("1.0.0.beta4")
error? ? fail : succeed
end
end
|