#include "headers.h" using namespace rapidxml; bool p_load_proxy_list(std::list *list, config *cfg, const char *path) { if(!list->empty()) list->clear(); std::ifstream file; if(!path) 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()) { 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; for(xml_node<> *node = proxy_list->first_node("Proxy"); node; node = node->next_sibling("Proxy")) { 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") e.type = config::SOCKS4; if(type == "SOCKS5") e.type = config::SOCKS5; e.host = node->first_node("Address")->value(); e.port = atoi(node->first_node("Port")->value()); e.options = atoi(node->first_node("Options")->value()); xml_node<> *auth = node->first_node("Authentication"); if(auth) { e.enable_auth = atoi(auth->first_attribute("enabled")->value()); e.login = auth->first_node("Username")->value(); e.password = auth->first_node("Password")->value(); //TODO: detect kind of hash, decrypt (needed for set auth settings in proxy list) } list->push_back(e); } if(!list->empty()) return true; // succesfully loaded some entries } return false; } bool p_load_proxy_chains(std::list *list, config *cfg, const char *path) { if(!list->empty()) list->clear(); std::ifstream file; if(!path) 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()) { 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; for(xml_node<> *node = chain_list->first_node("Chain"); node; node = node->next_sibling("Chain")) { config::p_proxy_chain c; c.id = atoi(node->first_attribute("id")->value()); c.name = node->first_node("Name")->value(); for(xml_node<> *proxy = node->first_node("Proxy"); proxy; proxy = proxy->next_sibling("Proxy")) { config::p_proxy_chain::chain_entry e; e.enabled = atoi(proxy->first_attribute("enabled")->value()); e.id = atoi(proxy->value()); c.chain.push_back(e); } list->push_back(c); } if(!list->empty()) return true; } return false; } bool p_save_proxy_list(std::list *list, config *cfg, const 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->remove_all_nodes(); proxy_list->remove_all_attributes(); } else { 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_element, "Address"); node->value(doc.allocate_string(i->host.c_str())); proxy->append_node(node); node = doc.allocate_node(node_element, "Port"); snprintf(val, 7, "%d", i->port); node->value(doc.allocate_string(val)); proxy->append_node(node); node = doc.allocate_node(node_element, "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"); auth->append_attribute(attr); node = doc.allocate_node(node_element, "Username"); node->value(doc.allocate_string(i->login.c_str())); auth->append_node(node); node = doc.allocate_node(node_element, "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<<"\n"; outfile< *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<> *rule_list = doc.first_node()->first_node("RuleList"); if(!rule_list) { rule_list = doc.allocate_node(node_element, "RuleList"); doc.append_node(rule_list); } xml_node<> *default_rule = NULL; for(xml_node<> *rule = rule_list->first_node("Chain"); rule; rule = rule->next_sibling("Chain")) { xml_node<> *name = rule->first_node("Name"); if(name) { if(!strcmp(name->value(), "Default")) { default_rule = rule; break; } } } char val[8]; snprintf(val, 7, "%d", entry->id); if(default_rule) //no validation here, assuming valid config { default_rule->first_attribute("enabled")->value("true"); xml_node<> *action = default_rule->first_node("Action"); action->first_attribute("type")->value("Proxy"); action->value(doc.allocate_string(val)); } else { default_rule = doc.allocate_node(node_element, "Rule"); rule_list->append_node(default_rule); default_rule->append_attribute(doc.allocate_attribute("enabled", "true")); default_rule->append_node(doc.allocate_node(node_element, "Name", "Default")); xml_node<> *action = doc.allocate_node(node_element, "Action", doc.allocate_string(val)); action->append_attribute(doc.allocate_attribute("type","Proxy")); default_rule->append_node(action); } } } bool p_save_proxy_chains(std::list *list, config *cfg, const char *path) { }