DnsPlugins/HurricaneElectric.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
function Add-DnsTxtHurricaneElectric {
    [CmdletBinding(DefaultParameterSetName='Secure')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword','')]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$TxtValue,
        [Parameter(ParameterSetName='Secure',Mandatory)]
        [pscredential]$HECredential,
        [Parameter(ParameterSetName='Insecure',Mandatory)]
        [string]$HEUsername,
        [Parameter(ParameterSetName='Insecure',Mandatory)]
        [string]$HEPassword,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    Connect-HurricaneElectric @PSBoundParameters

    $zone = Find-HEZone $RecordName
    Write-Verbose "Found domain $($zone.domain) ($($zone.id))"

    $rec = Get-HETxtRecord $zone.id $RecordName $TxtValue

    if ($rec) {
        Write-Debug "Record $RecordName already contains $TxtValue. Nothing to do."
    } else {
        # add the new record
        Write-Verbose "Adding a TXT record for $RecordName with value $TxtValue"

        # build the form body
        $addBody = "account=&menu=edit_zone&Type=TXT&hosted_dns_zoneid=$($zone.id)&hosted_dns_recordid=&hosted_dns_editzone=1&Priority=&Name=$RecordName&Content=$TxtValue&TTL=300&hosted_dns_editrecord=Submit"
        $iwrArgs = @{
            Uri = 'https://dns.he.net/'
            Method = 'Post'
            Body = $addBody
            WebSession = $script:HESession
            ErrorAction = 'Stop'
        }

        try {
            $response = Invoke-WebRequest @iwrArgs @script:UseBasic
        } catch { throw }

        $reStatus = '"dns_status"[^>]+>(?<status>[^<]+)<'

        if ($response.Content -match $reStatus) {
            $status = $matches['status']
            if ($status -notlike 'Successfully added new record*') {
                Write-Warning "Unexpected result status while adding record: $status"
            }
        } else {
            Write-Debug "No dns_status div found after add."
        }
    }

    <#
    .SYNOPSIS
        Add a DNS TXT record to Hurricane Electric
 
    .DESCRIPTION
        Add a DNS TXT record to Hurricane Electric
 
    .PARAMETER RecordName
        The fully qualified name of the TXT record.
 
    .PARAMETER TxtValue
        The value of the TXT record.
 
    .PARAMETER HECredential
        Username and password for Hurricane Electric. This PSCredential option can only be used from Windows or any OS running PowerShell 6.2 or later.
 
    .PARAMETER HEUsername
        Username for Hurricane Electric. This should be used from non-Windows OSes running PowerShell 6.0-6.1.
 
    .PARAMETER HEPassword
        Password for Hurricane Electric. This should be used from non-Windows OSes running PowerShell 6.0-6.1.
 
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
 
    .EXAMPLE
        Add-DnsTxtHurricaneElectric '_acme-challenge.example.com' 'txtvalue' -HECredential (Get-Credential)
 
        Adds a TXT record using after providing credentials in a prompt.
 
    .EXAMPLE
        Add-DnsTxtHurricaneElectric '_acme-challenge.example.com' 'txtvalue' -HEUsername 'myusername' -HEPassword 'mypassword'
 
        Adds a TXT record using plain text credentials.
    #>

}

function Remove-DnsTxtHurricaneElectric {
    [CmdletBinding(DefaultParameterSetName='Secure')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword','')]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=1)]
        [string]$TxtValue,
        [Parameter(ParameterSetName='Secure',Mandatory)]
        [pscredential]$HECredential,
        [Parameter(ParameterSetName='Insecure',Mandatory)]
        [string]$HEUsername,
        [Parameter(ParameterSetName='Insecure',Mandatory)]
        [string]$HEPassword,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )

    Connect-HurricaneElectric @PSBoundParameters

    $zone = Find-HEZone $RecordName
    Write-Verbose "Found domain $($zone.domain) ($($zone.id))"

    $rec = Get-HETxtRecord $zone.id $RecordName $TxtValue

    if ($rec) {
        # remove the record
        Write-Verbose "Removing TXT record for $RecordName with value $TxtValue"

        # build the form body
        $delBody = "menu=edit_zone&hosted_dns_zoneid=$($zone.id)&hosted_dns_recordid=$($rec.id)&hosted_dns_editzone=1&hosted_dns_delrecord=1&hosted_dns_delconfirm=delete"
        $iwrArgs = @{
            Uri = 'https://dns.he.net/'
            Method = 'Post'
            Body = $delBody
            WebSession = $script:HESession
            ErrorAction = 'Stop'
        }

        try {
            $response = Invoke-WebRequest @iwrArgs @script:UseBasic
        } catch { throw }

        $reStatus = '"dns_status"[^>]+>(?<status>[^<]+)<'

        if ($response.Content -match $reStatus) {
            $status = $matches['status']
            if ($status -ne 'Successfully removed record.') {
                Write-Warning "Unexpected result status while removing record: $status"
            }
        } else {
            Write-Debug "No dns_status div found after add."
        }

    } else {
        Write-Debug "Record $RecordName with value $TxtValue doesn't exist. Nothing to do."
    }

    <#
    .SYNOPSIS
        Remove a DNS TXT record from Hurricane Electric
 
    .DESCRIPTION
        Remove a DNS TXT record from Hurricane Electric
 
    .PARAMETER RecordName
        The fully qualified name of the TXT record.
 
    .PARAMETER TxtValue
        The value of the TXT record.
 
    .PARAMETER HECredential
        Username and password for Hurricane Electric. This PSCredential option can only be used from Windows or any OS running PowerShell 6.2 or later.
 
    .PARAMETER HEUsername
        Username for Hurricane Electric. This should be used from non-Windows OSes running PowerShell 6.0-6.1.
 
    .PARAMETER HEPassword
        Password for Hurricane Electric. This should be used from non-Windows OSes running PowerShell 6.0-6.1.
 
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
 
    .EXAMPLE
        Remove-DnsTxtHurricaneElectric '_acme-challenge.example.com' 'txtvalue' -HECredential (Get-Credential)
 
        Removes a TXT record using after providing credentials in a prompt.
 
    .EXAMPLE
        Remove-DnsTxtHurricaneElectric '_acme-challenge.example.com' 'txtvalue' -HEUsername 'myusername' -HEPassword 'mypassword'
 
        Removes a TXT record using plain text credentials.
    #>

}

function Save-DnsTxtHurricaneElectric {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromRemainingArguments)]
        $ExtraParams
    )
    <#
    .SYNOPSIS
        Not required
 
    .DESCRIPTION
        This provider does not require calling this function to save DNS records.
 
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
    #>

}

