CFMX: Execute Code Based Upon Debug IP Addresses...

Posted by Dan on Jul 14, 2005 @ 2:52 PM

I've been working with a lot of code recently that I didn't write and the code flow is very hard to follow. A lot of times there's no clear execution path flow and I'm finding fixing problems is very time consuming because I'm having to jump around to a bunch of seemingly unrelated files to find where the problem actually is.

One of the things I seem to be having to do a lot is dump out either little "I'm here" messages or having to output the SQL statements that are getting executed so I can run them in SQL Query Analyzer to find out what the results are and what the query really should be.

I've found myself writing a lot of <cfif cgi.remote_addr EQ "my.ip.addr.ess"> lines in order to only output that data to myself. This morning I decided I should just write a tag that checks the CF Admin for a list of valid Debugging IPs and uses that to decide if code should be excuted.

Thanks to some code Ben Forta wrote for managing debugging addresses without having CF Administrator access, I was able to put together this little tag.

In a nutshell, if IP address of the remote computer isn't found in the ColdFusion Administrator's "Debugging IP Addresses", then the code in between the starting and ending <cf_debugOutput> tags won't be executed. Feel free to use the source code however you want. Just copy-n-paste the below code into a file called "debugWrite.cfm" and copy it into your \CFusionMX\CustomTags folder.

<cfsilent>
    <!----------------------------------------------------------------------------
    Name: cf_debugOutput
    Author: Dan G. Switzer, II
    Date: July 14, 2005

    Description:
    This tag will only process the contents of the tag if the user's
    CGI.remote_addr is in the list of Debugging IPs in the CF Administrator.
    ----------------------------------------------------------------------------->

    <cfscript>
    function isValidDebugIP(){
        var oSF = createObject("java", "coldfusion.server.ServiceFactory");
        var oDebugService = oSF.getDebuggingService();

        var ip = cgi.remote_addr;
        if( arrayLen(arguments) GTE 1 ) ip = arguments[1];

        return yesNoFormat(listFindNoCase(oDebugService.iplist.iplist, ip) GT 0);
    }
    
</cfscript>
</cfsilent>

<cfswitch expression="#lCase(thisTag.executionMode)#">
    <cfcase value="start">
        <!---// if user's IP address is not valid, stop process //--->
        <cfif NOT isValidDebugIP()>
            <cfexit method="exittag" />
        </cfif>
    </cfcase>

    <cfcase value="end">
        <!---// if user's IP address is not valid, stop process //--->
        <cfif NOT isValidDebugIP()>
            <cfexit method="exittag" />
        </cfif>
    </cfcase>
</cfswitch>
NOTE:
This tag requires ColdFusion MX or higher.
Categories: HTML/ColdFusion

4 Comments

  • Dan,

    Here's a set of functions that support checking ip's and supports ip wild card's so you only have to provide the first few octets.

    Enjoy.

    function checkIPList(ipSource,ipCheckList){

    var ipValid = false;

        for(listLoop = 1;listLoop LTE ListLen(ipCheckList,",");listLoop = listLoop +1){

            ipValid = Request.checkIP(Trim(ipSource),Trim(ListGetAt(ipCheckList,listLoop,",")));

            if(ipValid)
                return ipValid;

        }

        return ipValid;
    }

    Request.checkIPList = checkIPList;

    function checkIP(ipSource,ipCheck){
        var ipWild = ListContains(ipCheck,"*",".");

        if(ipWild){
            ipSource = Request.ipTrim(ipSource,ipWild);
            ipCheck = Request.ipTrim(ipCheck,ipWild);
        }
        if (ipCheck eq ipSource)
            return true;
        else
            return false;
    }
    Request.checkIP = checkIP;

    function ipTrim(ipNum,trimNum){

        for(x = ListLen(ipNum,"."); x GTE trimNum; x = x - 1){

            ipNum = ListDeleteAt(ipNum,x,".");
        }
        return ipNum;

    }

    Request.ipTrim = ipTrim;
  • I have always used the office isDebugMode() function to check for this rather then relaying on the service factory.
  • Kurt,

    The oSF.getDebuggingService().iplist.iplist appears to be available regardless of whether Debug Mode is turned on--which is why I'm not using isDebugMode() to check the code.

    This allows me to use the tag even when debugging is turned off (which I generally don't run in a stable environment.)
  • Christopher,

    Thanks for the code. Using this will definitely give you a more robust solution, since my simple solution would only work on individual IP addresses.

    I know I have similar code lying around, but I was too lazy to look for it (besides I wanted to keep the source pretty simply so it was easy for novice developers to follow.)

Add Comment

Leave this field empty