From eBower Wiki
Jump to: navigation, search
(Webpage Configuration)
(Webpage Configuration)
Line 122: Line 122:
 
?>
 
?>
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
= What is my DNS? =
 +
 +
Detecting your public DNS server is a bit difficult in many cases. If you look at your computer configuration often it's your router so you have no idea when it changes. If you use Google DNS you may think it's 2001:4860:4860::8888 or 2001:4860:4860::8844 (if you're still rocking a TRS-80 you may use the old 8.8.8.8 and 8.8.4.4), but this is a common AnyCast address and you've got no idea what datacenter you're going to. So, how does one figure this out?
 +
 +
The premise is that you run a limited DNS server on (or close to) your web server. You don't even need to configure it, you just need it to listen to requests for a specific hostname structure. Now you can configure a website to grab an object from that non-existent hostname and check the DNS server logs via an AJAX call to figure out who made the request.
 +
 +
An alternative implementation is to make two simple requests in quick succession, first to the non-existent hostname and the other to the website passing the hostname to search for.

Revision as of 14:56, 7 July 2015

Everyone could use some quick, simple information every so often. There are plenty of options for many of these, but they're surprisingly simple to set up yourself.

What Is My IP?

You can find dozens of these sites, most with a lot of information you don't always care about like a map of where it thinks you are. I created a simple one mimicking whatismyip.akamai.com which only returns your IP address. This list of sites will return your IP address:

These sites will return your IPv4 address, assuming you have one:

And these sites will return your IPv6 address:

That's an awful lot of configuration, it must have taken me days and hundreds of lines of code and configuration to get it done, right?

DNS Configuration

Of course, you'll need to have the right DNS entries in most cases. Here I took each of the hostnames above and added a CNAME record pointing to www.ebower.com which represents the server they all run on. Note that one of them is just a directory off my server, that's potentially an easier way to go.

The IPv4-only sites only have an A record, so you can't reach them over IPv6. Conversely the IPv6-only sites only have a AAAA record so you can't get to them over IPv4. The Dual Stack sites, of course, have both.

Apache Configuration

If you're going the route of dedicated hostnames, you'll need an Apache configuration to support them. I created the following in /etc/apache2/sites-available/whatismyip.conf, then I created a symlink to /etc/apache2/sites-enabled/whatismyip.conf and did a sudo service apache2 reload to enable things.

<VirtualHost *:80>
        ServerAdmin webmaster@ebower.com
        ServerName whatismyip.ebower.com
        ServerAlias *.whatismyip.ebower.com
        DocumentRoot /var/www/whatismyip
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
 
        ErrorLog ${APACHE_LOG_DIR}/error.log
 
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
 
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

You'll see that the primary service (ServerName) is whatismyip.ebower.com, but anything under whatismyip.ebower.com will work as well thanks to the ServerAlias line. If I created a DNS record for foo.whatismyip.ebower.com it would work with no changes.

Webpage Configuration

The webpage itself is pretty simple. I placed it in /var/www/whatismyip/index.php so if I wanted to I could access it off my main page with the SSL cert.

<?php 
  $myip = $_SERVER['REMOTE_ADDR'];
  if ( $myip == '192.168.1.1' ) {   
    $myip = `curl whatismyip.akamai.com`;
  }
  echo $myip;
?>

This should be even simpler, I could just print $_SERVER['REMOTE_ADDR'] but if I'm coming from my router's IPv4 address I'm coming from my internal network. Rather than printing the RFC1918 address I would rather the behavior be to print my public address. So I just have my server make a call to Akamai's service since we're both behind the same NAT and the address is the same.

For IPv6 this isn't needed, IPv6 just works while IPv4 needs these little kludges.

What is my User Agent String?

The User Agent String is a text field that your browser uses to identify itself. Untouched, it represents the operating system and browser version - but you can easily modify it so it's hardly reliable. Sometimes you may need to know what's being presented by your browser. For example:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36

This is much simpler than "whatismyip" because we don't care about the transport type.

DNS Configuration

This is a simple CNAME, whatismyuastring.ebower.com to www.ebower.com.

Apache Configuration

Again, slightly simpler since we don't need the wildcard ServerAlias:

<VirtualHost *:80>
        ServerAdmin webmaster@ebower.com
        ServerName whatismyuastring.ebower.com
 
        DocumentRoot /var/www/whatismyuastring
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
 
        ErrorLog ${APACHE_LOG_DIR}/error.log
 
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
 
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Webpage Configuration

Simple again, I could even punt on the intermediary variable if I wanted.

<?php
  $myuastring = $_SERVER['HTTP_USER_AGENT'];
  echo $myuastring;
?>

What is my DNS?

Detecting your public DNS server is a bit difficult in many cases. If you look at your computer configuration often it's your router so you have no idea when it changes. If you use Google DNS you may think it's 2001:4860:4860::8888 or 2001:4860:4860::8844 (if you're still rocking a TRS-80 you may use the old 8.8.8.8 and 8.8.4.4), but this is a common AnyCast address and you've got no idea what datacenter you're going to. So, how does one figure this out?

The premise is that you run a limited DNS server on (or close to) your web server. You don't even need to configure it, you just need it to listen to requests for a specific hostname structure. Now you can configure a website to grab an object from that non-existent hostname and check the DNS server logs via an AJAX call to figure out who made the request.

An alternative implementation is to make two simple requests in quick succession, first to the non-existent hostname and the other to the website passing the hostname to search for.