############################
# Helper Functions
############################

# Adapted from
# https://github.com/Neilpang/acme.sh/blob/master/dnsapi/dns_he.sh
# Web scraping is obviously brittle and can easily break if the site owner changes their markup.
# But without a well-defined API, this is the only option for automated record manipulation.

function Connect-HurricaneElectric {
    [CmdletBinding(DefaultParameterSetName='Secure')]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword','')]
    param(
        [Parameter(ParameterSetName='Secure',Mandatory)]
        [pscredential]$HECredential,
        [Parameter(ParameterSetName='Insecure',Mandatory)]
        [string]$HEUsername,
        [Parameter(ParameterSetName='Insecure',Mandatory)]
        [string]$HEPassword,
        [Parameter(ValueFromRemainingArguments)]
        $ExtraConnectParams
    )

    # no need to login again if we already have an authenticated session
    if ($script:HESession) {
        Write-Debug "Using cached HE.net session"
        return
    }

    # get plain text versions of the pscredential we can work with
    if ('Secure' -eq $PSCmdlet.ParameterSetName) {
        $HEUsername = $HECredential.UserName
        $HEPassword = $HECredential.GetNetworkCredential().Password
    }

    # URI escape the credentials
    $userEscaped = [uri]::EscapeDataString($HEUsername)
    $passEscaped = [uri]::EscapeDataString($HEPassword)

    $siteRoot = 'https://dns.he.net/'

    $loginParams = @{
        Uri = $siteRoot
        Method = 'Post'
        Body = "email=$userEscaped&pass=$passEscaped"
        ErrorAction = 'Stop'
    }

    try {
        # Do an initial GET to establish the session cookie
        Invoke-WebRequest $siteRoot -SessionVariable 'HESession' @script:UseBasic -EA Stop | Out-Null

        # try to login
        $response = Invoke-WebRequest @loginParams -WebSession $HESession @script:UseBasic
    }
    catch { throw }

    if ($response.Content -like '*>Incorrect<*') {
        throw "Invalid he.net login credentials. Please check username and password."
    }

    # save the session variable for later
    $script:HESession = $HESession
}

