Have you ever wanted to have a web server to just return 200 OK no matter what it is fed? Basically, a web server to just say:
No matter what?
Recently I was working on a solution that was logging to a server that I didn’t have access to because I wasn’t inside of the network. The logging in this solution was deep and pervasive, so it ended up gumming up everything when the logging framework started complaining constantly about not being able to reach the logging endpoint.
Now, sure, it probably would’ve been easier to just disable logging locally while I worked on the solution, but what’s the fun in that? I decided, instead, to create a .NET core web application to simply return 200 OK every time it got something. Anything. 200 OK.
The code
is straightforward. Essentially, there’s a controller that implements the basic HTTP methods and returns OK.
Then, in Setup.cs where you define how the web application behaves, you just redirect any detected 404 to /
The nice thing about this code is it might actually be useful for a real application someday… You know, one that isn’t just a server that’s on fire.
And that’s it! Now you have a server that, when run, will just return 200 OK for anything.
Here’s the full source on GitHub.
The redirection
This isn’t all that useful. I mean, sure, now you have a server that you can run on localhost on some random port, and it will always return OK. That’s great and all, but what about the original scenario? I wanted to essentially spoof an external service to always return 200 OK, and that means I have to redirect my computer’s traffic to hit my arbitrary server.
hosts file
The easy answer is to use your hosts file (SystemRoot > system32 > drivers > etc > hosts) to redirect the url your project is trying to hit with yours.
This is great and all, but the hosts file won’t work with port numbers… so unless you want to run your Everything Is Fine server on port 80, it probably won’t do the job out of the box.
netsh
netsh is a utility built in to Windows that can display or modify the network configuration of the computer you’re running. The idea is that you can take a route your computer is trying to make and re-route it to a different location. I shamelessly stole this code from https://stackoverflow.com/a/36646749, but it works great!
Essentially, this will take anything that comes in on 127.66.66.1 (any localhost address (which is anything that starts with 127.) will do, but you have to be consistent) on port 80 and translate it to the 127.66.66.1 on port 6666. I arbitrarily picked 6s because, well, fire.
And that’s it! NOW you’re surfing with fire.