Implementing a Web Proxy server in C #

Source: Internet
Author: User
Tags aliases

Implementing a Web Proxy server in C #

The Agent service program is a widely used network application program. There are many kinds of agent, according to the protocol can be divided into HTTP Proxy service program, FTP Proxy service program, and the server running the Agent service program is also referred to as HTTP proxy server and FTP proxy server. This article describes the HTTP protocol that is represented by the Web Proxy service program.

   first, the advantages of network Agent program

Agent service is the role of a bridge, it is a transit point of network information. The application of proxy services in the network is generally based on the following reasons:

(1) Make full use of IP address resources. In the LAN, the general external IP address is very limited, in order to ensure that the host within the LAN can access the Internet resources, through the network agent can be achieved.
(2) To ensure network security. A network proxy can act as a firewall between the intranet and the Internet by filtering IP addresses to restrict access to external resources by certain IP addresses.
(3) Be able to effectively hide their IP address and host name. Because all external network requests are implemented through a proxy server, the destination host can only know the IP address of the proxy server.
(4) Improve network speed. Usually the proxy server has a large hard disk buffer, which stores the bounds of data, and when you revisit the same data, you can remove the information directly from the buffer, thereby increasing the access speed.

   ii. types of network agents and their implementation principles

The network Proxy service according to the work level, generally can divide into the application layer agent, the Transport Layer agent and the socks agent. The application layer agent works on the application layer of the TCP/IP reference model, and it supports proxies for application-layer protocols such as HTTP, FTP. It provides the most control, but is inflexible and must have the appropriate protocol support. If the protocol does not support proxies (such as SMTP and POP), it can only be applied at the application tier agent, which is the transport layer agent. The transport layer agent interacts directly with the TCP layer and is more flexible. A proxy server is required to have a part of the true server: listens on a specific TCP or UDP port, and receives a client's request to respond to the client at the same time. Another proxy needs to change the client's IP stack, the SOCKS proxy. It is the most powerful and flexible proxy standard protocol available. SOCK V4 allows clients inside the proxy server to fully connect to the external server, SOCK V5 increases the authorization and authentication to the client, so it is a highly secure proxy. The proxy described later in this section is a proxy above the application layer, and the proxy protocol is HTTP, which is often seen as a Web proxy.

As mentioned above, the network proxy is a bridge that connects the client (the computer that needs the proxy) and the server side (the server that provides access to the resource). To realize the function of this kind of bridge, the network agent must meet the following conditions, in fact, it is also the process of the agent service running:

(1) Receiving and parsing the client's request.
(2) Create a new connection to the server and forward the client's request information.
(3) Receiving information from the server feedback.
(4) interprets the response of the server and passes the response back to the client.

Figure 1 is a typical model diagram of the network Proxy service:

  
Network agents have many advantages, but since the use of proxies, all of their network requests are through the broker server this intermediary to achieve, it is possible to encounter a malicious person to listen to your input content. Similarly, if you choose a proxy server with a smaller bandwidth, using a proxy will also slow down the network.

In a word, the use of agents and pros and cons, users should be based on their own circumstances to decide. However, it is very important to choose a good proxy server.

Third, C # Implementation of Web Proxy service program

After the above introduction, I think we should have a basic understanding of the agency service, let us through an example to understand how to implement the Web Proxy service in C #. The functional order of the Web Proxy service is this:

(1) Listen for the port and wait for the Web request information sent by the client browser.
(2) After receiving the client Web request information, the address of the target Web server is resolved, and a socket instance is created and connected to the Web server with this instance.
(3) Send the client's Web request packet through the created socket to port 80 on the Web server.
(4) Receive the page data returned by the Web server.
(5) transmitting the received data to the client, thus implementing the Web Proxy.

A client's browsing of a web address may send a lot of Web request information (such as images in a Web page, flash, etc.), and in order to process this information more quickly and accurately, the Web Proxy service typically uses multithreading to process each Web request. The attentive reader may find that processing each client's Web request information, the proxy server software uses two sockets, one for receiving/transmitting the client's information, and the other is communicating with the Web server. In order to differentiate these two sockets, we call the server dialog called "Service Socket", and the client machine dialog called "Customer Socket".

The following is the beginning of the Web Proxy service program writing work. This example consists of three parts:

