Commit bee89d8f authored by Yuanle Song's avatar Yuanle Song
Browse files

init commit

parents
Loading
Loading
Loading
Loading

README

0 → 100644
+37 −0
Original line number Diff line number Diff line
Date: 2012-05-10
Time-stamp: <2012-05-10>

the ddclient package seems *weak*.
it does not keep the domain up-to-date.
    //since by default there is no error report or log, I can't know the reason.
    //the thing I know is the IP for my domain gets out-dated.
it does not keep a log.
    //ddclient can log to syslog. default is no log though.
it does not report errors.

* features
** works by running command in crontab
** send email to local admin when update IP fail.
   send email to local admin when network is down. i.e. can't do dig or curl
   at all.
** log to syslog, default target is /var/log/ddclient-http.log
   auto create syslog filter rule.
   auto create logrotate rule.
** easy installable.
   use make install or make install-deb.

   'make deb' will create a .deb package.
** 

* dependencies
  low level:
  shell

  high level:
  curl
  dig
  make

* TODO split config from source code.
  config variables should be set in /etc/ddclient-http.conf
  and it can be sourced by the source code.

doc/why-this-client

0 → 100644
+32 −0
Original line number Diff line number Diff line
* before I write configs and install script for this http client.
  let's first see if I can config ddclient the way I wish it to be.

  enable log:
  -syslog
  -facility ?

  enable retry:
  -retry

  can I use ifconfig.me to get IP?
  currently I set in /etc/ddclient.conf
  use=web
  but I don't know which provider does it use.
  -web provider|url default: dyndns

** test get IP
   /usr/sbin/ddclient -options query,noexec -use=web -web http://ifconfig.me

   no good. it tries to request a new binding.

   still no good. it requires root. query seems print binded IP on ddns
   server. not the IP that is going to be used?

** when using -use=cmd, what if cmd fail?
   -use=cmd                    : obtain IP from the -cmd {external-command}.

   -cmd program          : obtain IP address from by calling {program}.
   -cmd-skip pattern     : skip any IP addresses before 'pattern' in the output of {cmd}.

** I believe it's not wise to use ddclient.
   so I will continue this project.

src/Makefile

0 → 100644
+20 −0
Original line number Diff line number Diff line
default: install
install: install-dependence
	install bin/ddclient-http /usr/local/bin/
	install -m 644 etc/logrotate /etc/logrotate.d/ddclient-http
	install -m 644 etc/rsyslog-filter /etc/rsyslog.d/ddclient-http.conf
	install -m 644 etc/crontab /etc/cron.d/ddclient-http
# log file must be created before restarting rsyslog.
	install -m 640 -o root -g adm /dev/null /var/log/ddclient-http.log
# note: reload will not work. a full restart is required.
	/etc/init.d/rsyslog restart
install-dependence:
	./utils/install-dependence
uninstall:
	rm -f /usr/local/bin/ddclient-http /etc/logrotate.d/ddclient-http /etc/rsyslog.d/ddclient-http.conf /etc/cron.d/ddclient-http
	echo -e "you may optionally restart rsyslog:\n    \
/etc/init.d/rsyslog restart"
purge: uninstall
	rm -f /var/log/ddclient-http.log*

.PHONY: install install-dependence uninstall purge

src/bin/ddclient-http

0 → 100755
+98 −0
Original line number Diff line number Diff line
#!/bin/sh

# this is designed to run in crontab.
# run 'make install' to install this program.

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# exit codes
GET_MYIP_FAILED=1
DIG_FAILED=2
UPDATE_REQUEST_FAILED=3
UPDATE_REJECTED=4

# stores responses from remote DDNS provider's server.  usually it will be
# very short, like a word or two. but if there is a server error, it could be
# long.
HTTP_RESPONSE=/tmp/ddclient-http-api.html

# which host to update and it's credentials.
USERNAME=s12nospam@hotmail.com
PASSWORD=songyl
HOSTNAME=sylecn01.dnsd.me

# Usage: log [MESSAGE..]
log() {
	logger -t ddclient_http "$@"
}

# set MYIP variable to my internet IP address.
# Returns true on success, false on failure.
get_myip() {
	MYIP=`curl -s http://ifconfig.me`
	CURL_EXIT_CODE=$?
	if [ "$CURL_EXIT_CODE" -eq 0 ]; then
		return 0
	else
		log "get_myip() failed. curl exit code is $CURL_EXIT_CODE."
		exit $GET_MYIP_FAILED
	fi
}

# returns true if domain is outdated. returns false otherwise.
require_update() {
	OLDIP=`dig +short sylecn01.dnsd.me`
	# TODO dig can fail when network is down or DNS server is blocked by
	# GFW. check dig command's exit code.
	#
	# dig always return 0! if no A record is found, OLDIP will be empty.
	if [ -z "$OLDIP" ]; then
		log "Can't get current binding IP. dig failed."
		exit $DIG_FAILED
	fi
	if [ "$OLDIP" = "$MYIP" ]; then
		log "$HOSTNAME is up-to-date. IP is $OLDIP."
		return 1
	else
		log "$HOSTNAME is out-dated. Old IP is $OLDIP. New IP is $MYIP."
		return 0
	fi
}

do_update() {
	log "update $HOSTNAME using IP $MYIP"
	curl -s -o "$HTTP_RESPONSE" -u "$USERNAME":"$PASSWORD" \
		"https://www.dnsdynamic.org/api/?hostname=$HOSTNAME&myip=$MYIP"
	# check curl EXIT CODES
	CURL_EXIT_CODE=$?
	case $CURL_EXIT_CODE in
		0) log "curl ok." ;;
		6) log "curl error: Couldn't resolve host." ;;
		7) log "curl error: Failed to connect to host." ;;
		28) log "curl error: Operation timeout." ;;
		*) log "curl error: exit code $CURL_EXIT_CODE.";;
	esac
	if  [ $CURL_EXIT_CODE -ne 0 ]; then
		exit $UPDATE_REQUEST_FAILED
	fi

	# check HTTP_RESPONSE
	case "$HTTP_RESPONSE" in
		good) log "good: update accepted."
			return 0 ;;
		nochg) log "nochg: IP not changed." ;;
		badauth) log "badauth: user or password incorrect." ;;
		abuse) log "abuse: hostname is blocked for update abuse." ;;
		dnserr) log "dnserr: DNS error encountered." ;;
		911) log "911: server problem." ;;
		*) log "Error: return code is ${HTTP_RESPONSE}." ;;
	esac
	exit $UPDATE_REJECTED
}

# main
if get_myip; then
	if require_update; then
		do_update
	fi
fi

src/etc/crontab

0 → 100644
+6 −0
Original line number Diff line number Diff line
# /etc/cron.d/anacron: crontab entries for the anacron package

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

*/2 * * * *    sylecn    /usr/local/bin/ddclient-http
Loading