forked from pklaus/python-inwx-xmlrpc
/
example.dyndns-by-webcall.py
executable file
·135 lines (107 loc) · 4.76 KB
/
example.dyndns-by-webcall.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
#!/usr/bin/env python
# -*- encoding: UTF8 -*-
# author: Philipp Klaus, philipp.klaus →AT→ gmail.com
# author: Markus Roth, mail →AT→ rothmark.us
# author: Sergej Domme, sergej →AT→ domme.it
# This file is part of python-inwx-xmlrpc.
#
# python-inwx-xmlrpc is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# python-inwx-xmlrpc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with python-inwx-xmlrpc. If not, see <http://www.gnu.org/licenses/>.
#####################################################################
###### This is an example of how to use the inwx class to #######
###### update a DNS entry via the InterNetworX XMLRPC API. #######
###### It is a wsgi script, so you can put it on your #######
###### WebServer with the wsgi module. You can call it from #######
###### everywhere to update the DNS record with the callers #######
###### IP address #######
import sys, os
sys.path.append(os.path.dirname(__file__))
from inwx import domrobot
from configuration import get_account_data, get_domain_update
config_file = os.path.dirname(__file__) + '/python-inwx-xmlrpc.cfg'
def update_dns(new_ip):
record_types = ['A', 'AAAA']
api_url, username, password = get_account_data(True, config_file)
domain, subdomain, _ = get_domain_update(True, config_file)
# Instantiate the inwx class (does not connect yet but dispatches calls to domrobot objects with the correct API URL
inwx_conn = domrobot(api_url, username, password, 'en', False)
nsentries = inwx_conn.nameserver.info({'domain': domain})
# Filter entries for subdomain
records = [record for record in nsentries['record'] if subdomain == record['name']]
if not records:
status = "404 Failed"
return "Subdomain {0} not in list of nameserver entries".format(subdomain), status
for record in records:
record_type = record['type']
if record_type not in record_types:
status = "404 Failed"
return "Unsupported record type: {0}".format(record_type), status
if new_ip != record['content']:
try:
inwx_conn.nameserver.updateRecord({'id': record['id'], 'content': new_ip, 'ttl': 3600})
status = "200 OK"
return "Updating record {0} from {1} to {2}".format(record['name'], record['content'], new_ip), status
except Exception as e:
status = "404 Failed"
return "Failed {0} record of {1} from {2} to {3}::::{4}".format(record_type, record['name'], record['content'], new_ip, e.message), status
else:
status = "200 OK"
return "Same IP", status
def application(environ, start_response):
ip = environ['REMOTE_ADDR']
response_body, status = update_dns(ip)
response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
'''
###
### Configuration exmaple for Apache
###
WSGIPythonPath /usr/share/python-inwx-xmlrpc
<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName dyndns.example.com
ServerPath /
<Location />
Options -Indexes +FollowSymLinks
Require all granted
</Location>
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
DirectoryIndex index.php
ErrorLog logs/dyndns_error.log
CustomLog logs/dyndns_access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster@example.com
ServerName dyndns.example.com
ServerPath /
DocumentRoot "/usr/share/python-inwx-xmlrpc"
SSLEngine on
SSLCipherSuite AES256+EECDH:AES256+EDH
SSLProtocol All -SSLv2 -SSLv3
SSLCertificateFile /etc/pki/tls/certs/cert.pem
SSLCertificateKeyFile /etc/pki/tls/private/mail.key
SSLCertificateChainFile /etc/pki/tls/certs/chain.pem
WSGIScriptAlias / /usr/share/python-inwx-xmlrpc/example.dyndns-by-webcall.py
<Directory /usr/share/python-inwx-xmlrpc>
AuthType Basic
AuthName "Password Required"
AuthUserFile /etc/httpd/.htpasswd
Require valid-user
</Directory>
ErrorLog logs/dyndns_error.log
CustomLog logs/dyndns_access.log combined
</VirtualHost>
'''