ronin-support 0.4.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +11 -0
- data/.github/workflows/ruby.yml +28 -0
- data/.ruby-version +1 -0
- data/.yardopts +1 -1
- data/ChangeLog.md +121 -33
- data/Gemfile +17 -18
- data/README.md +57 -33
- data/Rakefile +10 -3
- data/gemspec.yml +16 -7
- data/lib/ronin/binary.rb +21 -0
- data/lib/ronin/binary/hexdump.rb +20 -0
- data/lib/ronin/binary/hexdump/parser.rb +403 -0
- data/lib/ronin/binary/struct.rb +567 -0
- data/lib/ronin/binary/template.rb +454 -0
- data/lib/ronin/extensions.rb +5 -5
- data/lib/ronin/extensions/enumerable.rb +5 -5
- data/lib/ronin/extensions/file.rb +39 -33
- data/lib/ronin/extensions/ip_addr.rb +29 -31
- data/lib/ronin/extensions/kernel.rb +5 -5
- data/lib/ronin/extensions/meta.rb +5 -5
- data/lib/ronin/extensions/regexp.rb +50 -5
- data/lib/ronin/extensions/resolv.rb +7 -9
- data/lib/ronin/extensions/string.rb +10 -10
- data/lib/ronin/formatting.rb +5 -5
- data/lib/ronin/formatting/binary.rb +5 -5
- data/lib/ronin/formatting/digest.rb +5 -5
- data/lib/ronin/formatting/extensions.rb +5 -5
- data/lib/ronin/formatting/extensions/binary.rb +7 -5
- data/lib/ronin/formatting/extensions/binary/array.rb +61 -0
- data/lib/ronin/formatting/extensions/binary/base64.rb +106 -0
- data/lib/ronin/formatting/extensions/binary/file.rb +44 -11
- data/lib/ronin/formatting/extensions/binary/float.rb +65 -0
- data/lib/ronin/formatting/extensions/binary/integer.rb +66 -50
- data/lib/ronin/formatting/extensions/binary/string.rb +81 -205
- data/lib/ronin/formatting/extensions/digest.rb +5 -5
- data/lib/ronin/formatting/extensions/digest/file.rb +5 -5
- data/lib/ronin/formatting/extensions/digest/string.rb +5 -5
- data/lib/ronin/formatting/extensions/html.rb +5 -5
- data/lib/ronin/formatting/extensions/html/integer.rb +9 -13
- data/lib/ronin/formatting/extensions/html/string.rb +31 -39
- data/lib/ronin/formatting/extensions/http.rb +5 -5
- data/lib/ronin/formatting/extensions/http/integer.rb +6 -6
- data/lib/ronin/formatting/extensions/http/string.rb +7 -7
- data/lib/ronin/formatting/extensions/sql.rb +5 -5
- data/lib/ronin/formatting/extensions/sql/string.rb +22 -24
- data/lib/ronin/formatting/extensions/text.rb +5 -5
- data/lib/ronin/formatting/extensions/text/array.rb +13 -11
- data/lib/ronin/formatting/extensions/text/string.rb +70 -13
- data/lib/ronin/formatting/html.rb +5 -5
- data/lib/ronin/formatting/http.rb +5 -5
- data/lib/ronin/formatting/sql.rb +5 -5
- data/lib/ronin/formatting/text.rb +5 -5
- data/lib/ronin/fuzzing.rb +5 -5
- data/lib/ronin/fuzzing/extensions.rb +5 -5
- data/lib/ronin/fuzzing/extensions/string.rb +42 -213
- data/lib/ronin/fuzzing/fuzzer.rb +110 -0
- data/lib/ronin/fuzzing/fuzzing.rb +33 -26
- data/lib/ronin/fuzzing/mutator.rb +161 -0
- data/lib/ronin/fuzzing/repeater.rb +81 -0
- data/lib/ronin/fuzzing/template.rb +133 -0
- data/lib/ronin/mixin.rb +2 -2
- data/lib/ronin/network.rb +7 -5
- data/lib/ronin/network/dns.rb +64 -24
- data/lib/ronin/network/esmtp.rb +5 -5
- data/lib/ronin/network/extensions.rb +5 -5
- data/lib/ronin/network/extensions/dns.rb +5 -5
- data/lib/ronin/network/extensions/dns/net.rb +5 -5
- data/lib/ronin/network/extensions/esmtp.rb +5 -5
- data/lib/ronin/network/extensions/esmtp/net.rb +5 -5
- data/lib/ronin/network/extensions/http.rb +5 -5
- data/lib/ronin/network/extensions/http/net.rb +5 -5
- data/lib/ronin/network/extensions/http/uri/http.rb +5 -5
- data/lib/ronin/network/extensions/imap.rb +5 -5
- data/lib/ronin/network/extensions/imap/net.rb +5 -5
- data/lib/ronin/network/extensions/pop3.rb +5 -5
- data/lib/ronin/network/extensions/pop3/net.rb +5 -5
- data/lib/ronin/network/extensions/smtp.rb +5 -5
- data/lib/ronin/network/extensions/smtp/net.rb +5 -5
- data/lib/ronin/network/extensions/ssl.rb +5 -5
- data/lib/ronin/network/extensions/ssl/net.rb +5 -5
- data/lib/ronin/network/extensions/tcp.rb +5 -5
- data/lib/ronin/network/extensions/tcp/net.rb +5 -5
- data/lib/ronin/network/extensions/telnet.rb +5 -5
- data/lib/ronin/network/extensions/telnet/net.rb +5 -5
- data/lib/ronin/network/extensions/udp.rb +5 -5
- data/lib/ronin/network/extensions/udp/net.rb +5 -5
- data/lib/ronin/network/ftp.rb +149 -0
- data/lib/ronin/network/http.rb +5 -5
- data/lib/ronin/network/http/exceptions.rb +5 -5
- data/lib/ronin/network/http/exceptions/unknown_request.rb +5 -5
- data/lib/ronin/network/http/http.rb +65 -70
- data/lib/ronin/network/http/proxy.rb +5 -5
- data/lib/ronin/network/imap.rb +16 -15
- data/lib/ronin/network/mixins.rb +6 -5
- data/lib/ronin/network/mixins/dns.rb +5 -5
- data/lib/ronin/network/mixins/esmtp.rb +5 -5
- data/lib/ronin/network/mixins/ftp.rb +155 -0
- data/lib/ronin/network/mixins/http.rb +58 -587
- data/lib/ronin/network/mixins/imap.rb +5 -5
- data/lib/ronin/network/mixins/mixin.rb +5 -5
- data/lib/ronin/network/mixins/pop3.rb +5 -5
- data/lib/ronin/network/mixins/smtp.rb +5 -5
- data/lib/ronin/network/mixins/ssl.rb +5 -5
- data/lib/ronin/network/mixins/tcp.rb +43 -10
- data/lib/ronin/network/mixins/telnet.rb +5 -5
- data/lib/ronin/network/mixins/udp.rb +126 -6
- data/lib/ronin/network/mixins/unix.rb +279 -0
- data/lib/ronin/network/network.rb +5 -5
- data/lib/ronin/network/pop3.rb +10 -10
- data/lib/ronin/network/proxy.rb +578 -0
- data/lib/ronin/network/smtp.rb +5 -5
- data/lib/ronin/network/smtp/email.rb +6 -6
- data/lib/ronin/network/smtp/smtp.rb +12 -13
- data/lib/ronin/network/ssl.rb +16 -17
- data/lib/ronin/network/tcp.rb +7 -310
- data/lib/ronin/network/tcp/proxy.rb +417 -0
- data/lib/ronin/network/tcp/tcp.rb +452 -0
- data/lib/ronin/network/telnet.rb +34 -28
- data/lib/ronin/network/udp.rb +7 -271
- data/lib/ronin/network/udp/proxy.rb +191 -0
- data/lib/ronin/network/udp/udp.rb +452 -0
- data/lib/ronin/network/unix.rb +286 -0
- data/lib/ronin/path.rb +35 -39
- data/lib/ronin/spec/ui/output.rb +6 -12
- data/lib/ronin/support.rb +6 -5
- data/lib/ronin/support/inflector.rb +8 -12
- data/lib/ronin/support/support.rb +7 -5
- data/lib/ronin/support/version.rb +6 -6
- data/lib/ronin/templates.rb +5 -5
- data/lib/ronin/templates/erb.rb +5 -5
- data/lib/ronin/templates/template.rb +5 -5
- data/lib/ronin/ui/output.rb +5 -5
- data/lib/ronin/ui/output/helpers.rb +42 -28
- data/lib/ronin/ui/output/output.rb +17 -21
- data/lib/ronin/ui/output/terminal.rb +5 -5
- data/lib/ronin/ui/output/terminal/color.rb +15 -9
- data/lib/ronin/ui/output/terminal/raw.rb +5 -5
- data/lib/ronin/ui/shell.rb +8 -11
- data/lib/ronin/wordlist.rb +110 -30
- data/ronin-support.gemspec +39 -109
- data/spec/binary/hexdump/helpers/hexdumps.rb +13 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/ascii.bin +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_decimal_shorts.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_hex_bytes.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_hex_shorts.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_octal_bytes.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_octal_shorts.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_repeated.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_bytes.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_ints.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_quads.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_shorts.txt +0 -0
- data/spec/binary/hexdump/helpers/hexdumps/od_doubles.txt +17 -0
- data/spec/binary/hexdump/helpers/hexdumps/od_floats.txt +17 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_bytes.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_ints.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_quads.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_shorts.txt +0 -0
- data/spec/binary/hexdump/helpers/hexdumps/od_named_chars.txt +17 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_bytes.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_ints.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_quads.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_shorts.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_repeated.txt +0 -0
- data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/repeated.bin +0 -0
- data/spec/binary/hexdump/parser_spec.rb +302 -0
- data/spec/binary/struct_spec.rb +496 -0
- data/spec/binary/template_spec.rb +416 -0
- data/spec/extensions/enumerable_spec.rb +4 -4
- data/spec/extensions/file_spec.rb +12 -14
- data/spec/extensions/ip_addr_spec.rb +76 -50
- data/spec/extensions/kernel_spec.rb +7 -7
- data/spec/extensions/regexp_spec.rb +119 -59
- data/spec/extensions/resolv_spec.rb +2 -2
- data/spec/extensions/string_spec.rb +31 -30
- data/spec/formatting/binary/array_spec.rb +26 -0
- data/spec/formatting/binary/base64_spec.rb +50 -0
- data/spec/formatting/binary/float_spec.rb +36 -0
- data/spec/formatting/binary/integer_spec.rb +76 -50
- data/spec/formatting/binary/string_spec.rb +91 -198
- data/spec/formatting/digest/string_spec.rb +5 -5
- data/spec/formatting/html/integer_spec.rb +6 -6
- data/spec/formatting/html/string_spec.rb +10 -10
- data/spec/formatting/http/integer_spec.rb +3 -3
- data/spec/formatting/http/string_spec.rb +5 -5
- data/spec/formatting/sql/string_spec.rb +21 -19
- data/spec/formatting/text/array_spec.rb +15 -15
- data/spec/formatting/text/string_spec.rb +58 -28
- data/spec/fuzzing/extensions/string_spec.rb +87 -0
- data/spec/fuzzing/fuzzer_spec.rb +109 -0
- data/spec/fuzzing/fuzzing_spec.rb +24 -0
- data/spec/fuzzing/mutator_spec.rb +112 -0
- data/spec/fuzzing/repeater_spec.rb +57 -0
- data/spec/fuzzing/template_spec.rb +54 -0
- data/spec/mixin_spec.rb +10 -12
- data/spec/network/dns_spec.rb +89 -23
- data/spec/network/ftp_spec.rb +81 -0
- data/spec/network/http/http_spec.rb +237 -144
- data/spec/network/http/proxy_spec.rb +37 -37
- data/spec/network/network_spec.rb +2 -2
- data/spec/network/proxy_spec.rb +121 -0
- data/spec/network/shared/unix_server.rb +31 -0
- data/spec/network/smtp/email_spec.rb +14 -14
- data/spec/network/ssl_spec.rb +53 -3
- data/spec/network/tcp/proxy_spec.rb +118 -0
- data/spec/network/tcp/tcp_spec.rb +316 -0
- data/spec/network/telnet_spec.rb +67 -0
- data/spec/network/udp/udp_spec.rb +298 -0
- data/spec/network/unix_spec.rb +182 -0
- data/spec/path_spec.rb +43 -18
- data/spec/spec_helper.rb +2 -3
- data/spec/support/inflector_spec.rb +4 -4
- data/spec/support_spec.rb +1 -1
- data/spec/templates/erb_spec.rb +3 -3
- data/spec/templates/template_spec.rb +10 -10
- data/spec/ui/shell_spec.rb +15 -15
- data/spec/wordlist_spec.rb +80 -19
- metadata +176 -121
- data/.gemtest +0 -0
- data/spec/formatting/binary/helpers/hexdumps.rb +0 -16
- data/spec/fuzzing/string_spec.rb +0 -158
- data/spec/network/tcp_spec.rb +0 -247
- data/spec/network/udp_spec.rb +0 -248
@@ -1,20 +1,20 @@
|
|
1
1
|
#
|
2
|
-
# Copyright (c) 2006-
|
2
|
+
# Copyright (c) 2006-2021 Hal Brodigan (postmodern.mod3 at gmail.com)
|
3
3
|
#
|
4
|
-
# This file is part of
|
4
|
+
# This file is part of ronin-support.
|
5
5
|
#
|
6
|
-
#
|
6
|
+
# ronin-support is free software: you can redistribute it and/or modify
|
7
7
|
# it under the terms of the GNU Lesser General Public License as published
|
8
8
|
# by the Free Software Foundation, either version 3 of the License, or
|
9
9
|
# (at your option) any later version.
|
10
10
|
#
|
11
|
-
#
|
11
|
+
# ronin-support is distributed in the hope that it will be useful,
|
12
12
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
13
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
14
|
# GNU Lesser General Public License for more details.
|
15
15
|
#
|
16
16
|
# You should have received a copy of the GNU Lesser General Public License
|
17
|
-
# along with
|
17
|
+
# along with ronin-support. If not, see <https://www.gnu.org/licenses/>.
|
18
18
|
#
|
19
19
|
|
20
20
|
require 'ronin/extensions/ip_addr'
|
data/lib/ronin/network/pop3.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
#
|
2
|
-
# Copyright (c) 2006-
|
2
|
+
# Copyright (c) 2006-2021 Hal Brodigan (postmodern.mod3 at gmail.com)
|
3
3
|
#
|
4
|
-
# This file is part of
|
4
|
+
# This file is part of ronin-support.
|
5
5
|
#
|
6
|
-
#
|
6
|
+
# ronin-support is free software: you can redistribute it and/or modify
|
7
7
|
# it under the terms of the GNU Lesser General Public License as published
|
8
8
|
# by the Free Software Foundation, either version 3 of the License, or
|
9
9
|
# (at your option) any later version.
|
10
10
|
#
|
11
|
-
#
|
11
|
+
# ronin-support is distributed in the hope that it will be useful,
|
12
12
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
13
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
14
|
# GNU Lesser General Public License for more details.
|
15
15
|
#
|
16
16
|
# You should have received a copy of the GNU Lesser General Public License
|
17
|
-
# along with
|
17
|
+
# along with ronin-support. If not, see <https://www.gnu.org/licenses/>.
|
18
18
|
#
|
19
19
|
|
20
20
|
require 'net/pop'
|
@@ -34,7 +34,7 @@ module Ronin
|
|
34
34
|
#
|
35
35
|
# @api public
|
36
36
|
#
|
37
|
-
def
|
37
|
+
def self.default_port
|
38
38
|
@default_port ||= DEFAULT_PORT
|
39
39
|
end
|
40
40
|
|
@@ -46,7 +46,7 @@ module Ronin
|
|
46
46
|
#
|
47
47
|
# @api public
|
48
48
|
#
|
49
|
-
def
|
49
|
+
def self.default_port=(port)
|
50
50
|
@default_port = port
|
51
51
|
end
|
52
52
|
|
@@ -80,9 +80,9 @@ module Ronin
|
|
80
80
|
# @api public
|
81
81
|
#
|
82
82
|
def pop3_connect(host,options={})
|
83
|
-
host
|
84
|
-
port
|
85
|
-
user
|
83
|
+
host = host.to_s
|
84
|
+
port = (options[:port] || POP3.default_port)
|
85
|
+
user = options[:user]
|
86
86
|
password = options[:password]
|
87
87
|
|
88
88
|
session = Net::POP3.start(host,port,user,password)
|
@@ -0,0 +1,578 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (c) 2006-2021 Hal Brodigan (postmodern.mod3 at gmail.com)
|
3
|
+
#
|
4
|
+
# This file is part of ronin-support.
|
5
|
+
#
|
6
|
+
# ronin-support is free software: you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU Lesser General Public License as published
|
8
|
+
# by the Free Software Foundation, either version 3 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# ronin-support is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public License
|
17
|
+
# along with ronin-support. If not, see <https://www.gnu.org/licenses/>.
|
18
|
+
#
|
19
|
+
|
20
|
+
module Ronin
|
21
|
+
module Network
|
22
|
+
#
|
23
|
+
# Base class for {TCP::Proxy TCP} and {UDP::Proxy UDP} Proxies.
|
24
|
+
#
|
25
|
+
# ## Callbacks
|
26
|
+
#
|
27
|
+
# The Proxy base class supports several callbacks for proxy events.
|
28
|
+
#
|
29
|
+
# ### client_data
|
30
|
+
#
|
31
|
+
# When a client sends data to the proxy.
|
32
|
+
#
|
33
|
+
# on_client_data do |client,server,data|
|
34
|
+
# data.gsub!(/foo/,'bar')
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# ### server_data
|
38
|
+
#
|
39
|
+
# When the server sends data to the proxy.
|
40
|
+
#
|
41
|
+
# on_server_data do |client,server,data|
|
42
|
+
# data.gsub!(/foo/,'bar')
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# ### data
|
46
|
+
#
|
47
|
+
# Alias for {#on_client_data} and {#on_server_data}.
|
48
|
+
#
|
49
|
+
# ## Actions
|
50
|
+
#
|
51
|
+
# The Proxy base class also provides methods to change how events are
|
52
|
+
# handled.
|
53
|
+
#
|
54
|
+
# * {#ignore!}
|
55
|
+
# * {#close!}
|
56
|
+
# * {#reset!}
|
57
|
+
#
|
58
|
+
# @since 0.5.0
|
59
|
+
#
|
60
|
+
class Proxy
|
61
|
+
|
62
|
+
# Default host to bind to
|
63
|
+
DEFAULT_HOST = '0.0.0.0'
|
64
|
+
|
65
|
+
DEFAULT_BUFFER_SIZE = 4096
|
66
|
+
|
67
|
+
# The host the proxy will listen on
|
68
|
+
attr_reader :host
|
69
|
+
|
70
|
+
# The port the proxy will listen on
|
71
|
+
attr_reader :port
|
72
|
+
|
73
|
+
# The remote port the proxy will relay data to
|
74
|
+
attr_reader :server_host
|
75
|
+
|
76
|
+
# The remote host the proxy will relay data to
|
77
|
+
attr_reader :server_port
|
78
|
+
|
79
|
+
# The size of read buffer
|
80
|
+
attr_accessor :buffer_size
|
81
|
+
|
82
|
+
# The connections maintained by the proxy
|
83
|
+
attr_reader :connections
|
84
|
+
|
85
|
+
#
|
86
|
+
# Creates a new Proxy.
|
87
|
+
#
|
88
|
+
# @param [Hash] options
|
89
|
+
# Options for the proxy.
|
90
|
+
#
|
91
|
+
# @option options [String] :host (DEFAULT_HOST)
|
92
|
+
# The host to listen on.
|
93
|
+
#
|
94
|
+
# @option options [Integer] :port
|
95
|
+
# The port to listen on.
|
96
|
+
#
|
97
|
+
# @option options [String, (host, port)] :server
|
98
|
+
# The server to forward connections to.
|
99
|
+
#
|
100
|
+
# @option options [Integer] :buffer_size (DEFAULT_BUFFER_SIZE)
|
101
|
+
# The maximum amount of data to read in.
|
102
|
+
#
|
103
|
+
# @yield [proxy]
|
104
|
+
# If a block is given, it will be passed the new Proxy, before it
|
105
|
+
# has been configured.
|
106
|
+
#
|
107
|
+
# @yieldparam [Proxy] proxy
|
108
|
+
# The new Proxy object.
|
109
|
+
#
|
110
|
+
# @example Proxies `0.0.0.0:1337` to `victim.com:80`:
|
111
|
+
# Proxy.new(:port => 1337, :server => ['victim.com', 80])
|
112
|
+
#
|
113
|
+
# @example Proxies `localhost:25` to `victim.com:25`:
|
114
|
+
# Proxy.new(:port => 25, :host => 'localhost', :server => 'victim.com')
|
115
|
+
#
|
116
|
+
def initialize(options={})
|
117
|
+
@host = options.fetch(:host,DEFAULT_HOST)
|
118
|
+
@port = options.fetch(:port)
|
119
|
+
|
120
|
+
@server_host, @server_port = options.fetch(:server)
|
121
|
+
@server_port ||= @port
|
122
|
+
|
123
|
+
@callbacks = {
|
124
|
+
:client_data => [],
|
125
|
+
:server_data => []
|
126
|
+
}
|
127
|
+
|
128
|
+
@buffer_size = options.fetch(:buffer_size,DEFAULT_BUFFER_SIZE)
|
129
|
+
@connections = {}
|
130
|
+
|
131
|
+
yield self if block_given?
|
132
|
+
end
|
133
|
+
|
134
|
+
#
|
135
|
+
# Creates a new Proxy and begins relaying data.
|
136
|
+
#
|
137
|
+
# @see #initialize
|
138
|
+
#
|
139
|
+
# @api public
|
140
|
+
#
|
141
|
+
def self.start(options={},&block)
|
142
|
+
new(options,&block).start
|
143
|
+
end
|
144
|
+
|
145
|
+
#
|
146
|
+
# Starts the proxy and begins relaying data.
|
147
|
+
#
|
148
|
+
# @return [Proxy]
|
149
|
+
# The proxy object.
|
150
|
+
#
|
151
|
+
# @api public
|
152
|
+
#
|
153
|
+
def start
|
154
|
+
open
|
155
|
+
listen
|
156
|
+
close
|
157
|
+
return self
|
158
|
+
end
|
159
|
+
|
160
|
+
#
|
161
|
+
# Opens the proxy.
|
162
|
+
#
|
163
|
+
# @api public
|
164
|
+
#
|
165
|
+
# @abstract
|
166
|
+
#
|
167
|
+
def open
|
168
|
+
end
|
169
|
+
|
170
|
+
#
|
171
|
+
# Polls the connections for data or errors.
|
172
|
+
#
|
173
|
+
# @api public
|
174
|
+
#
|
175
|
+
# @abstract
|
176
|
+
#
|
177
|
+
def poll
|
178
|
+
end
|
179
|
+
|
180
|
+
#
|
181
|
+
# Polls the connections for data.
|
182
|
+
#
|
183
|
+
# @api public
|
184
|
+
#
|
185
|
+
def listen
|
186
|
+
@listening = true
|
187
|
+
|
188
|
+
while @listening
|
189
|
+
begin
|
190
|
+
poll
|
191
|
+
rescue Interrupt
|
192
|
+
@listening = false
|
193
|
+
break
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
#
|
199
|
+
# Sends data to a connection.
|
200
|
+
#
|
201
|
+
# @param [connection] connection
|
202
|
+
# The connection.
|
203
|
+
#
|
204
|
+
# @param [String] data
|
205
|
+
# The data to send.
|
206
|
+
#
|
207
|
+
# @api public
|
208
|
+
#
|
209
|
+
# @abstract
|
210
|
+
#
|
211
|
+
def send(connection,data)
|
212
|
+
end
|
213
|
+
|
214
|
+
#
|
215
|
+
# Receives data from a connection.
|
216
|
+
#
|
217
|
+
# @param [connection] connection
|
218
|
+
# The connection.
|
219
|
+
#
|
220
|
+
# @api public
|
221
|
+
#
|
222
|
+
# @abstract
|
223
|
+
#
|
224
|
+
def recv(connection)
|
225
|
+
end
|
226
|
+
|
227
|
+
#
|
228
|
+
# Closes the proxy.
|
229
|
+
#
|
230
|
+
# @api public
|
231
|
+
#
|
232
|
+
def close
|
233
|
+
close_connections
|
234
|
+
close_proxy
|
235
|
+
end
|
236
|
+
|
237
|
+
#
|
238
|
+
# Stops the proxy from listening.
|
239
|
+
#
|
240
|
+
# @api public
|
241
|
+
#
|
242
|
+
def stop
|
243
|
+
@listening = false
|
244
|
+
return self
|
245
|
+
end
|
246
|
+
|
247
|
+
#
|
248
|
+
# Registers a callback for when a client sends data.
|
249
|
+
#
|
250
|
+
# @yield [client, server, data]
|
251
|
+
#
|
252
|
+
# @yieldparam [String] data
|
253
|
+
#
|
254
|
+
# @api public
|
255
|
+
#
|
256
|
+
def on_client_data(&block)
|
257
|
+
@callbacks[:client_data] << block
|
258
|
+
end
|
259
|
+
|
260
|
+
#
|
261
|
+
# Registers a callback for when a server sends data.
|
262
|
+
#
|
263
|
+
# @yield [client, server, data]
|
264
|
+
#
|
265
|
+
# @yieldparam [String] data
|
266
|
+
#
|
267
|
+
# @api public
|
268
|
+
#
|
269
|
+
def on_server_data(&block)
|
270
|
+
@callbacks[:server_data] << block
|
271
|
+
end
|
272
|
+
|
273
|
+
#
|
274
|
+
# Registers a callback for when either the client or the server sends
|
275
|
+
# data.
|
276
|
+
#
|
277
|
+
# @yield [client, server, data]
|
278
|
+
#
|
279
|
+
# @yieldparam [String] data
|
280
|
+
#
|
281
|
+
# @api public
|
282
|
+
#
|
283
|
+
def on_data(&block)
|
284
|
+
on_client_data(&block)
|
285
|
+
on_server_data(&block)
|
286
|
+
end
|
287
|
+
|
288
|
+
#
|
289
|
+
# Causes the proxy to ignore a message.
|
290
|
+
#
|
291
|
+
# @api public
|
292
|
+
#
|
293
|
+
def ignore!
|
294
|
+
throw(:action,:ignore)
|
295
|
+
end
|
296
|
+
|
297
|
+
#
|
298
|
+
# Causes the proxy to close a connection.
|
299
|
+
#
|
300
|
+
# @api public
|
301
|
+
#
|
302
|
+
def close!
|
303
|
+
throw(:action,:close)
|
304
|
+
end
|
305
|
+
|
306
|
+
#
|
307
|
+
# Causes the proxy to restart a connection.
|
308
|
+
#
|
309
|
+
# @api public
|
310
|
+
#
|
311
|
+
def reset!
|
312
|
+
throw(:action,:reset)
|
313
|
+
end
|
314
|
+
|
315
|
+
#
|
316
|
+
# Causes the proxy to stop processing data entirely.
|
317
|
+
#
|
318
|
+
# @api public
|
319
|
+
#
|
320
|
+
def stop!
|
321
|
+
throw(:action,:stop)
|
322
|
+
end
|
323
|
+
|
324
|
+
#
|
325
|
+
# Connections from clients.
|
326
|
+
#
|
327
|
+
# @return [Array<connection>]
|
328
|
+
# Client connections.
|
329
|
+
#
|
330
|
+
def client_connections
|
331
|
+
@connections.keys
|
332
|
+
end
|
333
|
+
|
334
|
+
#
|
335
|
+
# Connections to the server.
|
336
|
+
#
|
337
|
+
# @return [Array<connection>]
|
338
|
+
# Server connections.
|
339
|
+
#
|
340
|
+
def server_connections
|
341
|
+
@connections.values
|
342
|
+
end
|
343
|
+
|
344
|
+
#
|
345
|
+
# Finds the connection to the server, associated with the client.
|
346
|
+
#
|
347
|
+
# @param [connection] client_connection
|
348
|
+
# The connection from the client.
|
349
|
+
#
|
350
|
+
# @return [connection]
|
351
|
+
# The connection to the server.
|
352
|
+
#
|
353
|
+
def server_connection_for(client_connection)
|
354
|
+
@connections[client_connection]
|
355
|
+
end
|
356
|
+
|
357
|
+
if RUBY_VERSION < '1.9.'
|
358
|
+
#
|
359
|
+
# Finds the connection from the client, associated with the server
|
360
|
+
# connection.
|
361
|
+
#
|
362
|
+
# @param [connection] server_connection
|
363
|
+
# The connection to the server.
|
364
|
+
#
|
365
|
+
# @return [connection]
|
366
|
+
# The connection from the client.
|
367
|
+
#
|
368
|
+
def client_connection_for(server_connection)
|
369
|
+
@connections.index(server_connection)
|
370
|
+
end
|
371
|
+
else
|
372
|
+
#
|
373
|
+
# Finds the connection from the client, associated with the server
|
374
|
+
# connection.
|
375
|
+
#
|
376
|
+
# @param [connection] server_connection
|
377
|
+
# The connection to the server.
|
378
|
+
#
|
379
|
+
# @return [connection]
|
380
|
+
# The connection from the client.
|
381
|
+
#
|
382
|
+
def client_connection_for(server_connection)
|
383
|
+
@connections.key(server_connection)
|
384
|
+
end
|
385
|
+
end
|
386
|
+
|
387
|
+
#
|
388
|
+
# Converts the proxy to a String.
|
389
|
+
#
|
390
|
+
# @return [String]
|
391
|
+
# The String form of the proxy.
|
392
|
+
#
|
393
|
+
def to_s
|
394
|
+
"#{@host}:#{@port} <-> #{@server_host}:#{@server_port}"
|
395
|
+
end
|
396
|
+
|
397
|
+
#
|
398
|
+
# Inspects the proxy.
|
399
|
+
#
|
400
|
+
# @return [String]
|
401
|
+
# The inspected proxy.
|
402
|
+
#
|
403
|
+
def inspect
|
404
|
+
"#<#{self.class}:#{self.object_id}: #{self}>"
|
405
|
+
end
|
406
|
+
|
407
|
+
protected
|
408
|
+
|
409
|
+
#
|
410
|
+
# Creates a new connection to the server.
|
411
|
+
#
|
412
|
+
# @return [connection]
|
413
|
+
# The new connection.
|
414
|
+
#
|
415
|
+
# @abstract
|
416
|
+
#
|
417
|
+
def open_server_connection
|
418
|
+
end
|
419
|
+
|
420
|
+
#
|
421
|
+
# Closes a client connection to the proxy.
|
422
|
+
#
|
423
|
+
# @param [connection] connection
|
424
|
+
# The client connection.
|
425
|
+
#
|
426
|
+
# @abstract
|
427
|
+
#
|
428
|
+
def close_client_connection(connection)
|
429
|
+
end
|
430
|
+
|
431
|
+
#
|
432
|
+
# Closes a connection to the server.
|
433
|
+
#
|
434
|
+
# @param [connection] connection
|
435
|
+
# The server connection.
|
436
|
+
#
|
437
|
+
# @abstract
|
438
|
+
#
|
439
|
+
def close_server_connection(connection)
|
440
|
+
end
|
441
|
+
|
442
|
+
#
|
443
|
+
# Closes the proxy.
|
444
|
+
#
|
445
|
+
# @abstract
|
446
|
+
#
|
447
|
+
def close_proxy
|
448
|
+
end
|
449
|
+
|
450
|
+
#
|
451
|
+
# Resets a server connection.
|
452
|
+
#
|
453
|
+
# @param [connection] client_connection
|
454
|
+
# The connection from the client to the proxy.
|
455
|
+
#
|
456
|
+
# @param [connection] server_connection
|
457
|
+
# The connection from the proxy to the server.
|
458
|
+
#
|
459
|
+
def reset_connection(client_connection,server_connection)
|
460
|
+
close_server_connection(server_connection) if server_connection
|
461
|
+
|
462
|
+
@connections[client_connection] = open_server_connection
|
463
|
+
end
|
464
|
+
|
465
|
+
#
|
466
|
+
# Closes both the client and server connections.
|
467
|
+
#
|
468
|
+
# @param [connection] client_connection
|
469
|
+
# The connection from the client to the proxy.
|
470
|
+
#
|
471
|
+
# @param [connection] server_connection
|
472
|
+
# The connection from the proxy to the server.
|
473
|
+
#
|
474
|
+
def close_connection(client_connection,server_connection=nil)
|
475
|
+
close_server_connection(server_connection) if server_connection
|
476
|
+
close_client_connection(client_connection)
|
477
|
+
|
478
|
+
@connections.delete(client_connection)
|
479
|
+
end
|
480
|
+
|
481
|
+
#
|
482
|
+
# Closes all active connections.
|
483
|
+
#
|
484
|
+
def close_connections
|
485
|
+
@connections.each do |client_connection,server_connection|
|
486
|
+
close_server_connection(server_connection)
|
487
|
+
close_client_connection(client_connection)
|
488
|
+
end
|
489
|
+
|
490
|
+
@connections.clear
|
491
|
+
end
|
492
|
+
|
493
|
+
#
|
494
|
+
# Triggers the callbacks registered for an event.
|
495
|
+
#
|
496
|
+
# @param [Symbol] event
|
497
|
+
# The event being triggered.
|
498
|
+
#
|
499
|
+
# @param [connection] client_connection
|
500
|
+
# The connection from the client to the proxy.
|
501
|
+
#
|
502
|
+
# @param [connection] server_connection
|
503
|
+
# The connection from the proxy to the server.
|
504
|
+
#
|
505
|
+
# @param [String] data
|
506
|
+
# The data being sent.
|
507
|
+
#
|
508
|
+
# @yield []
|
509
|
+
# If none of the callbacks interrupted the event, the given block
|
510
|
+
# will be called.
|
511
|
+
#
|
512
|
+
def callback(event,client_connection,server_connection=nil,data=nil)
|
513
|
+
action = catch(:action) do
|
514
|
+
@callbacks[event].each do |block|
|
515
|
+
case block.arity
|
516
|
+
when 1
|
517
|
+
block.call(client_connection)
|
518
|
+
when 2
|
519
|
+
block.call(client_connection,server_connection)
|
520
|
+
when 3, -1
|
521
|
+
block.call(client_connection,server_connection,data)
|
522
|
+
end
|
523
|
+
end
|
524
|
+
end
|
525
|
+
|
526
|
+
case action
|
527
|
+
when :ignore
|
528
|
+
# no-op
|
529
|
+
when :reset
|
530
|
+
reset_connection(client_connection,server_connection)
|
531
|
+
when :close
|
532
|
+
close_connection(client_connection,server_connection)
|
533
|
+
when :stop
|
534
|
+
stop
|
535
|
+
else
|
536
|
+
yield if block_given?
|
537
|
+
end
|
538
|
+
end
|
539
|
+
|
540
|
+
#
|
541
|
+
# Triggers the `client_data` event.
|
542
|
+
#
|
543
|
+
# @param [connection] client_connection
|
544
|
+
# The connection from a client to the proxy.
|
545
|
+
#
|
546
|
+
# @param [connection] server_connection
|
547
|
+
# The connection from the proxy to the server.
|
548
|
+
#
|
549
|
+
# @param [String] data
|
550
|
+
# The data sent by the client.
|
551
|
+
#
|
552
|
+
def client_data(client_connection,server_connection,data)
|
553
|
+
callback(:client_data,client_connection,server_connection,data) do
|
554
|
+
send(server_connection,data)
|
555
|
+
end
|
556
|
+
end
|
557
|
+
|
558
|
+
#
|
559
|
+
# Triggers the `server_data` event.
|
560
|
+
#
|
561
|
+
# @param [connection] client_connection
|
562
|
+
# The connection from a client to the proxy.
|
563
|
+
#
|
564
|
+
# @param [connection] server_connection
|
565
|
+
# The connection from the proxy to the server.
|
566
|
+
#
|
567
|
+
# @param [String] data
|
568
|
+
# The data sent from the server.
|
569
|
+
#
|
570
|
+
def server_data(client_connection,server_connection,data)
|
571
|
+
callback(:server_data,client_connection,server_connection,data) do
|
572
|
+
send(client_connection,data)
|
573
|
+
end
|
574
|
+
end
|
575
|
+
|
576
|
+
end
|
577
|
+
end
|
578
|
+
end
|