What Is My IP?

UPDATE 5/27/15: WhatsMyIP.Me now gets 5,000 hits an hour! Incredible!

I recently have been experiencing issues at work where one of my two ISP’s would go down. I have a Cisco firewall that handles the fail over to my backup connection, so most of my users were unaffected.

The only users who even noticed were those who frequented websites who had additional security featues enabled to prevent against session hijacking. The constant ISP failover behaved like a multi-homed network that would randomly serve web pages across more than one Internet provider on a page by page basis.

This was a bit tricky to debug, so my first step was to create a php script that would let me know what is my external ip address, whatsmyip.me . I felt the need to create my own as many of the existing services that tell you your IP address are covered in advertisements and I just wanted a tool that would return the external IP address and nothing more. Also, most of them do a terrible job at detecting proxies and return incorrect information.

whatsmyip.me is a single php script and the code is below:

<?PHP
function whatsMyIP()
{ 
  foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key)
  {
      if (array_key_exists($key, $_SERVER) === true)
      {
        foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip)
          {
              if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false)
              {
                  return $ip;
              }
          }
      }
    }
}
 
$users_ip = whatsMyIP();
echo $users_ip; // Output IP address [Ex: 123.12.41.42]
?>

I also put this on github under “What Is My IP”

The way this script works by analyzing the php $_SERVER variable ( an array containing information such as headers, paths, and script locations created by the webserver) and returning the most reliable IP address possible. Please note, the only address you can really trust is REMOTE_ADDR, because it is the source IP of the TCP connection and cant be changed by spoofing/changing an http header. While it is technically possible to bidirectionally spoof IP addresses at the Border Gateway level, but you would have to have control over an ISP to do so.

After I had the webservice to accurately give me my ip address, i created a single bash file that would echo my IP address every 15 seconds for the next 24 hours.

MPB:dconroy$ for i in `seq 1 5760`;do  date;curl http://whatsmyip.me;sleep 15; done

If your linux box is in a different timezone, you can use the TZ environment variable to report back the correct time. In my case i just added ‘TZ=America/New_York’ directly before ‘date’.

Using this, I was able to pinpoint the exact windows where my ISP would drop making my technical support calls much more productive. This is what I love about linux and raspberry pi’s – if the tool you are looking for doesnt exist, build one!

Read More

Using PHP/cURL to Create News and Event Items in Sitefinity using WCF

Earlier in the year, my company had decided that we will be moving websites to a new provider, Sitefinity. I couldn’t be happier with the decision, as it is a very modern and robust Web CMS. My only issue is that it is built on a platform that I personally don’t have much experience, ASP.NET. The Sitefinity SDK and Documentation are very thorough, but I was unable to reuse many of the examples as I don’t have a .NET development environment and am only vaguely familiar with C#.

BUT, I have been developing in PHP as a hobby for a few years, and was determined to be able to migrate my old content to the new Sitefinity site programatically. So, I looked closely at Sitefinity‘s Web Services. The web service API is based on the Windows Communication Foundation (WCF) REST approach, which are two technologies that I had just learned earlier this year while working on the RAMCO API.

So, after many hours of debugging WireShark traces and http responses, I was finally able to create some sample content in Sitefinity using their Web Services, PHP, and cURL.

I’ve created a public repository for this sample code on github here:

https://github.com/dconroy/PHPSifefinityWebServices/

This sample code allows you to to Login, Create a News Item, Create a Event Item, Print all News Items, Print all Event Items…all in PHP!

Here is a snippet of my Create News Function:

 
function CreateNews($sitefinityHost,$newsServiceUrl, $cookie) {
 
$title ="The Title";
$newurl="The-URL";
$author="Web Service";
$sourceurl="http://www.daveconroy.com";
$sourcename="Dave Conroy";
$content="Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
 
//build News Service URL
$url = $sitefinityHost.$newsServiceUrl."00000000-0000-0000-0000-000000000000/";	
 
//build the News JSON
 
$objDateTime = new DateTime('NOW');
$date=$objDateTime->format('U');
$date=$date."000"; //add milliseconds required by sitefinity
$myDate= date("U");
$newsStory = array("Item"=>array(
                   "Title"=>array("PersistedValue"=>"$title",
                                  "Value"=>"$title"),
                   "UrlName"=>array("PersistedValue"=>"$newurl",
                                    "Value"=>"$newurl"),
                    "ItemDefaultUrl"=>array("PersistedValue"=>"$newurl",
                                            "Value"=>"$newurl"),
                    "Author"=>array("PersistedValue"=>"$author",
                                     "Value"=>"$author"),
                    "DefaultUrl"=>"$newurl",
                    "SourceName"=>"$sourcename",
                    "SourceSite"=>"$sourceurl",
                    "Content"=>array("PersistedValue"=>"$content",
                                     "Value"=>"$content"),
                    "PublicationDate"=>"/Date($date)/")
     );
 
    //encode news statement
    $newsStory = json_encode($newsStory);
 
    //start HTTP Command session
    $headers = array(
              'Content-Type: application/json',
              'Expect: 100-continue',
              'Accept:'
 
    );	
 
    $session = curl_init($url);
    curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($session, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($session, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($session, CURLOPT_POSTFIELDS, $newsStory);
    curl_setopt($session, CURLOPT_COOKIEJAR, $cookie );
    $response = curl_exec($session);
    echo $response;
    curl_close($session);
 
}

After I was able to use the Web Services to create place holders(content id guid’s) for each news item, injecting the HTML from my old site into the new one via direct SQL queries was a breeze.

I know there are other developers out there who are not experienced with .NET, and I wanted them to know that you can still use this great product.

If you find a problem, incorrect comment, obsolete or improper code, please me know on github or by replying to this blog post. Thanks for reading!

Read More