Initial commit

This commit is contained in:
2014-04-30 16:57:42 -04:00
commit 5c4a1c8b4c
126 changed files with 52769 additions and 0 deletions

42
Internet/Browser.cs Normal file
View File

@@ -0,0 +1,42 @@
using System.Collections.Generic;
using Microsoft.Win32;
namespace Common.Internet
{
public class Browser
{
public string Name { get; private set; }
public string Command { get; private set; }
public string DefaultIcon { get; private set; }
public override string ToString()
{
return Name;
}
public static Dictionary<string, Browser> DetectInstalledBrowsers()
{
var browsers = new Dictionary<string, Browser>();
RegistryKey browserKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Clients\StartMenuInternet");
if (browserKeys == null)
browserKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Clients\StartMenuInternet");
string[] browserNames = browserKeys.GetSubKeyNames();
foreach (string browserName in browserNames)
{
Browser browser = new Browser();
RegistryKey browserKey = browserKeys.OpenSubKey(browserName);
browser.Name = (string) browserKey.GetValue(null);
RegistryKey browserKeyPath = browserKey.OpenSubKey(@"shell\open\command");
browser.Command = (string) browserKeyPath.GetValue(null);
RegistryKey browserIconPath = browserKey.OpenSubKey(@"DefaultIcon");
browser.DefaultIcon = (string) browserIconPath.GetValue(null);
browsers.Add(browserName, browser);
}
return browsers;
}
}
}

256
Internet/FtpClient.cs Normal file
View File

