My Visitor

Like this site!

Wanna help other found this site? +1 us now!

Google +1 Button Privacy Policy

Synology Tags

My WebHost

I recommend hostgator, here is why.

Synology modding series 29 – Restricting WordPress accessible by home dynamic ip only

Update Aug 22 2011

The solution is redesigned so that the htaccess file is pushing from NAS to web hosting account. This greatly simplifed the overall process and resolved the deadlock situation.

I have a few private wordpress blog hosting on some web hosting provider and want to protect my private blog to be accessible only by home dynamic ip address.

This require rewriting htaccess file on web hosting account for every ip changes.

This may not related to Synology, but I need the help of DS207+ in order to perform the job by some automated scripts.

Solution

Firstly, a perl script sync-htaccess.pl is required to host on the NAS.

The script will

  1. Get External IP address of the NAS by calling the getip.php hosting on web hosting account
  2. Read the htaccess template, find the line @@@DYNAMIC@@@ and replace the line with ‘allow from 1.2.3.4′ (where 1.2.3.4 is the external IP address returned by getip.php)
  3. Write the actual .htaccess files to temporary directory.
  4. Upload the actual .htaccess files to web hosting account by synchronizing the temporary directory to the web hosting account. (using rsync over ssh)

The reason I am using rsync over ssh instead of scp/sftp to upload files because rsync and ssh is already provided by Synology, no addition ipkg package is required.

So, go ahead to create the perl script to /opt/usr/local/bin/sync-htaccess.pl


#!/usr/bin/perl

# v0.4
# Aug 22 2011
# Modified to run at NAS instead of webhost
# v0.31
# May 15 2011
# Add timeout and retries flag to wget command to prevent infinite lookup
# v0.3
# May 14 2011
# host not returning ip address, using wget instead
# v0.2
# May 3 2011
# Fix the host command to query type A record only

$version="0.4";

print "Executing sync-htaccess.pl version=$version\n";

##################################
### configuration begin here ###

# define sub domain 1
$site1output="/opt/tmp/sync-htaccess/.htaccess";
$site1template = "/opt/etc/sync/blog_htaccess_template";

# if you have more sub domain, copy the two lines and paste here
#$siteXXXoutput = "/opt/tmp/sync-htaccess/XXX/.htaccess";
#$siteXXXtemplate = "/opt/etc/sync/xxx_htaccess_template";

$logfile = "/opt/var/log/rsync-htaccess.log";
$sshkey = "/volume1/private/id_rsa";
$sshport = "22";
$source = "/opt/tmp/sync-htaccess/";
$remote = "/home/account/public_html";

$getipurl = "http://www.mydomain/getip.php";
$accid = "account_id";
$accdomain= "mydomain.com";

### END of CONFIGURATION, DO NOT MODIFY BELOW ####
##################################################

# get ip address
$ip=`wget --timeout=10 --tries=1 -qO - $getipurl | sed 's/^ *\(.*\) *\$/\1/'`;

print "ip address $ip\n";

# write to output file, if you have more subdomain, duplicate the line below and modified the variables
writeHtaccess($ip,$site1template,$site1output);

# upload to webhosting
system("/usr/syno/bin/rsync -avz --log-file=$logfile -e 'ssh -i $sshkey -p $sshport' $source $accid\@$accdomain:$remote");

print "done\n";

sub writeHtaccess{
my ($myip,$mytemplate,$myhtaccess) = @_;

open(TEMPLATE, $mytemplate) || die("Could not open file!");
@raw_data=