X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fhttputil.rb;h=b4f25c95e77aa6de7ac5e4689d7f11c9e4a928f8;hb=42fd60e6a81e592553cdc6bb3b6011386692dde2;hp=ff3216a6bd62df9efac78d3bb2ff5cf276792a9c;hpb=2a96c9198c1f6e13407d0999083f6ce5e0bc06fa;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/httputil.rb b/lib/rbot/httputil.rb index ff3216a6..b4f25c95 100644 --- a/lib/rbot/httputil.rb +++ b/lib/rbot/httputil.rb @@ -1,5 +1,7 @@ module Irc +module Utils +require 'resolv' require 'net/http' Net::HTTP.version_1_2 @@ -7,6 +9,23 @@ Net::HTTP.version_1_2 # this class can check the bot proxy configuration to determine if a proxy # needs to be used, which includes support for per-url proxy configuration. class HttpUtil + BotConfig.register BotConfigBooleanValue.new('http.use_proxy', + :default => false, :desc => "should a proxy be used for HTTP requests?") + BotConfig.register BotConfigStringValue.new('http.proxy_uri', :default => false, + :desc => "Proxy server to use for HTTP requests (URI, e.g http://proxy.host:port)") + BotConfig.register BotConfigStringValue.new('http.proxy_user', + :default => nil, + :desc => "User for authenticating with the http proxy (if required)") + BotConfig.register BotConfigStringValue.new('http.proxy_pass', + :default => nil, + :desc => "Password for authenticating with the http proxy (if required)") + BotConfig.register BotConfigArrayValue.new('http.proxy_include', + :default => [], + :desc => "List of regexps to check against a URI's hostname/ip to see if we should use the proxy to access this URI. All URIs are proxied by default if the proxy is set, so this is only required to re-include URIs that might have been excluded by the exclude list. e.g. exclude /.*\.foo\.com/, include bar\.foo\.com") + BotConfig.register BotConfigArrayValue.new('http.proxy_exclude', + :default => [], + :desc => "List of regexps to check against a URI's hostname/ip to see if we should use avoid the proxy to access this URI and access it directly") + def initialize(bot) @bot = bot @headers = { @@ -14,6 +33,49 @@ class HttpUtil } end + # if http_proxy_include or http_proxy_exclude are set, then examine the + # uri to see if this is a proxied uri + # the in/excludes are a list of regexps, and each regexp is checked against + # the server name, and its IP addresses + def proxy_required(uri) + use_proxy = true + if @bot.config["http.proxy_exclude"].empty? && @bot.config["http.proxy_include"].empty? + return use_proxy + end + + list = [uri.host] + begin + list.concat Resolv.getaddresses(uri.host) + rescue StandardError => err + puts "warning: couldn't resolve host uri.host" + end + + unless @bot.config["http.proxy_exclude"].empty? + re = @bot.config["http.proxy_exclude"].collect{|r| Regexp.new(r)} + re.each do |r| + list.each do |item| + if r.match(item) + use_proxy = false + break + end + end + end + end + unless @bot.config["http.proxy_include"].empty? + re = @bot.config["http.proxy_include"].collect{|r| Regexp.new(r)} + re.each do |r| + list.each do |item| + if r.match(item) + use_proxy = true + break + end + end + end + end + debug "using proxy for uri #{uri}?: #{use_proxy}" + return use_proxy + end + # uri:: Uri to create a proxy for # # return a net/http Proxy object, which is configured correctly for @@ -22,36 +84,27 @@ class HttpUtil # +http_proxy_include/exclude+ options. def get_proxy(uri) proxy = nil - if (ENV['http_proxy']) - proxy = URI.parse ENV['http_proxy'] - end - if (@bot.config["http_proxy"]) - proxy = URI.parse ENV['http_proxy'] - end - - # if http_proxy_include or http_proxy_exclude are set, then examine the - # uri to see if this is a proxied uri - if uri - if @bot.config["http_proxy_exclude"] - # TODO - end - if @bot.config["http_proxy_include"] - end - end - proxy_host = nil proxy_port = nil proxy_user = nil proxy_pass = nil - if @bot.config["http_proxy_user"] - proxy_user = @bot.config["http_proxy_user"] - if @bot.config["http_proxy_pass"] - proxy_pass = @bot.config["http_proxy_pass"] + + if @bot.config["http.use_proxy"] + if (ENV['http_proxy']) + proxy = URI.parse ENV['http_proxy'] rescue nil + end + if (@bot.config["http.proxy_uri"]) + proxy = URI.parse @bot.config["http.proxy_uri"] rescue nil + end + if proxy + debug "proxy is set to #{proxy.uri}" + if proxy_required(uri) + proxy_host = proxy.host + proxy_port = proxy.port + proxy_user = @bot.config["http.proxy_user"] + proxy_pass = @bot.config["http.proxy_pass"] + end end - end - if proxy - proxy_host = proxy.host - proxy_port = proxy.port end return Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user, proxy_port) @@ -84,5 +137,5 @@ class HttpUtil return nil end end - +end end