@@ -0,0 +1,256 @@
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
namespace Common.Internet
{
public class FtpClient
{
#region Private constants
private const int DATA_SIZE = 4096;
#endregion
#region Private member variables
private Socket _controlSocket; // The socket for sending control messsages
private Socket _dataSocket; // The socket for transferring data
private int _timeout = 30000; // The default send and receive timeouts
#endregion
#region Private socket management code
private Socket connectSocket(string server, int port)
{
// Resolve the server name into an IP Host
IPHostEntry ipHost = Dns.GetHostEntry(server);
// Get the first address from the host
IPAddress ipAddress = ipHost.AddressList[0];
// Create an end point on the host at the requested port
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, port);
// Create a TCP socket to the end point on the host
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Set the send and receive timeouts requested
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, _timeout);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, _timeout);
// Make the connection
socket.Connect(ipEndPoint);
return socket;
}
private static void disconnectSocket(ref Socket targetSocket)
{
if (targetSocket != null)
{
targetSocket.Shutdown(SocketShutdown.Both);
targetSocket.Close();
targetSocket = null;
}
}
private static void sendSocketMessage(Socket targetSocket, string message)
{
sendSocketMessage(targetSocket, message, true);
}
private static void sendSocketMessage(Socket targetSocket, string message, bool trailingNewline)
{
// Default to sending just the message
string send = message;
// If a trailing newline is requested then add that to the string
if (trailingNewline) send += "\r\n";
// Convert the requested message into a byte array
byte[] rawData = Encoding.ASCII.GetBytes(send);
// Send the message and get the byte count back
targetSocket.Send(rawData);
}
private static string receiveSocketMessage(Socket sourceSocket, bool stripTrailingNewline)
{
// Initialize the decoded string message to return
string message = string.Empty;
// Initialize the array of all lines
string[] lines;
do
{
// Initialize the byte array buffer
byte[] line = new byte[DATA_SIZE];
// Retrieve the data and get the byte count
int bytesReceived = sourceSocket.Receive(line);
// Decode the string into a string for the current line
string sLine = Encoding.ASCII.GetString(line, 0, bytesReceived);
// Replace the CR/LF pairs with just a LF
sLine.Replace("\r\n", "\n");
// Split the message into an array of lines (we might get more than one)
lines = sLine.Split('\n');
// Add this line to the main return string
message += sLine;
} while (lines[lines.Length - 2].Substring(3, 1) == "-");
// If requested to remove the trailing newline we should do it now
if (stripTrailingNewline && message.EndsWith("\n"))
{
message = message.Remove(message.Length - 1, 1);
}
return message;
}
public int GetResponseCode(string message)
{
// Get the FTP response code from the string message
return Convert.ToInt32(message.Substring(1, 3));
}
#endregion
#region Public interface methods
public void Connect(string server)
{
Connect(server, 21, _timeout);
}
public void Connect(string server, int port, int timeout)
{
// Store the timeout requested so we can use it later
_timeout = timeout * 1000;
// If we are currently connected in any way we should disconnect
if (_controlSocket != null || _dataSocket != null) Disconnect();
// Create and connect the control socket
_controlSocket = connectSocket(server, port);
// Retrieve the header message
receiveSocketMessage(_controlSocket, true);
}
public void Upload(string source)
{
string targetFile;
if (source.Contains(@"\"))
targetFile = source.Substring(source.LastIndexOf(@"\") + 1);
else
targetFile = source;
// Create a stream reader for the file
StreamReader streamReader = new StreamReader(source);
// Upload the file
Upload(streamReader.BaseStream, targetFile);
}
public void Upload(Stream source, string targetFile)
{
// Send a command requesting the passive port to connect to
sendSocketMessage(_controlSocket, "PASV");
// Get the response back with the data we need
string message = receiveSocketMessage(_controlSocket, true);
message = message.Replace("(", "<");
message = message.Replace(")", ">");
// Create a regular expression to get the port data out
Match match = new Regex("<(?<oct1>.*),(?<oct2>.*),(?<oct3>.*),(?<oct4>.*),(?<highport>.*),(?<lowport>.*)>").Match(message);
// Reconstruct the IP address the server wants
string address = match.Groups["oct1"].Value + "." + match.Groups["oct2"].Value + "." +
match.Groups["oct3"].Value + "." + match.Groups["oct4"].Value;
// Reconstruct the port the server wants
int port = Convert.ToInt32(match.Groups["highport"].Value) * 256 +
Convert.ToInt32(match.Groups["lowport"].Value);
// Connect the socket to that port
_dataSocket = connectSocket(address, port);
// Send the command requesting to store a file
sendSocketMessage(_controlSocket, "STOR " + targetFile);
// Get the response back
receiveSocketMessage(_controlSocket, false);
// Bring the stream to the beginning if it supports seek
if (source.CanSeek) source.Seek(0, SeekOrigin.Begin);
// Loop until the end of the file
while (source.Length != source.Position)
{
// Initialize the byte array buffer
byte[] rawMessage = new byte[DATA_SIZE];
// Get data from the stream
int bytesRead = source.Read(rawMessage, 0, DATA_SIZE);
// Send the data down the data socket
_dataSocket.Send(rawMessage, bytesRead, 0);
}
// Close the data socket
disconnectSocket(ref _dataSocket);
// Get the response back
receiveSocketMessage(_controlSocket, false);
}
public void Download(string sourceFile, string targetFile)
{
}
public void Logon(string userName, string password)
{
// Send the command to indicate the user we want
sendSocketMessage(_controlSocket, "USER " + userName);
receiveSocketMessage(_controlSocket, true);
// Send the command to indicate the password we want
sendSocketMessage(_controlSocket, "PASS " + password);
receiveSocketMessage(_controlSocket, true);
}
public void Logoff()
{
// Send the comment to close the current session
sendSocketMessage(_controlSocket, "QUIT");
receiveSocketMessage(_controlSocket, true);
}
public void Disconnect()
{
// Close and clear the data socket
disconnectSocket(ref _dataSocket);
// Close and clear the control socket
disconnectSocket(ref _controlSocket);
}
#endregion
}
}

253
Internet/TelnetClient.cs Normal file
View File

@@ -0,0 +1,253 @@
using System;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using Common.Debug;
namespace Common.Internet
{
public class TelnetClient
{
#region Member variables
private string _deviceLocation = ""; // Name or IP address of the device
private int _devicePort = 80; // Port the device is listening on
private SocketState _state; // Async state tracking object
private Socket _socket; // Socket for communications with the device
#endregion
#region Asynchronous socket state
private class SocketState
{
private readonly Socket _socket; // Network socket
private readonly byte[] _buffer; // Raw data buffer
private readonly int _bufferSize = 1024; // Data buffer size
private readonly TelnetClient _client; // Owner client
private readonly StringBuilder _stringBuffer; // Buffered data
private readonly ManualResetEvent _waitEvent; // Used as a signal when waiting for data
public SocketState(Socket socket, TelnetClient client, int bufferSize)
{
// Store the socket
_socket = socket;
// Store the client
_client = client;
// Store the buffer size
_bufferSize = bufferSize;
// Initialize the buffer
_buffer = new byte[_bufferSize];
// Initialize the string buffer
_stringBuffer = new StringBuilder();
// Initialize the wait event
_waitEvent = new ManualResetEvent(false);
}
public int BufferSize
{
get
{
return _bufferSize;
}
}
public Socket Socket
{
get
{
return _socket;
}
}
public TelnetClient Client
{
get
{
return _client;
}
}
public byte[] Buffer
{
get
{
return _buffer;
}
}
public StringBuilder StringBuffer
{
get
{
return _stringBuffer;
}
}
public ManualResetEvent WaitEvent
{
get
{
return _waitEvent;
}
}
}
#endregion
#region Delegates
public delegate void DataEventHandler(string data);
#endregion
#region Events
public event DataEventHandler DataReceived;
#endregion
#region Connection
public void Connect(string location, int port)
{
// Remember the location and port
_deviceLocation = location;
_devicePort = port;
// Create the socket
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Connect the socket
_socket.Connect(_deviceLocation, _devicePort);
// Setup our state object
_state = new SocketState(_socket, this, 1024);
// Start an asynchronous receive
_socket.BeginReceive(_state.Buffer, 0, _state.BufferSize, SocketFlags.None, new AsyncCallback(receiveCallback), _state);
}
#endregion
#region Disconnection
public void Disconnect()
{
// If we have a socket...
if (null != _socket)
{
// ...disconnect it
_socket.Disconnect(false);
_socket = null;
}
}
#endregion
#region Asynchronous callback
private static void receiveCallback(IAsyncResult asyncResult)
{
// Retrieve the socket state object
SocketState socketState = (SocketState) asyncResult.AsyncState;
// Read data from the socket
int bytesRead = socketState.Socket.EndReceive(asyncResult);
if (bytesRead > 0)
{
// Decode the data into a string
string stringBuffer = Encoding.UTF8.GetString(socketState.Buffer, 0, bytesRead);
// Add the string to the buffer
socketState.StringBuffer.Append(stringBuffer);
// Raise the DataReceived event with the data
if (socketState.Client.DataReceived != null)
socketState.Client.DataReceived(stringBuffer);
Tracer.WriteLine("Data received: {0}", stringBuffer);
socketState.WaitEvent.Set();
// Get more data from the socket
socketState.Socket.BeginReceive(socketState.Buffer, 0, socketState.BufferSize, 0, new AsyncCallback(receiveCallback), socketState);
}
}
#endregion
#region Data sending helpers
public void Send(string text, params object[] arguments)
{
// Format the text
text = string.Format(text, arguments);
Tracer.WriteLine("Send: {0}", text);
// Send the text to the socket
_socket.Send(Encoding.UTF8.GetBytes(text));
}
public void SendLine(string text, params object[] arguments)
{
// Send the text with a newline
Send(text + "\n", arguments);
}
#endregion
#region Data retrieval helpers
public string WaitForLine(string text)
{
// Add a CR/LF to the string and start waiting
return WaitForString(text + "\r\n");
}
public string WaitForString(string text)
{
// Check to see if the current buffer contains the text
while (!_state.StringBuffer.ToString().Contains(text))
{
Tracer.WriteLine("Waiting for: {0}", text);
// Wait for more data
_state.WaitEvent.Reset();
_state.WaitEvent.WaitOne(1000, false);
}
Tracer.WriteLine("String found: {0}", text);
// Get the text in the buffer from the start to after the string we are waiting for
string returnString = _state.StringBuffer.ToString().Substring(0, _state.StringBuffer.ToString().IndexOf(text) + text.Length);
// Strip off everything before what we are waiting for
_state.StringBuffer.Remove(0, _state.StringBuffer.ToString().IndexOf(text) + text.Length);
// Return the string
return returnString;
}
public string ReadLine()
{
// Wait for the first end of line
string line = WaitForString("\r\n");
// Get rid of the trailing CR/LF
line = line.Substring(0, line.Length - 2);
// Return the line
return line;
}
#endregion
}
}