• 0

Basic winsock proxy


Question

Hi, I am writing an HTTP proxy for a web browser, using Winsock and C++. For now the proxy should behave as if there was no proxy, just redirecting the requests towards the correct server, and the html data towards the browser. Also, it is synchronous.

It works, but performance is quite bad currently. Before I try anything else, I am wondering if at least the basic logic is correct. Here's some pseudocode:

Initialize Winsock
Socket listenSocket
listenSocket.Bind(host, port)
listenSocket.Listen()

while true:
     Socket browserSocket = listenSocket.Accept()
     httpRequest = browserSocket.ReceiveRequest()
     Socket serverSocket
     serverSocket.Connect(getHostName(httpRequest), port 80)
     serverSocket.SendRequest(httpRequest)
     transmitHtml(serverSocket, browserSocket)
     browserSocket.Close()
     serverSocket.Close()

listeSocket.Close()

I am mainly wondering if it is correct to close both browser and server sockets and create new ones everytime. For loading a simple page like Google, it will go 3 times through that loop; more involved pages will go numerous times through it.

Thanks!

Link to comment
https://www.neowin.net/forum/topic/889590-basic-winsock-proxy/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

The slow step with almost every webpage is the number of HTTP requests. Every image, every AJAX call, etc involves a separate round trip HTTP request and response. This is why lots of high-traffic webpages use CSS-sliced images (one image = one HTTP request).

Each HTTP request in your code will block subsequent requests until said request finishes processing. You need to do this asynchronously (multiple requests being handled at the same time) to increase performance.

As for your specific code, use MSDN. Although it has tons of bad programming practices in it, it is hands down the best piece of programming documentation on the planet, especially for the Windows API. Check out the Winsock reference, complete with usage scenarios and examples, some of which will cover your specific problem. Note that multi-threaded sockets programming is not an easy kill; even the experts have trouble with this stuff.

And yes, consider doing this in .NET. Version 3.5 is very nice and version 4.0 coming up has some new high-performance sockets implementations that would eliminate 90% of the headaches you'd get into doing native code.

If you're gonna stick with C++, recognize that someone else has already done what you want to do, except they've done it better than you will. Consider using a library, such as the Boost ASIO library. I hate cliches, but don't reinvent the wheel.

  • 0

Thank you. I was starting to think that the main bottleneck was probably the fact that this is synchronous, but just to be sure. So the logic seems correct to you?

This is an assignment so the language, library and high-level design choices (like making it synchronous) are forced. And yes I got a lot of MSDN code in there. :laugh:

  • 0

Well, it is possible to keep the HTTP connection open for more than one request.

You haven't said what you mean by bad performance though. I am guessing a big bottleneck though is that it is synchronous. In the real world, it's normal to have up to four connections to a 1.0 server and two connections to a 1.1 (which you keep open, or persist, for more than one request.) I'm guessing this is far out of scope for your assignment though, so I wouldn't worry about it. You'll want to understand the synchronous part (even though it's not really used on Windows) before you start venturing into asynchronous I/O and multi-threading.

  • 0
  On 05/04/2010 at 10:43, hdood said:
You haven't said what you mean by bad performance though.
Most sites load approximately 3x slower than normal, but some pages give me bigger issues. A page like http://en.wikipedia.org/wiki/World_War_I takes ages to load, with various 10060 (connection timed out) and 10054 (connection reset by peer) along the way. When the browser finally says "Done", the page is still missing a lot of formatting and pictures.

msnd.com loads fast, but like Wikipedia, when the browser says "Done" (and doesn't make any more connections to the proxy), the page seems to be missing stuff.

sanstitreay.jpg

yahoo.com simply doesn't work. The browser says "Done" and the page is still blank. :blink: But google works flawlessly along with most simple sites.

Also wondering what should I use for receive buffers (second argument for recv). For now I use a local char[30000] and I immediatly forward the result to the browser, and then repeat until the return value is <= 0. Does that make sense?

  • 0

Just wanted to say I asked the above question to my teacher and he said it was normal some pages would be incomplete with a synchronous proxy, given that Firefox tries to make several requests in parallel and will give up if they are not satisfied quickly enough.

After all, the level of performance is what should be expected of such a naïve implementation I think.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • TSMC's trade secret meltdown exposed by internal monitoring by Paul Hill Taiwan Semiconductor Manufacturing Co. (TSMC) has detected unauthorized activities using “comprehensive and robust monitoring mechanisms”. The chip maker said that it believes trade secrets have been leaked as a result and has taken disciplinary action against the personnel involved and initiated legal proceedings explaining that it has a zero-tolerance policy for compromising trade secrets or harming company interests. As the case is under judicial review, it is unable to provide further details about the case. While TSMC is not speaking on the matter, the leak allegedly involves critical proprietary information on 2-nanometer chip development and production. Production of the 2-nanometer chip is among the leading-edge manufacturing processes in the semiconductor industry right now, which explains why an actor would want to steal related information. TSMC is one of the world’s leading chip makers, with companies like Apple and Nvidia being among its clients. It is also the world’s biggest chip maker and among the most advanced. After this leak, the company has reaffirmed its commitment to safeguarding its core business competitiveness and the shared interests of all its employees. The jury is still out on the motivations behind the leaking of the trade secrets, but those involved have been fired, according to Nikkei. TSMC, similarly to Nvidia, has found itself in the geopolitical struggle between China and the US in recent years. As you likely know, the last several years have seen America really go hard after Huawei over national security concerns, and in more recent years, the US has sought to limit China’s access to AI hardware. Earlier this year, Neowin reported that TSMC faces a $1 billion fine for breaching export controls against Huawei. TSMC had made a chip for the Chinese firm Sophgo, but that same chip was later discovered to be used in Huawei's high-end Ascend 910B AI processor. This discovery was made by TechInsights, a Canadian company, and as a result, TSMC stopped shipments to Sophgo, and the US added Sophgo to its blacklist to prevent further circumvention along that route. It will certainly be fascinating to learn more about the motivation for the theft of trade secrets as more information comes to light. Via: CNBC | Image via Depositphotos.com
    • Apple will probably reciprocate by advertising their home products and mocking Google Home's continued debacles.
    • Files still rely on classic windows indexing rather than methods that 'everything' uses. So search is suboptimal. The compression algorithm list is really lacking not comparable to winrar, 7zip and peazip... The gui looks nice, but windhawk is a thing. I don't understand what files brings in 2025. Just a less efficient, less customizable file explorer. https://i.imgur.com/afVKXBj.png
    • I think I only know of a handful of people that don't have WhatsApp - somewhat frustratingly they're my OAP group that I support. But in those situations they always prefer to phone rather than text anyway, so this won't really do much to help me. If the functionality of Guest mode is only one-to-one messages and no media transfers, I can't see it getting used that much. A group option with photo attachments would be useful for my other business to send out a notification to our clients including those that don't have WhatsApp.
  • Recent Achievements

    • Week One Done
      Zojaji earned a badge
      Week One Done
    • First Post
      Soeaker4thedead earned a badge
      First Post
    • First Post
      kryptickid earned a badge
      First Post
    • First Post
      Nemesis-IV earned a badge
      First Post
    • First Post
      Aidan Helfrich earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      759
    2. 2
      ATLien_0
      187
    3. 3
      +FloatingFatMan
      151
    4. 4
      Xenon
      117
    5. 5
      wakjak
      113
  • Tell a friend

    Love Neowin? Tell a friend!