-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFunction1.cs
76 lines (62 loc) · 2.97 KB
/
Function1.cs
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
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Rest.Azure.Authentication;
using Microsoft.Azure.Management.Dns;
using Microsoft.Azure.Management.Dns.Models;
using System.Collections.Generic;
namespace DNSUpdater
{
public static class DNSUpdater
{
[FunctionName("DNSUpdater")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("IP Update requested");
string remoteip = req.Query["remoteip"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
remoteip = remoteip ?? data?.remoteip;
string dnsZone = "domain.com";
string hostname = "vpn";
string resourceGroupName = "DNS";
string appID = "appid";
string appSecret = "appsecret";
string subscriptionID = "subscriptionid";
string tenantID = "tenantid";
// Build the service credentials and DNS management client
Microsoft.Rest.ServiceClientCredentials serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantID, appID, appSecret);
DnsManagementClient dnsClient = new DnsManagementClient(serviceCreds)
{
SubscriptionId = subscriptionID
};
var recordSet = dnsClient.RecordSets.Get(resourceGroupName, dnsZone, hostname, RecordType.A);
// clean up older records
if (recordSet.ARecords.Count > 0)
{
recordSet.ARecords.Clear();
}
recordSet.ARecords.Add(new ARecord(remoteip));
// Update the record set in Azure DNS
// Note: ETAG check specified, update will be rejected if the record set has changed in the meantime
recordSet = await dnsClient.RecordSets.CreateOrUpdateAsync(resourceGroupName, dnsZone, hostname, RecordType.A, recordSet, recordSet.Etag);
// update TXT
recordSet = dnsClient.RecordSets.Get(resourceGroupName, dnsZone, hostname, RecordType.TXT);
recordSet.TxtRecords[0].Value.Clear();
recordSet.TxtRecords[0].Value.Add(DateTime.Now.ToString());
// Update the record set in Azure DNS
recordSet = await dnsClient.RecordSets.CreateOrUpdateAsync(resourceGroupName, dnsZone, hostname, RecordType.TXT, recordSet, recordSet.Etag);
return remoteip != null
? (ActionResult)new OkObjectResult($"OK")
: new BadRequestObjectResult("Unable to update IP");
}
}
}