stubby 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,58 +1,78 @@
1
1
  # This module extracts the OS level DNS configuration dependency for OSX.
2
- module Extensions
3
- module DNS
4
- module OSX
5
- private
6
- def servers_for(interface)
7
- servers = `networksetup -getdnsservers '#{interface}'`
2
+ module Stubby
3
+ module Extensions
4
+ module DNS
5
+ module OSX
6
+ private
7
+ def servers_for(interface)
8
+ servers = `networksetup -getdnsservers '#{interface}'`
8
9
 
9
- if servers =~ /There aren't any DNS Servers/
10
- return ["empty"]
11
- else
12
- return servers.split("\n")
10
+ if servers =~ /There aren't any DNS Servers/
11
+ return ["empty"]
12
+ else
13
+ return servers.split("\n")
14
+ end
13
15
  end
14
- end
15
16
 
16
- def setup_reference(interface)
17
- return if interface.include? "*"
18
- @network_interfaces[interface] = servers_for(interface)
19
- puts "[INFO] #{interface} configured with Stubby DNS. Will restore to #{@network_interfaces[interface]}"
20
- `networksetup -setdnsservers '#{interface}' #{STUBBY_MASTER}`
21
- end
17
+ def setup_reference(interface)
18
+ return if interface.include? "*"
19
+ puts "[INFO] #{interface} configured with Stubby DNS. Will restore to #{@network_interfaces[interface]}"
20
+ `networksetup -setdnsservers '#{interface}' #{STUBBY_MASTER}`
21
+ end
22
+
23
+ def teardown_reference(interface, servers)
24
+ `networksetup -setdnsservers '#{interface}' #{servers.join(" ")}`
25
+ puts "[INFO] #{interface} original DNS settings restored #{servers.join(" ")}"
26
+ rescue => e
27
+ puts e.inspect
28
+ end
22
29
 
23
- def teardown_reference(interface, servers)
24
- `networksetup -setdnsservers '#{interface}' #{servers.join(" ")}`
25
- puts "[INFO] #{interface} original DNS settings restored #{servers.join(" ")}"
26
- rescue => e
27
- puts e.inspect
28
- end
30
+ def setup_references
31
+ # TODO: if we connect to a new network, we'd like that to use us, too
32
+ return if @network_interfaces
29
33
 
30
- def setup_references
31
- # TODO: if we connect to a new network, we'd like that to use us, too
32
- return if @network_interfaces
34
+ network_interfaces.each do |interface, servers|
35
+ setup_reference(interface)
36
+ end
33
37
 
34
- @network_interfaces = {}
35
- `networksetup listallnetworkservices`.split("\n").each do |interface|
36
- next if interface.include? '*'
37
- setup_reference(interface)
38
+ flush
38
39
  end
39
40
 
40
- flush
41
- end
41
+ def network_interfaces
42
+ # TODO: if we connect to a new network, we'd like that to use us, too
43
+ return @network_interfaces if @network_interfaces
42
44
 
43
- def teardown_references
44
- interfaces, @network_interfaces = @network_interfaces, nil
45
+ @network_interfaces = {}
46
+ `networksetup listallnetworkservices`.split("\n").each do |interface|
47
+ next if interface.include? '*'
48
+ @network_interfaces[interface] = servers_for(interface)
49
+ end
50
+
51
+ @network_interfaces
52
+ end
45
53
 
46
- (interfaces || []).each do |interface, servers|
47
- teardown_reference(interface, servers)
54
+ # This is less of a complete restoration and actually
55
+ # a return to no defaults
56
+ def restore_references
57
+ network_interfaces.each do |interface, servers|
58
+ teardown_reference(interface, ["empty"])
59
+ end
48
60
  end
49
61
 
50
- flush
51
- end
62
+ def teardown_references
63
+ interfaces, @network_interfaces = @network_interfaces, nil
52
64
 
53
- def flush
54
- `dscacheutil -flushcache`
55
- end
65
+ (interfaces || []).each do |interface, servers|
66
+ teardown_reference(interface, servers)
67
+ end
68
+
69
+ flush
70
+ end
71
+
72
+ def flush
73
+ `dscacheutil -flushcache`
74
+ end
75
+ end
56
76
  end
57
77
  end
58
78
  end
@@ -4,237 +4,242 @@ require 'liquid'
4
4
  require 'httpi'
5
5
  require 'rack/ssl'
6
6
 
7
- module Extensions
8
- module HTTP
9
- class NotFoundException < Exception
10
- end
7
+ module Stubby
8
+ module Extensions
9
+ module HTTP
10
+ class NotFoundException < Exception
11
+ end
11
12
 
