I am under DDoS. What can I do? already addresses this kind of "canonical question", but I think I'm asking a distinguishable variant, as described below. But please advise if this post inappropriate.
The attacks I'm getting are from various and ever-changing ip's; in the last few minutes from 66.135.63.82. All attacks are against a cgi I'm using on my site that I wrote myself (it's gpl'ed http://www.forkosh.com/nutshell.html but not really of much interest to anybody but me (and, apparently, the hacker)). Attacks run my cgi with a goofy-looking ?query_string, and I added a qvalidate() function that seems to be reliably identifying them, so far. And for invalid queries, I now sleep(15) and then emit a ~25MB page to the attacker (just to show I can be annoying, too:). And they do seem to wait -- rather than multiple attacks per second, they're now spread out at approximately those 15-sec intervals.
But this semi-fix seems silly and ultimately ineffective in the long run. And it wastes lots of my time checking up on things several times a day, and occasionally tweaking the procedure. So what I'm asking is -- what can I code into my cgi to better deal with these attacks as they occur??? I have the attack in my hand, so to speak, in realtime as it occurs. And I have the ip from a getenv("REMOTE_ADDR"). So I can do pretty much whatever I want at this point (except bleep the bleep out of him, which is what I'd really like to do, but don't know how to code that:). What would be the best thing to be doing? Thanks.
edit -- Thanks for replies, guys. I should have mentioned my site is on shared host dreamhost.com so some suggestions are unavailable to me. And I'm waiting for a reply from isp. But since it's my own cgi that's targetted, I was hoping that gave me an opportunity to do something not usually possible.
edit -- @HBruijn Why off-topic? At the very top of this post, I gave a link to this site's "canonical question" also on this very same topic. And then I went on to explain why I thought I was asking a "distinguishable variant" of that (apparently on-topic) question. Moreover, I originally posted https://softwareengineering.stackexchange.com/questions/316052/how-to-best-deal-with-ddos-attack-against-your-my-site and they told me to post it here. Go figure:)