Detecting Restricted Countries
Determine whether a visitor's country (using ISO 3166-1 alpha-2 codes) is in a list of countries of restricted group.
// Sanctioned countries
const sanctionedCountries = ["RU", "BY", "IR", "KP", "AF", "ML", "NE"];
// Fetch geolocation data
const response = await fetch(url, { headers: headers });
const data = await response.json();
// Extract IP and country code
const ipAddress = data["ip"];
const countryCode = data["locationData"]["countryCode"];
if (sanctionedCountries.includes(countryCode)) {
console.log(`${ipAddress} is from a sanctioned country. Country Code: ${countryCode}`);
} else {
console.log(`${ipAddress} is from a non-sanctioned country. Country Code: ${countryCode}`);
}package main
import (
"encoding/json"
"fmt"
)
func isSanctioned(code string, list []string) bool {
for _, c := range list {
if c == code {
return true
}
}
return false
}
func main() {
sanctioned := []string{"RU", "BY", "IR", "KP", "AF", "ML", "NE"}
// Assuming data is already fetched and parsed
var data map[string]interface{}
// data = ... (JSON response)
ip := data["ip"].(string)
country := data["locationData"].(map[string]interface{})["countryCode"].(string)
if isSanctioned(country, sanctioned) {
fmt.Printf("%s is from a sanctioned country: %s\n", ip, country)
} else {
fmt.Printf("%s is from a non-sanctioned country: %s\n", ip, country)
}
}# Sanctioned countries
sanctioned_countries = ["RU", "BY", "IR", "KP", "AF", "ML", "NE"]
# Fetch geolocation data
response = requests.get(url, headers=headers)
data = response.json()
# Storing the results data
ip_address = data["ip"]
country_code = data["locationData"]["countryCode"]
if country_code in sanctioned_countries:
print(f"{ip_address} is from a sanctioned country. Country Code: {country_code}")
else:
print(f"{ip_address} is from a non-sanctioned country. Country Code: {country_code}")Updated 2 days ago
What’s Next
