emonti-rbkb 0.6.1.2 → 0.6.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/plugsrv +220 -0
  2. data/usage.txt +2 -0
  3. metadata +5 -3
data/bin/plugsrv ADDED
@@ -0,0 +1,220 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ #
4
+
5
+ require 'rubygems'
6
+ require 'eventmachine'
7
+ require 'socket'
8
+ require 'optparse'
9
+ require 'rbkb'
10
+
11
+ def bail(*msg)
12
+ STDERR.puts msg
13
+ exit 1
14
+ end
15
+
16
+ class Plug
17
+ module UI
18
+ def log( *msg )
19
+ unless PLUG_OPTS[:quiet]
20
+ PLUG_OPTS[:out].puts msg
21
+ end
22
+ end
23
+ module_function :log
24
+ end
25
+
26
+ class Controller
27
+ attr_accessor :tgtaddr, :tgtport, :tgtclient
28
+ @@controllers=nil
29
+
30
+ def initialize(tgtaddr, tgtport, tgtclient)
31
+
32
+ @tgtaddr = tgtaddr
33
+ @tgtport = tgtport
34
+ @tgtclient = tgtclient
35
+
36
+ @@controllers = self
37
+ end
38
+
39
+ ##----------------------------------------
40
+
41
+ def dispatch_rcv(snder, data)
42
+ data # XXX for now
43
+ end
44
+
45
+ ##----------------------------------------
46
+
47
+ def dispatch_close(snder)
48
+ nil # XXX for now
49
+ end
50
+
51
+ ##----------------------------------------
52
+
53
+ def self.proxy(cli)
54
+ unless (ctrl = @@controllers)
55
+ raise "No controller exists for this connection: #{cli.sock_peername}"
56
+ end
57
+
58
+ tgtaddr = ctrl.tgtaddr
59
+ tgtport = ctrl.tgtport
60
+ tgtclient = ctrl.tgtclient
61
+
62
+ srv = EventMachine::connect(tgtaddr, tgtport, tgtclient)
63
+ srv.plug_peers.push cli
64
+ cli.plug_peers.push srv
65
+
66
+ srv.controller = cli.controller = ctrl
67
+ end
68
+ end # class Plug::Controller
69
+
70
+
71
+ module BaseTCP
72
+ include UI
73
+
74
+ attr_accessor :plug_peers, :controller, :kind
75
+ attr_reader :sock_peer, :sock_peername
76
+
77
+ def post_init
78
+ @plug_peers = Array.new
79
+ @kind = :conn # default
80
+ end
81
+
82
+
83
+ def receive_data data
84
+ log "%#{kind.to_s.upcase}-#{sock_peername}-SAYS", data.hexdump, "%"
85
+ if @controller and (data = @controller.dispatch_rcv(self, data)).nil?
86
+ return
87
+ end
88
+ @plug_peers.each {|p| p.send_data data}
89
+ end
90
+
91
+
92
+ def notify_connection
93
+ log "%#{kind.to_s.upcase}-#{@sock_peername}-CONNECTED"
94
+ end
95
+
96
+
97
+ def unbind
98
+ log "%#{kind.to_s.upcase}-#{@sock_peername}-CLOSED"
99
+
100
+ cret = (@controller and @controller.dispatch_close(self))
101
+
102
+ @plug_peers.each do |p|
103
+ p.plug_peers.delete(self)
104
+ p.close_connection unless cret
105
+ end
106
+ end
107
+ end
108
+
109
+
110
+ module TCPListener
111
+ include Plug::BaseTCP
112
+ attr_accessor :tgtaddr, :tgtport
113
+
114
+ def post_init
115
+ super
116
+ @kind = :client
117
+ @sock_peer = Socket.unpack_sockaddr_in(get_peername).reverse
118
+ @sock_peername = @sock_peer.join(':')
119
+
120
+ @controller = Plug::Controller.proxy(self)
121
+
122
+ notify_connection
123
+ end
124
+
125
+ end # module TCPListener
126
+
127
+
128
+ module TCPClient
129
+ include Plug::BaseTCP
130
+ attr_accessor :connected
131
+
132
+ def post_init
133
+ super
134
+ @kind = :server
135
+ end
136
+
137
+ def connection_completed
138
+ @sock_peer = Socket.unpack_sockaddr_in(get_peername).reverse
139
+ @sock_peername = @sock_peer.join(':')
140
+ notify_connection
141
+ end
142
+
143
+ end # module TCPClient
144
+
145
+ end # module Plug
146
+
147
+ PLUG_OPTS={ :quiet => false, :out => STDOUT }
148
+
149
+ if __FILE__ == $0
150
+
151
+
152
+ #############################################################################
153
+ ### MAIN
154
+ #############################################################################
155
+ #
156
+ # Get arguments
157
+ opts = OptionParser.new do |opts|
158
+ opts.banner = "Usage: #{$0} [options] target:tport[@[laddr:]lport]\n",
159
+ " <target:tport> = the address of the target service\n",
160
+ " <@laddr:lport> = optional address and port to listen on\n"
161
+
162
+ opts.separator ""
163
+ opts.separator "Options:"
164
+
165
+ opts.on_tail("-h", "--help", "Show this message") do
166
+ puts opts
167
+ exit 1
168
+ end
169
+
170
+ opts.on("-o", "--output FILE", "send output to a file") do |o|
171
+ PLUG_OPTS[:out] = File.open(o, "w") rescue (bail $!)
172
+ end
173
+
174
+ opts.on("-l", "--listen ADDR:PORT",
175
+ "optional listener address:port",
176
+ "(default: 0.0.0.0:<tport>)"
177
+ ) do |addr|
178
+
179
+ unless m = /^([\w\.]+)?(?::(\d+))?$/.match(addr)
180
+ STDERR.puts "invalid listener address"
181
+ exit 1
182
+ end
183
+ PLUG_OPTS[:svraddr] = m[1]
184
+ PLUG_OPTS[:svrport] = (m[2])? m[2].to_i : nil
185
+ end
186
+
187
+ opts.on("-q", "--[no-]quiet", "Suppress/Enable conversation dumps.") do |q|
188
+ PLUG_OPTS[:quiet] = q
189
+ end
190
+
191
+ end
192
+
193
+ opts.parse!(ARGV) rescue (STDERR.puts $!; exit 1)
194
+
195
+
196
+ # Get target/listen argument
197
+ rx = /^([\w\.]+):(\d+)(?:@(?:([\w\.]+):)?(\d+))?$/
198
+ unless (m = rx.match(ARGV.shift)) and ARGV.shift.nil?
199
+ $stderr.puts opts.banner
200
+ exit 1
201
+ end
202
+
203
+ PLUG_OPTS[:tgtaddr] = m[1]
204
+ PLUG_OPTS[:tgtport] = m[2].to_i
205
+ PLUG_OPTS[:svraddr] ||= (m[3] || "0.0.0.0")
206
+ PLUG_OPTS[:svrport] ||= (m[4] || PLUG_OPTS[:tgtport]).to_i
207
+
208
+
209
+ # Start controller
210
+ ctrl = Plug::Controller.new(PLUG_OPTS[:tgtaddr], PLUG_OPTS[:tgtport], Plug::TCPClient)
211
+
212
+ # Start event loop
213
+ Plug::UI.log "%Starting TCP PlugServer #{PLUG_OPTS[:svraddr]}:#{PLUG_OPTS[:svrport]} -> #{PLUG_OPTS[:tgtaddr]}:#{PLUG_OPTS[:tgtport]}"
214
+
215
+ EventMachine::run {
216
+ EventMachine::start_server(PLUG_OPTS[:svraddr], PLUG_OPTS[:svrport], Plug::TCPListener)
217
+ }
218
+
219
+
220
+ end
data/usage.txt CHANGED
@@ -1,5 +1,7 @@
1
1
  == Command Line Tools
2
2
 
3
+ Below is a list of the command line utilities with short descriptions and
4
+ usage information. Examples to come.
3
5
 
4
6
  === b64
5
7
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emonti-rbkb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1.2
4
+ version: 0.6.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Monti
@@ -9,7 +9,7 @@ autorequire: rbkb
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-13 00:00:00 -08:00
12
+ date: 2009-02-25 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.12.2
23
+ version: 0.12.0
24
24
  version:
25
25
  description:
26
26
  email: emonti@matasano.com
@@ -42,6 +42,7 @@ executables:
42
42
  - urldec
43
43
  - urlenc
44
44
  - xor
45
+ - plugsrv
45
46
  extensions: []
46
47
 
47
48
  extra_rdoc_files: []
@@ -66,6 +67,7 @@ files:
66
67
  - bin/urldec
67
68
  - bin/urlenc
68
69
  - bin/xor
70
+ - bin/plugsrv
69
71
  - lib/rbkb.rb
70
72
  - lib/rbkb/command_line.rb
71
73
  - lib/rbkb/extends.rb