summaryrefslogtreecommitdiff
path: root/server/server/p_xml.cpp
blob: 3d41428d5637ba3839edeee3eb518936f802076d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "headers.h"
/*
using namespace rapidxml;

bool p_load_proxy_list(std::list<config::p_proxy_entry> *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<char>(file)), std::istreambuf_iterator<char>());
		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<config::p_proxy_chain> *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<char>(file)), std::istreambuf_iterator<char>());
		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<config::p_proxy_entry> *list, config *cfg, const char *path)
{
	std::list<config::p_proxy_entry> *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<char>(infile)), std::istreambuf_iterator<char>());
		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<config::p_proxy_entry>::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<<"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
			outfile<<doc;
			return true;
		}
	}
	return false;
}

bool p_sset_default_rule(config::p_proxy_entry *entry, config *cfg, const char *path)
{
	std::list<config::p_proxy_entry> *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<char>(infile)), std::istreambuf_iterator<char>());
		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<config::p_proxy_chain> *list, config *cfg, const char *path)
{
}
*/