diff --git a/osinaweb/db.sqlite3 b/osinaweb/db.sqlite3 index 3dfb6436..0458854c 100644 Binary files a/osinaweb/db.sqlite3 and b/osinaweb/db.sqlite3 differ diff --git a/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc b/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc index 4627dc72..8fba627e 100644 Binary files a/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc and b/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc differ diff --git a/osinaweb/osichat/__pycache__/urls.cpython-310.pyc b/osinaweb/osichat/__pycache__/urls.cpython-310.pyc index ea2bf730..cf027c11 100644 Binary files a/osinaweb/osichat/__pycache__/urls.cpython-310.pyc and b/osinaweb/osichat/__pycache__/urls.cpython-310.pyc differ diff --git a/osinaweb/osichat/__pycache__/views.cpython-310.pyc b/osinaweb/osichat/__pycache__/views.cpython-310.pyc index 979e480e..0c366823 100644 Binary files a/osinaweb/osichat/__pycache__/views.cpython-310.pyc and b/osinaweb/osichat/__pycache__/views.cpython-310.pyc differ diff --git a/osinaweb/osichat/urls.py b/osinaweb/osichat/urls.py index 5ac52c4c..50aa8737 100644 --- a/osinaweb/osichat/urls.py +++ b/osinaweb/osichat/urls.py @@ -3,5 +3,5 @@ from . import views urlpatterns = [ - + path('get-client-ip/', views.get_client_ip, name='get-client-ip'), ] diff --git a/osinaweb/osichat/views.py b/osinaweb/osichat/views.py index b0fc71d7..3a82fd19 100644 --- a/osinaweb/osichat/views.py +++ b/osinaweb/osichat/views.py @@ -1,3 +1,18 @@ from django.shortcuts import render from django.http import JsonResponse +import requests + +# Create your views here. + +def get_client_ip(request): + client_ip = request.META.get('REMOTE_ADDR', '') + + try: + response = requests.get(f'http://ipinfo.io/{client_ip}/json') + data = response.json() + country = data.get('country', 'Unknown') + except Exception as e: + country = "Unknown" + + return JsonResponse({'ip': client_ip, 'country': country}) \ No newline at end of file diff --git a/osinaweb/osinaweb/__pycache__/settings.cpython-310.pyc b/osinaweb/osinaweb/__pycache__/settings.cpython-310.pyc index 51626b9e..3eb05540 100644 Binary files a/osinaweb/osinaweb/__pycache__/settings.cpython-310.pyc and b/osinaweb/osinaweb/__pycache__/settings.cpython-310.pyc differ diff --git a/osinaweb/osinaweb/settings.py b/osinaweb/osinaweb/settings.py index 3eb4444b..b1fd8b57 100644 --- a/osinaweb/osinaweb/settings.py +++ b/osinaweb/osinaweb/settings.py @@ -49,6 +49,7 @@ INSTALLED_APPS = [ 'addressbook', 'billing', 'colorfield', + 'corsheaders', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', @@ -65,8 +66,13 @@ MIDDLEWARE = [ 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'corsheaders.middleware.CorsMiddleware', + ] +CORS_ALLOW_ALL_ORIGINS = True + + ROOT_URLCONF = 'osinaweb.urls' TEMPLATES = [ diff --git a/osinaweb/static/js/osichat/visitors.js b/osinaweb/static/js/osichat/visitors.js index e18346e4..920022a2 100644 --- a/osinaweb/static/js/osichat/visitors.js +++ b/osinaweb/static/js/osichat/visitors.js @@ -1,52 +1,52 @@ const visitors_ws_scheme = window.location.protocol === "https:" ? "wss" : "ws"; const my_domain = "osina.ositcom.com"; -const referrer = document.referrer; const visitorsSocketUrl = `${visitors_ws_scheme}://${my_domain}/ws/osichat/visitors/`; -const getClientIPAndCountry = (callback) => { - fetch('https://ipinfo.io/json?token=YOUR_IPINFO_TOKEN') +// Function to fetch client IP and country from the API +function fetchClientData() { + return fetch('https://osina.ositcom.com/get-client-ip/') .then(response => response.json()) - .then(data => { - callback({ - ip: data.ip, - country: data.country - }); - }) + .then(data => ({ + client_ip: data.ip, + client_country: data.country + })) .catch(error => { - console.error('Error fetching IP and country:', error); - callback({ - ip: null, - country: 'unknown' - }); + console.error('Error fetching client data:', error); + return { + client_ip: 'Unknown', + client_country: 'Unknown' + }; }); -}; +} -const establishWebSocketConnection = (client_ip, client_country) => { - const visitorsSocket = new WebSocket(visitorsSocketUrl); +function initializeWebSocket() { + const referrer = document.referrer; - visitorsSocket.onopen = () => { - console.log('WebSocket connection to visitors established'); - - const event_message = { - 'event_type': 'new_visitor', - 'referrer': referrer, - 'url': window.location.href, - 'client_ip': client_ip, - 'client_country': client_country + // Fetch client data and then initialize WebSocket + fetchClientData().then(({ client_ip, client_country }) => { + const visitorsSocket = new WebSocket(visitorsSocketUrl); + + visitorsSocket.onopen = () => { + console.log('WebSocket connection to visitors established'); + + const event_message = { + 'event_type': 'new_visitor', + 'referrer': referrer, + 'url': window.location.href, + 'client_ip': client_ip, + 'client_country': client_country + }; + visitorsSocket.send(JSON.stringify(event_message)); }; - visitorsSocket.send(JSON.stringify(event_message)); - }; - visitorsSocket.onclose = () => { - console.log('WebSocket connection to visitors closed'); - }; + visitorsSocket.onclose = () => { + console.log('WebSocket connection to visitors closed'); + }; - visitorsSocket.onerror = (error) => { - console.error('WebSocket error:', error); - }; -}; + visitorsSocket.onerror = (error) => { + console.error('WebSocket error:', error); + }; + }); +} -// Get the client IP and country first, then establish the WebSocket connection -getClientIPAndCountry((client_info) => { - establishWebSocketConnection(client_info.ip, client_info.country); -}); +initializeWebSocket();