12
- class HTTPApp < Sinatra::Base
13
- class << self
14
- def port
15
- 80
16
- end
13
+ class HTTPApp < Sinatra::Base
14
+ class << self
15
+ def port
16
+ 80
17
+ end
17
18
 
18
- def run!(session, server_settings={})
19
- puts self.inspect + ": " + port.to_s
20
-
21
- set :bind, STUBBY_MASTER
22
- set :port, port
23
- set :stubby_session, session
24
- set :server, 'thin'
19
+ def run!(session, server_settings={})
20
+ puts self.inspect + ": " + port.to_s
21
+
22
+ set :bind, STUBBY_MASTER
23
+ set :port, port
24
+ set :stubby_session, session
25
+ set :server, 'thin'
25
26
 
26
- super(:server_settings => server_settings)
27
- end
27
+ super(:server_settings => server_settings)
28
+ end
28
29
 
29
- def adapter(*names, &block)
30
- names.each do |name|
31
- adapters[name] = block
30
+ def adapter(*names, &block)
31
+ names.each do |name|
32
+ adapters[name] = block
33
+ end
32
34
  end
33
- end
34
35
 
35
- def adapters
36
- @@adapters ||= {}
36
+ def adapters
37
+ @@adapters ||= {}
38
+ end
37
39
  end
38
- end
39
40
 
40
- set :run, false
41
- set :static, false
41
+ set :run, false
42
+ set :static, false
42
43
 
43
- adapter "http-redirect" do
44
- r = URI.parse(instruction_params["to"])
45
- r.path = request.path if r.path.blank?
44
+ adapter "http-redirect" do
45
+ r = URI.parse(instruction_params["to"])
46
+ r.path = request.path if r.path.blank?
46
47
 
47
- redirect to(r.to_s)
48
- end
48
+ redirect to(r.to_s)
49
+ end
49
50
 
50
- adapter "file" do
51
- paths = []
51
+ adapter "file" do
52
+ paths = []
52
53
 
53
- if url.host == "-"
54
- paths << File.expand_path(File.join("~/.stubby/#{request.host}", request.path))
55
- paths << File.expand_path(File.join("/usr/local/stubby/#{request.host}", request.path))
56
- else
57
- paths << File.expand_path(File.join(url.path, request.path))
58
- end
54
+ if url.host == "-"
55
+ paths << File.expand_path(File.join("~/.stubby/#{request.host}", request.path))
56
+ paths << File.expand_path(File.join("/usr/local/stubby/#{request.host}", request.path))
57
+ else
58
+ paths << File.expand_path(File.join(url.path, request.path))
59
+ end
59
60
 
60
- paths.each do |path|
61
- next if path.index(url.path) != 0
62
-
63
- p = [path, File.join(path, "index.html")].select { |path|
64
- File.exists?(path) and !File.directory?(path)
65
- }.first
61
+ paths.each do |path|
62
+ next if path.index(url.path) != 0
63
+
64
+ p = [path, File.join(path, "index.html")].select { |path|
65
+ File.exists?(path) and !File.directory?(path)
66
+ }.first
66
67
 
67
- send_file(p) and break unless p.nil?
68
- end
68
+ send_file(p) and break unless p.nil?
69
+ end
69
70
 
70
- not_found(paths.join(",\n"))
71
- end
71
+ not_found(paths.join(",\n"))
72
+ end
72
73
 
73
- adapter "http-proxy" do
74
- run_proxy("http")
75
- end
74
+ adapter "http-proxy" do
75
+ run_proxy("http")
76
+ end
76
77
 
77
- adapter "https-proxy" do
78
- run_proxy("https")
79
- end
78
+ adapter "https-proxy" do
79
+ run_proxy("https")
80
+ end
80
81
 
