-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_ip.py
46 lines (37 loc) · 1.48 KB
/
check_ip.py
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
import requests
API_STATUS_GRANTED = 'Granted'
API_STATUS_DENIED = 'Denied'
def log(msg):
msg = 'Multifactor OpenVPN AS: {}'.format(msg)
syslog.syslog(msg)
def get_country(ip_address):
try:
# Using ipapi to get geolocation information
response = requests.get(f'https://ipapi.co/{ip_address}/json/')
response.raise_for_status() # Raise an error for bad responses
data = response.json()
# Return the country name
return data.get("country_name")
except requests.RequestException as e:
print(f"Error retrieving location for IP {ip_address}: {e}")
return None
def check_ip_and_disconnect(ip_address):
country = get_country(ip_address)
if country is None:
print("Could not determine the country.")
return
print(f"IP Address: {ip_address} is from {country}.")
if country != "United States":
print("Disconnecting... User is not in the USA.")
# Here you would implement your disconnect logic
else:
print("User is in the USA. Connection allowed.")
if __name__ == "__main__":
try:
# Make a request to ipify to get the public IP
public_ip = requests.get('https://api.ipify.org').text
print("Your Public IP Address is:", public_ip)
check_ip_and_disconnect(public_ip)
except requests.RequestException as e:
print(f"Error retrieving public IP address: {e}")
# Replace this with the actual IP address you want to check