Skip to content

Commit

Permalink
Merge branch 'release/v0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
eternalharvest committed Mar 24, 2015
2 parents e91f448 + 7cf02c2 commit 263bb5e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bash_completion.d/r53update
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/bash
#
# R53Update Dynamic DNS Updater v0.2.0
# R53Update Dynamic DNS Updater v0.3.0
# (C)2014 Takuya Sawada All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
47 changes: 44 additions & 3 deletions r53update
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# R53Update Dynamic DNS Updater v0.2.0
# R53Update Dynamic DNS Updater v0.3.0
# (C)2014 Takuya Sawada All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -27,6 +27,7 @@ import dns.resolver
import dns.exception
import logging
import logging.handlers
import netifaces
import exceptions

try:
Expand Down Expand Up @@ -134,6 +135,23 @@ class R53UpdateApp(App):
profiles = self.__parent.ctx.session.available_profiles
return (x for x in profiles if x.startswith(prefix))

##
# Netifaces Completer
class NetifacesCompleter(object):
def __init__(self, parent):
self.__parent = parent

def __call__(self, prefix, **kwargs):
return (x for x in netifaces.interfaces() if x.startswith(prefix))

# Resolver Completer
class ResolverCompleter(object):
def __init__(self, parent):
self.__parent = parent

def __call__(self, prefix, **kwargs):
return (x for x in self.__parent._gipresolvers if x.startswith(prefix))

##
# Global IP Resolver
class GlobalIP_Resolver(object):
Expand Down Expand Up @@ -167,6 +185,18 @@ class R53UpdateApp(App):
resolver.nameservers = self.__resolveServerName(self._resolvername)
return resolver.query(self._hostname, 'A')[0].to_text()

class NETIFACES_GlobalIP_Resolver(GlobalIP_Resolver):
def __init__(self, app):
super(R53UpdateApp.NETIFACES_GlobalIP_Resolver, self).__init__(app)

def resolveGlobalIP(self):
try:
inet = netifaces.ifaddresses(self._app._opts.iface)[netifaces.AF_INET]
except Exception as e:
raise Exception("%s: no inet address found" % self._app._opts.iface)

return inet[0]['addr']

def _pre_init(self):
super(R53UpdateApp, self)._pre_init()
self.ctx = R53UpdateApp.Context()
Expand All @@ -189,12 +219,15 @@ class R53UpdateApp(App):
self._gipresolvers['ipecho.net'] = R53UpdateApp.HTTP_GlobalIP_Resolver(self, 'http://ipecho.net/plain')
self._gipresolvers['icanhazip.com'] = R53UpdateApp.HTTP_GlobalIP_Resolver(self, 'http://icanhazip.com')
self._gipresolvers['opendns.com'] = R53UpdateApp.DNS_GlobalIP_Resolver(self, 'myip.opendns.com', 'resolver1.opendns.com')
self._gipresolvers['localhost'] = R53UpdateApp.NETIFACES_GlobalIP_Resolver(self)

# optional argument
self._parser.add_argument('--profile', type=str, metavar='PROFILE', default='',
help='プロファイル名').completer = R53UpdateApp.ProfileCompleter(self)
self._parser.add_argument('--resolver', type=str, metavar='RESOLVER',
default='opendns.com', help='グローバルIP 取得方法')
default='opendns.com', help='グローバルIP 取得方法').completer = R53UpdateApp.ResolverCompleter(self)
self._parser.add_argument('--iface', type=str, metavar='IFACE',
help='インターフェイス名').completer = R53UpdateApp.NetifacesCompleter(self)
self._parser.add_argument('--dns', nargs='+', type=str, metavar='DNS',
default=['8.8.8.8', '8.8.4.4'], help='default: 8.8.8.8, 8.8.4.4')
self._parser.add_argument('--ttl', type=int, metavar='TTL',
Expand All @@ -221,6 +254,14 @@ class R53UpdateApp(App):
if opts.debug:
self.logger.setLevel(logging.DEBUG)

if opts.resolver == 'localhost' and not opts.iface:
raise Exception("you must specify network interface with '--iface' option")

if opts.iface:
if opts.iface not in netifaces.interfaces():
raise Exception("interface name '%s' not found" % opts.iface)
self._opts.resolver = 'localhost'

def __get_global_ip(self):
self.logger.debug('resolving global ip adreess with \'%s\'', self._opts.resolver)
return self._gipresolvers[self._opts.resolver].resolveGlobalIP()
Expand Down Expand Up @@ -286,7 +327,7 @@ class R53UpdateApp(App):

def show_version(self):
print >>sys.stderr, "Copyrights (c)2014 Takuya Sawada All rights reserved."
print >>sys.stderr, "Route53Update Dynamic DNS Updater 0.2.0"
print >>sys.stderr, "Route53Update Dynamic DNS Updater 0.3.0"

##
# DOCUMENT
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ argparse==1.3.0
boto==2.36.0
botocore==0.96.0
dnspython==1.12.0
netifaces==0.10.4
awscli==1.7.15

0 comments on commit 263bb5e

Please sign in to comment.