-
Notifications
You must be signed in to change notification settings - Fork 12
/
sync.py
executable file
·119 lines (91 loc) · 5.11 KB
/
sync.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
from bottle import Bottle, run, route, template, request, response, abort, static_file, default_app, redirect
import urllib.parse
import config
import util
import sigutil
import json
@route('/sync_index', method='GET')
def sync_index():
return template('sync_index.tpl')
@route('/sync_post', method='POST')
def sync_post():
# This only works for the hosting website over the supported protocol
if request.headers['Host'] != config.hosting_website or request.urlparts.scheme != config.protocol:
return abort(404)
# Get the domain/message and validate
domain = request.forms.get('domain')
subdomain = request.forms.get('subdomain')
message = request.forms.get('message')
if domain == None or domain == '' or not util.is_valid_hostname(domain) or message == None or message == '' or not util.is_valid_message(message):
return template('invalid_data.tpl')
# See if the DNS Provider supports domain connect
json_data, txt, error_message = util.get_domainconnect_json(domain)
if json_data == None:
return template('no_domain_connect.tpl', {'reason' : str(error_message)})
width = 750
if 'width' in json_data:
width = json_data['width']
height = 750
if 'height' in json_data:
height =json_data['height']
# See if our templates are supported
check_url1 = json_data['urlAPI'] + '/v2/domainTemplates/providers/' + config.provider + '/services/' + config.template1
check_url2 = json_data['urlAPI'] + '/v2/domainTemplates/providers/' + config.provider + '/services/' + config.template2
if not util.check_template(check_url1) or not util.check_template(check_url2):
return template('no_domain_connect.tpl', {'reason' : 'Missing template support'})
dns_message_data = util.dns_message_data(message)
# Generate the query string for synchronous calls
qs = 'domain=' + urllib.parse.quote(domain, '') + '&RANDOMTEXT=' + urllib.parse.quote(dns_message_data) + '&IP=' + urllib.parse.quote(config.ip, '')
if subdomain != '' and subdomain != None:
qs = qs + '&host=' + urllib.parse.quote(subdomain, '')
# Create the URL to configure template 1
synchronousUrl1 = json_data['urlSyncUX'] + '/v2/domainTemplates/providers/' + config.provider + '/services/' + config.template1 + '/apply?' + qs
# Create the URL to configure template2. Template 2 needs a singature
sig = sigutil.generate_sig(config.priv_key, qs)
synchronousSignedUrl2 = json_data['urlSyncUX'] + '/v2/domainTemplates/providers/' + config.provider + '/services/' + config.template2 + '/apply?' + qs + '&sig=' + urllib.parse.quote(sig, '') + '&key=_dck1'
# Generate the redirect uri
redirect_uri = config.protocol + "://" + config.hosting_website + "/sync_confirm?domain=" + domain + "&subdomain=" + subdomain
# Query string with the redirect
qsRedirect = qs + "&redirect_uri=" + urllib.parse.quote(redirect_uri, '')
# Create the URL to configure template 1 with a redirect back
synchronousRedirectUrl1 = json_data['urlSyncUX'] + '/v2/domainTemplates/providers/' + config.provider + '/services/' + config.template1 + '/apply?' + qsRedirect
# Create the URL to configure template 2 with a redirect back. Template 2 needs a signature
sigRedirect = sigutil.generate_sig(config.priv_key, qsRedirect)
synchronousSignedRedirectUrl2 = json_data['urlSyncUX'] + '/v2/domainTemplates/providers/' + config.provider + '/services/' + config.template2 + '/apply?' + qsRedirect + '&sig=' + urllib.parse.quote(sigRedirect, '') + '&key=_dck1'
return template('sync_post.tpl',
{
'txt': txt,
'json': json.dumps(json_data),
'check_url1' : check_url1,
'check_url2' : check_url2,
'domain': domain,
'providerName' : json_data['providerName'],
'width': width,
'height': height,
'synchronousUrl1' : synchronousUrl1,
'synchronousSignedUrl2' : synchronousSignedUrl2,
'qs': qs,
'sig': sig,
'synchronousRedirectUrl1' : synchronousRedirectUrl1,
'synchronousSignedRedirectUrl2' : synchronousSignedRedirectUrl2,
'qsRedirect' : qsRedirect,
'sigRedirect': sigRedirect
})
@route('/sync_confirm', method='GET')
def sync_confirm():
# This only works for the hosting website over the supported protocol
if request.headers['Host'] != config.hosting_website or request.urlparts.scheme != config.protocol:
return abort(404)
domain = request.query.get('domain')
subdomain = request.query.get('subdomain')
error = request.query.get('error')
if error != None and error != '':
return template('sync_error.tpl',
{
'error': error
})
return template('sync_confirm.tpl',
{
'domain': domain,
'subdomain': subdomain
})