-
Notifications
You must be signed in to change notification settings - Fork 0
/
locality.ps1
77 lines (59 loc) · 2.33 KB
/
locality.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
<#
Author: Ainsley Cabading
Script: locality.ps1
Function:
- Checks for a device's network locality by extracting a JSON response from http://ip-api.com/json/.
- Iterates through the UN Sanctions list to determine whether traffic is to be blocked or not.
Result: localityCheck [Pass/Fail]
#>
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~GetLocation Function~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Sends web request to API for geo-information and stores JSON response in a PSCustomObject for future reference.
function GetGeolocation {
param (
[string]$URL
)
try {
$response = Invoke-RestMethod -Uri $URL
if ($response.status -eq "success") {
return [PSCustomObject]@{
Status = $response.status
Country = $response.country
}
}
} catch {
# Handling exceptions
Write-Host "An error has occured."
}
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~CheckUNSanction Function~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Iterates through list of UN Sanctioned countries. Checks if client geolocation is within sanctions list
function CheckUNSanction {
param (
[string]$country,
[array]$sanctionList
)
$result = "Pass"
foreach ($item in $sanctionList) {
if ($item -eq $country) {
$result = "Fail"
}
}
return $result
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~M A I N~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Defining main localityCheck variable
$localityCheck = "Fail"
# Defining the URL of the geolocation API
$ipAPI = "http://ip-api.com/json/?fields=status,message,country,query"
# Defining API response value from GetGeolocation
$response = GetGeolocation -URL $ipAPI
$country = $response.country
# Defining array of country names that are under the UN Sanctions list in UNSanction array.
$UNSanction = "Central African Republic", "Democratic Republic of Congo", "Eritrea", "Guinea-Bissau", "Iran", "Iraq", "Lebanon", "Libya", "Mali", "North Korea", "Russia", "Somalia", "South Sudan", "Sudan", "Yemen"
# Defining response from CheckUNSanction to determine value of localityCheck
$sanctionCheck = CheckUNSanction -country $country -sanctionList $UNSanction
if ($sanctionCheck -eq "Pass") {
$localityCheck = "Pass"
}
Write-Host "locality.ps1: Result of Locality Check: $($localityCheck)" -ForegroundColor DarkBlue
Write-Output $localityCheck