using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using Tor.Net; using System.Diagnostics; namespace Tor.IO { /// /// A class which connects to a remote address using the tor network service, and provides stream methods for communication. /// [DebuggerStepThrough] public sealed class Socks5Stream : System.IO.Stream { private readonly Client client; private readonly string host; private readonly int port; private readonly object synchronize; private volatile bool disposed; private ForwardSocket socket; private NetworkStream stream; /// /// Initializes a new instance of the class. /// /// The client hosting the SOCKS5 connection. /// The remote host address. /// The remote port number. internal Socks5Stream(Client client, string host, int port) { this.client = client; this.disposed = false; this.host = host; this.port = port; this.socket = null; this.synchronize = new object(); this.Start(); } #region Properties /// /// When overridden in a derived class, gets a value indicating whether the current stream supports reading. /// public override bool CanRead { get { return true; } } /// /// When overridden in a derived class, gets a value indicating whether the current stream supports seeking. /// public override bool CanSeek { get { return false; } } /// /// When overridden in a derived class, gets a value indicating whether the current stream supports writing. /// public override bool CanWrite { get { return true; } } /// /// When overridden in a derived class, gets the length in bytes of the stream. /// public override long Length { get { throw new NotSupportedException(); } } /// /// When overridden in a derived class, gets or sets the position within the current stream. /// public override long Position { get { throw new NotSupportedException(); } set { throw new NotSupportedException(); } } #endregion #region System.IO.Stream /// /// Releases the unmanaged resources used by the and optionally releases the managed resources. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected override void Dispose(bool disposing) { base.Dispose(disposing); if (disposed) return; if (disposing) { Shutdown(); disposed = true; } } /// /// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device. /// public override void Flush() { if (disposed) throw new ObjectDisposedException("this"); if (stream == null) throw new Exception("The stream is not connected"); stream.Flush(); } /// /// When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. /// /// An array of bytes. When this method returns, the buffer contains the specified byte array with the values between and ( + - 1) replaced by the bytes read from the current source. /// The zero-based byte offset in at which to begin storing the data read from the current stream. /// The maximum number of bytes to be read from the current stream. /// /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached. /// public override int Read(byte[] buffer, int offset, int count) { if (disposed) throw new ObjectDisposedException("this"); if (stream == null) throw new Exception("The stream is not connected"); return stream.Read(buffer, offset, count); } /// /// When overridden in a derived class, sets the position within the current stream. /// /// A byte offset relative to the parameter. /// A value of type indicating the reference point used to obtain the new position. /// /// The new position within the current stream. /// public override long Seek(long offset, System.IO.SeekOrigin origin) { throw new NotSupportedException(); } /// /// When overridden in a derived class, sets the length of the current stream. /// /// The desired length of the current stream in bytes. public override void SetLength(long value) { throw new NotSupportedException(); } /// /// When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. /// /// An array of bytes. This method copies bytes from to the current stream. /// The zero-based byte offset in at which to begin copying bytes to the current stream. /// The number of bytes to be written to the current stream. public override void Write(byte[] buffer, int offset, int count) { if (disposed) throw new ObjectDisposedException("this"); if (stream == null) throw new Exception("The stream is not connected"); stream.Write(buffer, offset, count); } #endregion /// /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. /// /// An array of bytes. When this method returns, the buffer contains the bytes read from the current source. /// /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached. /// public int Read(byte[] buffer) { if (buffer == null) throw new ArgumentNullException("buffer"); if (buffer.Length == 0) throw new ArgumentOutOfRangeException("buffer", "The read buffer should be greater than zero in length"); return Read(buffer, 0, buffer.Length); } /// /// Starts the SOCKS5 stream by connecting to the remote host using the forwarding socket. /// private void Start() { if (host == null) throw new InvalidOperationException("The specified host is null"); if (disposed) throw new ObjectDisposedException("this"); lock (synchronize) { try { if (socket != null) return; string address = host; if (address.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase)) address = address.Substring(7); if (address.StartsWith("https://", StringComparison.CurrentCultureIgnoreCase)) address = address.Substring(8); socket = new ForwardSocket(client); socket.ProxyAddress = "127.0.0.1"; socket.ProxyPort = client.Configuration.SocksPort; socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1); socket.Connect(address, port); stream = new NetworkStream(socket, false); } catch (Exception exception) { throw new TorException("An attempt to connect to a SOCKS5 host failed", exception); } finally { if (stream != null) { stream.Dispose(); stream = null; } if (socket != null) { try { socket.Shutdown(SocketShutdown.Both); } catch { } socket.Dispose(); socket = null; } } } } /// /// Shuts down the SOCKS5 connection and disables streaming. /// private void Shutdown() { lock (synchronize) { if (stream != null) { stream.Dispose(); stream = null; } if (socket != null) { try { socket.Shutdown(SocketShutdown.Both); } catch { } socket.Dispose(); socket = null; } } } /// /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. /// /// An array of bytes. This method copies bytes from to the current stream. public void Write(byte[] buffer) { if (buffer == null) throw new ArgumentNullException("buffer"); Write(buffer, 0, buffer.Length); } } }