-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonedotcom.ps1
316 lines (271 loc) · 9.98 KB
/
onedotcom.ps1
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<#
.SYNOPSIS
Adds and deletes TXT record to your one.com domain with win-wacs.
.DESCRIPTION
Works with win-wacs to add and delete TXT records for use with Let's encrypt certificates.
.NOTES
File Name : onedotcom.ps1
Version : 1.0 (Initial version)
Author : Morten Hansen
.LINK
https://github.com/morhans/win-acme_dns_one.com
.EXAMPLE
onedotcom.ps1 create <Identifier> <RecordName> <Token>
.EXAMPLE
onedotcom.ps1 delete <Identifier> <RecordName> <Token>
.EXAMPLE
onedotcom.ps1 setcred
#>
[CmdletBinding()]
param(
[Parameter(Position=0)]
[string]$action,
[Parameter(Position=1)]
[string]$Identifier,
[Parameter(Position=2)]
[string]$RecordName,
[Parameter(Position=3)]
[string]$Token
)
$global:apiRoot = 'https://www.one.com/admin'
function Add-DnsRecord {
[CmdletBinding()]
param(
[Parameter(Mandatory,Position=0)]
[string]$Identifier,
[Parameter(Mandatory,Position=1)]
[string]$RecordName,
[Parameter(Mandatory,Position=2)]
[string]$TxtValue,
[Parameter(Mandatory,Position=3)]
[object]$LoginSession
)
# add the new TXT record
$topdomain = getTopDomain $Identifier
$RecordName = $RecordName.Substring(0,$RecordName.Length-$topdomain.Length-1)
$PostData = @{type="dns_custom_records";attributes=@{priority=0;ttl=600;type="TXT";prefix=$RecordName;content=$TxtValue}}|ConvertTo-Json
$url = "$apiRoot/api/domains/$topdomain/dns/custom_records"
Write-Debug $url
Write-Verbose "Adding $RecordName with value $TxtValue to $Identifier"
try {
$webrequest = Invoke-WebRequest -Uri $url -Body $PostData -WebSession $LoginSession -Method POST -UseBasicParsing -ContentType "application/json" -ErrorAction Stop
}
catch [System.Net.WebException] {
Write-Verbose "An exception was caught: $($_.Exception.Message)"
$_.Exception.Response
}
#Check if adding was a success
$Result = ConvertFrom-Json $webrequest.content
if ([String]::IsNullOrWhiteSpace($Result.result.data.id)) {
throw "TXT record for $RecordName ws not added!"
}
<#
.SYNOPSIS
Add a DNS TXT record to One.com.
.DESCRIPTION
Use One.com api to add a TXT record to a One.com DNS zone.
.PARAMETER Identifier
DNS name to be added a TXT record.
.PARAMETER RecordName
The fully qualified name of the TXT record.
.PARAMETER TxtValue
The value of the TXT record.
.EXAMPLE
Add-DnsRecord 'example.com' '_acme-challenge.site1' 'asdfqwer12345678' '$SessionID'
Adds a TXT record for the specified site with the specified value.
#>
}
function Remove-DnsRecord {
[CmdletBinding()]
param(
[Parameter(Mandatory,Position=0)]
[string]$Identifier,
[Parameter(Mandatory,Position=1)]
[string]$RecordName,
[Parameter(Mandatory,Position=2)]
[string]$TxtValue,
[Parameter(Mandatory,Position=3)]
[object]$LoginSession
)
# check for an existing record
$topdomain = getTopDomain $Identifier
$RecordName = $RecordName.Substring(0,$RecordName.Length-$topdomain.Length-1)
$RecId = Find-RecordId $topdomain $RecordName $TxtValue $LoginSession
if ([String]::IsNullOrWhiteSpace($RecId)) {
throw "Unable to find record id for $RecordName"
}
# remove the txt record if it exists
Write-Verbose "Removing $RecordName with value $TxtValue from $Identifier"
$url = "$apiRoot/api/domains/$topdomain/dns/custom_records/$RecId"
Write-Debug $url
try {
$webrequest = Invoke-WebRequest -Uri $url -WebSession $LoginSession -Method DELETE -UseBasicParsing -ContentType "application/json" -ErrorAction Stop
}
catch [System.Net.WebException] {
Write-Verbose "An exception was caught: $($_.Exception.Message)"
$_.Exception.Response
}
# Check if removal was a success
if (!($webrequest.content -eq '{"result":null,"metadata":null}')) {
throw "Unable to delete record $RecordName!"
}
<#
.SYNOPSIS
Remove a DNS TXT record from One.com.
.DESCRIPTION
Use One.com api to remove a TXT record to a One.com DNS zone.
.PARAMETER Identifier
DNS name to have TXT record deleted.
.PARAMETER RecordName
The fully qualified name of the TXT record.
.PARAMETER TxtValue
The value of the TXT record.
.EXAMPLE
Remove-DnsRecord Example 'example.com' '_acme-challenge.site1' 'asdfqwer12345678' '$SessionID'
Removes a TXT record for the specified site with the specified value.
#>
}
function EncryptCred {
#Ask for credentials
$Credential = Get-Credential -Message "Login and password for One.com"
#Save credentials
$Credential | Export-CliXml -Path "${env:\userprofile}\One.com.dat"
<#
.SYNOPSIS
Saves One.com credentials to file.
.DESCRIPTION
Encrypt credentials to use on One.com login. Credentials is only readable by the creating user.
#>
}
############################
# Helper Functions
############################
function getTopDomain {
[CmdletBinding()]
param(
[Parameter(Mandatory,Position=0)]
[string]$Identifier
)
$pieces = $Identifier.Split(".")
for ($i=1; $i -lt ($pieces.Count-1); $i++) {
$topdomain = "$( $pieces[$i..($pieces.Count-1)] -join '.' )"
}
if (([String]::IsNullOrWhiteSpace($topdomain))) {
$topdomain = $Identifier
}
return $topdomain
}
function getCustomRecords {
[CmdletBinding()]
param(
[Parameter(Mandatory,Position=0)]
[string]$Identifier,
[Parameter(Mandatory,Position=1)]
[object]$LoginSess
)
$url = "$apiroot/api/domains/$topdomain/dns/custom_records"
Write-Debug $url
try {
$webrequest = Invoke-WebRequest -Uri $url -Method Default -WebSession $LoginSess -UseBasicParsing -ErrorAction Stop
}
catch [System.Net.WebException] {
Write-Verbose "An exception was caught: $($_.Exception.Message)"
$_.Exception.Response
}
$jsonObj = ConvertFrom-Json $webrequest.content
return $jsonObj.result.data
}
function Find-RecordId {
[CmdletBinding()]
param(
[Parameter(Mandatory,Position=0)]
[string]$Identifier,
[Parameter(Mandatory,Position=1)]
[string]$RecordName,
[Parameter(Mandatory,Position=2)]
[object]$TxtValue,
[Parameter(Mandatory,Position=3)]
[object]$LoginSess
)
$RecObj = getCustomRecords $Identifier $LoginSess
ForEach($rec in $RecObj) {
if ($rec.attributes.prefix -eq $RecordName -and $rec.attributes.content -eq $TxtValue -and $rec.attributes.type -eq "TXT") {
$RecId = $rec.id
}
}
Write-Debug "ID (Empty if not found): $RecId"
return $RecId
}
function DecryptCred {
if (!(Test-Path "${env:\userprofile}\One.com.dat")) {
throw "Login and password not set (run with option setcred to set them."
}
$Credential = Import-CliXml -Path "${env:\userprofile}\One.com.dat"
return $Credential
}
function onedotcom_login {
$SearchString = '<form id="kc-form-login" class="Login-form login autofill" onsubmit="login.disabled = true; return true;" action="'
$odcCred = DecryptCred
$usr = $odcCred.UserName
$pwd = $odcCred.GetNetworkCredential().Password
if (([String]::IsNullOrWhiteSpace($usr)) -or ([String]::IsNullOrWhiteSpace($pwd))) {
throw "Login and/or password are not set correctly. Reissue with option setcred"
}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
try {
$webrequest = Invoke-WebRequest -Uri $apiRoot -Method Default -SessionVariable websession -UseBasicParsing -ErrorAction Stop
$pos = $webrequest.content.LastIndexOf($SearchString) + $SearchString.Length
$resulttxt = $webrequest.content.Substring($pos)
}
catch [System.Net.WebException] {
Write-Verbose "An exception was caught: $($_.Exception.Message)"
$_.Exception.Response
}
try {
$pos = $resulttxt.IndexOf('"')
$LoginUrl = $resulttxt.Substring(0, $pos)
$LoginUrl = $LoginUrl.replace('&','&')
$formFields = @{username=$usr;password=$pwd;credentialId=''}
$webrequest = Invoke-WebRequest -Uri $LoginUrl -Body $formFields -WebSession $websession -Method POST -UseBasicParsing -ErrorAction Stop
}
catch [System.Net.WebException] {
Write-Verbose "An exception was caught: $($_.Exception.Message)"
$_.Exception.Response
}
Remove-Variable usr, pwd
return $websession
}
############################
# Main program
############################
$ProgressPreference = 'SilentlyContinue'
switch ($action) {
"create" {
if (!([String]::IsNullOrWhiteSpace($Identifier)) -and !([String]::IsNullOrWhiteSpace($RecordName)) -and !([String]::IsNullOrWhiteSpace($Token))) {
$sess = onedotcom_login
Add-DnsRecord $Identifier $RecordName $Token $Sess
}
else {
Write-Error "Argument(s) Identifier, RecordName and/or Token were not applied!"
}
}
"delete" {
if (!([String]::IsNullOrWhiteSpace($Identifier)) -and !([String]::IsNullOrWhiteSpace($RecordName)) -and !([String]::IsNullOrWhiteSpace($Token))) {
$sess = onedotcom_login
Remove-DnsRecord $Identifier $RecordName $Token $Sess
}
else {
Write-Error "Argument(s) Identifier, RecordName and/or Token were not applied!"
}
}
"setcred" {
EncryptCred
}
Default {
Write-Error "No or wrong arguments were passed. Valid arguments are create, delete and setcred.`n
Syntax:`n
onedotcom.ps1 create <Identifier> <RecordName> <Token>`n
onedotcom.ps1 delete <Identifier> <RecordName> <Token>`n
onedotcom.ps1 setcred (Set the credentials for one.com)"
}
}