Someone tried to host a web server inside Portal 2, and it actually works

Web servers and game servers do not have a lot in common. Besides sometimes accessing web pages for a “message of the day” on a custom multiplayer server, or perhaps something like browsing the web in your Steam overlay, there isn’t a lot that the two have in common. Despite that, YouTuber PortalRunner managed to not only host a complete web server within the Source game engine, but control it using in-game actions.

The Source engine has a lot of quirks

Valve’s workhorse game engine

Credit: PortalRunner (YouTube)

The Source engine is one of the most prolific engines in gaming, powering everything from absolute classics like Half-Life 2, to other non-Valve titles like Titanfall, which used a heavily modifed version. In the 20 years it has been active, there have been a ton of different commands and methods added for developers to take advantage of. One such command, -netconport, was discovered to be active in Portal 2 by PortalRunner, which allowed the server console to be remotely accessible on a specified port, using something like Telnet.

One important distinction to make is the difference between TCP and UDP when it comes to hosting. TCP is used to transfer HTTP data, which is the kind that you’d need for a web server. Game servers use UDP to transmit gameplay-related traffic, which doesn’t ensure delivery of every packet, which doesn’t suit it for websites. Using the command that allows for remote management of a server, you can actually transmit any kind of text you want through it, and it will simply send it through the game’s developer console. Pointing a web browser at the port you specify won’t simply print a webpage to the console, but it does “see” the HTTP headers. It just doesn’t know what to do with them yet.

Getting the game server to recognize HTTP

Easier than you might think

These HTTP headers that the game receives are basically just descriptors for what data is on the web page. The clever method that PortalRunner devises is using “alias” commands to essentially create the response necessary for the web page to function. These alias commands have a long history in multiplayer Source games, namely Counter-Strike for things like one-key jump-throw grenade binds or for other specialized kinds of movement. There is a ton that you can do with aliases, but here, all PortalRunner is doing is mapping specific commands that get passed to the server to appropriate responses using aliases with something called VScript, which is a scripting language for Source.

While this does create the response the browser is looking for, there’s one final TCP-related hurdle to get past before the web page can properly render. When a web browser is communicating with a web server, TCP will usually tell the browser when to stop expecting more packets just by closing the connection. Since closing the game would defeat the purpose, PortalRunner gets around this by sending a Content-Length header along with the content of his web page.

With this, a basic looking web page is born, and is essentially running completely off of a Portal 2 server.

What is possible with this?

Quite a bit, it turns out

While running a web server through Portal 2 has almost no realistic use case, it’s a very fun and interesting way to exploit the Source engine, and it’s surprising what’s possible. Using VScript, it’s possible to grab the name of the map and print it to the browser, check the position of specific entities on the map, and, after fuddling around with JavaScript and fixing some line-break discrepancies, automatically update that data as it moves around the map. PortalRunner even created a minimap that updated live with the player’s position and the surrounding geometry; a fully featured web app, running completely off of the game engine. You can even control the player by pressing buttons in the browser.

Taking this a step further, PortalRunner demonstrates in the video that it’s possible to build a functioning web page using the in-game cube entities and their positions in the game world. By scripting a system that associates each HTML tag with cubes in-game, you can position the cubes in the game world in such a way that it displays a web page. Using this method, PortalRunner attempts to recreate the home page for DuckDuckGo, but is largely limited by the amount of CSS that’s on top of the basic HTML, as seen in some screenshots.

A very creative use for a game server

Game servers are really different from your average web server, not just in the traffic they serve, but the protocols they use to communicate. MOTD messages were cutting edge when they were first spotted in multiplayer games decades ago, but they don’t hold a candle to hosting a website in the game itself. If you’re interested in trying this out for yourself, PortalRunner has posted this entire project on GitHub.

Continue Reading