#!/usr/bin/perl

$i=0;

#use strict;
#use vars qw($ua $rq $date_time);
#use vars qw($sec $min $hour $mday $mon $year $wday $yday $isdst);
#use vars qw($myip $firewall $target $username $password $version);
#use vars qw($pass $url $sock @result $result $code);

use LWP::UserAgent;
use MIME::Base64 ();
use Sys::Syslog qw(:DEFAULT setlogsock);
use IO::Socket;


##### ƒ‚ƒfƒ€‚©‚çÚ‘±’†‚ÌIPƒAƒhƒŒƒX‚ðŽæ“¾ #####

$ua = LWP::UserAgent->new;
$rq = HTTP::Request->new(GET => 'http://192.168.0.1/cgi-bin/main.cgi?cc_webname=STATUS');
$rq->authorization_basic('››››››››', '››››››››');
foreach (split(/\n/, $ua->request($rq)->as_string)) {
last if (($myip) = m|^<TR><TD NOWARP>ADSL IP</TD><TD NOWARP>:(.*?)</TD>|);
}



##### Œ»Ý‚Ì“úŽž‚ÆIPƒAƒhƒŒƒX‚ð•Û‘¶ #####

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
$mon++;
$year = $year + 1900;
$date_time = sprintf("%04d/%02d/%02d %02d:%02d:%02d", $year, $mon, $mday, $hour,$min, $sec);

open (OUT,">./adsl.new");
print OUT "date=\'".$date_time."\'\n";
print OUT "IP=\'".$myip."\'\n";
close OUT;

print "$date_time\n";
print "ADSL IP is $myip \n";



##### IPƒAƒhƒŒƒX‚̃`ƒFƒbƒN #####
open (IN,"./adsl.js");
while (<IN>) {
chomp;
$adsl_data[$i] = substr($_, 4, length($_) - 5);
++$i;
}
close IN;
print "Old IP is $adsl_data[1] \n";

if ($myip eq $adsl_data[1]) {
print "IP is NOT changed.\n";
print "Don't need hammernode.pl.\n";
system ("rm ./adsl.new");
exit(0);
} else {
print "IP was Changed!\n";
system ("mv ./adsl.new ./adsl.js");
}



##### hammernode.pl‚̃Xƒ^[ƒg #####

print "Starting hammernode.pl\n";
# YOUR hn.org name and password go here.
$username = "ryo";
$password = "››››››››";

# If you're behind a firewall or HTTP proxy, set this to 1.
# If you're not sure, set it to 1; that's the safer setting anyway.
# If you KNOW you're not behind a firewall or proxy, set to 0.
$firewall = 1;

# Okay, that's all you'll need to configure *here*. You're not done,
# though... You still need to configure this to run automatically, and to
# use the correct IP address. For Linux users with pppd, the easiest way
# to put a line like this in /etc/ppp/ip-up :
#
# /usr/local/sbin/hammernode.pl $4
#
# If that doesn't make much sense, see `man pppd' which details what
# parameters pppd sends to ip-up.
#
# A clever trick for dhcpcd users... put this in your
# /etc/dhcpcd/dhcpcd-eth0.exe file:
#
# source /etc/dhcpc/dhcpcd-eth0.info && /usr/local/sbin/hammernode.pl $IPADDR
#
# (I discovered this was necessary -- my cable modem company puts an
# "invisible" HTTP proxy in between me and the 'net, and my domains were
# being assigned the address of the proxy.
#
# (C)2000-2001 David E. Smith <dave@bureau42.com> and released to the
# public under the terms of the GNU General Public License.
#
# Modified by Daniel Hagan <dhagan@colltech.com> on 4/2001 to use IO::Socket,
# Syslog, and some error checking. Now logs all output to daemon facility.
#
# Other changes made/suggested by Aurelien Beaujean <aure@dagobah.eu.org>
# Sorry, but I can't type the accent over the first "e" in Aurelien.
#
###############


# If you don't let syslogd listen on an inet port, uncomment this line.
#
#setlogsock 'unix';

#openlog ("hammernode", 'pid', 'daemon');

#$myip = shift;

if ($firewall && !$myip) {
# syslog ('err', "FATAL: IP required as command line argument when \$firewall is set to true.");
print "FATAL: IP required as command line argument when \$firewall is set to true.\n";
exit(1);
}

$target = "dup.hn.org";
$version = "v0.22pl0";
$pass = MIME::Base64::encode_base64("$username:$password");

if ($firewall == 1) {
$url = "/vanity/update/?VER=1&IP=$myip" ;
} else {
$url = "/vanity/update/?VER=1" ;
}

#syslog ('info', 'Setting domain to %s.', $myip);
print "Setting domain to $myip\n";
$sock = new IO::Socket::INET(
PeerAddr => "$target",
PeerPort => 'http(80)'
);

if (!$sock) {
# syslog('err', "FATAL: Connect to $target, port 80, failed: %m");
print "FATAL: Connect to $target, port 80, failed\n";
exit(1);
}

$sock->autoflush(1);

$sock->print("GET $url HTTP/1.0\r\n");
$sock->print("User-Agent: hammenode.pl $version\r\n");
$sock->print("Host: $target\r\n");
$sock->print("Authorization: Basic $pass\r\n\r\n");

# Legacy sleep from v0.21pl2. May be necessary on some slow PC platforms.
# If you are having problems, try uncommenting this first.
#
#sleep 5;

@result = $sock->getlines();

undef $sock; #Close the socket

$result = join '', @result;
$result =~ m/DDNS_Response_Code=(\d+)/i;
$code = $1;

if ($code == 101) {
# syslog ('info', "Succeeded in setting domain to $myip.");
print "Succeeded in setting domain to $myip\n";
exit(0);
} else {
# syslog ('notice', "Received DDNS Reponse Code $code, probably failed.");
print "Received DDNS Reponse Code $code, probably failed.\n";
exit(1);
}