Jump to content

'whitelist' For Fsockopen Proxy Check


Zixtyy

Recommended Posts

Hey

 

I'm running a website where I get quite a few proxy users evading bans. As an attempt to block them, I added this code:

 

if(@fsockopen($_SERVER['REMOTE_ADDR'], 80, $errstr, $errno, 1))
die("Your magical proxy adventure ends here.");
if(@fsockopen($_SERVER['REMOTE_ADDR'], 3128, $errstr, $errno, 1))
die("Your magical proxy adventure ends here.");
if(@fsockopen($_SERVER['REMOTE_ADDR'], 8080, $errstr, $errno, 1))
die("Your magical proxy adventure ends here.");

 

I know it probably could be a little shorter, but it worked which was the main thing. However, a few of my users run legitimate websites off their home networks, so they ended up being blocked by this. I've attempted something, but I am receiving the error:

 

syntax error, unexpected '{', expecting '('

on the first ELSE IF clause.

 

The full code is:

 

if (in_array($_SERVER['REMOTE_ADDR'], $whitelist)) {
// User is whitelisted, so we continue without going to the check.
} else if {
if (@fsockopen($_SERVER['REMOTE_ADDR'], 80, $errstr, $errno, 1) || (@fsockopen($_SERVER['REMOTE_ADDR'], 8080, $errstr, $errno, 1) || (@fsockopen($_SERVER['REMOTE_ADDR'], 3128, $errstr, $errno, 1)
} else {
// User isn't whitelisted, but is not a proxy, continue!
}

 

Can anyone point me in the right direction?

Edited by Zixtyy
Link to comment
Share on other sites

You're missing your condition for your else if() branch.  The if below it has extra ( and no ){ at the end.

 

If you just re-structure it a little bit the code becomes easier to read and follow too.

if (!in_array($_SERVER['REMOTE_ADDR'], $whitelist)) {
   if (@fsockopen($_SERVER['REMOTE_ADDR'], 80, $errstr, $errno, 1) || @fsockopen($_SERVER['REMOTE_ADDR'], 8080, $errstr, $errno, 1) || @fsockopen($_SERVER['REMOTE_ADDR'], 3128, $errstr, $errno, 1)){
       echo "Banned";
   }
}

Link to comment
Share on other sites

Another way it could be solved, is by having the ports in an array and looping through it. Will make it a bit more extensible, in case you want to add more ports to it later on.

 

Example:

$proxyPorts = array (80, 3128, 8080);
if (!in_array ($_SERVER['REMOTE_ADDR'], $whitelist)) {
   foreach ($proxyPorts as $port) {
       if (@fsockopen ($_SERVER['REMOTE_ADDR'], $port, $errstr, $errno, 1)) {
           echo 'Banned';
           break;
       }
   }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.