function Get-HEDomains {
    [CmdletBinding()]
    param()

    $url = 'https://dns.he.net/'
    $reDomains = [regex]'onclick="delete_dom\(this\);" name="(?<domain>[-_.a-z0-9]+)" value="(?<id>\d+)"'

    Write-Debug "Querying domains page"
    try {
        $response = Invoke-WebRequest $url -WebSession $script:HESession @script:UseBasic -EA Stop
    } catch { throw }
    $domains = $reDomains.Matches($response.Content)

    if ($domains.Count -eq 0) {
        throw "Unable to find any domains."
    } else {
        Write-Debug "$($domains.Count) domain matches found."
    }

    $domains | ForEach-Object {
        [pscustomobject]@{domain=$_.Groups['domain'].value; id=$_.Groups['id'].value}
    }
}

function Get-HETxtRecord {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory,Position=0)]
        [string]$DomainID,
        [Parameter(Mandatory,Position=1)]
        [string]$RecordName,
        [Parameter(Mandatory,Position=2)]
        [string]$TxtValue
    )

    $url = "https://dns.he.net/?hosted_dns_zoneid=$DomainID&menu=edit_zone&hosted_dns_editzone"
    $reVal = [regex]'data="&quot;(?<val>[^&]+)&quot;'
    $reName = [regex]'deleteRecord\(''(?<id>\d+)'',''(?<fqdn>[-_.a-z0-9]+)'''

    Write-Debug "Querying records for domain $DomainID"
    $response = Invoke-WebRequest $url -WebSession $script:HESession @script:UseBasic

    # split the content by line breaks so we can loop through it looking for the data
    $lines = $response.Content -split "`r?`n"

    for ($i=0; $i -lt $lines.Count; $i++) {

        if ($lines[$i] -like '*rrlabel TXT*') {
            # the next line should have the record value
            if ($lines[$i+1] -notmatch $reVal) {
                Write-Debug "Failed to parse TXT record value from line following 'rrlabel TXT'"
                continue
            }
            $recVal = $matches['val']

            # the line after that should have the record name and ID
            if ($lines[$i+2] -notmatch $reName) {
                Write-Debug "Failed to parse TXT record name/ID from line following 'rrlabel TXT'"
                continue
            }
            $recFQDN = $matches['fqdn']
            $recID = $matches['id']

            # send back a match if we found it
            if ($recFQDN -eq $RecordName -and $recVal -eq $TxtValue) {
                [pscustomobject]@{
                    fqdn = $recFQDN
                    id = $recID
                    value = $recVal
                }
                return
            }

            $i += 2
        }
    }
}

function Find-HEZone {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string]$RecordName
    )

    # setup a module variable to cache the record to zone ID mapping
    # so it's quicker to find later
    if (!$script:HERecordZones) { $script:HERecordZones = @{} }

    # check for the record in the cache
    if ($script:HERecordZones.ContainsKey($RecordName)) {
        return $script:HERecordZones.$RecordName
    }

    # grab the set of owned domains for this account
    $domains = Get-HEDomains

    # Search for the zone from longest to shortest set of FQDN pieces.
    $pieces = $RecordName.Split('.')
    for ($i=1; $i -lt ($pieces.Count-1); $i++) {
        $zoneTest = $pieces[$i..($pieces.Count-1)] -join '.'
        Write-Debug "Checking $zoneTest"
        if ($zoneTest -in $domains.domain) {
            $script:HERecordZones.$RecordName = $domains | Where-Object { $zoneTest -eq $_.domain }
            return $script:HERecordZones.$RecordName
        }
    }

    throw "No zone found for $RecordName"
}