Class: AutoselHttpProxy::Setting
- Inherits:
-
Object
- Object
- AutoselHttpProxy::Setting
- Defined in:
- lib/autosel_http_proxy.rb
Constant Summary collapse
- @@proxy_url =
nil
- @@proxy_setting =
nil
Class Method Summary collapse
- .create_conf(filename) ⇒ Object
- .hostname_found?(hostname) ⇒ Boolean
- .init ⇒ Object
- .init_proxy_setting(list) ⇒ Object
- .load_conf(filename) ⇒ Object
- .overwrite_base_class! ⇒ Object
- .proxy_setting ⇒ Object
- .proxy_url ⇒ Object
- .set_proxy_env! ⇒ Object
Class Method Details
.create_conf(filename) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/autosel_http_proxy.rb', line 55 def self.create_conf(filename) File.open(filename, 'w', 0600) {|io| io.puts <<EOF PROXY_LIST = [{:if => 'hostname_found? "hostname.example.com"', :proxy_host => 'proxy.example.com', :proxy_port => '8080', :proxy_user => nil, :proxy_pass => nil,},] EOF } end |
.hostname_found?(hostname) ⇒ Boolean
19 20 21 22 23 24 25 26 |
# File 'lib/autosel_http_proxy.rb', line 19 def self.hostname_found?(hostname) begin Resolv.getaddress(hostname) true rescue false end end |
.init ⇒ Object
86 87 88 89 90 |
# File 'lib/autosel_http_proxy.rb', line 86 def self.init load_conf PROXY_CONFFILE init_proxy_setting PROXY_LIST set_proxy_env! end |
.init_proxy_setting(list) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/autosel_http_proxy.rb', line 28 def self.init_proxy_setting(list) list.each{|a| return @@proxy_setting = a if eval a[:if] } @@proxy_setting = nil end |
.load_conf(filename) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/autosel_http_proxy.rb', line 68 def self.load_conf(filename) unless File.exist?(filename) puts "*** \"#{filename}\" created." puts "*** Please config it." create_conf filename raise ConfigNotFound end load filename if PROXY_LIST[0][:if] == 'hostname_found? "hostname.example.com"' puts "*** \"#{filename}\" isn't configured." puts "*** Please config it." raise ConfigNotConfigured end end |
.overwrite_base_class! ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/autosel_http_proxy.rb', line 92 def self.overwrite_base_class! require 'net/http' Net::HTTP.instance_eval { if defined? @@method_new_orig raise DoubleCallOverwrite end @@method_new_orig = self.method(:new) # backup orignal 'new' method def new(address, port = nil, p_addr = nil, p_port = nil, p_user = nil, p_pass = nil) # overwrite! if AutoselHttpProxy::setting p_addr = AutoselHttpProxy::setting[:proxy_host] p_port = AutoselHttpProxy::setting[:proxy_port] p_user = AutoselHttpProxy::setting[:proxy_user] p_pass = AutoselHttpProxy::setting[:proxy_pass] end # call orignal 'new' method @@method_new_orig.call(address, port, p_addr, p_port, p_user, p_pass) end } end |
.proxy_setting ⇒ Object
17 |
# File 'lib/autosel_http_proxy.rb', line 17 def self.proxy_setting() @@proxy_setting end |
.proxy_url ⇒ Object
16 |
# File 'lib/autosel_http_proxy.rb', line 16 def self.proxy_url() @@proxy_url end |
.set_proxy_env! ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/autosel_http_proxy.rb', line 35 def self.set_proxy_env! return nil unless proxy_setting unless proxy_setting[:proxy_host] puts "*** Need \":proxy_host\" in \"#{PROXY_CONFFILE}\"." raise NeedHostname end url = "http://" if proxy_setting[:proxy_user] and proxy_setting[:proxy_pass] url += "#{proxy_setting[:proxy_user]}:#{proxy_setting[:proxy_pass]}@" end url += proxy_setting[:proxy_host] if proxy_setting[:proxy_host] url += ":" + proxy_setting[:proxy_port] if proxy_setting[:proxy_port] ENV['http_proxy'] = url ENV['https_proxy'] = url ENV['ftp_proxy'] = url @@proxy_url = url end |