Skip to content

Commit

Permalink
Added multi-account support
Browse files Browse the repository at this point in the history
  • Loading branch information
HotCakeX committed May 13, 2024
1 parent eeb433a commit ce15f32
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions NextDNS API/Add Domains to the NextDNS AllowList.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,53 @@ $MicrosoftDomains = [System.Collections.Generic.HashSet[System.String]] @($Micro
Write-Host -Object "$($MicrosoftDomains.Count) domains available on GitHub" -ForegroundColor Magenta

# Get your API key from here: https://my.nextdns.io/account
[System.String]$ApiKey = ''
# NextDNS profile ID
[System.String]$ProfileId = ''

# Creating the header with the API key
[System.Collections.Hashtable]$Header = @{
'X-Api-Key' = $ApiKey
'Content-Type' = 'application/json'
[System.Collections.Hashtable]$NextDNSAccounts = @{
'Account1' = @{
ApiKey = ''
ProfileID = ''
}
'Account2' = @{
ApiKey = ''
ProfileID = ''
}
# Add more as accounts as needed
}

# Send the GET request to the API endpoint to get the allowlist
[System.Object]$AllowListRaw = Invoke-RestMethod -Method 'Get' -Uri "https://api.nextdns.io/profiles/$ProfileId/allowlist" -Headers $Header
foreach ($Account in $NextDNSAccounts.GetEnumerator()) {

# Extract the domains from response - removing the empty lines
$AllowList = [System.Collections.Generic.HashSet[System.String]] @($AllowListRaw.data.id | Where-Object -FilterScript { -NOT ([System.String]::IsNullOrEmpty($_)) })
# Creating the header with the API key
[System.Collections.Hashtable]$Header = @{
'X-Api-Key' = $Account.Value['ApiKey']
'Content-Type' = 'application/json'
}

Write-Host -Object "$($AllowList.Count) domain(s) available in the NextDNS Allowlist" -ForegroundColor Cyan
# Send the GET request to the API endpoint to get the allowlist
[System.Object]$AllowListRaw = Invoke-RestMethod -Method 'Get' -Uri "https://api.nextdns.io/profiles/$($Account.Value['ProfileID'])/allowlist" -Headers $Header

# Compare the two lists
$DomainsNotInAllowList = [System.Collections.Generic.HashSet[System.String]] @($MicrosoftDomains | Where-Object -FilterScript { -NOT ($AllowList.Contains($_)) })
# Extract the domains from response - removing the empty lines
$AllowList = [System.Collections.Generic.HashSet[System.String]] @($AllowListRaw.data.id | Where-Object -FilterScript { -NOT ([System.String]::IsNullOrEmpty($_)) })

Write-Host -Object "$($DomainsNotInAllowList.Count) domain(s) that are not in the allowlist" -ForegroundColor Yellow
Write-Host -Object "$($AllowList.Count) domain(s) available in the NextDNS Allowlist of the account $($Account.Name)" -ForegroundColor Cyan

# Loop through the domains that are not in the allowlist
foreach ($Domain in $DomainsNotInAllowList) {
# Compare the two lists
$DomainsNotInAllowList = [System.Collections.Generic.HashSet[System.String]] @($MicrosoftDomains | Where-Object -FilterScript { -NOT ($AllowList.Contains($_)) })

# Create the body with the domain id
[System.Collections.Hashtable]$Body = @{
'id' = $Domain
}
Write-Host -Object "$($DomainsNotInAllowList.Count) domain(s) are not in the allowlist of the account $($Account.Name)" -ForegroundColor Yellow

# Convert the body to JSON format
[System.String]$JsonBody = $Body | ConvertTo-Json
# Loop through the domains that are not in the allowlist
foreach ($Domain in $DomainsNotInAllowList) {

Write-Host -Object "Adding $Domain to the allowlist" -ForegroundColor Green
# Create the body with the domain id
[System.Collections.Hashtable]$Body = @{
'id' = $Domain
}

# Send the POST request to the API endpoint to add the domain to the allowlist in the NextDNS profile
Invoke-RestMethod -Method Post -Uri "https://api.nextdns.io/profiles/$ProfileId/allowlist" -Headers $Header -Body $JsonBody | Out-Null
# Convert the body to JSON format
[System.String]$JsonBody = $Body | ConvertTo-Json

Write-Host -Object "Adding $Domain to the allowlist for the account $($Account.Name)" -ForegroundColor Green

# Send the POST request to the API endpoint to add the domain to the allowlist in the NextDNS profile
Invoke-RestMethod -Method Post -Uri "https://api.nextdns.io/profiles/$($Account.Value['ProfileID'])/allowlist" -Headers $Header -Body $JsonBody | Out-Null
}
}

0 comments on commit ce15f32

Please sign in to comment.