This repository has been archived by the owner on Feb 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
dyndns53.py
212 lines (161 loc) · 5.3 KB
/
dyndns53.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from __future__ import print_function
import os
import sys
import csv
import StringIO
from collections import namedtuple
from flask import Flask, request, abort
from flask.ext.basicauth import BasicAuth
from boto import connect_route53, connect_s3
from boto.route53.exception import DNSServerError
from boto.s3.key import Key
from boto.s3.connection import OrdinaryCallingFormat
# Server database management and setup.
DATABASE = {}
def initialise_database():
global DATABASE
try:
data = download_database()
input = StringIO.StringIO(data)
reader = csv.reader(input)
for row in reader:
hostname, password = row
DATABASE[hostname] = password
except Exception:
DATABASE.clear()
raise
# Utility functions for interfacing with Amazon services.
def download_database():
bucket_name = os.environ['DYNDNS_BUCKET']
connection = connect_s3(calling_format=OrdinaryCallingFormat())
bucket = connection.get_bucket(bucket_name)
bucket_data = Key(bucket)
bucket_data.key = os.environ['DYNDNS_DATABASE']
return bucket_data.get_contents_as_string()
def upload_database(data):
bucket_name = os.environ['DYNDNS_BUCKET']
connection = connect_s3(calling_format=OrdinaryCallingFormat())
bucket = connection.get_bucket(bucket_name)
bucket_data = Key(bucket)
bucket_data.key = os.environ['DYNDNS_DATABASE']
bucket_data.set_contents_from_string(data)
def register_ip(domain, hostname, ipaddr):
connection = connect_route53()
zone = connection.get_zone(domain)
record = zone.get_a(hostname)
if record is not None:
old_ipaddr = record.resource_records[0]
if old_ipaddr == ipaddr:
return False
try:
zone.update_a(hostname, ipaddr, 300)
except DNSServerError:
zone.add_a(hostname, ipaddr, 300)
else:
zone.add_a(hostname, ipaddr, 300)
return True
# Flask application implementing dynamic DNS server.
app = Flask(__name__)
#app.debug = True
class BasicAuthDatabase(BasicAuth):
def check_credentials(self, username, password):
if not DATABASE:
initialise_database()
if username not in DATABASE:
return False
if password != DATABASE[username]:
return False
return True
basic_auth = BasicAuthDatabase(app)
@app.route('/register_ip')
@basic_auth.required
def register_ip_handler():
hostname = request.authorization.username
domain = '.'.join(hostname.split('.')[1:])
ipaddr = request.environ.get('HTTP_X_FORWARDED_FOR')
if not ipaddr:
ipaddr = request.remote_addr
register_ip(domain, hostname, ipaddr)
return ''
@app.route('/check_ip')
def check_ip_handler():
remote_addr = request.environ.get('HTTP_X_FORWARDED_FOR')
if not remote_addr:
remote_addr = request.remote_addr
return remote_addr
# Command line administration commands.
_commands = {}
def command(name, options='', description='', hidden=False,
log_intercept=True):
def wrapper(callback):
callback.name = name
callback.options = options
callback.description = description
callback.hidden = hidden
callback.log_intercept = log_intercept
_commands[name] = callback
return callback
return wrapper
def usage(name):
details = _commands[name]
print('Usage: dyndns53 %s %s' % (name, details.options))
@command('help', '[command]', hidden=True)
def help(args):
if not args:
print('Usage: dyndns53 command [options]')
print()
print("Type 'dyndns53 help <command>'", end='')
print("for help on a specific command.")
print()
print("Available commands are:")
commands = sorted(_commands.keys())
for name in commands:
details = _commands[name]
if not details.hidden:
print(' ', name)
else:
name = args[0]
if name not in _commands:
print("Unknown command '%s'." % name, end=' ')
print("Type 'dyndns53 help' for usage.")
else:
details = _commands[name]
print('Usage: dyndns53 %s %s' % (name, details.options))
if details.description:
print()
print(details.description)
@command('upload-database', 'input_file',
"""Upload the database file to S3.""")
def upload_database_command(args):
if len(args) == 0:
usage('upload-database')
sys.exit(1)
input_file = args[0]
with open(input_file, 'r') as input:
data = input.read()
upload_database(data)
@command('download-database', '[output_file]',
"""Download the database file from S3.""")
def download_database_command(args):
if len(args) > 0:
output_file = args[0]
else:
output_file = None
data = download_database()
if output_file:
with open(output_file, 'w') as output:
output.write(data)
else:
print(data)
def main():
try:
if len(sys.argv) > 1:
command = sys.argv[1]
else:
command = 'help'
callback = _commands[command]
except Exception:
print("Unknown command '%s'." % command, end='')
print("Type 'dyndns53 help' for usage.")
sys.exit(1)
callback(sys.argv[2:])