rex-core 0.1.23 → 0.1.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +1 -3
- data/lib/rex/core/version.rb +1 -1
- data/lib/rex/io/stream_server.rb +182 -202
- data.tar.gz.sig +0 -0
- metadata +1 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 879343c39c0c1769143a3d0b5c61ce102e26ac898a50e5eec9581bc2bee47c91
|
4
|
+
data.tar.gz: fca03a370a59dc3d03369fb29f47a0fb5e42dcb66db9d6e7b4f3b0824e9620f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fda24ae80de4cfda9e66f3bfe45b93bee23e4b35b57bb55f1fca0784721fa333154c8d6972a01594794a3b479e3a07c7809c0911a573b79315dfa8d01148faea
|
7
|
+
data.tar.gz: a6c8d8c2783a611b50f240e664ab2011da5e6ec5d46b3d7353e12cae0e56140f0ec33973fa7d25c0e10226a12b53815bc76fa3de6363678eb6b8e72cc2e7a569
|
checksums.yaml.gz.sig
CHANGED
data/lib/rex/core/version.rb
CHANGED
data/lib/rex/io/stream_server.rb
CHANGED
@@ -1,228 +1,208 @@
|
|
1
1
|
# -*- coding: binary -*-
|
2
|
-
require 'thread'
|
3
2
|
|
4
3
|
module Rex
|
5
|
-
module IO
|
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
|
-
if (on_client_connect_proc)
|
38
|
-
on_client_connect_proc.call(client)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
#
|
43
|
-
# This callback is notified when a client connection has data that needs to
|
44
|
-
# be processed.
|
45
|
-
#
|
46
|
-
def on_client_data(client)
|
47
|
-
if (on_client_data_proc)
|
48
|
-
on_client_data_proc.call(client)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
#
|
53
|
-
# This callback is notified when a client connection has closed.
|
54
|
-
#
|
55
|
-
def on_client_close(client)
|
56
|
-
if (on_client_close_proc)
|
57
|
-
on_client_close_proc.call(client)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
#
|
62
|
-
# Start monitoring the listener socket for connections and keep track of
|
63
|
-
# all client connections.
|
64
|
-
#
|
65
|
-
def start
|
66
|
-
self.clients = []
|
67
|
-
self.client_waiter = ::Queue.new
|
68
|
-
|
69
|
-
self.listener_thread = Rex::ThreadFactory.spawn("StreamServerListener", false) {
|
70
|
-
monitor_listener
|
71
|
-
}
|
72
|
-
self.clients_thread = Rex::ThreadFactory.spawn("StreamServerClientMonitor", false) {
|
73
|
-
monitor_clients
|
74
|
-
}
|
75
|
-
end
|
76
|
-
|
77
|
-
#
|
78
|
-
# Terminates the listener monitoring threads and closes all active clients.
|
79
|
-
#
|
80
|
-
def stop
|
81
|
-
self.listener_thread.kill
|
82
|
-
self.clients_thread.kill
|
83
|
-
|
84
|
-
self.clients.each { |cli|
|
85
|
-
close_client(cli)
|
86
|
-
}
|
87
|
-
end
|
4
|
+
module IO
|
5
|
+
###
|
6
|
+
#
|
7
|
+
# This mixin provides the framework and interface for implementing a streaming
|
8
|
+
# server that can listen for and accept stream client connections. Stream
|
9
|
+
# servers extend this class and are required to implement the following
|
10
|
+
# methods:
|
11
|
+
#
|
12
|
+
# accept
|
13
|
+
# fd
|
14
|
+
#
|
15
|
+
###
|
16
|
+
module StreamServer
|
17
|
+
##
|
18
|
+
#
|
19
|
+
# Abstract methods
|
20
|
+
#
|
21
|
+
##
|
22
|
+
|
23
|
+
##
|
24
|
+
#
|
25
|
+
# Default server monitoring and client management implementation follows
|
26
|
+
# below.
|
27
|
+
#
|
28
|
+
##
|
29
|
+
|
30
|
+
#
|
31
|
+
# This callback is notified when a client connects.
|
32
|
+
#
|
33
|
+
def on_client_connect(client)
|
34
|
+
on_client_connect_proc.call(client) if on_client_connect_proc
|
35
|
+
end
|
88
36
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
detach_client(client)
|
96
|
-
|
97
|
-
begin
|
98
|
-
client.close
|
99
|
-
rescue IOError
|
37
|
+
#
|
38
|
+
# This callback is notified when a client connection has data that needs to
|
39
|
+
# be processed.
|
40
|
+
#
|
41
|
+
def on_client_data(client)
|
42
|
+
on_client_data_proc.call(client) if on_client_data_proc
|
100
43
|
end
|
101
|
-
end
|
102
|
-
end
|
103
44
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
45
|
+
#
|
46
|
+
# This callback is notified when a client connection has closed.
|
47
|
+
#
|
48
|
+
def on_client_close(client)
|
49
|
+
on_client_close_proc.call(client) if on_client_close_proc
|
50
|
+
end
|
110
51
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
52
|
+
#
|
53
|
+
# Start monitoring the listener socket for connections and keep track of
|
54
|
+
# all client connections.
|
55
|
+
#
|
56
|
+
def start
|
57
|
+
self.clients = []
|
58
|
+
self.client_waiter = ::Queue.new
|
117
59
|
|
118
|
-
|
119
|
-
|
120
|
-
# Callback procedures.
|
121
|
-
#
|
122
|
-
##
|
123
|
-
|
124
|
-
#
|
125
|
-
# This callback procedure can be set and will be called when new clients
|
126
|
-
# connect.
|
127
|
-
#
|
128
|
-
attr_accessor :on_client_connect_proc
|
129
|
-
#
|
130
|
-
# This callback procedure can be set and will be called when clients
|
131
|
-
# have data to be processed.
|
132
|
-
#
|
133
|
-
attr_accessor :on_client_data_proc
|
134
|
-
#
|
135
|
-
# This callback procedure can be set and will be called when a client
|
136
|
-
# disconnects from the server.
|
137
|
-
#
|
138
|
-
attr_accessor :on_client_close_proc
|
139
|
-
|
140
|
-
attr_accessor :clients # :nodoc:
|
141
|
-
attr_accessor :listener_thread, :clients_thread # :nodoc:
|
142
|
-
attr_accessor :client_waiter
|
143
|
-
|
144
|
-
protected
|
145
|
-
|
146
|
-
#
|
147
|
-
# This method monitors the listener socket for new connections and calls
|
148
|
-
# the +on_client_connect+ callback routine.
|
149
|
-
#
|
150
|
-
def monitor_listener
|
151
|
-
|
152
|
-
while true
|
153
|
-
begin
|
154
|
-
cli = accept
|
155
|
-
if not cli
|
156
|
-
elog("The accept() returned nil in stream server listener monitor: #{fd.inspect}")
|
157
|
-
::IO.select(nil, nil, nil, 0.10)
|
158
|
-
next
|
60
|
+
self.listener_thread = Rex::ThreadFactory.spawn('StreamServerListener', false) do
|
61
|
+
monitor_listener
|
159
62
|
end
|
63
|
+
self.clients_thread = Rex::ThreadFactory.spawn('StreamServerClientMonitor', false) do
|
64
|
+
monitor_clients
|
65
|
+
end
|
66
|
+
end
|
160
67
|
|
161
|
-
|
162
|
-
|
68
|
+
#
|
69
|
+
# Terminates the listener monitoring threads and closes all active clients.
|
70
|
+
#
|
71
|
+
def stop
|
72
|
+
listener_thread.kill
|
73
|
+
clients_thread.kill
|
163
74
|
|
164
|
-
|
165
|
-
|
75
|
+
clients.each do |cli|
|
76
|
+
close_client(cli)
|
77
|
+
end
|
78
|
+
end
|
166
79
|
|
167
|
-
|
168
|
-
|
80
|
+
#
|
81
|
+
# This method closes a client connection and cleans up the resources
|
82
|
+
# associated with it.
|
83
|
+
#
|
84
|
+
def close_client(client)
|
85
|
+
if client
|
86
|
+
detach_client(client)
|
87
|
+
|
88
|
+
begin
|
89
|
+
client.close
|
90
|
+
rescue IOError
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
169
94
|
|
170
|
-
#
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
elog("Error in stream server server monitor: #{$!}")
|
176
|
-
rlog(ExceptionCallStack)
|
177
|
-
break
|
95
|
+
#
|
96
|
+
# Detach a client. You are now responsible for it, not us
|
97
|
+
#
|
98
|
+
def detach_client(client)
|
99
|
+
clients.delete(client)
|
178
100
|
end
|
179
|
-
end
|
180
|
-
end
|
181
101
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
begin
|
188
|
-
|
189
|
-
# Wait for a notify if our client list is empty
|
190
|
-
if (clients.length == 0)
|
191
|
-
self.client_waiter.pop
|
192
|
-
next
|
102
|
+
#
|
103
|
+
# This method waits on the server listener thread
|
104
|
+
#
|
105
|
+
def wait
|
106
|
+
listener_thread.join if listener_thread
|
193
107
|
end
|
194
108
|
|
195
|
-
|
109
|
+
##
|
110
|
+
#
|
111
|
+
# Callback procedures.
|
112
|
+
#
|
113
|
+
##
|
114
|
+
|
115
|
+
#
|
116
|
+
# This callback procedure can be set and will be called when new clients
|
117
|
+
# connect.
|
118
|
+
#
|
119
|
+
attr_accessor :on_client_connect_proc
|
120
|
+
#
|
121
|
+
# This callback procedure can be set and will be called when clients
|
122
|
+
# have data to be processed.
|
123
|
+
#
|
124
|
+
attr_accessor :on_client_data_proc
|
125
|
+
#
|
126
|
+
# This callback procedure can be set and will be called when a client
|
127
|
+
# disconnects from the server.
|
128
|
+
#
|
129
|
+
attr_accessor :on_client_close_proc
|
130
|
+
|
131
|
+
attr_accessor :clients, :listener_thread, :clients_thread, :client_waiter # :nodoc: # :nodoc:
|
132
|
+
|
133
|
+
protected
|
134
|
+
|
135
|
+
#
|
136
|
+
# This method monitors the listener socket for new connections and calls
|
137
|
+
# the +on_client_connect+ callback routine.
|
138
|
+
#
|
139
|
+
def monitor_listener
|
140
|
+
while true
|
141
|
+
begin
|
142
|
+
cli = accept
|
143
|
+
unless cli
|
144
|
+
elog("The accept() returned nil in stream server listener monitor: #{fd.inspect}")
|
145
|
+
::IO.select(nil, nil, nil, 0.10)
|
146
|
+
next
|
147
|
+
end
|
148
|
+
|
149
|
+
# Append to the list of clients
|
150
|
+
clients << cli
|
151
|
+
|
152
|
+
# Initialize the connection processing
|
153
|
+
on_client_connect(cli)
|
154
|
+
|
155
|
+
# Notify the client monitor
|
156
|
+
client_waiter.push(cli)
|
157
|
+
|
158
|
+
# Skip exceptions caused by accept() [ SSL ]
|
159
|
+
rescue ::EOFError, ::Errno::ECONNRESET, ::Errno::ENOTCONN, ::Errno::ECONNABORTED
|
160
|
+
rescue ::Interrupt
|
161
|
+
raise $!
|
162
|
+
rescue ::Exception
|
163
|
+
elog("Error in stream server server monitor: #{$!}")
|
164
|
+
rlog(ExceptionCallStack)
|
165
|
+
break
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
196
169
|
|
197
|
-
|
170
|
+
#
|
171
|
+
# This method monitors client connections for data and calls the
|
172
|
+
# +on_client_data+ routine when new data arrives.
|
173
|
+
#
|
174
|
+
def monitor_clients
|
198
175
|
begin
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
176
|
+
# Wait for a notify if our client list is empty
|
177
|
+
if clients.length == 0
|
178
|
+
client_waiter.pop
|
179
|
+
next
|
180
|
+
end
|
181
|
+
|
182
|
+
sd = Rex::ThreadSafe.select(clients, nil, nil, nil)
|
183
|
+
|
184
|
+
sd[0].each do |cfd|
|
185
|
+
on_client_data(cfd) if clients.include? cfd
|
186
|
+
rescue ::EOFError, ::Errno::ECONNRESET, ::Errno::ENOTCONN, ::Errno::ECONNABORTED
|
187
|
+
on_client_close(cfd)
|
188
|
+
close_client(cfd)
|
189
|
+
rescue ::Interrupt
|
190
|
+
raise $!
|
191
|
+
rescue ::Exception
|
192
|
+
close_client(cfd)
|
193
|
+
elog("Error in stream server client monitor: #{$!}")
|
194
|
+
rlog(ExceptionCallStack)
|
195
|
+
end
|
196
|
+
rescue ::Rex::StreamClosedError => e
|
197
|
+
# Remove the closed stream from the list
|
198
|
+
detach_client(e.stream)
|
203
199
|
rescue ::Interrupt
|
204
200
|
raise $!
|
205
201
|
rescue ::Exception
|
206
|
-
close_client(cfd)
|
207
202
|
elog("Error in stream server client monitor: #{$!}")
|
208
203
|
rlog(ExceptionCallStack)
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
rescue ::Rex::StreamClosedError => e
|
214
|
-
# Remove the closed stream from the list
|
215
|
-
detach_client(e.stream)
|
216
|
-
rescue ::Interrupt
|
217
|
-
raise $!
|
218
|
-
rescue ::Exception
|
219
|
-
elog("Error in stream server client monitor: #{$!}")
|
220
|
-
rlog(ExceptionCallStack)
|
221
|
-
end while true
|
204
|
+
end while true
|
205
|
+
end
|
206
|
+
end
|
222
207
|
end
|
223
|
-
|
224
208
|
end
|
225
|
-
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
metadata.gz.sig
CHANGED
Binary file
|