From 56a85c090f5461beab85d82c05ebed01b23995a9 Mon Sep 17 00:00:00 2001 From: Violet Date: Thu, 2 May 2024 00:11:34 +0300 Subject: [PATCH] Removed old scripts that are no longer necessary --- ...on for JSON and newtonsoft.json module.ps1 | 97 ------------------- ...ex for JSON and newtonsoft.json Module.ps1 | 90 ----------------- 2 files changed, 187 deletions(-) delete mode 100644 NextDNS API/Misc/Stream the logs - using String manipulation for JSON and newtonsoft.json module.ps1 delete mode 100644 NextDNS API/Misc/Stream the logs - using regex for JSON and newtonsoft.json Module.ps1 diff --git a/NextDNS API/Misc/Stream the logs - using String manipulation for JSON and newtonsoft.json module.ps1 b/NextDNS API/Misc/Stream the logs - using String manipulation for JSON and newtonsoft.json module.ps1 deleted file mode 100644 index e1a16ce..0000000 --- a/NextDNS API/Misc/Stream the logs - using String manipulation for JSON and newtonsoft.json module.ps1 +++ /dev/null @@ -1,97 +0,0 @@ -# Define the API key and the profile ID -$apiKey = '' -$profileId = '' - -# Define the URL for streaming the logs -# https://nextdns.github.io/api/#streaming -$url = "https://api.nextdns.io/profiles/$profileId/logs/stream" - -# Create a header with the API key as a hashtable -$header = @{ - 'X-Api-Key' = $apiKey -} - -# Create an empty NameValueCollection -$headerNVC = [System.Collections.Specialized.NameValueCollection]::new() - -# Loop over the hashtable keys and values and add them to the NameValueCollection -foreach ($key in $header.Keys) { - $value = $header[$key] - $headerNVC.Add($key, $value) -} - -# Create a web request object -$webrequest = [System.Net.HttpWebRequest]::Create($url) - -# Set the header with the API key -$webrequest.Headers.Add($headerNVC) - -# Set the timeout to infinite -$webrequest.Timeout = -1 - -# Get the web response object -$response = $webrequest.GetResponse() - -# Get the response stream -$responseStream = $response.GetResponseStream() - -# Create a stream reader object -$streamReader = [System.IO.StreamReader]::new($responseStream) - - -# Import the Newtonsoft.Json library -Import-Module newtonsoft.json - -# Define a custom function to test the JSON validity -function Test-JsonCustom { - param ( - [Parameter(Mandatory = $true)] - [string]$Json - ) - - # Try to parse the JSON string using Newtonsoft.Json library - try { - $result = [Newtonsoft.Json.JsonConvert]::DeserializeObject($Json) - return $true - } - catch { - return $false - } -} - -# Loop through the stream and process each line as a JSON object -while (-not $streamReader.EndOfStream) { - # Read one line from the stream - $line = $streamReader.ReadLine() - - # Split the line by colon and space characters - $parts = $line.Split(': ', 2) - - # Check if the line has two parts - if ($parts.Count -eq 2) { - # Use the second part as the JSON response - $jsonResponse = $parts[1] - - # Check if the JSON response is not empty - if ($jsonResponse -ne '') { - # Test if the JSON response is a valid JSON object using the custom function - $isValidJson = Test-JsonCustom $jsonResponse - - # Check if the JSON response is a valid JSON object - if ($isValidJson) { - # Convert the JSON response to a hashtable - $log = $jsonResponse | ConvertFrom-Json -AsHashtable - - # Select only the properties that you are interested in - $log = $log | Select-Object timestamp, domain, root, encrypted, protocol, clientIp, status - - # Do something with the log object, such as displaying it or filtering it - Write-Output $log - } - } - } -} - -# Close the response and the stream -$response.Close() -$responseStream.Close() diff --git a/NextDNS API/Misc/Stream the logs - using regex for JSON and newtonsoft.json Module.ps1 b/NextDNS API/Misc/Stream the logs - using regex for JSON and newtonsoft.json Module.ps1 deleted file mode 100644 index 0b7c01d..0000000 --- a/NextDNS API/Misc/Stream the logs - using regex for JSON and newtonsoft.json Module.ps1 +++ /dev/null @@ -1,90 +0,0 @@ -# Define the API key and the profile ID -$apiKey = '' -$profileId = '' - -# Define the URL for streaming the logs -# https://nextdns.github.io/api/#streaming -$url = "https://api.nextdns.io/profiles/$profileId/logs/stream" - -# Create a header with the API key as a hashtable -$header = @{ - 'X-Api-Key' = $apiKey -} - -# Create an empty NameValueCollection -$headerNVC = [System.Collections.Specialized.NameValueCollection]::new() - -# Loop over the hashtable keys and values and add them to the NameValueCollection -foreach ($key in $header.Keys) { - $value = $header[$key] - $headerNVC.Add($key, $value) -} - -# Create a web request object -$webrequest = [System.Net.HttpWebRequest]::Create($url) - -# Set the header with the API key -$webrequest.Headers.Add($headerNVC) - -# Set the timeout to infinite -$webrequest.Timeout = -1 - -# Get the web response object -$response = $webrequest.GetResponse() - -# Get the response stream -$responseStream = $response.GetResponseStream() - -# Create a stream reader object -$streamReader = [System.IO.StreamReader]::new($responseStream) - -# Import the Newtonsoft.Json library -Import-Module newtonsoft.json - -# Define a custom function to test the JSON validity -function Test-JsonCustom { - param ( - [Parameter(Mandatory = $true)] - [string]$Json - ) - - # Try to parse the JSON string using Newtonsoft.Json library - try { - $result = [Newtonsoft.Json.JsonConvert]::DeserializeObject($Json) - return $true - } - catch { - return $false - } -} - -# Loop through the stream and process each line as a JSON object -while (-not $streamReader.EndOfStream) { - # Read one line from the stream - $line = $streamReader.ReadLine() - - # Remove the prefix from the line - $line = $line -replace '^(id|data): ' - - # Check if the line is not empty - if ($line -ne '') { - # Test if the line is a valid JSON object using the custom function - $isValidJson = Test-JsonCustom $line - - # Check if the line is a valid JSON object - if ($isValidJson) { - # Convert the line to a hashtable - $log = $line | ConvertFrom-Json -AsHashtable - - # Select only the properties that you are interested in - $log = $log | Select-Object timestamp, domain, root, encrypted, protocol, clientIp, status - - # Do something with the log object, such as displaying it or filtering it - Write-Output $log - } - } -} - -# Close the response and the stream -$response.Close() -$responseStream.Close()