Initial
This commit is contained in:
Binary file not shown.
548
Socket.cs
Normal file
548
Socket.cs
Normal file
@@ -0,0 +1,548 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Net;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
// Filename: Socket.cs
|
||||||
|
// Author:Sean Kessler
|
||||||
|
|
||||||
|
namespace IPMonitor
|
||||||
|
{
|
||||||
|
internal class ContentHeader
|
||||||
|
{
|
||||||
|
private String server;
|
||||||
|
private String date;
|
||||||
|
private String pragma;
|
||||||
|
private String connection;
|
||||||
|
private String contentLength;
|
||||||
|
private String contentType;
|
||||||
|
private String cookie;
|
||||||
|
private String cache;
|
||||||
|
|
||||||
|
public ContentHeader()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
public String Server
|
||||||
|
{
|
||||||
|
get { return server; }
|
||||||
|
set { server = value; }
|
||||||
|
}
|
||||||
|
public String Date
|
||||||
|
{
|
||||||
|
get { return date; }
|
||||||
|
set { date = value; }
|
||||||
|
}
|
||||||
|
public String Pragma
|
||||||
|
{
|
||||||
|
get { return pragma; }
|
||||||
|
set { pragma = value; }
|
||||||
|
}
|
||||||
|
public String Connection
|
||||||
|
{
|
||||||
|
get { return connection; }
|
||||||
|
set { connection = value; }
|
||||||
|
}
|
||||||
|
public String ContentLength
|
||||||
|
{
|
||||||
|
get { return contentLength; }
|
||||||
|
set { contentLength = value; }
|
||||||
|
}
|
||||||
|
public String ContentType
|
||||||
|
{
|
||||||
|
get { return contentType; }
|
||||||
|
set { contentType = value; }
|
||||||
|
}
|
||||||
|
public String Cookie
|
||||||
|
{
|
||||||
|
get { return cookie; }
|
||||||
|
set { cookie = value; }
|
||||||
|
}
|
||||||
|
public String Cache
|
||||||
|
{
|
||||||
|
get { return cache; }
|
||||||
|
set { cache = value; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class SocketConnector : TcpClient
|
||||||
|
{
|
||||||
|
public SocketConnector(Socket serverSocket)
|
||||||
|
{
|
||||||
|
Client = serverSocket;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class ServerSocketControl
|
||||||
|
{
|
||||||
|
private Socket acceptSocket;
|
||||||
|
private IPEndPoint ipLocalEndPoint;
|
||||||
|
|
||||||
|
public ServerSocketControl(int port)
|
||||||
|
{
|
||||||
|
int index;
|
||||||
|
acceptSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||||
|
acceptSocket.ReceiveTimeout = -1;
|
||||||
|
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
|
||||||
|
IPAddress ipAddress = null;
|
||||||
|
for (index = 0; index < hostEntry.AddressList.Length; index++)
|
||||||
|
{
|
||||||
|
if (IPAddress.TryParse(hostEntry.AddressList[index].ToString(), out ipAddress))
|
||||||
|
{
|
||||||
|
switch (ipAddress.AddressFamily)
|
||||||
|
{
|
||||||
|
case System.Net.Sockets.AddressFamily.InterNetwork:
|
||||||
|
break;
|
||||||
|
case System.Net.Sockets.AddressFamily.InterNetworkV6:
|
||||||
|
ipAddress = null;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ipAddress = null;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (null != ipAddress) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (null == ipAddress) throw new Exception("Could not determine IPV4 address.");
|
||||||
|
ipLocalEndPoint = new IPEndPoint(ipAddress, port);
|
||||||
|
SocketControl.TraceOut("[ServerSocketControl] Binding to endpoint " + ipLocalEndPoint);
|
||||||
|
acceptSocket.Bind(ipLocalEndPoint);
|
||||||
|
SocketControl.TraceOut("[ServerSocketControl] socket bound");
|
||||||
|
acceptSocket.Listen(-1);
|
||||||
|
}
|
||||||
|
public ServerSocketControl()
|
||||||
|
{
|
||||||
|
int index;
|
||||||
|
acceptSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||||
|
acceptSocket.ReceiveTimeout = -1;
|
||||||
|
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
|
||||||
|
IPAddress ipAddress = null;
|
||||||
|
for (index = 0; index < hostEntry.AddressList.Length; index++)
|
||||||
|
{
|
||||||
|
if (IPAddress.TryParse(hostEntry.AddressList[index].ToString(), out ipAddress))
|
||||||
|
{
|
||||||
|
switch (ipAddress.AddressFamily)
|
||||||
|
{
|
||||||
|
case System.Net.Sockets.AddressFamily.InterNetwork:
|
||||||
|
break;
|
||||||
|
case System.Net.Sockets.AddressFamily.InterNetworkV6:
|
||||||
|
ipAddress = null;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ipAddress = null;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (null != ipAddress) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (null == ipAddress) throw new Exception("Could not determine IPV4 address.");
|
||||||
|
ipLocalEndPoint = new IPEndPoint(ipAddress, 0);
|
||||||
|
SocketControl.TraceOut("[ServerSocketControl] Binding to endpoint " + ipLocalEndPoint);
|
||||||
|
acceptSocket.Bind(ipLocalEndPoint);
|
||||||
|
SocketControl.TraceOut("[ServerSocketControl] socket bound");
|
||||||
|
acceptSocket.Listen(-1);
|
||||||
|
ipLocalEndPoint = (IPEndPoint)acceptSocket.LocalEndPoint;
|
||||||
|
}
|
||||||
|
public IPEndPoint LocalEndPoint()
|
||||||
|
{
|
||||||
|
return ipLocalEndPoint;
|
||||||
|
}
|
||||||
|
public IPAddress Address()
|
||||||
|
{
|
||||||
|
return ipLocalEndPoint.Address;
|
||||||
|
}
|
||||||
|
public int Port()
|
||||||
|
{
|
||||||
|
return ipLocalEndPoint.Port;
|
||||||
|
}
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
if (null == acceptSocket) return;
|
||||||
|
acceptSocket.Shutdown(SocketShutdown.Both);
|
||||||
|
acceptSocket.Close();
|
||||||
|
acceptSocket = null;
|
||||||
|
}
|
||||||
|
public SocketControl Accept()
|
||||||
|
{
|
||||||
|
Socket socket = acceptSocket.Accept();
|
||||||
|
ipLocalEndPoint = (IPEndPoint)socket.LocalEndPoint;
|
||||||
|
SocketControl.TraceOut("[ServerSocketControl::Accept] connect accepted on " + ipLocalEndPoint);
|
||||||
|
SocketConnector socketConnector = new SocketConnector(socket);
|
||||||
|
return new SocketControl(socketConnector);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class SocketControl
|
||||||
|
{
|
||||||
|
public const int OkResult = 220;
|
||||||
|
public const int ErrorResult = 500;
|
||||||
|
private TcpClient tcpClient;
|
||||||
|
private NetworkStream networkStream;
|
||||||
|
private BinaryWriter binaryWriter;
|
||||||
|
private BinaryReader binaryReader;
|
||||||
|
|
||||||
|
private bool isConnected = false;
|
||||||
|
|
||||||
|
public SocketControl(SocketConnector socketConnector)
|
||||||
|
{
|
||||||
|
tcpClient = socketConnector;
|
||||||
|
tcpClient.LingerState = new LingerOption(false, 0);
|
||||||
|
tcpClient.ReceiveTimeout = -1;
|
||||||
|
networkStream = tcpClient.GetStream();
|
||||||
|
binaryWriter = new BinaryWriter(networkStream);
|
||||||
|
binaryReader = new BinaryReader(networkStream);
|
||||||
|
isConnected = true;
|
||||||
|
}
|
||||||
|
public SocketControl(String strHost, int port)
|
||||||
|
{
|
||||||
|
tcpClient = new TcpClient();
|
||||||
|
tcpClient.Connect(strHost, port);
|
||||||
|
tcpClient.LingerState = new LingerOption(false, 0);
|
||||||
|
tcpClient.ReceiveTimeout = -1;
|
||||||
|
networkStream = tcpClient.GetStream();
|
||||||
|
binaryWriter = new BinaryWriter(networkStream);
|
||||||
|
binaryReader = new BinaryReader(networkStream);
|
||||||
|
isConnected = true;
|
||||||
|
}
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
if (!isConnected) return;
|
||||||
|
isConnected = false;
|
||||||
|
binaryWriter.Close();
|
||||||
|
binaryWriter = null;
|
||||||
|
binaryReader.Close();
|
||||||
|
binaryReader = null;
|
||||||
|
tcpClient.Close();
|
||||||
|
tcpClient = null;
|
||||||
|
}
|
||||||
|
public string ReadLine()
|
||||||
|
{
|
||||||
|
StringBuilder strLine = new StringBuilder();
|
||||||
|
if (!isConnected) return null;
|
||||||
|
byte[] streamByte = new byte[1];
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
binaryReader.Read(streamByte, 0, 1);
|
||||||
|
if (13 == streamByte[0]) continue;
|
||||||
|
else if (10 == streamByte[0]) break;
|
||||||
|
else if ('\0' == (char)streamByte[0]) break;
|
||||||
|
strLine.Append((char)streamByte[0]);
|
||||||
|
streamByte[0] = 0;
|
||||||
|
}
|
||||||
|
return strLine.ToString();
|
||||||
|
}
|
||||||
|
public bool WriteLine(string strLine)
|
||||||
|
{
|
||||||
|
if (!isConnected || 0 == strLine.Length) return false;
|
||||||
|
strLine += "\r\n";
|
||||||
|
char[] streamData = strLine.ToCharArray();
|
||||||
|
binaryWriter.Write(streamData);
|
||||||
|
binaryWriter.Flush();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public int GetResultCode(string strLine)
|
||||||
|
{
|
||||||
|
if (0 == strLine.Length) return 0;
|
||||||
|
string strResult = "";
|
||||||
|
int strIndex = 0;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (strIndex >= 3) break;
|
||||||
|
if (' ' == strLine[strIndex]) break;
|
||||||
|
strResult += strLine[strIndex];
|
||||||
|
strIndex++;
|
||||||
|
}
|
||||||
|
if (0 == strResult.Length) return 0;
|
||||||
|
return int.Parse(strResult);
|
||||||
|
}
|
||||||
|
public string GetResult(string strLine)
|
||||||
|
{
|
||||||
|
string strResult = "";
|
||||||
|
int strIndex = 0;
|
||||||
|
int strLength = strLine.Length;
|
||||||
|
if (0 == strLength) return strResult;
|
||||||
|
while (' ' != strLine[strIndex] && strIndex < strLength) strIndex++;
|
||||||
|
strIndex++;
|
||||||
|
if (strIndex >= strLength) return strResult;
|
||||||
|
while (strIndex < strLength)
|
||||||
|
{
|
||||||
|
strResult += strLine[strIndex];
|
||||||
|
strIndex++;
|
||||||
|
}
|
||||||
|
return strResult;
|
||||||
|
}
|
||||||
|
public char[] GetRequest(string strRequest, int max)
|
||||||
|
{
|
||||||
|
if (!isConnected || null == strRequest || 0 == max) return null;
|
||||||
|
WriteLine("GET " + strRequest);
|
||||||
|
return Receive(max);
|
||||||
|
}
|
||||||
|
public MemoryStream GetRequest(string strRequest)
|
||||||
|
{
|
||||||
|
if (!isConnected || null == strRequest) return null;
|
||||||
|
WriteLine("GET " + strRequest);
|
||||||
|
return Receive();
|
||||||
|
}
|
||||||
|
public String GetRequestToString(String strRequest)
|
||||||
|
{
|
||||||
|
if (!isConnected || null == strRequest) return null;
|
||||||
|
WriteLine("GET " + strRequest);
|
||||||
|
MemoryStream memoryStream = Receive();
|
||||||
|
memoryStream.Seek(0, SeekOrigin.Begin);
|
||||||
|
StreamReader streamReader = new StreamReader(memoryStream);
|
||||||
|
String strResponse = streamReader.ReadToEnd();
|
||||||
|
streamReader.Close();
|
||||||
|
memoryStream.Close();
|
||||||
|
return strResponse;
|
||||||
|
}
|
||||||
|
public bool Write(char[] chars)
|
||||||
|
{
|
||||||
|
if (!isConnected || null == chars || 0 == chars.Length) return false;
|
||||||
|
binaryWriter.Write(chars);
|
||||||
|
binaryWriter.Flush();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public bool Write(byte[] bytes)
|
||||||
|
{
|
||||||
|
if (!isConnected || null == bytes || 0 == bytes.Length) return false;
|
||||||
|
binaryWriter.Write(bytes);
|
||||||
|
binaryWriter.Flush();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public bool Write(byte[] bytes, int length)
|
||||||
|
{
|
||||||
|
if (!isConnected || null == bytes || 0 == bytes.Length) return false;
|
||||||
|
binaryWriter.Write(bytes, 0, length);
|
||||||
|
binaryWriter.Flush();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public int ReadInt()
|
||||||
|
{
|
||||||
|
char[] chars = new char[4];
|
||||||
|
Receive(chars);
|
||||||
|
return (((byte)chars[3]) << 24) | (((byte)chars[2]) << 16) + (((byte)chars[1]) << 8) + ((byte)chars[0]);
|
||||||
|
}
|
||||||
|
public bool IsConnected()
|
||||||
|
{
|
||||||
|
return isConnected;
|
||||||
|
}
|
||||||
|
public bool Receive(char[] streamChars)
|
||||||
|
{
|
||||||
|
byte[] cb;
|
||||||
|
int count = 0;
|
||||||
|
int max;
|
||||||
|
int readCount;
|
||||||
|
|
||||||
|
max = streamChars.Length;
|
||||||
|
if (!isConnected || 0 == max) return false;
|
||||||
|
cb = new byte[1];
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
readCount = binaryReader.Read(cb, 0, 1);
|
||||||
|
if (0 == readCount) break;
|
||||||
|
streamChars[count++] = (char)cb[0];
|
||||||
|
if (count >= max) break;
|
||||||
|
}
|
||||||
|
return count == max;
|
||||||
|
}
|
||||||
|
public int Receive(ref byte[] streamBytes)
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
int max;
|
||||||
|
int readLength = 0;
|
||||||
|
|
||||||
|
max = streamBytes.Length;
|
||||||
|
if (!isConnected || 0 == max) return count;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
readLength = binaryReader.Read(streamBytes, count, max - count);
|
||||||
|
if (0 == readLength) break;
|
||||||
|
count += readLength;
|
||||||
|
if (count >= max) break;
|
||||||
|
}
|
||||||
|
if (0 == count) return count;
|
||||||
|
byte[] stream = new byte[count];
|
||||||
|
Array.Copy(streamBytes, stream, count);
|
||||||
|
streamBytes = stream;
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
public char[] Receive(int maxChars)
|
||||||
|
{
|
||||||
|
char[] streamChar;
|
||||||
|
char[] stream;
|
||||||
|
byte[] cb;
|
||||||
|
int count = 0;
|
||||||
|
int readCount = 0;
|
||||||
|
|
||||||
|
if (!isConnected || 0 == maxChars) return null;
|
||||||
|
streamChar = new char[maxChars];
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
cb = new byte[1];
|
||||||
|
readCount = binaryReader.Read(cb, 0, 1);
|
||||||
|
if (0 == readCount) break;
|
||||||
|
streamChar[count++] = (char)cb[0];
|
||||||
|
if (count >= maxChars) break;
|
||||||
|
}
|
||||||
|
stream = new char[count];
|
||||||
|
Array.Copy(streamChar, stream, count);
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
public MemoryStream Receive()
|
||||||
|
{
|
||||||
|
MemoryStream memoryStream = new MemoryStream();
|
||||||
|
byte[] cb;
|
||||||
|
int readCount = 0;
|
||||||
|
|
||||||
|
if (!isConnected) return null;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
cb = new byte[1];
|
||||||
|
readCount = binaryReader.Read(cb, 0, 1);
|
||||||
|
if (0 == readCount) break;
|
||||||
|
memoryStream.WriteByte(cb[0]);
|
||||||
|
}
|
||||||
|
memoryStream.Seek(0, SeekOrigin.Begin);
|
||||||
|
return memoryStream;
|
||||||
|
}
|
||||||
|
public bool Receive(ArrayList receiveStrings, ArrayList responseStrings)
|
||||||
|
{
|
||||||
|
bool isInMultiLine = false;
|
||||||
|
bool returnCode = true;
|
||||||
|
string seriesItem = "";
|
||||||
|
string stringData = "";
|
||||||
|
|
||||||
|
receiveStrings.Clear();
|
||||||
|
if (!isConnected) return false;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
stringData = ReadLine();
|
||||||
|
if (null == stringData || 0 == stringData.Length) break;
|
||||||
|
if (0 == receiveStrings.Count)
|
||||||
|
{
|
||||||
|
seriesItem = stringData.Substring(0, 3);
|
||||||
|
if (!IsInResponse(seriesItem, responseStrings))
|
||||||
|
{
|
||||||
|
returnCode = false;
|
||||||
|
receiveStrings.Add(stringData);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if ('-' == stringData[3]) isInMultiLine = true;
|
||||||
|
}
|
||||||
|
if (0 != receiveStrings.Count && isInMultiLine && '-' != stringData[3] &&
|
||||||
|
stringData.Substring(0, 3).Equals(seriesItem))
|
||||||
|
{
|
||||||
|
receiveStrings.Add(stringData);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
receiveStrings.Add(stringData);
|
||||||
|
if (!isInMultiLine) break;
|
||||||
|
}
|
||||||
|
return returnCode;
|
||||||
|
}
|
||||||
|
public bool Receive(ArrayList receiveStrings)
|
||||||
|
{
|
||||||
|
bool isInMultiLine = false;
|
||||||
|
string seriesItem = "";
|
||||||
|
string stringData = "";
|
||||||
|
|
||||||
|
receiveStrings.Clear();
|
||||||
|
if (!isConnected) return false;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
stringData = ReadLine();
|
||||||
|
if (null == stringData || 0 == stringData.Length) break;
|
||||||
|
if (0 == receiveStrings.Count)
|
||||||
|
{
|
||||||
|
seriesItem = stringData.Substring(0, 3);
|
||||||
|
try { if ('-' == stringData[3])isInMultiLine = true; }
|
||||||
|
catch (Exception) { ;}
|
||||||
|
}
|
||||||
|
if (0 != receiveStrings.Count && isInMultiLine && '-' != stringData[3] &&
|
||||||
|
stringData.Substring(0, 3).Equals(seriesItem))
|
||||||
|
{
|
||||||
|
receiveStrings.Add(stringData);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
receiveStrings.Add(stringData);
|
||||||
|
if (!isInMultiLine) break;
|
||||||
|
}
|
||||||
|
if (0 != receiveStrings.Count) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
public bool ReceivePage(ArrayList stringArray)
|
||||||
|
{
|
||||||
|
string stringData = "";
|
||||||
|
int reps = 0;
|
||||||
|
|
||||||
|
stringArray.Clear();
|
||||||
|
if (!isConnected) return false;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
stringData = ReadLine();
|
||||||
|
if (null == stringData || 0 == stringData.Length) reps++;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
stringArray.Add(stringData);
|
||||||
|
reps = 0;
|
||||||
|
}
|
||||||
|
if (2 == reps) break;
|
||||||
|
}
|
||||||
|
return 0 != stringArray.Count ? true : false;
|
||||||
|
}
|
||||||
|
public MemoryStream ReceivePage()
|
||||||
|
{
|
||||||
|
MemoryStream memoryStream;
|
||||||
|
ContentHeader contentHeader;
|
||||||
|
|
||||||
|
contentHeader = ReadContentHeader();
|
||||||
|
byte[] bytes = new byte[int.Parse(contentHeader.ContentLength)];
|
||||||
|
Receive(ref bytes);
|
||||||
|
memoryStream = new MemoryStream(bytes);
|
||||||
|
return memoryStream;
|
||||||
|
}
|
||||||
|
public bool IsInResponse(string responseString, ArrayList responseStrings)
|
||||||
|
{
|
||||||
|
if (0 == responseStrings.Count) return false;
|
||||||
|
for (int index = 0; index < responseStrings.Count; index++)
|
||||||
|
{
|
||||||
|
if (responseString.Equals((string)responseStrings[index])) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
private ContentHeader ReadContentHeader()
|
||||||
|
{
|
||||||
|
ContentHeader contentHeader;
|
||||||
|
String stringLine;
|
||||||
|
String token;
|
||||||
|
String value;
|
||||||
|
|
||||||
|
contentHeader = new ContentHeader();
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
stringLine = ReadLine();
|
||||||
|
if (null == stringLine || 0 == stringLine.Length) break;
|
||||||
|
String[] elements = stringLine.Split(':');
|
||||||
|
if (2 != elements.Length) continue;
|
||||||
|
token = elements[0].Trim().ToUpper();
|
||||||
|
value = elements[1].Trim().ToUpper();
|
||||||
|
if ("SERVER".Equals(token)) contentHeader.Server = value;
|
||||||
|
else if ("DATE".Equals(token)) contentHeader.Date = value;
|
||||||
|
else if ("PRAGMA".Equals(token)) contentHeader.Pragma = value;
|
||||||
|
else if ("CACHE-CONTROL".Equals(token)) contentHeader.Cache = value;
|
||||||
|
else if ("CONNECTION".Equals(token)) contentHeader.Connection = value;
|
||||||
|
else if ("CONTENT-LENGTH".Equals(token)) contentHeader.ContentLength = value;
|
||||||
|
else if ("CONTENT-TYPE".Equals(token)) contentHeader.ContentType = value;
|
||||||
|
else if ("SET-COOKIE".Equals(token)) contentHeader.Cookie = value;
|
||||||
|
}
|
||||||
|
return contentHeader;
|
||||||
|
}
|
||||||
|
[Conditional("TRACENET")]
|
||||||
|
public static void TraceOut(String strLine)
|
||||||
|
{
|
||||||
|
String strThread = "[Thread=" + System.Threading.Thread.CurrentThread.GetHashCode() + "]";
|
||||||
|
String strDate = "[" + DateTime.Now.ToString() + "]";
|
||||||
|
Trace.WriteLine(strThread + "[]" + strDate + strLine);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
120
UserAgent.cs
Normal file
120
UserAgent.cs
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace IPMonitor
|
||||||
|
{
|
||||||
|
public class UserAgent
|
||||||
|
{
|
||||||
|
private static UserAgent instance=null;
|
||||||
|
//private bool useVersioning=false;
|
||||||
|
private String[] userAgents=
|
||||||
|
{
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.3 24.31",
|
||||||
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3.1 Safari/605.1.1 23.33",
|
||||||
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Safari/605.1.1 14.12",
|
||||||
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.3 13.92",
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0. 3.92",
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Agency/93.8.2357.5 3.92",
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 OPR/107.0.0. 3.92",
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.3 3.92",
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:123.0) Gecko/20100101 Firefox/123. 3.14",
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103. 1.57",
|
||||||
|
"Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.3 0.78",
|
||||||
|
"Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115. 0.78",
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Viewer/99.9.8853.8 0.78",
|
||||||
|
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115. 0.78",
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.3 0.78"
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (Windows NT 5.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (iPhone; CPU iPhone OS 13_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Mobile/15E148 Safari/604.1",
|
||||||
|
//"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/605.1.15 (KHTML, like Gecko)",
|
||||||
|
//"Mozilla/5.0 (iPad; CPU OS 9_3_5 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13G36",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36",
|
||||||
|
//"Mozilla/4.0 (compatible; MSIE 6.0; Windows 98) Internet Explorer 6",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (iPhone; CPU iPhone OS 12_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.2 Mobile/15E148 Safari/604.1",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063",
|
||||||
|
//"Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Mobile/15E148 Safari/604.1",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15",
|
||||||
|
//"Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko",
|
||||||
|
//"Mozilla/5.0 (Windows NT 5.1; rv:36.0) Gecko/20100101 Firefox/36.0",
|
||||||
|
//"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko)",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36",
|
||||||
|
//"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)",
|
||||||
|
//"Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0",
|
||||||
|
//"Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299",
|
||||||
|
//"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)",
|
||||||
|
//"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (iPhone; CPU iPhone OS 13_6_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Mobile/15E148 Safari/604.1",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0",
|
||||||
|
//"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2)",
|
||||||
|
//"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Safari/605.1.15",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (iPhone; CPU iPhone OS 13_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Mobile/15E148 Safari/604.1",
|
||||||
|
//"Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:24.0) Gecko/20100101 Firefox/24.0",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0",
|
||||||
|
//"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.3 24.31",
|
||||||
|
//"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3.1 Safari/605.1.1 23.33",
|
||||||
|
//"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Safari/605.1.1 14.12",
|
||||||
|
//"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.3 13.92",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0. 3.92",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Agency/93.8.2357.5 3.92",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 OPR/107.0.0. 3.92",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.3 3.92",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:123.0) Gecko/20100101 Firefox/123. 3.14",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103. 1.57",
|
||||||
|
//"Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.3 0.78",
|
||||||
|
//"Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115. 0.78",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Viewer/99.9.8853.8 0.78",
|
||||||
|
//"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115. 0.78",
|
||||||
|
//"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.3 0.78"
|
||||||
|
};
|
||||||
|
|
||||||
|
private Random random=new Random();
|
||||||
|
private UserAgent()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
public static UserAgent GetInstance()
|
||||||
|
{
|
||||||
|
lock(typeof(UserAgent))
|
||||||
|
{
|
||||||
|
if(null==instance) instance=new UserAgent();
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public String GetUserAgent()
|
||||||
|
{
|
||||||
|
lock(this)
|
||||||
|
{
|
||||||
|
double randomNumber=random.NextDouble(); // number between 0 and 1
|
||||||
|
int index=(int)(randomNumber*((double)userAgents.Length));
|
||||||
|
return userAgents[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
78
ZoneEditResponse.cs
Normal file
78
ZoneEditResponse.cs
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace IPMonitor
|
||||||
|
{
|
||||||
|
public class ZoneEditResponses : List<ZoneEditResponse>
|
||||||
|
{
|
||||||
|
public ZoneEditResponses()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
public bool IsSuccess()
|
||||||
|
{
|
||||||
|
if(Count==0)return false;
|
||||||
|
return this.Any(x => x.SuccessCode.Equals("200") || x.SuccessCode.Equals("201"));
|
||||||
|
}
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
StringBuilder sb=new StringBuilder();
|
||||||
|
for(int index=0;index<Count;index++)
|
||||||
|
{
|
||||||
|
ZoneEditResponse zoneEditResponse=this[index];
|
||||||
|
sb.Append(zoneEditResponse).ToString();
|
||||||
|
if(index<Count-1)sb.Append("\n");
|
||||||
|
}
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
public ZoneEditResponses(String httpResponseString)
|
||||||
|
{
|
||||||
|
if(null==httpResponseString)return;
|
||||||
|
String[] responseItems = httpResponseString.Split('\n');
|
||||||
|
foreach(String item in responseItems)
|
||||||
|
{
|
||||||
|
int successCodeIndex= item.IndexOf("SUCCESS CODE=");
|
||||||
|
int textIndex=item.IndexOf("TEXT=");
|
||||||
|
int zoneIndex=item.IndexOf("ZONE=");
|
||||||
|
if(-1==successCodeIndex || -1==textIndex || -1==zoneIndex)continue;
|
||||||
|
String successCode=Utility.BetweenString(item.Substring(successCodeIndex),"\"","\"");
|
||||||
|
String text=Utility.BetweenString(item.Substring(textIndex),"\"","\"");
|
||||||
|
String zone=Utility.BetweenString(item.Substring(zoneIndex),"\"","\"");
|
||||||
|
ZoneEditResponse zoneEditResponse= new ZoneEditResponse(successCode, text, zone);
|
||||||
|
Add(zoneEditResponse);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResponseString "<SUCCESS CODE=\"201\" TEXT=\"no update required for diversified-software.com to 67.191.114.201\" ZONE=\"diversified-software.com\">\n<SUCCESS CODE=\"200\" TEXT=\"diversified-software.com updated to 67.191.114.201\" ZONE=\"diversified-software.com\">\n" string
|
||||||
|
|
||||||
|
|
||||||
|
public class ZoneEditResponse
|
||||||
|
{
|
||||||
|
public String SuccessCode { get; set; }
|
||||||
|
public String Text { get; set; }
|
||||||
|
public String Zone { get; set; }
|
||||||
|
|
||||||
|
public ZoneEditResponse()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
public ZoneEditResponse(String successCode, String text, String zone)
|
||||||
|
{
|
||||||
|
SuccessCode=successCode;
|
||||||
|
Text=text;
|
||||||
|
Zone=zone;
|
||||||
|
}
|
||||||
|
public override String ToString()
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.Append("SUCCESS CODE=").Append(SuccessCode);
|
||||||
|
sb.Append(",");
|
||||||
|
sb.Append("TEXT=").Append(Text);
|
||||||
|
sb.Append(",");
|
||||||
|
sb.Append("ZONE=").Append(Zone);
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user