summaryrefslogtreecommitdiff
path: root/client/Proxifier.h
blob: 712e79c234fc025fd37d43bb67273c0b3811299b (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290


#ifndef PROXIFIER_CONFIG
#define PROXIFIER_CONFIG

#include <vector>
#include <string>
#include <QDomElement>
#include <QString>
#include "Config.h"
#include "Proxy.h"


using namespace std;


class QDomDocument;
class Proxy;

/**
 * @brief	Special class that represents proxifier's configuration file
 * @note	All operation that turn on/off and add/remove proxies and chain will apply to "Default" rule
 * 			All proxies are put into "Client" chain event if it's only one proxy
 */
class Proxifier
{
public:
	/**
	 * @brief	Get application-wide singleton instance of Proxifier class
	 * @return	pointer to Proxifier singleton
	 */
	static Proxifier* GetInstance();
    /**
     * @brief   Reload Proxifier's profile
     * @return  true on success or false otherwise
     */
    bool ReloadConfig();
	/**
	 * @brief	Chack if the current config is valid
	 * @return	true if the config is valid or false otherwise
	 * @note	you may wish to try ReadConfig() before but if it doesn't help
	 */
	bool IsValid();
	/**
	 * @brief	Check if certain server is turned on in Proxifier's tules and list
	 */
	bool IsOn(string& addr, short port);
	/**
	 * @brief	Get list of proxies that is being used by Proxifier right now
	 * @return	list of active Proxy records
	 */
	vector<Proxy> GetActiveProxies();
	/**
	 * @brief	turn proxy on, all traffic will go throught this proxy
	 * @param	proxy	Proxy class that represents particular proxy
	 * @return	true on success or false otherwise
	 * @note	adds specified proxy to Proxifier's Proxy list and in "Client" chain<br/>
	 * 			Also adds "Client" chain to "Default" rule if its not there
	 */
	bool TurnProxyOn(Proxy& proxy);
	/**
	 * @brief	turn proxy off
	 * @param	proxy	Proxy class that represents particular proxy
	 * @return	true on success or false otherwise<br/>
	 *			alse true will be returned even if proxy is not exist in config
	 * @note	removes specified proxy from "Client" chain and from Proxy list<br/>
	 * 			If its the last proxy in client chain then "Default" rule will be resetted to<br/>
	 * 			send all traffic directly to servers
	 */
	bool TurnProxyOff(Proxy& proxy);
	/**
	 * @brief	apply firewall rules
	 * @param	rules	firewall rules to apply
	 * @return	true on success or false otherwise
	 * @note	all existing rules will be overwritten
	 */
	bool ApplyFirewallRules(vector<Config::FirewallEntry> rules);
private:
	/**
	 * @brief	class representing Proxifier's proxy setting
	 */
	class ProxifierProxy: public Proxy
	{
	public:
		int id;
		short option;
		bool useAuth;		// whether to authenticate on server
		bool emptyAuth;		// self-closing auth (for Proxifier it means to use current login+pass)
		
		ProxifierProxy();
	};

	/**
	 * @brief	class representing Proxifier's proxy chain
	 */
	class Chain
	{
	public:
		/**
		 * @brief	chain id
		 */
		int id;
		/**
		 * @brief	chain name
		 */
		string name;
		/**
		 * @brief	associative array of proxies in the chain - [id: isEnabled]
		 */
		map<int, bool> proxies;

		Chain();
	};

	class Rule
	{
	public:
		/**
		 * @brief	indicates if the rule is enabled
		 */
		bool isEnabled;
		/**
		 * @brief	rule name
		 */
		string name;
		/**
		 * @brief	list of applications the rule affects
		 */
		string apps;
		/**
		 * @brief	rule targets (list of hosts separeted w ';')
		 */
		string targets;
		/**
		 * @brief	rule ports (e.g.: 80; 8000-9000; 8080)
		 */
		string ports;
		/**
		 * @brief	rule action (one of: 'Direct', 'Block', 'Chain', 'Proxy')
		 */
		string action;
		/**
		 * @brief	action parameter (applicable only to 'Chain' and 'Proxy' actions)
		 */
		int actId;

		Rule();
	};
	/**
	 * @brief	XML indentation level
	 */
	static const int indent = 4;
	/**
	 * @brief	default chain name to add proxies to
	 */
	static string defaultChain;
	/**
	 * @brief	default rule name to add proxies to
	 */
	static string defaultRule;
	/**
	 * @brief	default firewall rule name
	 */
	static string firewallRule;
	/**
	 * @brief	pointer to sigleton instance
	 */
	static Proxifier *instance;
	/**
	 * @brief	XML file name to read data from
	 */
	QString filePath;
	/**
	 * @brief	indicates whether config valid or not
	 */
	bool valid;
	vector<ProxifierProxy> proxyList;
	vector<Chain> chainList;
	vector<Rule> ruleList;
	
	Proxifier();
	/**
	 * @brief	read Proxifier config from file and parse it
	 * @note	you should check IsValid() method to get to know if the config is valid
	 */
	void ReadConfig();
	/**
	 * @brief	read ProxyList section in Proxifier's config
	 * @param	root	root DOM element
	 * @return	true on success or false otherwise
	 */
	bool ReadProxyList(QDomElement& root);
	/**
	 * @brief	read ChainList section in Proxifier's config
	 * @param	root	root DOM element
	 * @return	true on success or false otherwise
	 */	
	bool ReadChainList(QDomElement& root);
	/**
	 * @brief	read RuleList section in Proxifier's config
	 * @param	root	root DOM element
	 * @return	true on success or false otherwise
	 */	
	bool ReadRuleList(QDomElement& root);
	/**
	 * @brief	gets the next valid ID fo proxy/chain
	 * @note	this ID numbers are shared among chains and proxies
	 * @return	next valid ID number
	 */
	int GetNextId();
	/**
	 * @brief	add new proxy record to Proxifier config
	 * @param	proxy	proxy class representing Proxifier's proxy entry
	 * 			id		new proxy id will be set there
	 * @return	true on success or false otherwise
	 */
	bool AddProxy(ProxifierProxy& proxy, int* id);
	/**
	 * @brief	remove proxy entry from Proxifier's config
	 * @param	proxyId	proxy id to remove
	 * @return	true on success or false otherwise
	 */
	bool RemoveProxy(int proxyId);
	/**
	 * @brief	add new chain to proxifier config
	 * @param	name	the name of the chain
	 * 			id		new chain id
	 * @return	true on success or false otherwise
	 */
	bool AddChain(string& name, int* id);
	/**
	 * @brief	add proxy to proxy chain
	 * @param	proxyId	proxy ID to add to proxy chain
	 * 			chainId	chain ID to add proxy to
	 * @return	true on success or false otherwise
	 */
	bool AddProxyToChain(int proxyId, int chainId, bool isHttp = false);
	/**
	 * @brief	remove proxy from proxy chain
	 * @param	proxyId	proxy ID to remove to proxy chain
	 * 			chainId	chain ID to remove proxy from
	 * @return	true on success or false otherwise
	 */
	bool RemoveProxyFromChain(int proxyId, int chainId);
	/**
	 * @brief	add new rule to Proxifier config
	 * @param	rule	rule class representing Proxifier's rule entry
	 * @return	true on success or false otherwise
	 */
	bool AddRule(Rule& rule);
	/**
	 * @brief	modify action on existing rule
	 * @param	name	name of the rule to modify
	 * 			action	action name to set to
	 * 			actId	id  of action target (if applicable)
	 * @return	true on success or false otherwise
	 */
	bool SetRuleAction(string& name, string& action, int actId = -1);
	/**
	 * @brief	remove rule to Proxifier config
	 * @param	name	name of the rule to remove from Rule list
	 * @return	true on success or false otherwise
	 */
	bool RemoveRule(string& name);
	/**
	 * @brief	Check if certain proxy address exists in Proxifier's Proxy list
	 * @param	addr	proxy address to check for
	 * 			port	proxy port to check for
	 * 			id		if proxy exist then there'll be it's id number otherwise this value is undetermined
	 * @return	true if proxy exist or false otherwise
	 * @note	DNS names are not resolved and not compared to IPs<br/>
	 * 			proxies are compared only by checking address and port values
	 */
	bool isProxyExists(string& addr, short port, int* id = NULL);
	/**
	 * @brief	Check if certain chain exisis in Proxifier's Chain list
	 * @param	name	chain name to check for
	 * 			id		if chain exist then there'll be it's id number otherwise this value is undetermined
	 * @return	true if chain exist or false otherwise
	 */
	bool isChainExists(string& name, int* id = NULL);
	/**
	 * @brief	Check if certain rule exists
	 * @param	name	rule name to check for
	 * @return	true if rule exist or false otherwise
	 */
	bool isRuleExists(string& name);
};

#endif