From 4eb87664cba921b8aa1ac5b5b901cf7ffd7e1aae Mon Sep 17 00:00:00 2001 From: Bjoern Sikora Date: Tue, 25 Aug 2009 18:01:08 +0200 Subject: [PATCH] Implemented first part of http proxy ability. --- src/config.cpp | 34 ++++++++++++++++++++++++++++++++++ src/config.h | 6 ++++++ src/iphelper.cpp | 12 +++++++++++- src/iphelper.h | 4 +++- src/updater.cpp | 6 +++++- 5 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index 79f35c1..f1fc0ea 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -74,6 +74,8 @@ Config::Config(Logger::Ptr _log, Serviceholder::Ptr _serviceholder) ("enable_ipv6",po::value()->default_value(false),"Try to use IPv6.") ("webcheck_url",po::value()->default_value(""),"Use this URL to determine IP.") ("webcheck_url_alt",po::value()->default_value(""),"Use this alternative URL to determine IP.") + ("http_proxy",po::value()->default_value(""),"Use this proxy for all http requests.") + ("http_proxy_port",po::value()->default_value(0),"Port of the proxy.") ; // Define valid command line parameters @@ -199,6 +201,12 @@ int Config::parse_cmd_line(int argc, char *argv[]) if ( VariablesMap.count("webcheck_url_alt") ) WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as(); + if ( VariablesMap.count("http_proxy") ) + Proxy = VariablesMap["http_proxy"].as(); + + if ( VariablesMap.count("http_proxy_port") ) + ProxyPort = VariablesMap["http_proxy_port"].as(); + } catch(po::unknown_option e) { @@ -351,6 +359,12 @@ int Config::load_main_config_file(const string& full_filename) if ( VariablesMap.count("webcheck_url_alt") ) WebcheckIpUrlAlt = VariablesMap["webcheck_url_alt"].as(); + if ( VariablesMap.count("http_proxy") ) + Proxy = VariablesMap["http_proxy"].as(); + + if ( VariablesMap.count("http_proxy_port") ) + ProxyPort = VariablesMap["http_proxy_port"].as(); + } catch ( po::unknown_option e ) // at the moment 04-08-2009 this exception is never thrown :-( { @@ -511,3 +525,23 @@ string Config::get_webcheck_ip_url_alt() const { return WebcheckIpUrlAlt; } + + +/** + * Get member Proxy + * @return Proxy + */ +string Config::get_proxy() const +{ + return Proxy; +} + + +/** + * Get member ProxyPort + * @return ProxyPort + */ +int Config::get_proxy_port() const +{ + return ProxyPort; +} diff --git a/src/config.h b/src/config.h index 36006f8..a33f8c0 100644 --- a/src/config.h +++ b/src/config.h @@ -40,6 +40,8 @@ private: std::string ConfigPath; std::string WebcheckIpUrl; std::string WebcheckIpUrlAlt; + std::string Proxy; + int ProxyPort; Service::Ptr create_service(const std::string &protocol,const std::string &hostname, const std::string &login, const std::string &password, const int update_interval, const int max_updates_within_interval, const int dns_cache_ttl); int load_main_config_file(const std::string& full_filename); @@ -71,6 +73,10 @@ public: bool get_enable_ipv6() const; + std::string get_proxy() const; + + int get_proxy_port() const; + std::string get_webcheck_ip_url() const; std::string get_webcheck_ip_url_alt() const; diff --git a/src/iphelper.cpp b/src/iphelper.cpp index 9704b59..7dd8e01 100644 --- a/src/iphelper.cpp +++ b/src/iphelper.cpp @@ -22,6 +22,8 @@ IPHelper::IPHelper() : Hostname("") , WebcheckIpUrl("") , WebcheckIpUrlAlt("") + , Proxy("") + , ProxyPort(0) , UseIPv6(false) , Log(new Logger) { @@ -31,12 +33,14 @@ IPHelper::IPHelper() /** * Constructor. */ -IPHelper::IPHelper(const Logger::Ptr _log, const string& _webcheck_url, const string& _webcheck_url_alt, const bool _use_ipv6) +IPHelper::IPHelper(const Logger::Ptr _log, const string& _webcheck_url, const string& _webcheck_url_alt, const bool _use_ipv6, const string& _proxy, const int _proxy_port) : Hostname("") { Log = _log; WebcheckIpUrl = _webcheck_url; WebcheckIpUrlAlt = _webcheck_url_alt; + Proxy = _proxy; + ProxyPort = _proxy_port; UseIPv6 = _use_ipv6; Hostname = net::ip::host_name(); @@ -265,6 +269,12 @@ CURL * IPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff) cons curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEFUNCTION,http_receive); curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEDATA,&curl_writedata_buff); + if ( !Proxy.empty() ) + { + curl_easy_setopt(curl_easy_handle,CURLOPT_PROXY,Proxy); + curl_easy_setopt(curl_easy_handle,CURLOPT_PROXYPORT,ProxyPort); + } + return curl_easy_handle; } diff --git a/src/iphelper.h b/src/iphelper.h index 7b6fdbb..95e2170 100644 --- a/src/iphelper.h +++ b/src/iphelper.h @@ -24,6 +24,8 @@ private: std::string Hostname; std::string WebcheckIpUrl; std::string WebcheckIpUrlAlt; + std::string Proxy; + int ProxyPort; bool UseIPv6; @@ -35,7 +37,7 @@ public: IPHelper(); - IPHelper(const Logger::Ptr _log, const std::string& _webcheck_url, const std::string& _webcheck_url_alt, const bool _use_ipv6); + IPHelper(const Logger::Ptr _log, const std::string& _webcheck_url, const std::string& _webcheck_url_alt, const bool _use_ipv6, const std::string& _proxy, const int _proxy_port); ~IPHelper(); diff --git a/src/updater.cpp b/src/updater.cpp index 484444d..f25bc93 100644 --- a/src/updater.cpp +++ b/src/updater.cpp @@ -127,7 +127,7 @@ Logger::Ptr Updater::get_logger() const int Updater::init_ip_helper() { // initialize IPHelper - IPHelper::Ptr _iphelp(new IPHelper(Log,Conf->get_webcheck_ip_url(),Conf->get_webcheck_ip_url_alt(),Conf->get_enable_ipv6())); + IPHelper::Ptr _iphelp(new IPHelper(Log,Conf->get_webcheck_ip_url(),Conf->get_webcheck_ip_url_alt(),Conf->get_enable_ipv6(),Conf->get_proxy(),Conf->get_proxy_port())); IPHelp = _iphelp; _iphelp.reset(); @@ -188,6 +188,7 @@ void Updater::update_services() if ( service->get_last_updates()->size() > 0 ) lastupdated = service->get_last_updates()->front(); + // If the dns cache ttl is expired, then get the actual ip of the dns record (this should be the IP in the last update) if ( (lastupdated != 0) && ((lastupdated + service->get_dns_cache_ttl()) < current_time) ) { Log->print_recheck_dns_entry(service->get_hostname(),lastupdated,service->get_dns_cache_ttl(),current_time); @@ -197,6 +198,9 @@ void Updater::update_services() dns_recheck_ip = _dns_recheck_ip; } + // In case the local hosts IP (ip) differ from the IP set in the last update (actual_ip) or + // the IP of the dns record (dns_recheck_ip) differs from the IP of the local host (ip) + // then perform an update. This implies that the update is not performed if actual_ip == dns_recheck_ip. if ( (service->get_actual_ip() != ip) || (dns_recheck_ip != ip) ) service->update(ip,current_time); } -- 1.7.1