CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/ui/text/bidirectional_pipe.rb
Views: 11655
1
# -*- coding: binary -*-
2
module Rex
3
module Ui
4
module Text
5
6
7
class BidirectionalPipe < Rex::Ui::Text::Input
8
9
def initialize
10
@subscribers_out = {}
11
@subscribers_ref = {}
12
@subscribers_idx = 0
13
@pipe_input = Rex::Ui::Text::Input::Buffer.new
14
15
# We are the shell, the input, and the output
16
self.output = self
17
self.input = self
18
end
19
20
def pipe_input
21
@pipe_input
22
end
23
24
def close
25
@pipe_input.close
26
end
27
28
def has_subscriber?(id)
29
@subscribers_out.has_key?(id)
30
end
31
32
def create_subscriber(id=nil)
33
id ||= (@subscribers_idx += 1).to_s
34
@subscribers_out[id] = Rex::Ui::Text::Output::Buffer.new
35
return id
36
end
37
38
def create_subscriber_proc(id=nil, &block)
39
id = create_subscriber(id)
40
@subscribers_ref[id] = block
41
end
42
43
def remove_subscriber(id)
44
@subscribers_out.delete(id)
45
@subscribers_ref.delete(id)
46
end
47
48
def write_input(buf)
49
@pipe_input.put(buf)
50
end
51
52
def read_subscriber(id)
53
output = @subscribers_out[id]
54
55
return '' if output.nil?
56
57
buf = output.buf
58
59
output.reset
60
61
buf
62
end
63
64
def print(msg='')
65
@subscribers_out.each_pair { |id, buf|
66
begin
67
@subscribers_ref[id] ? @subscribers_ref[id].call(msg) : buf.print(msg)
68
rescue ::Exception => e
69
# $stderr.puts "Error handling subscriber #{id}: #{e} #{e.backtrace.inspect}"
70
raise e
71
end
72
}
73
msg
74
end
75
76
def print_error(msg='')
77
print_line('[-] ' + msg)
78
end
79
80
alias_method :print_bad, :print_error
81
82
def print_line(msg='')
83
print(msg + "\n")
84
end
85
86
def print_good(msg='')
87
print_line('[+] ' + msg)
88
end
89
90
def flush
91
end
92
93
def print_status(msg='')
94
print_line('[*] ' + msg)
95
end
96
97
def print_warning(msg='')
98
print_line('[!] ' + msg)
99
end
100
101
#
102
# Wrappers for the pipe_input methods
103
#
104
105
def close
106
@pipe_input.close
107
end
108
109
def sysread(len = 1)
110
@pipe_input.sysread(len)
111
end
112
113
def put(msg)
114
@pipe_input.put(msg)
115
end
116
117
def gets
118
@pipe_input.gets
119
end
120
121
def eof?
122
@pipe_input.eof?
123
end
124
125
def fd
126
@pipe_input.fd
127
end
128
129
#
130
# Wrappers for shell methods
131
#
132
133
attr_accessor :output, :prompt, :input
134
135
def intrinsic_shell?
136
true
137
end
138
139
def supports_readline
140
false
141
end
142
143
def supports_color?
144
false
145
end
146
147
def pgets
148
gets
149
end
150
151
152
protected
153
154
end
155
156
end
157
end
158
end
159
160