HTTP tunnel (HTTP proxy socket customer)

xiaoxiao2021-03-06  95

This article contributed from Akash Kava, Translation: Bugfree / 9CBS

Environment: VC6

※ HTTP tunnel ※ -------- HTTP is a protocol based on the text-based browser. In most cases you hide behind the proxy server, access the Internet via the LAN. In the Connection Option in IE, you give your LAN settings. This proxy server runs text-based protocol, which you can get the external network HTTP related data from it. Yes, use HTTP to connect to the external world with small hopes above it, and use binary agreements to get the data you want, or even your agreement. It passes HTTP.

※ HTTPS explanation ※ --------- In HTTPS, data is from the browser to the server and from the server to the browser in a safe way. It is a binary agreement; when he passes through the agent, the agent does not know what it is. The agent only allows the binary stream to open, allowing the server to exchange data between the servers and customers. The proxy server thinks that we are making a safe session.

For HTTPS, your browser is connected to the proxy server and send a command

Connect neurospeech.com:443 http / 1.0 host neurospeech.com:443 [... If necessary, other lines of the HTTP head are ended in ] < CR> // last empty line

Next, the proxy server opens it as an HTTP security session, opens a binary stream to the requirements server and port. If the connection is established, the proxy server returns the following response:

HTTP / 1.0 200 Connection Established [... Ignore all other lines of all HTTP headers in end,] // last blank line

Now, the browser is connected to the terminal server, and the data can be exchanged in a binary and secure way.

※ How to do this ※ ------------- Now is your program task to fool the proxy server, the behavior is the same as IE, SECURE HTTP.

1. Issue Connect Host: Port http / 1.1 . 3. Issue . 4. Wait for a line of response. If it Contains HTTP / 1 .X 200, The Connection IS SUCCESSFUL. 5. Read Further Lines of Response Until You Receive An Empty Line. 6. Now, You are consNected to outside world through. Do any data Exchange you want.

Example source code

// You need to connect to mail.yahoo.com on port 25 // Through a proxy on 192.0.1.1, on HTTP Proxy 4480 // CSocketClient is Socket wrapping class // When you apply operator << on CString, it writes CString // to socket ending with crlf // when you apply Operator >> On CString, IT Receives // a line of response from socket until crlftry {cstring request, response; csocketclient client

Client.connectto ("192.0.1.1.1.1.1", 4480);

// Issue Connect Command Request = "Connect mail.yahoo.com:25 http / 1.0"; client << Request;

// Issue Empty Line Request = ""; Client << Request;

// Receive Response from Server Client >> RESPONSE

// ignore http version

INT n = response.find (''); response = response.mid (n 1);

// HTTP Response Must Be 200 Only if (Response.Left (3)! = "200") {// Connection Refuser Http Proxy Server AfxMessageBox (Response);

// read response lines unsponse; if (response.isempty ()) Break;} while

// Coooooooool .... now connection to mail.yahoo.com:25 // Do Further SMTP Protocol Here ..

} Catch (csocketexception * pe) {pe-> reporterror ();

※ The library source code ※ ------------- file DNS.H contains all DNS-related source code. It uses other libraries such as SocketEx.h, SocketClient.h, and Neurobuffer.h

※ Csocketex ※ -------------

As a wrapper class for a socket function. (If you don't know how CSocket work, it is very bulky and untrusted) all function root CSocket is the same name. You can apply this class directly

※ CsocketClient ※ -----------------

Derive from CSocketex and throw appropriate exceptions based on the detailed Winsock error. It defines two operators, >> and <<; if it is also required to switch network sequences. And the main sequence is a network order.

※ ChttpProxySocketClient ※ ---------------- Delicious self CSocketClient, you can call the setProxySettings (ProxyServer, Port) method and make proxy settings. Next, you can connect to the host and port you want. The ConnnectTo method is overwritten, and it automatically implements an HTTP proxy protocol to give you a connection.

※ How to use ChttpProxySocketClient ※ -------------------------------- // EG you need to connect to mail.yahoo. com on port 25 // Through a proxy on 192.0.1.1, on HTTP Proxy 4480 // CSocketClient is Socket wrapping class // When you apply operator << on CString, it writes CString // To Socket ending with CRLF // When you Apply Operator >> On CString, IT Receives // Line of Response from Socket Until CRLF TRY {ChttpproxysocketClient Client

Client.SetProxySettings ("192.0.1.1.1.1.11", 1979);

// Connect to Server mail.yahoo.com on Port 25 Client.connectto ("mail.yahoo.com", 25);

// you now have access to mail.yahoo.com on port 25 // if you do not call setproxySettings, then // you are connection to mail.yahoo.com directly if // you have direct access, so always use ///// ChttpproxysocketClient and no need to do any // extra coding.

} Catch (csocketexception * pe) {pe-> reporterror ();

[Bugfree Translation] The author said in its article, why not use .h .cpp style programming, this I also give me the reason, because his style is not easy, everyone can read its source code. There are two shortcomings, (a) overall feels chaos, uncomfortable, it is difficult to grasp from the whole class. (2) With the increase of code, the compilation time is too long, it may be seen. You may know. One of the benefits of .H.CPP is that .CPP code is compiled once, if you don't change the content of the .cpp file, this The file will not be compiled. Also, so your class interface is very clear, just share the class .h file between the module.

转载请注明原文地址:https://www.9cbs.com/read-102145.html

New Post(0)