em-proxy 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/em-proxy/backend.rb +1 -0
- data/lib/em-proxy/connection.rb +15 -9
- data/spec/proxy_spec.rb +11 -9
- metadata +3 -4
- data/examples/schemaless-mysql/client.rb +0 -17
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/lib/em-proxy/backend.rb
CHANGED
data/lib/em-proxy/connection.rb
CHANGED
@@ -2,12 +2,12 @@ module EventMachine
|
|
2
2
|
module ProxyServer
|
3
3
|
class Connection < EventMachine::Connection
|
4
4
|
attr_accessor :debug
|
5
|
-
|
5
|
+
|
6
6
|
##### Proxy Methods
|
7
7
|
def on_data(&blk); @on_data = blk; end
|
8
8
|
def on_response(&blk); @on_response = blk; end
|
9
9
|
def on_finish(&blk); @on_finish = blk; end
|
10
|
-
def on_connect(&blk); blk
|
10
|
+
def on_connect(&blk); @on_connect = blk; end
|
11
11
|
|
12
12
|
##### EventMachine
|
13
13
|
def initialize(options)
|
@@ -17,7 +17,7 @@ module EventMachine
|
|
17
17
|
|
18
18
|
def receive_data(data)
|
19
19
|
debug [:connection, data]
|
20
|
-
processed = @on_data.call(data)
|
20
|
+
processed = @on_data.call(data) if @on_data
|
21
21
|
|
22
22
|
return if processed == :async or processed.nil?
|
23
23
|
relay_to_servers(processed)
|
@@ -38,7 +38,7 @@ module EventMachine
|
|
38
38
|
s.send_data data unless data.nil?
|
39
39
|
end
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
#
|
43
43
|
# initialize connections to backend servers
|
44
44
|
#
|
@@ -57,7 +57,8 @@ module EventMachine
|
|
57
57
|
# [ip, port] of the connected client
|
58
58
|
#
|
59
59
|
def peer
|
60
|
-
|
60
|
+
peername = get_peername
|
61
|
+
@peer ||= peername ? Socket.unpack_sockaddr_in(peername).reverse : nil
|
61
62
|
end
|
62
63
|
|
63
64
|
#
|
@@ -66,13 +67,18 @@ module EventMachine
|
|
66
67
|
def relay_from_backend(name, data)
|
67
68
|
debug [:relay_from_backend, name, data]
|
68
69
|
|
69
|
-
data = @on_response.call(name, data)
|
70
|
+
data = @on_response.call(name, data) if @on_response
|
70
71
|
send_data data unless data.nil?
|
71
72
|
end
|
72
73
|
|
74
|
+
def connected(name)
|
75
|
+
debug [:connected]
|
76
|
+
@on_connect.call(name) if @on_connect
|
77
|
+
end
|
78
|
+
|
73
79
|
def unbind
|
74
80
|
debug [:unbind, :connection]
|
75
|
-
|
81
|
+
|
76
82
|
# terminate any unfinished connections
|
77
83
|
@servers.values.compact.each do |s|
|
78
84
|
s.close_connection
|
@@ -85,7 +91,7 @@ module EventMachine
|
|
85
91
|
|
86
92
|
# if all connections are terminated downstream, then notify client
|
87
93
|
close_connection_after_writing if @servers.values.compact.size.zero?
|
88
|
-
|
94
|
+
|
89
95
|
if @on_finish
|
90
96
|
@on_finish.call(name)
|
91
97
|
|
@@ -95,7 +101,7 @@ module EventMachine
|
|
95
101
|
end
|
96
102
|
|
97
103
|
private
|
98
|
-
|
104
|
+
|
99
105
|
def debug(*data)
|
100
106
|
if @debug
|
101
107
|
require 'pp'
|
data/spec/proxy_spec.rb
CHANGED
@@ -26,11 +26,13 @@ describe Proxy do
|
|
26
26
|
connected = false
|
27
27
|
EM.run do
|
28
28
|
EventMachine.add_timer(0.1) do
|
29
|
-
EventMachine::HttpRequest.new('http://127.0.0.1:8080/
|
29
|
+
EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1})
|
30
30
|
end
|
31
31
|
|
32
32
|
Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn|
|
33
|
-
conn.
|
33
|
+
conn.server :goog, :host => "google.com", :port => 80
|
34
|
+
|
35
|
+
conn.on_connect do |name|
|
34
36
|
connected = true
|
35
37
|
EventMachine.stop
|
36
38
|
end
|
@@ -62,7 +64,7 @@ describe Proxy do
|
|
62
64
|
it "should duplex TCP traffic to two backends google & yahoo" do
|
63
65
|
EM.run do
|
64
66
|
EventMachine.add_timer(0.1) do
|
65
|
-
EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1})
|
67
|
+
EventMachine::HttpRequest.new('http://127.0.0.1:8080/test').get({:timeout => 1})
|
66
68
|
end
|
67
69
|
|
68
70
|
Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn|
|
@@ -74,10 +76,10 @@ describe Proxy do
|
|
74
76
|
conn.on_response do |backend, resp|
|
75
77
|
case backend
|
76
78
|
when :goog then
|
77
|
-
resp.should =~ /
|
79
|
+
resp.should =~ /404/
|
78
80
|
seen.push backend
|
79
81
|
when :yhoo
|
80
|
-
resp.should =~ /
|
82
|
+
resp.should =~ /404/
|
81
83
|
seen.push backend
|
82
84
|
end
|
83
85
|
seen.uniq!
|
@@ -134,12 +136,12 @@ describe Proxy do
|
|
134
136
|
http =EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1})
|
135
137
|
http.callback { EventMachine.stop }
|
136
138
|
end
|
137
|
-
|
139
|
+
|
138
140
|
Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn|
|
139
141
|
conn.server :goog, :host => "google.com", :port => 80, :relay_client => true
|
140
142
|
conn.on_data { |data| raise "Should not be here"; data }
|
141
143
|
conn.on_response { |backend, resp| resp }
|
142
|
-
|
144
|
+
|
143
145
|
end
|
144
146
|
end
|
145
147
|
}.should_not raise_error
|
@@ -152,12 +154,12 @@ describe Proxy do
|
|
152
154
|
http =EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1})
|
153
155
|
http.callback { EventMachine.stop }
|
154
156
|
end
|
155
|
-
|
157
|
+
|
156
158
|
Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn|
|
157
159
|
conn.server :goog, :host => "google.com", :port => 80, :relay_server => true
|
158
160
|
conn.on_data { |data| data }
|
159
161
|
conn.on_response { |backend, resp| raise "Should not be here"; }
|
160
|
-
|
162
|
+
|
161
163
|
end
|
162
164
|
end
|
163
165
|
}.should_not raise_error
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ilya Grigorik
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-05-29 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -98,7 +98,6 @@ test_files:
|
|
98
98
|
- examples/line_interceptor.rb
|
99
99
|
- examples/port_forward.rb
|
100
100
|
- examples/relay_port_forward.rb
|
101
|
-
- examples/schemaless-mysql/client.rb
|
102
101
|
- examples/schemaless-mysql/mysql_interceptor.rb
|
103
102
|
- examples/selective_forward.rb
|
104
103
|
- examples/smtp_spam_filter.rb
|
@@ -1,17 +0,0 @@
|
|
1
|
-
require "rubygems"
|
2
|
-
require "mysql" # gem install ruby-mysql -v 2.9.2
|
3
|
-
|
4
|
-
my = Mysql.connect('127.0.0.1', 'root', '', 'noschema', 3307)
|
5
|
-
|
6
|
-
p my.list_tables
|
7
|
-
__END__
|
8
|
-
|
9
|
-
# look ma, no schema! ooh yeah.
|
10
|
-
my.query("create table posts")
|
11
|
-
|
12
|
-
# insert a few records into our schemaless MySQL store... :-)
|
13
|
-
my.query("insert into posts values('first post'('author', 'Ilya'),('nick', 'igrigorik'))")
|
14
|
-
my.query("insert into posts values('author:Ilya','location:Waterloo')")
|
15
|
-
|
16
|
-
my.query("select author from posts") { |r| puts r }
|
17
|
-
my.query("select nick from posts") { |r| puts r }
|