-
Notifications
You must be signed in to change notification settings - Fork 8
/
hook-dns-transip-api.php
executable file
·174 lines (151 loc) · 4.23 KB
/
hook-dns-transip-api.php
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
#!/usr/bin/php
<?php
// Include domainservice
require_once('Transip/DomainService.php');
require_once('dns.inc.php');
require_once('config.inc.php');
echo (new DateTime)->format(DATE_RFC850)." Executing hook with params: ".implode(", ", $argv)."\n";
$action = $argv[1]; // Letsencrypt.sh hook action
$domain = sizeof($argv) > 2 ? $argv[2] : null; // Full domain-name to work on
$zone = null;
$domains_pattern = implode("|", $my_domains);
$domains_pattern = str_replace(".", "\.", $domains_pattern);
$pattern = '/^((.*)\.)?(' . $domains_pattern . ')$/';
if( $action === "deploy_cert" )
{
$file1 = $argv[3];
$file2 = $argv[4];
$result = `cat "$file1" "$file2" > "$(dirname "$file1")/privcert.pem"`;
exit(0);
}
if( $domain) {
if( preg_match($pattern, $argv[2], $matches ) )
{
// echo "Host-part: ", $matches[1], "\n";
$subdomain = $matches[2];
rtrim($subdomain, ".");
// echo "Domain-part: ", $matches[2], "\n";
$zone = $matches[3];
}
else
{
echo "No domain-name and/or subdomain found\n";
exit(1);
}
// What record should we update
if ( empty($subdomain))
{
$acmedomain = "_acme-challenge";
}
else
{
$acmedomain = "_acme-challenge.$subdomain";
}
// Retrieve all DNS records for the zone
$dnsEntries = Transip_DomainService::getInfo($zone)->dnsEntries;
}
if( $action === "deploy_challenge" )
{
$tokenfile = $argv[3];
$tokenvalue = $argv[4];
$found = 0;
echo "DEPLOY_CHALLENGE with tokenvalue: '$tokenvalue' on zone: '$zone' and record: '$acmedomain'\n\n";
$dnsEntries[] = new Transip_DnsEntry("$acmedomain", $ttl, Transip_DnsEntry::TYPE_TXT, $tokenvalue);
try
{
// Commit the changes to the TransIP DNS servers
Transip_DomainService::setDnsEntries($zone, $dnsEntries);
echo "DNS updated\n\n";
}
catch(SoapFault $f)
{
echo "DNS not updated. " . $f->getMessage() , "\n\n";
exit(1);
}
$continue = 0;
while( $continue < sizeof($dns_servers) )
{
foreach( $dns_servers as $dns_server)
{
$dns_query=new DNSQuery($dns_server);
$dns_result=$dns_query->Query("$acmedomain.$zone","TXT");
if ( ($dns_result===false) || ($dns_query->error!=0) ) // error occured
{
echo $dns_query->lasterror;
exit();
}
//Process Results
$dns_result_count=$dns_result->count; // number of results returned
if( $dns_result_count >= 1) // Allow for multiple results
{
foreach( $dns_result->results as $dns_txt )
{
if ( $tokenvalue == $dns_txt->data )
{
$continue++;
}
}
}
}
if ($continue < sizeof($dns_servers) )
{
echo "Result not ready, retrying in $sleeptime seconds, waiting for DNS record to become known.\n";
sleep($sleeptime);
$continue++;
}
}
echo "Sleeping an additional 5 minute, waiting for DNS record to become known.";
sleep(300);
}
elseif( $action === "clean_challenge" )
{
$tokenfile = $argv[3];
$tokenvalue = $argv[4];
$found = 0;
foreach ($dnsEntries as $key => $dnsEntry)
{
if($dnsEntry->name == "$acmedomain")
{
unset($dnsEntries[$key]);
$found++;
}
}
if( $found > 0)
{
$dnsEntries = array_values($dnsEntries);
try
{
// Commit the changes to the TransIP DNS servers
Transip_DomainService::setDnsEntries($zone, $dnsEntries);
echo "DNS updated\n\n";
}
catch(SoapFault $f)
{
echo "DNS not updated. ", $f->getMessage() , "\n\n";
echo "Please remove manually\n\n";
}
}
else
{
echo "No need to update, record not found\n";
exit(0);
}
}
elseif( $action === "unchanged_cert" )
{
}
elseif( $action === "startup_hook" )
{
}
elseif( $action === "exit_hook" )
{
}
elseif( $action === "this_hookscript_is_broken__dehydrated_is_working_fine__please_ignore_unknown_hooks_in_your_script" )
{
}
else
{
echo "Unknown hook action, '$action'";
}
exit(0);
?>