This is an old revision of the document!


Programming guide for Smart Subdomains

I'll just assume you already created a smart subdomain called www.example.com and you want to write code for it. Smart subdomains can only return A, AAAA and CNAME records (for now).

Note: If you want to redirect visitors based on their country of origin, network name, uptime information, or extra information, please don't forget to select the “share” checkboxes in the Edit smart subdomain page.

Simple Script

The simplest script that you can write is:

$output[] = array("A", "127.0.0.1");

As I said in the Overview, smart subdomains run limited PHP scripts. This code returns 127.0.0.1 to anybody that requests the IP address of the www.example.com subdomain.

The $output array represents the only output that your script can send to the DNS server. Once the script has finished running, GeoScaling searches for the $output variable, and if it is well formatted, it generated the reply to the user. If the scrpt fails to run or it has invalid output, DNS2 uses the IP address which you put in the “Failover IP” section when you defined the smart subdomain.

Country Redirection

This product includes GeoLite data created by MaxMind, available from http://www.maxmind.com/.

Next, let's add redirection based on the country. Let's say I want to return 127.0.0.1 for everybody from inside Romania, and 127.0.0.2 for anybody else:

if($country == "ro")
  $output[] = array("A", "127.0.0.1");
else
  $output[] = array("A", "127.0.0.2");

All country names are lower-case. You can find a list of possible countries, including codes for Satellite Providers and Anonymous Proxies, here.

Return serveral IPs

As the $output variable is an array, you can return more than one IP address:

  $output[] = array("A", "127.0.0.3");
  $output[] = array("A", "127.0.0.4");

This would return both ip addresses.

Set the TTL

The default TTL (time-to-live) in GeoScaling is 300 (5 minutes). You cannot define a record with a TTL lower than 300 (well, unless you contact to us). If you want to output an IP address with a custom TTL in a smart subdomain, you can do it like this:

  $output[] = array("A", "127.0.0.5", "400");

This returns a ttl of 400 seconds for 127.0.0.5.

Fail without returning failure ip

If you don't want to return any ip address, you can just define $output like this:

$output[] = array("fail");

Returning CNAMEs

You can return a CNAME is a similar way you return an IP address:

$output[] = array("CNAME", "ghs.google.com");

This returns the CNAME of ghs.google.com (pleasae note the lack of an ending dot after ghs.google.com).

Redirecting based on the Network Name (AS/ASN)

GeoScaling DNS2 can redirect based on the AS number of the network the visitor comes from.

If it is defined, the $as variable contains an array of lower case possible network names. The array usually contains only one element. You can make a simple redirect:

if( in_array("as30890", $as) )
  $output[] = array("A", "127.0.0.1");
else
  $output[] = array("A", "127.0.0.2");

Or you can check for lists of network names, like this:

if( sizeof( array_intersect ( $as, array("as30890", "as24745") ) ) )
  $output[] = array("A", "127.0.0.1");
else
  $output[] = array("A", "127.0.0.2");

Redirecting on uptime information

Redirecting based on uptime is also easy. All the results of your uptime checks are made available in the $uptime array. Let's say you set up an uptime check named “simpliq”. If simpliq is down, you can redirect visitors to another mirror like this:

if($uptime['simpliq']==1)
 $output[] = array("A", "127.0.0.1");
else
 $output[] = array("A", "127.0.0.2");

Here's another real-life example that you can base your code on:

if($uptime['fisierulmeu-teentelecom']==1)
  $output[] = array("A", "193.138.195.114");
 
if($uptime['fisierulmeu-evolva']==1)
  $output[] = array("A", "86.55.16.126");
 
if(!isset($output) || !sizeof($output))
  $output[] = array("A", "96.9.130.197"); // backup mirror

Redirecting based on extra information

As described here, you can send extra information in the form of a string that can be accessed by your scripts. The following script returns reuturns 4.4.4.4 when the variable datacenter3 is smaller than 80. This could be useful to redirect users to mirrors based on the load of your servers.

if(isset($extra_info) && $extra_info!=false )
{
  $extra_info = unserialize($extra_info);
  if($extra_info['datacenter3']<80)
    $output[] = array("A", "4.4.4.4");
  else
    $output[] = array("A", "5.5.5.5");
}
else
{
  $output[] = array("A", "3.3.3.3");
}

This particular example uses data sent by the script given as example in the Sending extra information to DNS2 with the XML-RPC API page:

$payload['datacenter1'] = "43";
$payload['datacenter2'] = "22";
$payload['datacenter3'] = "87";

Other useful variables sent to the script

  • $subdomain - the complete name of the subdomain + domain (in our case, www.example.com)
  • $remote_ip - the IP of the user

Real-life example that combines two types of redirects

 
/*
as30890 - Evolva
as8708 - RDS
as34304 - Teen Telecom
as24745 - Romtelecom
*/
 
if($country=="ro") // if our user comes from Romania
{
  if( in_array( $as, array("as30890", "as24745") ) ) // if the network of the visitor is either evolva or romtelecom,
  {
    $output[] = array("A", "86.55.16.126"); // give them this ip
  }
  else if( in_array( $as, array("as34304", "as8708") ) ) // else if their network is teen telecom or rds
  {
    $output[] = array("A", "193.138.195.114"); // give them this ip
  }
  else // if he/she comes from another network, just send both ips
  {
    $output[] = array("A", "86.55.16.126");
    $output[] = array("A", "193.138.195.114");
  }
}
else // if our user comes from outside Romania
{
  $output[] = array("A", "91.121.158.100", "400"); // return IP 91.121.158.100 with a TTL of 400
}
 
short_programming_guide_for_smart_subdomains.1422135508.txt.gz · Last modified: 2015/01/30 00:41 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki