Posted by
mharman on
3/28/2011 6:39 PM |
Comments (0)

The objective: To host a Java-based application from a server located in my office.
Sounds easy doesn't it? The problem is, if you do not have a static IP address it can be tricky.
There are a number of sites that will do for you what I will outline, but there is just something about doing it yourself and having control over the entire process. That being said, if you do not have external hosting with database access, one of these services may be your only option. Another thing I should mention before we begin, make sure that you are allowed to have outside access to your servers. Many ISPs do not allow website serving using non-business accounts which is why I suspect they have dynamic IP addresses in the first place. In my case, the weekly page requests will be far less than the number of emails I receive in any given day.
I maintain a shared hosting account where I host, among other things, this blog. It has a static IP address and will provide the platform that will allow me to reroute requests. Here are the things that will need to be done:
1. Create a domain or subdomain that will be routed to your home server.
2. Find your home IP address and map a connection from your router to your home server.
3. Figure out a way to reroute requests from our hosted site to our home server.
4. Create a database table to maintain updated IP infomration.
5. Figure out a way to update the IP information in the database.
Creating a domain or subdomain is fairly easy and requires a trip to your domain registrar (Network Solutions or GoDaddy) or add a Host(a) record to your domain record on your DNS server.
To find your home IP address, you can look it up on your router or just visit this page I created. To set up your port mapping is really beyond what we are going to cover here, consult your router's manual.
Rerouting the request is a little different depending on the language used. I used ColdFusion which makes rerouting requests easy but most languages I have worked with have provisions for managing the page lifecycle. I just inserted this code in my index.cfm page. It just checks to see of the subdomain is being requested then redirects the request to my home server.
<cfif findNoCase("subdomain.smartpitbull","#CGI.SERVER_NAME#", 0)>
<cflocation url="http://123.123.123.123:8010" addtoken="false" />
</cfif>
That works great, but we need a way to keep up with the IP address changes and the code above will not do that very well. Now, we will create a database to store the IP address of our home router. This will allow us to update it as necessary. I just created a table with the following fields:
dynamicID - varchar(50)
dynamicIPAddress - varchar(20)
portNo - int
Make the dynamicID field the key field and make its default value a GUID, I'm using MS SQL so I made the default value NEWID(). The dynamicIPAddress field is where we store, you guessed it, our IP address. Finally, we add a starting record. Add the IP address of 0.0.0.0 and what ever port you are using (the above example uses port 8010). Make a note of the GUID of this record, we will need it in the next step.
This last part requires some creativity and how much depends on what language you are using. Again, I maintain a ColdFusion server in my office which made this much easier. I first created a page on my hosted site to accept a request with one URL variable.
<cfif NOT structKeyExists(url,"domainGUID") >
<cfabort>
</cfif>
<cfset dynamicID = url.domainGUID />
<cfset domainAddress = CGI.REMOTE_HOST />
<cfquery datasource="yourDataSource">
UPDATE yourHostTable
SET
dynamicIPAddress = <cfqueryparam value="#domainAddress#"
cfsqltype="cf_sql_varchar" maxlength="50" />
WHERE
dynamicID = <cfqueryparam value="#dynamicID#"
cfsqltype="cf_sql_varchar" maxlength="50" />
</cfquery>
Call the page what ever you like, we will call ours changeMyAddress.cfm so that the scheduled task that runs calls http://www.smartpitbull.com/changeMyAddress.cfm?domainGUID=1266A3A9-672A-9872-EA34-AB23AFE89827 and this will update the database with the current home IP address.
The last thing that we need to do is rewrite our index.cfm page to pull the most recent address from the database.
<cfif findNoCase("subdomain.smartpitbull","#CGI.SERVER_NAME#", 0)>
<cfquery name="query" datasource="yourDataSource">
SELECT
CASE
WHEN portNo <> 80 THEN
'http://' + dynamicIPAddress + ':' + CAST(portNo AS varchar(5))
ELSE
'http://' + dynamicIPAddress
END AS ipAddress
FROM
ourAddressTable
WHERE
dynamicID = '1266A3A9-672A-9872-EA34-AB23AFE89827'
</cfquery>
<cflocation url="#query.ipAddress#" addtoken="false" />
</cfif>
That should be it. Our repeating task will automatically update our database with the most current IP address of our home router. Requests that come in to our site are intercepted, interpreted and rerouted to our home address. If our port mapping is set up correctly, the request will be routed to our home server for the response.
In no way do I claim that this is the best or the most efficient way to accomplish this. But it is what I came up with and seems to work for me. I hope it helps you and if you have any ideas or suggestions, please don't hesitate to share.
Before I go, one note about security. Name the page that updates your database and the variable that you pass to it carefully. I recommend making it a bit obscure because if anyone ever guesses those things and hits the page, they can hijack your routing.
~mike