diff options
Diffstat (limited to 'server')
-rwxr-xr-x | server/server/config.cpp | 30 | ||||
-rwxr-xr-x | server/server/config.h | 8 | ||||
-rw-r--r-- | server/server/speedtest.cpp | 79 | ||||
-rw-r--r-- | server/server/speedtest.h | 3 |
4 files changed, 104 insertions, 16 deletions
diff --git a/server/server/config.cpp b/server/server/config.cpp index 55d8d1e..5d80cab 100755 --- a/server/server/config.cpp +++ b/server/server/config.cpp @@ -1,6 +1,6 @@ #include "headers.h" -void reload_config_thread_func(config *cfg) +void reload_config_thread_func(config *cfg, boost::mutex *m) { while(!boost::this_thread::interruption_requested()) { @@ -9,8 +9,10 @@ void reload_config_thread_func(config *cfg) return; cfg->load_vars(); cfg->load_cvars(); + m->lock(); cfg->load_proxy_list(); cfg->load_static_proxy_list(); + m->unlock(); cfg->load_firewall_list(); cfg->load_deleted_list(); cfg->load_upload_list(); @@ -18,6 +20,29 @@ void reload_config_thread_func(config *cfg) } } +void speed_test_thread_func(std::list<config::proxy_entry>* proxy_list, std::list<config::static_proxy_entry> *static_proxy_list, const std::string& test_url, boost::mutex *m) +{ + while(!boost::this_thread::interruption_requested()) + { + m->lock(); + for(std::list<config::static_proxy_entry>::iterator i = static_proxy_list->begin(), end = static_proxy_list->end(); i != end; ++i) + { + speedtest t(test_url); + t.test(&(*i)); + boost::this_thread::sleep(boost::posix_time::seconds(30)); + } +/* for(std::list<config::proxy_entry>::iterator i = proxy_list->begin(), end = proxy_list->end(); i != end; ++i) + { + speedtest t(test_url); + t.test(&(*i)); + boost::this_thread::sleep(boost::posix_time::seconds(30)); + } */ + m->unlock(); + boost::this_thread::sleep(boost::posix_time::minutes(180)); + } +} + + void config::load_vars() { std::ifstream config; @@ -84,7 +109,8 @@ config::config(const char *pth) load_deleted_list(); load_upload_list(); load_servers_list(); - cfg_reload_thr = new boost::thread(boost::bind(reload_config_thread_func, this)); + cfg_reload_thr = new boost::thread(boost::bind(reload_config_thread_func, this, &lists_mutex)); + speed_test_thr = new boost::thread(boost::bind(speed_test_thread_func, &proxy_list, &static_proxy_list, "http://sss.chaoslab.ru", &lists_mutex)); } const int config::get_int(const std::string& data, const char* var, int default_) { diff --git a/server/server/config.h b/server/server/config.h index ee8a9ee..86c3988 100755 --- a/server/server/config.h +++ b/server/server/config.h @@ -13,8 +13,9 @@ public: { std::string host, login, password, country, state, city; int port; + size_t speed; proxy_type type; - proxy_entry(): port(0), type(UNKNOWN) + proxy_entry(): port(0), speed(0), type(UNKNOWN) {} proxy_entry (std::string login_, std::string password_, std::string host_, int port_, std::string country_, std::string state_, std::string city_, proxy_type t) { @@ -68,7 +69,7 @@ public: struct static_proxy_entry { int port, position; - uint64_t speed; + size_t speed; std::string host, login, password, label; proxy_type type; static_proxy_entry(): port(0), position(0), speed(0), type(UNKNOWN) @@ -222,7 +223,8 @@ private: std::list<upload_entry> upload_list; std::list<conn_server> servers_list; std::list<p_proxy_entry> p_proxy_list; - boost::thread *cfg_reload_thr; + boost::thread *cfg_reload_thr, *speed_test_thr; + boost::mutex lists_mutex; }; #endif
\ No newline at end of file diff --git a/server/server/speedtest.cpp b/server/server/speedtest.cpp index 5c151e1..06aaf76 100644 --- a/server/server/speedtest.cpp +++ b/server/server/speedtest.cpp @@ -18,7 +18,7 @@ size_t speedtest::curl_data_callback(void *contents, size_t size, size_t nmemb, } -size_t speedtest::test(const config::proxy_entry& p) +size_t speedtest::test(config::proxy_entry *p) { curl = curl_easy_init(); /* if(!curl) @@ -27,14 +27,14 @@ size_t speedtest::test(const config::proxy_entry& p) chunk.memory = (char*)malloc(1); chunk.size = 0; curl_easy_setopt(curl, CURLOPT_URL, test_url.c_str()); - curl_easy_setopt(curl, CURLOPT_PROXY, p.host.c_str()); + curl_easy_setopt(curl, CURLOPT_PROXY, p->host.c_str()); char port[8]; - snprintf(port, 7, "%d", p.port); + snprintf(port, 7, "%d", p->port); curl_easy_setopt(curl, CURLOPT_PROXYPORT, port); - if(!p.password.empty() && !p.login.empty()) - curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, std::string(p.login + ":" + p.password).c_str()); + if(!p->password.empty() && !p->login.empty()) + curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, std::string(p->login + ":" + p->password).c_str()); curl_proxytype type = CURLPROXY_HTTP; - switch(p.type) + switch(p->type) { case config::HTTP: type = CURLPROXY_HTTP; @@ -64,13 +64,72 @@ size_t speedtest::test(const config::proxy_entry& p) curl_easy_cleanup(curl); free(chunk.memory); + size_t speed = 0; if(chunk.size) { if(time) - return chunk.size/time; + speed = chunk.size/time; else - return chunk.size; + speed = chunk.size; } - else - return 0; + p->speed = speed; + return speed; +} + +size_t speedtest::test(config::static_proxy_entry *p) +{ + curl = curl_easy_init(); +/* if(!curl) + ;//handle error */ + memory_struct chunk; + chunk.memory = (char*)malloc(1); + chunk.size = 0; + curl_easy_setopt(curl, CURLOPT_URL, test_url.c_str()); + curl_easy_setopt(curl, CURLOPT_PROXY, p->host.c_str()); + char port[8]; + snprintf(port, 7, "%d", p->port); + curl_easy_setopt(curl, CURLOPT_PROXYPORT, port); + if(!p->password.empty() && !p->login.empty()) + curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, std::string(p->login + ":" + p->password).c_str()); + curl_proxytype type = CURLPROXY_HTTP; + switch(p->type) + { + case config::HTTP: + type = CURLPROXY_HTTP; + break; + case config::HTTPS: + type = CURLPROXY_HTTP; //is it correct ? + curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L); //required for ssl + break; + case config::SOCKS4: + type = CURLPROXY_SOCKS4; + break; + case config::SOCKS5: + type = CURLPROXY_SOCKS5; + break; + } + curl_easy_setopt(curl, CURLOPT_PROXYTYPE, type); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &speedtest::curl_data_callback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&chunk); + curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-speedtest/0.1"); + + timer.restart(); + + curl_easy_perform(curl); + + double time = timer.elapsed(); + + curl_easy_cleanup(curl); + + free(chunk.memory); + size_t speed = 0; + if(chunk.size) + { + if(time) + speed = chunk.size/time; + else + speed = chunk.size; + } + p->speed = speed; + return speed; } diff --git a/server/server/speedtest.h b/server/server/speedtest.h index aaed918..2d006b8 100644 --- a/server/server/speedtest.h +++ b/server/server/speedtest.h @@ -6,7 +6,8 @@ class speedtest { public: - size_t test(const config::proxy_entry&); + size_t test(config::proxy_entry*); + size_t test(config::static_proxy_entry*); speedtest() : curl(NULL){} speedtest(const std::string& url) : curl(NULL) { |