diff --git a/gec22_demo/web_redirector/README.md b/gec22_demo/web_redirector/README.md new file mode 100644 index 0000000..e900bcc --- /dev/null +++ b/gec22_demo/web_redirector/README.md @@ -0,0 +1,10 @@ +# Thanks to @minhajuddin for the template +# http://minhajuddin.com/2010/06/06/redirector-a-simple-rack-application-which-makes-redirection-of-multiple-domains-a-breeze + +# Install Ruby Rack + +gem install rack --no-ri --no-rdoc + +# Run with: + +rackup -o IP_of_your_server config.ru diff --git a/gec22_demo/web_redirector/config.ru b/gec22_demo/web_redirector/config.ru new file mode 100644 index 0000000..6ec2f29 --- /dev/null +++ b/gec22_demo/web_redirector/config.ru @@ -0,0 +1,11 @@ +require './redirector' + +use Rack::ContentLength + +app = proc do |env| + app = Redirector.new() + status, headers, response = app.call(env) + [ status, headers, response ] +end + +run app diff --git a/gec22_demo/web_redirector/config.yaml b/gec22_demo/web_redirector/config.yaml new file mode 100644 index 0000000..1fe488c --- /dev/null +++ b/gec22_demo/web_redirector/config.yaml @@ -0,0 +1,19 @@ +# NO CIDR NOTATION SUPPORT!!! +# Convention: +# - net_A_B_C_D for the network IP address A.B.C.D +# - net_A_B_C for the network IP address A.B.C.0 +# - net_A_B for the network IP address A.B +# - net_A for the network IP address A +# - default for everyting else +# +net_10_15: + status: 303 + location: 'http://www.nicta.com.au' + +net_203_143_174: + status: 303 + location: 'http://www.w3.org' + +default: + status: 303 + location: 'http://www.google.com.au' \ No newline at end of file diff --git a/gec22_demo/web_redirector/redirector.rb b/gec22_demo/web_redirector/redirector.rb new file mode 100644 index 0000000..b12c100 --- /dev/null +++ b/gec22_demo/web_redirector/redirector.rb @@ -0,0 +1,34 @@ +# Thanks to @minhajuddin for the template +# http://minhajuddin.com/2010/06/06/redirector-a-simple-rack-application-which-makes-redirection-of-multiple-domains-a-breeze +# +require 'yaml' + +class Redirector + + PREFIX = "net_" + @@config = YAML::load(File.open('config.yaml')) + + def self.config + @@config + end + + def call(env) + redirect_info = get_redirect_info(env['REMOTE_ADDR']) + puts "-- #{Time.now} -- Host: #{env['REMOTE_ADDR']} -- Redirect: #{redirect_info['location']}" + [redirect_info['status'], {'Content-Type' => 'text','Location' => redirect_info['location']}, get_host_response( redirect_info ) ] + #['200', {'Content-Type' => 'text/html'}, ['get rack\'d']] + end + + private + def get_host_response(redirect_info) + ["#{redirect_info['status']} moved. The document has moved to #{redirect_info['location']}"] + end + + def get_redirect_info(host) + (0..3).each do |i| + net = PREFIX+host.split('.')[0,(4-i)].join('_') + return @@config[net] unless @@config[net].nil? + end + return @@config['default'] + end +end