81
- %w(get post put patch delete options link unlink).each do |method|
82
- send(method, //) do
83
- run_handler
82
+ %w(get post put patch delete options link unlink).each do |method|
83
+ send(method, //) do
84
+ run_handler
85
+ end
84
86
  end
85
- end
86
87
 
87
- private
88
- def run_handler
89
- if instruction.nil?
90
- not_found
91
- elsif adapter=self.class.adapters[url.scheme]
92
- instance_eval &adapter
93
- else
94
- instance_eval &self.class.adapters["default"]
88
+ private
89
+ def run_handler
90
+ if instruction.nil?
91
+ not_found
92
+ elsif adapter=self.class.adapters[url.scheme]
93
+ instance_eval &adapter
94
+ else
95
+ instance_eval &self.class.adapters["default"]
96
+ end
95
97
  end
96
- end
97
98
 
98
- def run_proxy(scheme)
99
- to = url.dup
100
- to.scheme = scheme
101
- to.path = request.path if to.path.empty?
102
- to.query = request.query_string
99
+ def run_proxy(scheme)
100
+ to = url.dup
101
+ to.scheme = scheme
102
+ to.path = request.path if to.path.empty?
103
+ to.query = request.query_string
103
104
 
104
- puts "#{to.to_s} scheme: #{request.scheme}"
105
+ puts "#{to.to_s} scheme: #{request.scheme}"
105
106
 
106
- r = HTTPI::Request.new
107
- r.url = to.to_s
108
-
109
- # TODO: this is a hack, should support streaming the bodies
110
- # and handling the headers more systematically (allow
111
- # keep-alives and transfer encoding)
112
- r.headers.merge! Hash[(env.select { |k,v| v.is_a? String }.collect { |k,v| [k.gsub("HTTP_", "").gsub("_", "-"), v] })]
113
- r.headers["HOST"] = request.host
114
- r.headers["STUBBY-ENV"] = settings.stubby_session.environment
115
- r.headers["STUBBY-KEY"] = settings.stubby_session.key(instruction)
116
- r.headers["STUBBY-USER"] = settings.stubby_session.user_key
117
- r.headers["X-FORWARDED-PROTO"] = request.scheme
118
- r.headers["X-FORWARDED-FOR"] = request.ip
119
- r.headers["CONNECTION"] = "close"
120
- r.headers.delete "ACCEPT-ENCODING"
121
-
122
- request.body.rewind
123
- r.body = request.body.read
124
-
125
- response = HTTPI.request(request.request_method.downcase.to_sym, r)
107
+ r = HTTPI::Request.new
108
+ r.url = to.to_s
126
109
 
127
- response.headers.delete "TRANSFER-ENCODING"
110
+ # TODO: this is a hack, should support streaming the bodies
111
+ # and handling the headers more systematically (allow
112
+ # keep-alives and transfer encoding)
113
+ r.headers.merge! Hash[(env.select { |k,v| v.is_a? String }.collect { |k,v| [k.gsub("HTTP_", "").gsub("_", "-"), v] })]
114
+ r.headers["HOST"] = request.host
115
+ r.headers["STUBBY-ENV"] = settings.stubby_session.environment
116
+ r.headers["STUBBY-KEY"] = settings.stubby_session.key(instruction)
117
+ r.headers["STUBBY-USER"] = settings.stubby_session.user_key
118
+ r.headers["X-FORWARDED-PROTO"] = request.scheme
119
+ r.headers["X-FORWARDED-FOR"] = request.ip
120
+ r.headers["CONNECTION"] = "close"
121
+ r.headers.delete "ACCEPT-ENCODING"
122
+
123
+ request.body.rewind
124
+ r.body = request.body.read
125
+
126
+ response = HTTPI.request(request.request_method.downcase.to_sym, r)
127
+
128
+ response.headers.delete "TRANSFER-ENCODING"
128
129
 
129
- status(response.code)
130
- puts "response: #{response.headers}"
130
+ status(response.code)
131
+ puts "response: #{response.headers}"
131
132
 
132
- headers(response.headers)
133
- body(response.body)
134
- end
133
+ headers(response.headers)
134
+ body(response.body)
135
+ end
135
136
 
136
- def forbidden
137
- [403, "Forbidden"]
138
- end
137
+ def forbidden
138
+ [403, "Forbidden"]
139
+ end
139
140
 
140
- def not_found(resource="*unknown*")
141
- [404, "Not Found:\n #{resource}"]
142
- end
141
+ def not_found(resource="*unknown*")
142
+ [404, "Not Found:\n #{resource}"]
143
+ end
143
144
 
144
- def instruction
145
- @instruction ||= MultiJson.load(HTTPI.post("http://#{STUBBY_MASTER}:9000/rules/search.json",
146
- trigger: "#{request.scheme}://#{request.host}").body)
147
- end
145
+ def instruction
146
+ @instruction ||= MultiJson.load(HTTPI.post("http://#{STUBBY_MASTER}:9000/rules/search.json",
147
+ trigger: "#{request.scheme}://#{request.host}").body)
148
+ end
148
149
 
149
- def instruction_params
150
- Rack::Utils.parse_nested_query url.query
151
- end
150
+ def instruction_params
151
+ Rack::Utils.parse_nested_query url.query
152
+ end
152
153
 
153
- def url
154
- @url ||= URI.parse(instruction)
154
+ def url
155
+ @url ||= URI.parse(instruction)
156
+ end
155
157
  end
156
- end
157
158
 
158
- # TODO: get SSL support running. Self signed cert
159
- class HTTPSApp < HTTPApp
160
- use Rack::SSL
161
-
162
- class << self
163
- def port
164
- 443
165
- end
159
+ # TODO: get SSL support running. Self signed cert
160
+ class HTTPSApp < HTTPApp
161
+ use Rack::SSL
166
162
 
167
- def run!(session)
168
- set :bind, STUBBY_MASTER
169
- set :port, port
170
- set :stubby_session, session
163
+ class << self
164
+ def port
165
+ 443
166
+ end
171
167
 
172
- Rack::Handler::Thin.run(self, {
173
- :Host => STUBBY_MASTER,
174
- :Port => 443
175
- }) do |server|
176
- server.ssl = true
177
- server.ssl_options = {
178
- :verify_peer => false
179
- }
168
+ def run!(session)
169
+ set :bind, STUBBY_MASTER
170
+ set :port, port
171
+ set :stubby_session, session
172
+
173
+ Rack::Handler::Thin.run(self, {
174
+ :Host => STUBBY_MASTER,
175
+ :Port => 443
176
+ }) do |server|
177
+ server.ssl = true
178
+ server.ssl_options = {
179
+ :verify_peer => false
180
+ }
181
+ end
182
+ rescue => e
183
+ puts "#{e.inspect}"
180
184
  end
181
- rescue => e
182
- puts "#{e.inspect}"
183
185
  end
184
186
  end
185
- end
186
187
 
187
- class Server
188
- def initialize
189
- @log = Logger.new(STDOUT)
190
- end
188
+ class Server
189
+ def initialize
190
+ @log = Logger.new(STDOUT)
191
+ end
191
192
 
192
- def run!(session, options)
193
- return if options[:http] == false
193
+ def run!(session, options)
194
+ return if options[:http] == false
194
195
 
195
- @session = session
196
- HTTPApp.run!(session)
197
- end
196
+ @session = session
197
+ HTTPApp.run!(session)
198
+ end
198
199
 
199
- # http://blah.com => localhost:3000
200
- # =>
201
- # http://blah.com => http-proxy://localhost:3000
202
- def expand_rule(trigger, instruction, proto='http')
203
- u = URI.parse(instruction)
204
-
205
- (if u.scheme.nil?
206
- { trigger => "http-proxy://#{instruction}" }
207
- elsif u.scheme == "http"
208
- u.scheme = "http-proxy"
209
- { trigger => u.to_s }
210
- else
211
- { trigger => instruction }
212
- end).merge({
213
- "#{trigger.gsub(proto + "://", "dns://")}/a" => "dns-a://#{STUBBY_MASTER}"
214
- })
215
- end
200
+ # http://blah.com => localhost:3000
201
+ # =>
202
+ # http://blah.com => http-proxy://localhost:3000
203
+ def expand_rule(trigger, instruction, proto='http')
204
+ u = URI.parse(instruction)
205
+
206
+ (if u.scheme.nil?
207
+ { trigger => "http-proxy://#{instruction}" }
208
+ elsif u.scheme == "http"
209
+ u.scheme = "http-proxy"
210
+ { trigger => u.to_s }
211
+ else
212
+ { trigger => instruction }
213
+ end).merge({
214
+ "#{trigger.gsub(proto + "://", "dns://")}/a" => "dns-a://#{STUBBY_MASTER}"
215
+ })
216
+ end
217
+
218
+ def stop!
219
+ HTTPApp.quit!
220
+ end
216
221
 
217
- def stop!
218
- HTTPApp.quit!
222
+ def restore!
223
+ # nil
224
+ end
219
225
  end
220
- end
221
226
 
222
- class SSLServer < Server
223
- def run!(session, options)
224
- return if options[:https] == false
227
+ class SSLServer < Server
228
+ def run!(session, options)
229
+ return if options[:https] == false
225
230
 
226
- @session = session
227
- HTTPSApp.run!(session)
228
- end
231
+ @session = session
232
+ HTTPSApp.run!(session)
233
+ end
229
234
 
230
- def stop!
231
- HTTPSApp.quit!
232
- end
235
+ def stop!
236
+ HTTPSApp.quit!
237
+ end
233
238
 
234
- def expand_rule(trigger, instruction)
235
- super(trigger, instruction, "https")
239
+ def expand_rule(trigger, instruction)
240
+ super(trigger, instruction, "https")
241
+ end
236
242
  end
237
243
  end
238
244
  end
239
245
  end
240
-