Инструменты пользователя

Инструменты сайта


other:visual-studio-proxy-server

proxy

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace MyProxyServer
{
class MyProxyServer
{
Socket clientSocket;
Byte[] readbyte = new byte[4096];
Byte[] buffermessage = null;
Byte[] receivebuffer = new Byte[4096];
int iCountFiles = 1;
string sFileName = "myproxyserver_data_";
string sFileExt = ".dat";
string sFullFileName = "";
long count = 0;
string logFileName = "MyProxyLogFile.txt";

public MyProxyServer(Socket socket)
{
this.clientSocket = socket;
}

public void runProxy()
{
String sMessage = " ";
String swebhost = " ";
string stmpstring = "";
int iHostNameStart = 0;
int iHostNameEnd = 0;
int iHostNameEndFirstSlash = 0;
string sRequest2LogFile = "";
string sRequest2LogFileOld = "";
string sDataFileToSend = "";
long total = 0;

//Client socket, the client is using this program as a proxy server
int ireadbytes = clientSocket.Receive(readbyte, 4096, 0);
stmpstring = Encoding.ASCII.GetString(readbyte);
sMessage = (String)stmpstring;

//Get the request from the message
sRequest2LogFile = sMessage.Substring(0, sMessage.IndexOf("HTTP", 10) - 1);
//Check if we have already this request on the disk
sDataFileToSend = WebRequestExists(sRequest2LogFile);
if (sDataFileToSend != "")
{
//We have a file to send instead of get the webpage
FileStream fileDataFromDisk = new FileStream(sDataFileToSend, FileMode.Open);
buffermessage = new Byte[fileDataFromDisk.Length];

int ireadbytesfromfile = fileDataFromDisk.Read(buffermessage, 0, buffermessage.Length);
while (ireadbytesfromfile != 0)
{
clientSocket.Send(buffermessage, ireadbytesfromfile, 0);
ireadbytesfromfile = fileDataFromDisk.Read(buffermessage, 0, buffermessage.Length);
}
clientSocket.Close();
}
else
{
while (ireadbytes != 0)
{
FileStream logFileStream = new FileStream(logFileName, FileMode.Append);
total = 0;
//Parse the string to find the host and web page
iHostNameStart = sMessage.IndexOf("http://");
iHostNameEnd = sMessage.IndexOf(" ", 7 + iHostNameStart);
iHostNameEndFirstSlash = sMessage.IndexOf("//");
iHostNameEndFirstSlash = sMessage.IndexOf("/", iHostNameEndFirstSlash + 3);

if (sMessage.Substring(0, 7) != "CONNECT")
{
swebhost = sMessage.Substring(iHostNameStart + 7, iHostNameEndFirstSlash - 11);

Console.WriteLine("Connecting to the page {0}",
sMessage.Substring(iHostNameStart, iHostNameEnd - iHostNameStart));
Console.WriteLine("Connection from {0} the client", clientSocket.RemoteEndPoint);
IPHostEntry IPHost = Dns.GetHostEntry(swebhost);
IPAddress[] ipAddressList = IPHost.AddressList;
IPEndPoint ipEndPoint = new IPEndPoint(ipAddressList[0], 80);
//This is a socket to the real webserver with the webpage
Socket websocket = new Socket
(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
websocket.Connect(ipEndPoint);

if (websocket.Connected)
{
Console.WriteLine("Socket connected OK to webserver");
}
//sMessage is the request from the client
string hostmessage = sMessage;
Byte[] getmessage = Encoding.ASCII.GetBytes(hostmessage);
//Send the message package to the webserver
websocket.Send(getmessage, getmessage.Length, 0);
//Receive data from the webserver
Int32 ireceivedbyte = websocket.Receive(receivebuffer, receivebuffer.Length, 0);
Console.WriteLine("Received from webserver socket {0} bytes", +ireceivedbyte);
String spage = null;
spage = spage + Encoding.ASCII.GetString(receivebuffer, 0, ireceivedbyte);

while (ireceivedbyte > 0)
{
ireceivedbyte = websocket.Receive(receivebuffer, receivebuffer.Length, 0);
spage = spage + Encoding.ASCII.GetString(receivebuffer, 0, ireceivedbyte);
Console.WriteLine("Received from webserver socket {0} bytes", +ireceivedbyte);
}

if (sRequest2LogFile != sRequest2LogFileOld)
{
sRequest2LogFile = "";
sFullFileName = sFileName + iCountFiles.ToString() + sFileExt;
while (File.Exists(sFullFileName))
{
iCountFiles++;
sFullFileName = sFileName + iCountFiles.ToString() + sFileExt;
}
//new FILE
FileStream file1 = new FileStream(sFullFileName, FileMode.Create);
sRequest2LogFile = sMessage.Substring(0, sMessage.IndexOf("HTTP", 10) - 1);
string sTmpStr = "REQUEST:" + sRequest2LogFile + "," + "FILE:" + file1.Name + "\r\n";
byte[] byte2logfile = new byte[sTmpStr.Length];
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte2logfile = encoding.GetBytes(sTmpStr);
logFileStream.Write(byte2logfile, 0, byte2logfile.Length);
logFileStream.Close();
sRequest2LogFileOld = sRequest2LogFile;

//Read into memory stream
MemoryStream memStream = new MemoryStream(receivebuffer);
memStream.WriteTo(file1);
//file1.Write(receivebuffer, 0, receivebuffer.Length);
count = memStream.Length;

total += count;
Console.WriteLine(" wrote {0} total bytes", total);
}

//Now we send the message back to the orginal client who was asking for some webpage
buffermessage = new Byte[spage.Length + 1];
int ilength = Encoding.ASCII.GetBytes(spage, 0, spage.Length, buffermessage, 0);
clientSocket.Send(buffermessage, ilength, 0);
websocket.Shutdown(SocketShutdown.Both);
websocket.Close();
ireadbytes = clientSocket.Receive(readbyte, 4096, 0);
//file1.Close();

}
}
}
}

public string WebRequestExists(string sRequest)
{
string sDataFileName = "";
string sLine = "";
string sLocalRequest = "";
int iStartOffFileName = 0;
int ilen;

if (File.Exists(logFileName))
{
FileStream logFileCheck = new FileStream(logFileName, FileMode.Open);

StreamReader inStream = new StreamReader(logFileCheck);
sLine = inStream.ReadLine();
while (sLine != null)
{
Console.WriteLine(sLine);
iStartOffFileName = sLine.IndexOf("FILE:", 15);

sLocalRequest = sLine.Substring(8, iStartOffFileName - 9);

//We already have the reqeust
if (sLocalRequest == sRequest)
{
ilen = sLine.Length;

sDataFileName = sLine.Substring
(iStartOffFileName + 5, sLine.Length - iStartOffFileName - 5);
}
sLine = inStream.ReadLine();
}

logFileCheck.Close();
}
return sDataFileName;
}

public static int Main(string[] args)
{
const int proxyport = 5678;
TcpListener tcplistener = new TcpListener(IPAddress.Any, proxyport);
Console.WriteLine("Proxy server is listening to port {0}", +proxyport);
tcplistener.Start();
while (true)
{
Socket socket = tcplistener.AcceptSocket();
MyProxyServer myproxyserver = new MyProxyServer(socket);
Thread thread = new Thread(new ThreadStart(myproxyserver.runProxy));
thread.Start();
return 0;
}
}
}
}
other/visual-studio-proxy-server.txt · Последние изменения: 2017/03/23 21:59 (внешнее изменение)