1. Create a Web proxy class.
Instantiation of the class of the 2.WEB Proxy service.
3. How to implement the Web Proxy service through an instance of this Web proxy class.

(i) Create a Web proxy class

The following are the steps:

1. Start Visual Studio.NET, select File, new, Project menu, and in the New Project dialog box, set the project type to Visual C # project, set the template to Windows application, and in the name Enter "WebProxy" in the text box and enter "E:vs" in the Position text box. NET project, and then click the OK button so that the project is set up.
2. Select the menu "project", "Add Class" and the "Add New Item" dialog box will appear.
3. Set "Template" to "class".
4. Enter "Proxy" in the "Name" text box and click on the "Open" button, as shown in 2:
5. In the Solution Explorer window, double-click the Proxy.cs file to enter the editing bounds of the Proxy.cs file
6. At the beginning of the Proxy.cs source file, add the following code, which is the namespace to be used in the import Proxy.cs:

Using System;
Using System.Net;
Using System.Net.Sockets;
Using System.Text;
Using System.IO;

7. Replace the default constructor with the following constructor. The following code creates a constructor in the proxy class. The proxy class has only one constructor, and this constructor has only one parameter, which is the socket object, which is used primarily for data exchange with the client and is a "customer socket":

Public Proxy (socket socket)
{
//
TODO: Add constructor logic here
//
This.clientsocket = socket;
}

8. Add the following code to the Define proxy class code area, which defines some of the variables used in the proxy class, which are mainly used in the subsequent definition of the Run method.

Socket Clientsocket;
byte[] Read = new byte[1024];
Define a space to store request packets from a client
Byte [] Buffer = null;
Encoding ASCII = encoding.ascii;
Set the encoding
byte[] recvbytes = new byte[4096];
Define a space to store the data returned by the Web server

9. Create the Run method in the proxy class. The Run method is the only method in the proxy class. Its function is to receive the HTTP request from the client and transfer it to the Web server, and then receive the feedback from the Web server and transmit the data to the client. To achieve these two different aspects of data transmission, the Run method is implemented by two socket instances. When writing the Run method, pay attention to the following two points:

(1) Because HTTP is built on top of the TCP protocol, the socket instance created should use the TCP protocol. The following code creates a socket instance that can deliver the HTTP request command to the Web server and receive information from the Web server feedback:

Socket ipsocket = new socket (addressfamily.internetwork, SocketType.Stream, protocoltype.tcp);

(2) Another socket is in the agent service program listening port number, receive connection request when received, so should take this socket as a parameter, the proxy class using the constructor to create a proxy instance. This socket implementation receives HTTP request information from the client and transmits the data to the client.

