diff --git a/osinaweb/db.sqlite3 b/osinaweb/db.sqlite3 index 48739554..5409c0d5 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 921d42dd..819d98f9 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 new file mode 100644 index 00000000..a576b215 Binary files /dev/null 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 new file mode 100644 index 00000000..fc503a33 Binary files /dev/null and b/osinaweb/osichat/__pycache__/views.cpython-310.pyc differ diff --git a/osinaweb/osichat/consumers.py b/osinaweb/osichat/consumers.py index 3c1b91d4..db30b25b 100644 --- a/osinaweb/osichat/consumers.py +++ b/osinaweb/osichat/consumers.py @@ -10,18 +10,6 @@ import requests class OsitcomVisitor(WebsocketConsumer): def connect(self): self.accept() - headers = dict(self.scope.get('headers', [])) - ip_address = headers.get(b'x-forwarded-for', None) - if ip_address: - # Extract the actual IP address if there are multiple addresses - self.visitor_ip = ip_address.split(',')[0].strip() - else: - # Fallback to the standard remote address - self.visitor_ip = self.scope.get('client', [None])[0] - if self.visitor_ip: - response = requests.get(f'http://ipinfo.io/{self.visitor_ip}/json') - data = response.json() - self.visitor_country = data.get('country', 'Unknown') async_to_sync(self.channel_layer.group_add)( 'ositcom_visitors', self.channel_name ) @@ -41,6 +29,7 @@ class OsitcomVisitor(WebsocketConsumer): event = { 'type': 'new_visitor_handler', 'session_id': text_data_json.get('session_id'), + 'client_ip': text_data_json.get('client_ip'), 'referrer': text_data_json.get('referrer'), 'url': text_data_json.get('url'), @@ -52,8 +41,7 @@ class OsitcomVisitor(WebsocketConsumer): def new_visitor_handler(self, event): visitor = Visitor.objects.create( session_id = event['session_id'], - ip_address = self.visitor_ip, - country = self.visitor_country, + ip_address = event['client_ip'], referrer = event['referrer'], url = event['url'], visit_date = datetime.now(), diff --git a/osinaweb/osichat/urls.py b/osinaweb/osichat/urls.py new file mode 100644 index 00000000..1f5bb45c --- /dev/null +++ b/osinaweb/osichat/urls.py @@ -0,0 +1,9 @@ +from django.urls import path, include +from . import views + + +urlpatterns = [ + # DETAILS + 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 91ea44a2..6aa6d91a 100644 --- a/osinaweb/osichat/views.py +++ b/osinaweb/osichat/views.py @@ -1,3 +1,7 @@ from django.shortcuts import render +from django.http import JsonResponse # Create your views here. +def get_client_ip(request): + client_ip = request.META.get('REMOTE_ADDR', '') + return JsonResponse({'ip': client_ip}) \ No newline at end of file diff --git a/osinaweb/osinaweb/__pycache__/urls.cpython-310.pyc b/osinaweb/osinaweb/__pycache__/urls.cpython-310.pyc index b045de36..80235dbb 100644 Binary files a/osinaweb/osinaweb/__pycache__/urls.cpython-310.pyc and b/osinaweb/osinaweb/__pycache__/urls.cpython-310.pyc differ diff --git a/osinaweb/osinaweb/urls.py b/osinaweb/osinaweb/urls.py index 7c7019df..2d3d9f72 100644 --- a/osinaweb/osinaweb/urls.py +++ b/osinaweb/osinaweb/urls.py @@ -24,6 +24,7 @@ urlpatterns = [ path('', include('customercore.urls')), path('', include('billing.urls')), path('', include('support.urls')), + path('', include('osichat.urls')), path('admin/', admin.site.urls), ] diff --git a/osinaweb/static/js/osichat/conversation.js b/osinaweb/static/js/osichat/conversation.js index b7062555..927b7be9 100644 --- a/osinaweb/static/js/osichat/conversation.js +++ b/osinaweb/static/js/osichat/conversation.js @@ -1,6 +1,6 @@ const chat_ws_scheme = window.location.protocol === "https:" ? "wss" : "ws"; -const domain = "192.168.1.111:8000"; +const domain = "192.168.1.106:8000"; const sessionId = document.getElementById('session_id').textContent.trim(); const osichatSocketUrl = `${chat_ws_scheme}://${domain}/ws/osichat/${sessionId}/`; const osichatSocket = new WebSocket(osichatSocketUrl); diff --git a/osinaweb/static/js/osichat/visitors.js b/osinaweb/static/js/osichat/visitors.js index 42b2dd12..75a8ba93 100644 --- a/osinaweb/static/js/osichat/visitors.js +++ b/osinaweb/static/js/osichat/visitors.js @@ -1,29 +1,42 @@ - const visitors_ws_scheme = window.location.protocol === "https:" ? "wss" : "ws"; -const my_domain = "osina.ositcom.com"; +const my_domain = "192.168.1.106:8000"; const session_id = document.getElementById('session_id').textContent.trim(); const current_url = document.getElementById('current_url').textContent.trim(); -const referrer = document.referrer; +const referrer = document.referrer; const visitorsSocketUrl = `${visitors_ws_scheme}://${my_domain}/ws/osichat/visitors/`; const visitorsSocket = new WebSocket(visitorsSocketUrl); +const clientIpUrl = `http://${my_domain}/get-client-ip/`; +fetch(clientIpUrl) + .then(response => { + if (!response.ok) { + throw new Error('Network response was not ok'); + } + return response.json(); + }) + .then(data => { + const client_ip = data.ip; -visitorsSocket.onopen = () => { - console.log('WebSocket connection to visitors established'); - const event_message = { - 'event_type': 'new_visitor', - 'session_id': session_id, - 'referrer': referrer, - 'url': current_url, - }; - visitorsSocket.send(JSON.stringify(event_message)); -}; - + visitorsSocket.onopen = () => { + console.log('WebSocket connection to visitors established'); + const event_message = { + 'event_type': 'new_visitor', + 'session_id': session_id, + 'referrer': referrer, + 'url': current_url, + 'client_ip': client_ip, + }; + 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.log('WebSocket error:', error); -}; + visitorsSocket.onerror = (error) => { + console.error('WebSocket error:', error); + }; + }) + .catch(error => { + console.error('Error fetching client IP:', error); + });