Initial project commit
This commit is contained in:
37
TorControlLibrary/Responses/CircuitStatusResponse.cs
Normal file
37
TorControlLibrary/Responses/CircuitStatusResponse.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TorControlLibrary.Responses
|
||||
{
|
||||
public class CircuitStatusResponse : CommandResponse
|
||||
{
|
||||
public CircuitStatusResponse()
|
||||
{
|
||||
Nodes = new SortedList<int, string>();
|
||||
}
|
||||
public static CircuitStatusResponse Parse(String line)
|
||||
{
|
||||
CircuitStatusResponse resp = new CircuitStatusResponse();
|
||||
resp.PopulateData(line, resp);
|
||||
return resp;
|
||||
}
|
||||
protected SortedList<Int32, String> ParseNodes(String data)
|
||||
{
|
||||
SortedList<Int32, String> nodes = new SortedList<int, string>();
|
||||
String[] nodeList = data.Split(',');
|
||||
for (int i = 0; i < nodeList.Length; i++)
|
||||
nodes.Add(i, nodeList[i]);
|
||||
return nodes;
|
||||
}
|
||||
[ParseAttribute(1)]
|
||||
public Int32 CircuitID { get; set; }
|
||||
[ParseAttribute(2)]
|
||||
public String Status { get; set; }
|
||||
[ParseAttribute(3, CustomParser="ParseNodes")]
|
||||
public SortedList<Int32,String> Nodes { get; set; }
|
||||
[ParseAttribute(4)]
|
||||
public String Purpose { get; set; }
|
||||
}
|
||||
}
|
||||
57
TorControlLibrary/Responses/CommandResponse.cs
Normal file
57
TorControlLibrary/Responses/CommandResponse.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
|
||||
namespace TorControlLibrary.Responses
|
||||
{
|
||||
public class CommandResponse
|
||||
{
|
||||
public CommandResponse()
|
||||
{
|
||||
}
|
||||
public CommandResponse(Int32 status, String response)
|
||||
{
|
||||
StatusCode = status;
|
||||
Response = response.EndsWith(Environment.NewLine) ? response : response + Environment.NewLine;
|
||||
}
|
||||
public virtual void AppendResponse(String line)
|
||||
{
|
||||
Response += line + Environment.NewLine;
|
||||
}
|
||||
|
||||
protected virtual void PopulateData(String dataLine, CommandResponse response)
|
||||
{
|
||||
String[] parts = dataLine.Split(' ');
|
||||
List<PropertyInfo> props = response.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(ParseAttribute), true).Any())
|
||||
.OrderBy(p => ((ParseAttribute)p.GetCustomAttributes(typeof(ParseAttribute), true).FirstOrDefault()).Order).ToList();
|
||||
|
||||
foreach (PropertyInfo prop in props)
|
||||
{
|
||||
ParseAttribute attribute = (ParseAttribute)prop.GetCustomAttributes(true).FirstOrDefault();
|
||||
String value = String.Empty;
|
||||
for (int i = 0; i < (attribute.DataElementsToRead == 0 ? 1 : attribute.DataElementsToRead ); i++)
|
||||
value += parts[(i + attribute.Order) - 1] + " ";
|
||||
value = value.Trim();
|
||||
|
||||
MethodInfo parseMethod;
|
||||
if (!String.IsNullOrWhiteSpace(attribute.CustomParser))
|
||||
{
|
||||
parseMethod = response.GetType().GetMethod(attribute.CustomParser,BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
if (parseMethod != null)
|
||||
prop.SetValue(response, parseMethod.Invoke(response, new object[] { value }), null);
|
||||
}
|
||||
else if (prop.PropertyType == typeof(string))
|
||||
prop.SetValue(response, value, null);
|
||||
else
|
||||
{
|
||||
parseMethod = prop.PropertyType.GetMethod("Parse", new Type[] { typeof(String) });
|
||||
prop.SetValue(response, parseMethod.Invoke(null, new object[] { value }), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
public Int32 StatusCode { get; set; }
|
||||
public String Response { get; set; }
|
||||
}
|
||||
}
|
||||
14
TorControlLibrary/Responses/ErrorResponse.cs
Normal file
14
TorControlLibrary/Responses/ErrorResponse.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TorControlLibrary.Responses
|
||||
{
|
||||
public class ErrorResponse : CommandResponse
|
||||
{
|
||||
public ErrorResponse(Int32 errorCode, String line)
|
||||
: base(errorCode, line)
|
||||
{ }
|
||||
}
|
||||
}
|
||||
29
TorControlLibrary/Responses/EventResponse.cs
Normal file
29
TorControlLibrary/Responses/EventResponse.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TorControlLibrary.Responses
|
||||
{
|
||||
public class EventResponse
|
||||
{
|
||||
public static EventResponse Parse(String line)
|
||||
{
|
||||
EventResponse resp = new EventResponse();
|
||||
string[] parts = line.Split(' ');
|
||||
Int32 id;
|
||||
resp.EventType = parts[1];
|
||||
if (Int32.TryParse(parts[2], out id))
|
||||
resp.ID = id;
|
||||
resp.Action = parts[3];
|
||||
for (int i = 2; i < parts.Length; i++)
|
||||
resp.EventInformation += parts[i] + ' ';
|
||||
resp.EventInformation = resp.EventInformation.Trim();
|
||||
return resp;
|
||||
}
|
||||
public String EventType { get; set; }
|
||||
public String Action { get; set; }
|
||||
public Int32 ID { get; set; }
|
||||
public String EventInformation { get; set; }
|
||||
}
|
||||
}
|
||||
12
TorControlLibrary/Responses/EventTypes.cs
Normal file
12
TorControlLibrary/Responses/EventTypes.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TorControlLibrary.Responses
|
||||
{
|
||||
public enum EventTypes
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
18
TorControlLibrary/Responses/ParseAttribute.cs
Normal file
18
TorControlLibrary/Responses/ParseAttribute.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TorControlLibrary.Responses
|
||||
{
|
||||
public class ParseAttribute : System.Attribute
|
||||
{
|
||||
public ParseAttribute(Int32 order)
|
||||
{
|
||||
Order = order;
|
||||
}
|
||||
public Int32 DataElementsToRead { get; set; }
|
||||
public String CustomParser { get; set; }
|
||||
public readonly Int32 Order;
|
||||
}
|
||||
}
|
||||
22
TorControlLibrary/Responses/RouterDescription.cs
Normal file
22
TorControlLibrary/Responses/RouterDescription.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TorControlLibrary.Responses
|
||||
{
|
||||
public class RouterDescription : CommandResponse
|
||||
{
|
||||
public RouterDescription(String nickname)
|
||||
{
|
||||
this.NickName = nickname;
|
||||
}
|
||||
|
||||
public Int32 BandwidthObserved { get; set; }
|
||||
public Int32 BandwidthAverage { get; set; }
|
||||
public Int32 BandwidthBurst { get; set; }
|
||||
public TimeSpan Uptime { get; set; }
|
||||
public String NickName { get; set; }
|
||||
public ExitPolicy ExitPolicy { get; set; }
|
||||
}
|
||||
}
|
||||
45
TorControlLibrary/Responses/RouterStatusResponse.cs
Normal file
45
TorControlLibrary/Responses/RouterStatusResponse.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TorControlLibrary.Responses
|
||||
{
|
||||
public class RouterStatusResponse : CommandResponse
|
||||
{
|
||||
public static RouterStatusResponse Parse(String routerLine, String flagLine)
|
||||
{
|
||||
RouterStatusResponse resp = new RouterStatusResponse();
|
||||
|
||||
resp.PopulateData(routerLine, resp);
|
||||
resp.Flags = flagLine.Substring(2).Split(' ');
|
||||
return resp;
|
||||
}
|
||||
protected String ParseEncodedHashes(String value)
|
||||
{
|
||||
if (value.Length % 2 != 0)
|
||||
value += "=";
|
||||
Byte[] digest = Convert.FromBase64String(value);
|
||||
String hash = String.Empty;
|
||||
for (int i = 0; i < digest.Length; i++)
|
||||
hash += String.Format("{0:X2}", digest[i]);
|
||||
return hash;
|
||||
}
|
||||
//Starts at 2, line is r <data>
|
||||
[ParseAttribute(2)]
|
||||
public String NickName { get; set; }
|
||||
[ParseAttribute(3, CustomParser="ParseEncodedHashes")]
|
||||
public String Identity { get; set; }
|
||||
[ParseAttribute(4, CustomParser = "ParseEncodedHashes")]
|
||||
public String Digest { get; set; }
|
||||
[ParseAttribute(5,DataElementsToRead=2)]
|
||||
public DateTime Publication { get; set; }
|
||||
[ParseAttribute(7)]
|
||||
public System.Net.IPAddress Address { get; set; }
|
||||
[ParseAttribute(8)]
|
||||
public Int32 ORPort { get; set; }
|
||||
[ParseAttribute(9)]
|
||||
public Int32 DirPort { get; set; }
|
||||
public String[] Flags { get; set; }
|
||||
}
|
||||
}
|
||||
33
TorControlLibrary/Responses/StreamStatusResponse.cs
Normal file
33
TorControlLibrary/Responses/StreamStatusResponse.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
namespace TorControlLibrary.Responses
|
||||
{
|
||||
public class StreamStatusResponse : CommandResponse
|
||||
{
|
||||
public static StreamStatusResponse Parse(String line)
|
||||
{
|
||||
StreamStatusResponse resp = new StreamStatusResponse();
|
||||
resp.PopulateData(line, resp);
|
||||
return resp;
|
||||
}
|
||||
[Obsolete()]
|
||||
protected IPEndPoint ParseIPEndPoint(String data)
|
||||
{
|
||||
return new IPEndPoint(IPAddress.Parse(data.Substring(0, data.IndexOf(':'))),
|
||||
Int32.Parse(data.Substring(data.IndexOf(':') + 1)));
|
||||
}
|
||||
[ParseAttribute(1)]
|
||||
public Int32 StreamID { get; set; }
|
||||
[ParseAttribute(2)]
|
||||
public String StreamStatus { get; set; }
|
||||
[ParseAttribute(3)]
|
||||
public Int32 CircuitID { get; set; }
|
||||
[ParseAttribute(4)]
|
||||
public String Target { get; set; }
|
||||
//[ParseAttribute(4, CustomParser="ParseIPEndPoint")]
|
||||
//public IPEndPoint Target { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user