Socket creation and use are key to implementing Web proxy software. After the constructor code, enter the following code:
public void Run ()
{
String clientmessage = "";
Storing the HTTP request string from the client
String URL = "";
Store parse out address request information
int bytes = Readmessage (read, ref clientsocket, ref clientmessage);
if (bytes = = 0)
{
return;
}
int index1 = Clientmessage. IndexOf (");
int index2 = Clientmessage. IndexOf (", index1 + 1);
if ((index1 = =-1) | | (Index2 = =-1))
{
throw new IOException ();
}
String part1 = Clientmessage. Substring (index1 + 1, index2-index1);
int index3 = Part1. IndexOf ('/', index1 + 8);
int index4 = Part1. IndexOf (", index1 + 8);
int index5 = INDEX4-INDEX3;
URL = Part1. Substring (Index1 + 4, (part1. LENGTH-INDEX5)-8);
Try
{
Iphostentry iphost = Dns.resolve (URL);
Console.WriteLine ("Remote Host Name:" + iphost.hostname);
string [] aliases = iphost.aliases;
ipaddress[] address = iphost.addresslist;
Console.WriteLine ("Web server IP Address:" + address[0]);
Resolves the server address to be accessed
IPEndPoint IPEndPoint = new IPEndPoint (address[0], 80);
Socket ipsocket = new socket (addressfamily.internetwork, SocketType.Stream, protocoltype.tcp);
Create a socket object that connects to the Web server side
Ipsocket.connect (IPEndPoint);
Socket Connection Web Server
if (ipsocket.connected)
Console.WriteLine ("Socket properly connected! ");
string GET = Clientmessage;
byte[] Byteget = ASCII. GetBytes (GET);
Ipsocket.send (Byteget, byteget.length, 0);
Proxy access software sends HTTP request commands to server side
Int32 rbytes = ipsocket.receive (recvbytes, recvbytes.length, 0);
Proxy access software receives feedback from the Web server side
Console.WriteLine ("Bytes Received:" + rbytes.tostring ());
String strretpage = null;
Strretpage = Strretpage + ASCII. GetString (recvbytes, 0, rbytes);
while (Rbytes > 0)
{
Rbytes = Ipsocket.receive (recvbytes, recvbytes.length, 0);
Strretpage = Strretpage + ASCII. GetString (recvbytes, 0, rbytes);
}
Ipsocket.shutdown (Socketshutdown.both);
Ipsocket.close ();
SendMessage (Clientsocket, strretpage);
Agent Service software transmits received information to the client
}
catch (Exception Exc2)
  
}
  
Receiving HTTP request data from the client
private int Readmessage (byte [] ByteArray, ref Socket S, ref String Clientmessage)
{
int bytes = S.receive (ByteArray, 1024, 0);
String messagefromclient = Encoding.ASCII.GetString (ByteArray);
Clientmessage = (String) messagefromclient;
return bytes;
}
  
Transmitting data from the Web server to the client
private void SendMessage (Socket s, string message)
{
Buffer = new Byte[message. Length + 1];
int length = ASCII. GetBytes (message, 0, message. Length, Buffer, 0);
Console.WriteLine ("Number of bytes transferred:" + length.) ToString ());
S.send (Buffer, length, 0);
}

At this point, the definition process of the proxy class is complete.

  (ii) Using proxy class to implement Web Proxy

The following is a concrete implementation of the proxy class implementation of the Web agent, the proxy class is defined in the namespace WebProxy:

1. In Visual Studio. NET in the code Editor to open the Class1.cs file into the Class1.cs code-editing interface.

2. Import the following namespaces at the beginning of the Class1.cs source file:

Using System;
Using System.Net;
Using System.Net.Sockets;
Using System.Text;
Using System.IO;
Using System.Threading;
Using WebProxy;

3. Add the following code to the main function, using the proxy class to implement the Web proxy:

const int port = 8000;
Define port Numbers
TcpListener TcpListener = new TcpListener (port);
Console.WriteLine ("Listening port number:" + port.) ToString ());
TcpListener. Start ();
Listening port number
while (true)
{
Socket socket = TcpListener. AcceptSocket ();
And get a scoket instance of transmitting and receiving data
Proxy proxy = new proxy (socket);
Proxy class instantiation
Thread thread = new Thread (The new ThreadStart (proxy). Run));
Creating Threads
Thread. Start ();
Start thread
}

Save all the steps above so that a simple Web agent is done. This web Agent listens on a 8000 port number.

  (iii) Testing the Web code program

The Web Proxy is implemented by two computers, one of which runs the Web proxy as a Web proxy, and the other computer acts as a client to browse the Web through a Web proxy server. After you have determined that the Web proxy software is running, you need to make the necessary settings for the client:

1. Open IE Browser.

2. Select Tools, choose Internet Options, select the Connections page in the Internet Options dialog box that pops up, click the LAN Settings button in the pop-up local area network (LAN) Settings dialog box, select Use a proxy server for your LAN (X), ( These settings do not apply to dial-up and VPN connections), and in the Address text box in which you enter the IP address of the proxy server, such as "10.138.198.213", enter "8000" in the Port text box. The client's settings are now complete. After you have determined that this computer with the IP address "10.138.198.213" has already run the Web Agent described above, open the client's IE browser and enter the URL that you want to browse through the Web Proxy server to browse the Web.

Iv. Summary

At this point a simple Web Proxy service software is basically done. Although the implementation principle of proxy service is relatively simple, the implementation is still very cumbersome. Network agent is a rich content, to achieve a complex topic, this section describes the agent service software, regardless of the type of protocol implemented, or the implementation of functions, can only be counted as a small part. I hope you can use the introduction of this article, combined with other related knowledge, to create more powerful, more secure, using a more stable network Agent service program.

Implementing a Web Proxy server in C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.