/// <summary> /// Dispatches the command to the client control port and produces a <typeparamref name="T" /> response result. /// </summary> /// <param name="connection">The control connection where the command should be dispatched.</param> /// <returns> /// A <typeparamref name="T" /> object instance containing the response data. /// </returns> protected override Response Dispatch(Connection connection) { if (routers.Count == 0) { return(new Response(false)); } int circuitID = 0; if (circuit != null) { circuitID = circuit.ID; } StringBuilder builder = new StringBuilder("extendcircuit"); builder.AppendFormat(" {0}", circuitID); foreach (string router in routers) { builder.AppendFormat(" {0}", router); } if (connection.Write(builder.ToString())) { ConnectionResponse response = connection.Read(); return(new Response(response.Success)); } return(new Response(false)); }
/// <summary> /// Dispatches the command to the client control port and produces a <typeparamref name="T" /> response result. /// </summary> /// <param name="connection">The control connection where the command should be dispatched.</param> /// <returns> /// A <typeparamref name="T" /> object instance containing the response data. /// </returns> protected override Response Dispatch(Connection connection) { if (connection.Write("signal cleardnscache")) { ConnectionResponse response = connection.Read(); return(new Response(response.Success)); } return(new Response(false)); }
/// <summary> /// Dispatches the command to the client control port and produces a <typeparamref name="T" /> response result. /// </summary> /// <param name="connection">The control connection where the command should be dispatched.</param> /// <returns> /// A <typeparamref name="T" /> object instance containing the response data. /// </returns> protected override Response Dispatch(Connection connection) { if (name == null || value == null) { return(new Response(false)); } if (connection.Write("setconf {0}={1}", name, value.Contains(" ") ? string.Format("\"{0}\"", value) : value)) { ConnectionResponse response = connection.Read(); return(new Response(response.Success)); } return(new Response(false)); }
/// <summary> /// Dispatches the command to the client control port and produces a <typeparamref name="T" /> response result. /// </summary> /// <param name="connection">The control connection where the command should be dispatched.</param> /// <returns> /// A <typeparamref name="T" /> object instance containing the response data. /// </returns> protected override Response Dispatch(Connection connection) { if (circuit == null || circuit.Status == CircuitStatus.Closed) { return(new Response(false)); } if (connection.Write("closecircuit {0}", circuit.ID)) { ConnectionResponse response = connection.Read(); return(new Response(response.Success)); } return(new Response(false)); }
/// <summary> /// Dispatches the command to the client control port and produces a <typeparamref name="T" /> response result. /// </summary> /// <param name="connection">The control connection where the command should be dispatched.</param> /// <returns> /// A <typeparamref name="T" /> object instance containing the response data. /// </returns> protected override GetConfResponse Dispatch(Connection connection) { StringBuilder builder = new StringBuilder("getconf"); foreach (string name in configurations) { builder.Append(' '); builder.Append(name); } if (connection.Write(builder.ToString())) { ConnectionResponse response = connection.Read(); if (!response.Success) { return(new GetConfResponse(false, null)); } ResponsePairs values = new ResponsePairs(response.Responses.Count); foreach (string value in response.Responses) { string[] parts = value.Split(new[] { '=' }, 2); string name = parts[0].Trim(); if (parts.Length != 2) { values[name] = null; } else { values[name] = parts[1].Trim(); } } return(new GetConfResponse(true, values)); } return(new GetConfResponse(false, null)); }
/// <summary> /// Dispatches the command to the client control port and produces a <typeparamref name="T" /> response result. /// </summary> /// <param name="connection">The control connection where the command should be dispatched.</param> /// <returns> /// A <typeparamref name="T" /> object instance containing the response data. /// </returns> protected override GetInfoResponse Dispatch(Connection connection) { if (request == null) { return(new GetInfoResponse(false)); } if (connection.Write("getinfo {0}", request)) { ConnectionResponse response = connection.Read(); if (!response.Success || !response.Responses[0].StartsWith(request, StringComparison.CurrentCultureIgnoreCase)) { return(new GetInfoResponse(false)); } List <string> values = new List <string>(response.Responses.Count); if (response.Responses.Count == 1) { string[] parts = response.Responses[0].Split(new[] { '=' }, 2); values.Add(parts.Length == 1 ? null : parts[1]); } else { for (int i = 1; i < response.Responses.Count; i++) { if (".".Equals(response.Responses[i])) { break; } values.Add(response.Responses[i]); } } return(new GetInfoResponse(true, values.AsReadOnly())); } return(new GetInfoResponse(false)); }
/// <summary> /// Dispatches the command to the client control port and produces a <typeparamref name="T" /> response result. /// </summary> /// <param name="connection">The control connection where the command should be dispatched.</param> /// <returns> /// A <typeparamref name="T" /> object instance containing the response data. /// </returns> protected override CreateCircuitResponse Dispatch(Connection connection) { StringBuilder builder = new StringBuilder("extendcircuit 0"); foreach (string router in routers) { builder.Append(' '); builder.Append(router); } if (connection.Write(builder.ToString())) { ConnectionResponse response = connection.Read(); if (!response.Success) { return(new CreateCircuitResponse(false, -1)); } string[] parts = StringHelper.GetAll(response.Responses[0], ' '); if (parts.Length < 2 || !"extended".Equals(parts[0], StringComparison.CurrentCultureIgnoreCase)) { return(new CreateCircuitResponse(false, -1)); } int circuitID; if (!int.TryParse(parts[1], out circuitID)) { return(new CreateCircuitResponse(false, -1)); } return(new CreateCircuitResponse(true, circuitID)); } return(new CreateCircuitResponse(false, -1)); }
/// <summary> /// Dispatches the command to the client control port and produces a <typeparamref name="T" /> response result. /// </summary> /// <param name="connection">The control connection where the command should be dispatched.</param> /// <returns> /// A <typeparamref name="T" /> object instance containing the response data. /// </returns> protected override Response Dispatch(Connection connection) { if (stream == null || stream.ID <= 0) { return(new Response(false)); } if (stream.Status == StreamStatus.Failed || stream.Status == StreamStatus.Closed) { return(new Response(false)); } if (reason == StreamReason.None || reason == StreamReason.PrivateAddr || reason == StreamReason.End) { return(new Response(false)); } if (connection.Write("closestream {0} {1}", stream.ID, (int)reason)) { ConnectionResponse response = connection.Read(); return(new Response(response.Success)); } return(new Response(false)); }
/// <summary> /// Authenticates the connection by sending the password to the control port. /// </summary> /// <param name="password">The password used for authentication.</param> /// <returns><c>true</c> if the authentication succeeds; otherwise, <c>false</c>.</returns> public bool Authenticate(string password) { if (disposed) { throw new ObjectDisposedException("this"); } if (password == null) { password = ""; } if (Write("authenticate \"{0}\"", password)) { ConnectionResponse response = Read(); if (response.Success) { return(true); } } return(false); }
/// <summary> /// Dispatches the command to the client control port and produces a <typeparamref name="T" /> response result. /// </summary> /// <param name="connection"></param> /// <returns> /// A <typeparamref name="T" /> object instance containing the response data. /// </returns> protected override GetRouterStatusResponse Dispatch(Connection connection) { if (identity == null) { return(new GetRouterStatusResponse(false, null)); } string request = string.Format("ns/id/{0}", identity); if (connection.Write("getinfo {0}", request)) { ConnectionResponse response = connection.Read(); if (!response.Success || !response.Responses[0].StartsWith(request, StringComparison.CurrentCultureIgnoreCase)) { return(new GetRouterStatusResponse(false, null)); } Router router = null; foreach (string line in response.Responses) { string stripped = line.Trim(); if (string.IsNullOrWhiteSpace(stripped)) { continue; } if (stripped.StartsWith("r")) { string[] values = stripped.Split(' '); if (values.Length < 9) { continue; } DateTime publication = DateTime.MinValue; if (!DateTime.TryParse(string.Format("{0} {1}", values[4], values[5]), out publication)) { publication = DateTime.MinValue; } int orPort = 0; if (!int.TryParse(values[7], out orPort)) { orPort = 0; } int dirPort = 0; if (!int.TryParse(values[8], out dirPort)) { dirPort = 0; } IPAddress ipAddress = null; if (!IPAddress.TryParse(values[6], out ipAddress)) { ipAddress = null; } router = new Router(); router.Digest = values[3]; router.DIRPort = dirPort; router.Identity = values[2]; router.IPAddress = ipAddress; router.Nickname = values[1]; router.ORPort = orPort; router.Publication = publication; continue; } if (stripped.StartsWith("s") && router != null) { string[] values = stripped.Split(' '); for (int i = 1, length = values.Length; i < length; i++) { RouterFlags flag = ReflectionHelper.GetEnumerator <RouterFlags, DescriptionAttribute>(attr => values[i].Equals(attr.Description, StringComparison.CurrentCultureIgnoreCase)); if (flag != RouterFlags.None) { router.Flags |= flag; } } continue; } if (stripped.StartsWith("w") && router != null) { string[] values = stripped.Split(' '); if (values.Length < 2 || !values[1].StartsWith("bandwidth=", StringComparison.CurrentCultureIgnoreCase)) { continue; } string[] value = values[1].Split(new[] { '=' }, 2); if (value.Length < 2) { continue; } int bandwidth; if (int.TryParse(value[1].Trim(), out bandwidth)) { router.Bandwidth = new Bytes((double)bandwidth, Bits.KB).Normalize(); } } } return(new GetRouterStatusResponse(true, router)); } return(new GetRouterStatusResponse(false, null)); }