From 8a1608b14343f8612199a5fae9173df6d0361821 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Sat, 10 Dec 2011 04:38:27 +0200 Subject: added proxy type (type//:) support to config loading added proxy list saving to proxifier config some xml fixes //all untested --- server/server/config.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++++--- server/server/config.h | 19 ++++++---- server/server/headers.h | 1 + server/server/p_xml.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 189 insertions(+), 12 deletions(-) diff --git a/server/server/config.cpp b/server/server/config.cpp index 7a1c80b..7e46bdc 100755 --- a/server/server/config.cpp +++ b/server/server/config.cpp @@ -198,7 +198,7 @@ void config::load_proxy_list(char *pth) if(!cfg_str.empty()) { std::string::size_type p1 = 0, p2 = 0, l = 1; - if(cfg_str.find("\r\n") != std::string::npos) + if(cfg_str.rfind("\r\n") != std::string::npos) l = 2; p2 = cfg_str.find(';'); while(p2 != std::string::npos) @@ -207,9 +207,21 @@ void config::load_proxy_list(char *pth) p1 = p2+l+1; std::string::size_type lp1 = 0, lp2 = 0; std::string host, login, password, country, state, city; + proxy_type type = UNKNOWN; int port = 0; if(line.find('@') != std::string::npos) { + lp2 = line.find("//:", lp1); + std::string s_type = line.substr(lp1, lp2-lp1); + if(s_type == "http") + type = HTTP; + else if(s_type == "https") + type = HTTPS; + else if(s_type == "socks4") + type = SOCKS4; + else if(s_type == "socks5") + type = SOCKS5; + lp1 = lp2+3; lp2 = line.find(':', lp1); login = line.substr(lp1, lp2-lp1); lp1 = lp2+1; @@ -233,6 +245,17 @@ void config::load_proxy_list(char *pth) } else { + lp2 = line.find("//:", lp1); + std::string s_type = line.substr(lp1, lp2-lp1); + if(s_type == "http") + type = HTTP; + else if(s_type == "https") + type = HTTPS; + else if(s_type == "socks4") + type = SOCKS4; + else if(s_type == "socks5") + type = SOCKS5; + lp1 = lp2+3; lp2 = line.find(':', lp1); host = line.substr(lp1, lp2-lp1); lp1 = lp2+1; @@ -248,7 +271,7 @@ void config::load_proxy_list(char *pth) lp2 = line.find('"', lp1); city = line.substr(lp1, lp2-lp1); } - proxy_list.push_back(proxy_entry(login, password, host, port, country, state, city)); + proxy_list.push_back(proxy_entry(login, password, host, port, country, state, city, type)); p2 = cfg_str.find(';', p1); } } @@ -270,7 +293,7 @@ void config::load_static_proxy_list(char *pth) if(!cfg_str.empty()) { std::string::size_type p1 = 0, p2 = 0, l = 1; - if(cfg_str.find("\r\n") != std::string::npos) + if(cfg_str.rfind("\r\n") != std::string::npos) l = 2; p2 = cfg_str.find(';'); while(p2 != std::string::npos) @@ -279,9 +302,20 @@ void config::load_static_proxy_list(char *pth) p1 = p2+l+1; std::string::size_type lp1 = 0, lp2 = 0; std::string host, login, password, label; + proxy_type type = UNKNOWN; int port = 0, position = 0; if(line.find('@') != std::string::npos) { + lp2 = line.find("//:", lp1); + std::string s_type = line.substr(lp1, lp2-lp1); + if(s_type == "http") + type = HTTP; + else if(s_type == "https") + type = HTTPS; + else if(s_type == "socks4") + type = SOCKS4; + else if(s_type == "socks5") + type = SOCKS5; lp2 = line.find(':', lp1); login = line.substr(lp1, lp2-lp1); lp1 = lp2+1; @@ -302,6 +336,16 @@ void config::load_static_proxy_list(char *pth) } else { + lp2 = line.find("//:", lp1); + std::string s_type = line.substr(lp1, lp2-lp1); + if(s_type == "http") + type = HTTP; + else if(s_type == "https") + type = HTTPS; + else if(s_type == "socks4") + type = SOCKS4; + else if(s_type == "socks5") + type = SOCKS5; lp2 = line.find(':'), lp1; host = line.substr(lp1, lp2-lp1); lp1 = lp2+1; @@ -314,7 +358,7 @@ void config::load_static_proxy_list(char *pth) lp2 = line.size(); position = atoi(line.substr(lp1, lp2-lp1).c_str()); } - static_proxy_list.push_back(static_proxy_entry(login, password, host, port, label, position)); + static_proxy_list.push_back(static_proxy_entry(login, password, host, port, label, position, type)); p2 = cfg_str.find(';', p1); } } @@ -377,6 +421,45 @@ void config::load_deleted_list(char* pth) } } +std::list *config::make_p_proxy_list() +{ + p_proxy_list.clear(); + if(!static_proxy_list.empty()) + { + for(std::list::iterator i = static_proxy_list.begin(), end = static_proxy_list.end(); i != end; ++i) + { + p_proxy_entry e; + e.enable_auth = (!i->login.empty())?true:false; + e.host = i->host; + e.id = !p_proxy_list.empty()?p_proxy_list.back().id+1:100; + e.login = i->login; + e.options = 0; + e.password = i->password; + e.port = i->port; + e.type = i->type; + p_proxy_list.push_back(e); + } + } + + if(!proxy_list.empty()) + { + for(std::list::iterator i = proxy_list.begin(), end = proxy_list.end(); i != end; ++i) + { + p_proxy_entry e; + e.enable_auth = (!i->login.empty())?true:false; + e.host = i->host; + e.id = !p_proxy_list.empty()?p_proxy_list.back().id+1:100; + e.login = i->login; + e.options = 0; + e.password = i->password; + e.port = i->port; + e.type = i->type; + p_proxy_list.push_back(e); + } + } + return &p_proxy_list; +} + const int config::ban_time() { return vars.ban_time; diff --git a/server/server/config.h b/server/server/config.h index 90b9d27..d94bf14 100755 --- a/server/server/config.h +++ b/server/server/config.h @@ -8,13 +8,15 @@ class config { public: + enum proxy_type {UNKNOWN = -1, HTTP, HTTPS, SOCKS4, SOCKS5}; struct proxy_entry { std::string host, login, password, country, state, city; int port; - proxy_entry(): port(0) + proxy_type type; + proxy_entry(): port(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_entry (std::string login_, std::string password_, std::string host_, int port_, std::string country_, std::string state_, std::string city_, proxy_type t) { login = login_; password = password_; @@ -23,15 +25,15 @@ public: country = country_; state = state_; city = city_; + type = t; } }; - enum p_proxy_type {UNKNOWN = -1, HTTPS, SOCKS4, SOCKS5}; struct p_proxy_entry { std::string host, login, password; int port, id, options; bool enable_auth; - p_proxy_type type; + proxy_type type; p_proxy_entry() { port = 3128; @@ -68,9 +70,10 @@ public: int port, position; uint64_t speed; std::string host, login, password, label; - static_proxy_entry(): port(0), position(0), speed(0) + proxy_type type; + static_proxy_entry(): port(0), position(0), speed(0), type(UNKNOWN) {} - static_proxy_entry(std::string login_, std::string password_, std::string host_, int port_, std::string label_, int position_) + static_proxy_entry(std::string login_, std::string password_, std::string host_, int port_, std::string label_, int position_, proxy_type t) { login = login_; password = password_; @@ -78,6 +81,7 @@ public: port = port_; label = label_; position = position_; + type = t; } }; @@ -162,6 +166,7 @@ public: std::list *get_deleted_list(); std::list *get_upload_list(); std::list *get_server_list(); + std::list *make_p_proxy_list(); private: struct cfg_data { @@ -207,6 +212,8 @@ private: std::list firewall_list, deleted_list; std::list upload_list; std::list servers_list; + + std::list p_proxy_list; }; #endif \ No newline at end of file diff --git a/server/server/headers.h b/server/server/headers.h index 65f9b37..8592f9f 100755 --- a/server/server/headers.h +++ b/server/server/headers.h @@ -23,6 +23,7 @@ #include #include +#include #include diff --git a/server/server/p_xml.cpp b/server/server/p_xml.cpp index 3fac837..9c6258d 100755 --- a/server/server/p_xml.cpp +++ b/server/server/p_xml.cpp @@ -7,7 +7,7 @@ bool p_load_proxy_list(std::list *list, char *path) { std::ifstream file; if(!path) - file.open(std::string(cfg->proxifier_path()+"/Default/Default.ppx").c_str(), std::ios::in); + file.open(std::string(cfg->proxifier_path()+"/Default/Default.ppx").c_str(), std::ios::in); //TODO: handle different locations else file.open(path, std::ios::in); if(file.is_open()) @@ -15,6 +15,7 @@ bool p_load_proxy_list(std::list *list, char *path) std::string data((std::istreambuf_iterator(file)), std::istreambuf_iterator()); file.close(); xml_document<> doc; + doc.parse<0>(doc.allocate_string(data.c_str())); xml_node<> *proxy_list = doc.first_node()->first_node("ProxyList"); if(!proxy_list) return false; @@ -23,6 +24,8 @@ bool p_load_proxy_list(std::list *list, char *path) config::p_proxy_entry e; e.id = atoi(node->first_attribute("id")->value()); std::string type = node->first_attribute("type")->value(); + if(type == "HTTP") + e.type = config::HTTP; if(type == "HTTPS") e.type = config::HTTPS; if(type == "SOCKS4") @@ -51,7 +54,7 @@ bool p_load_proxy_chains(std::list *list, char *path) { std::ifstream file; if(!path) - file.open(std::string(cfg->proxifier_path()+"/Default/Default.ppx").c_str(), std::ios::in); + file.open(std::string(cfg->proxifier_path()+"/Default/Default.ppx").c_str(), std::ios::in); //TODO: handle different locations else file.open(path, std::ios::in); if(file.is_open()) @@ -59,6 +62,7 @@ bool p_load_proxy_chains(std::list *list, char *path) std::string data((std::istreambuf_iterator(file)), std::istreambuf_iterator()); file.close(); xml_document<> doc; + doc.parse<0>(doc.allocate_string(data.c_str())); xml_node<> *chain_list = doc.first_node()->first_node("ChainList"); if(!chain_list) return false; @@ -84,6 +88,88 @@ bool p_load_proxy_chains(std::list *list, char *path) bool p_save_proxy_list(std::list *list, char *path) { + std::list *p_list = cfg->make_p_proxy_list(); + if(p_list->empty()) + return false; + std::ifstream infile; + if(!path) + infile.open(std::string(cfg->proxifier_path()+"/Default/Default.ppx").c_str(), std::ios::in); //TODO: handle different locations + else + infile.open(path, std::ios::in); + if(infile.is_open()) + { + std::string data((std::istreambuf_iterator(infile)), std::istreambuf_iterator()); + infile.close(); + xml_document<> doc; + doc.parse<0>(doc.allocate_string(data.c_str())); + xml_node<> *proxy_list = doc.first_node()->first_node("ProxyList"); + if(!proxy_list) + { + proxy_list = doc.allocate_node(node_element, "ProxyList"); + doc.append_node(proxy_list); + } + for(std::list::iterator i = p_list->begin(), end = p_list->end(); i != end; ++i) + { + xml_node<> *proxy = doc.allocate_node(node_element, "Proxy"); + char val[8]; + snprintf(val, 7, "%d", i->id); + xml_attribute<> *attr = doc.allocate_attribute("id", doc.allocate_string(val)); + proxy->append_attribute(attr); + std::string type; + switch(i->type) + { + case config::HTTP: + type = "HTTP"; + break; + case config::HTTPS: + type = "HTTPS"; + break; + case config::SOCKS4: + type = "SOCKS4"; + break; + case config::SOCKS5: + type = "SOCKS5"; + break; + } + attr = doc.allocate_attribute("type", doc.allocate_string(type.c_str())); + proxy->append_attribute(attr); + xml_node<> *node = doc.allocate_node(node_data, "Address"); + node->value(doc.allocate_string(i->host.c_str())); + proxy->append_node(node); + node = doc.allocate_node(node_data, "Port"); + snprintf(val, 7, "%d", i->port); + node->value(doc.allocate_string(val)); + proxy->append_node(node); + node = doc.allocate_node(node_data, "Options"); + snprintf(val, 7, "%d", i->options); + node->value(doc.allocate_string(val)); + proxy->append_node(node); + if(!i->login.empty()) + { + xml_node<> *auth = doc.allocate_node(node_element, "Authentication"); + attr = doc.allocate_attribute("enabled", i->enable_auth?"true":"false"); + node = doc.allocate_node(node_data, "Username"); + node->value(doc.allocate_string(i->login.c_str())); + auth->append_node(node); + node = doc.allocate_node(node_data, "Password"); + node->value(doc.allocate_string(i->password.c_str())); + auth->append_node(node); + proxy->append_node(auth); + } + proxy_list->append_node(proxy); + } + std::ofstream outfile; + if(!path) + outfile.open(std::string(cfg->proxifier_path()+"/Default/Default.ppx").c_str()); //TODO: handle different locations + else + outfile.open(path); + if(outfile.is_open()) + { + outfile< *list, char *path) -- cgit v1.2.3