-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathClientCreateParams.cs
More file actions
289 lines (247 loc) · 12.1 KB
/
ClientCreateParams.cs
File metadata and controls
289 lines (247 loc) · 12.1 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Tor.Config;
using Tor.Helpers;
namespace Tor
{
/// <summary>
/// A class containing the configurations needed when launching a tor application executable.
/// </summary>
[DebuggerStepThrough]
[Serializable]
public sealed class ClientCreateParams
{
private string defaultConfigurationFile;
private string configurationFile;
private string controlPassword;
private int controlPort;
private Dictionary<ConfigurationNames, object> overrides;
private string path;
/// <summary>
/// Initializes a new instance of the <see cref="ClientCreateParams"/> class.
/// </summary>
public ClientCreateParams() : this(null, 9051, "", null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ClientCreateParams"/> class.
/// </summary>
/// <param name="path">The path to the application executable file.</param>
public ClientCreateParams(string path) : this(path, 9051, "", null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ClientCreateParams"/> class.
/// </summary>
/// <param name="path">The path to the application executable file.</param>
/// <param name="configurationFile">The path to the configuration file.</param>
public ClientCreateParams(string path, string configurationFile) : this(path, 9051, "", configurationFile)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ClientCreateParams"/> class.
/// </summary>
/// <param name="path">The path to the application executable file.</param>
/// <param name="controlPort">The port number which the application will listening on for control connections.</param>
public ClientCreateParams(string path, int controlPort) : this(path, controlPort, "", null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ClientCreateParams"/> class.
/// </summary>
/// <param name="path">The path to the application executable file.</param>
/// <param name="controlPort">The port number which the application will listening on for control connections.</param>
/// <param name="controlPassword">The password used when authenticating with the control connection.</param>
public ClientCreateParams(string path, int controlPort, string controlPassword) : this(path, controlPort, controlPassword, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ClientCreateParams"/> class.
/// </summary>
/// <param name="path">The path to the application executable file.</param>
/// <param name="controlPort">The port number which the application will listening on for control connections.</param>
/// <param name="controlPassword">The password used when authenticating with the control connection.</param>
/// <param name="configurationFile">The path to the configuration file.</param>
public ClientCreateParams(string path, int controlPort, string controlPassword, string configurationFile)
{
this.configurationFile = configurationFile;
this.controlPassword = controlPassword;
this.controlPort = controlPort;
this.defaultConfigurationFile = null;
this.overrides = new Dictionary<ConfigurationNames, object>();
this.path = path;
}
#region Properties
/// <summary>
/// Gets or sets the path to the configuration file containing the default values. A value of <c>null</c>
/// indicates that no default configuration file will be loaded.
/// </summary>
public string DefaultConfigurationFile
{
get { return defaultConfigurationFile; }
set { defaultConfigurationFile = value; }
}
/// <summary>
/// Gets or sets the path to the configuration file. A value of <c>null</c> indicates that no
/// configuration file will be loaded.
/// </summary>
public string ConfigurationFile
{
get { return configurationFile; }
set { configurationFile = value; }
}
/// <summary>
/// Gets or sets the password used when authenticating with the tor application on the control connection. A value of
/// <c>null</c> or blank indicates that no password has been configured.
/// </summary>
public string ControlPassword
{
get { return controlPassword; }
set { controlPassword = value; }
}
/// <summary>
/// Gets or sets the port number which the application will be listening on for control connections.
/// </summary>
public int ControlPort
{
get { return controlPort; }
set { controlPort = value; }
}
/// <summary>
/// Gets or sets the path to the application executable file.
/// </summary>
public string Path
{
get { return path; }
set { path = value; }
}
#endregion
#region System.Object
/// <summary>
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
ClientCreateParams compare = obj as ClientCreateParams;
if (compare == null)
return false;
return compare.configurationFile == configurationFile &&
compare.controlPassword == controlPassword &&
compare.controlPort == controlPort &&
compare.defaultConfigurationFile == defaultConfigurationFile &&
compare.path == path;
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 23 + (defaultConfigurationFile != null ? defaultConfigurationFile.GetHashCode() : 0);
hash = hash * 23 + (configurationFile != null ? configurationFile.GetHashCode() : 0);
hash = hash * 23 + (controlPassword != null ? controlPassword.GetHashCode() : 0);
hash = hash * 23 + (controlPort);
hash = hash * 23 + (path != null ? path.GetHashCode() : 0);
return hash;
}
}
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public override string ToString()
{
StringBuilder builder;
builder = new StringBuilder();
builder.Append("--ignore-missing-torrc ");
builder.Append("--allow-missing-torrc ");
builder.AppendFormat("--ControlPort {0}", controlPort);
if (!string.IsNullOrWhiteSpace(configurationFile))
builder.AppendFormat(" -f \"{0}\"", configurationFile);
if (!string.IsNullOrWhiteSpace(defaultConfigurationFile))
builder.AppendFormat(" --defaults-torrc \"{0}\"", defaultConfigurationFile);
foreach (KeyValuePair<ConfigurationNames, object> over in overrides)
{
string value = "";
ConfigurationAssocAttribute attribute = ReflectionHelper.GetAttribute<ConfigurationNames, ConfigurationAssocAttribute>(over.Key);
if (attribute == null)
continue;
if (over.Value != null)
value = ReflectionHelper.Convert(over.Value, typeof(string)) as string;
if (value == null)
value = "";
if (value.Contains(" "))
value = "\"" + value + "\"";
builder.AppendFormat(" --{0} {1}", attribute.Name, value);
}
return builder.ToString();
}
#endregion
/// <summary>
/// Sets the value of a configuration supplied with the process arguments. Setting a configuration value within the create
/// parameters overrides any values stored in the configuration file. The control port can also be set using this method.
/// </summary>
/// <param name="configuration">The configuration which should be overridden.</param>
/// <param name="value">The value of the configuration. This should align with the type specified in the <see cref="Tor.Config.Configuration"/> class.</param>
public void SetConfig(ConfigurationNames configuration, object value)
{
ConfigurationAssocAttribute attribute = ReflectionHelper.GetAttribute<ConfigurationNames, ConfigurationAssocAttribute>(configuration);
if (attribute == null)
return;
if (value != null && !value.GetType().Equals(attribute.Type))
throw new ArgumentException("The value of this configuration should be of type " + attribute.Type, "value");
ConfigurationValidation validation = attribute.Validation;
if (validation != ConfigurationValidation.None)
{
if (validation.HasFlag(ConfigurationValidation.NonNull) && value == null)
throw new ArgumentNullException("value");
if (validation.HasFlag(ConfigurationValidation.NonZero) ||
validation.HasFlag(ConfigurationValidation.NonNegative) ||
validation.HasFlag(ConfigurationValidation.PortRange))
{
int converted = Convert.ToInt32(value);
if (validation.HasFlag(ConfigurationValidation.NonZero) && converted == 0)
throw new ArgumentOutOfRangeException("value", "The value of this configuration cannot be zero");
if (validation.HasFlag(ConfigurationValidation.NonNegative) && converted < 0)
throw new ArgumentOutOfRangeException("value", "The value of this configuration cannot be below zero");
if (validation.HasFlag(ConfigurationValidation.PortRange) && (converted <= 0 || short.MaxValue < converted))
throw new ArgumentOutOfRangeException("value", "The value of this configuration must be within a valid port range");
}
if (validation.HasFlag(ConfigurationValidation.SizeDivision))
{
double converted = Convert.ToDouble(value);
if (converted % 1024 != 0)
throw new ArgumentException("The value of this configuration must be divisible by 1024");
}
}
if (configuration == ConfigurationNames.ControlPort)
controlPort = Convert.ToInt32(value);
else
overrides[configuration] = value;
}
/// <summary>
/// Validates the parameters assigned to the object, and throws relevant exceptions where necessary.
/// </summary>
internal void ValidateParameters()
{
if (string.IsNullOrWhiteSpace(path))
throw new TorException("The path cannot be null or white-space");
if (controlPort <= 0 || short.MaxValue < controlPort)
throw new TorException("The control port number must be within a valid port range");
}
}
}