emile 10 months ago
parent e29ab2c80c
commit 0997f656c1

Binary file not shown.

@ -3,5 +3,5 @@ from . import views
urlpatterns = [ urlpatterns = [
path('get-client-ip/', views.get_client_ip, name='get-client-ip'),
] ]

@ -1,3 +1,18 @@
from django.shortcuts import render from django.shortcuts import render
from django.http import JsonResponse 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})

@ -49,6 +49,7 @@ INSTALLED_APPS = [
'addressbook', 'addressbook',
'billing', 'billing',
'colorfield', 'colorfield',
'corsheaders',
'django.contrib.admin', 'django.contrib.admin',
'django.contrib.auth', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.contenttypes',
@ -65,8 +66,13 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
] ]
CORS_ALLOW_ALL_ORIGINS = True
ROOT_URLCONF = 'osinaweb.urls' ROOT_URLCONF = 'osinaweb.urls'
TEMPLATES = [ TEMPLATES = [

@ -1,52 +1,52 @@
const visitors_ws_scheme = window.location.protocol === "https:" ? "wss" : "ws"; const visitors_ws_scheme = window.location.protocol === "https:" ? "wss" : "ws";
const my_domain = "osina.ositcom.com"; const my_domain = "osina.ositcom.com";
const referrer = document.referrer;
const visitorsSocketUrl = `${visitors_ws_scheme}://${my_domain}/ws/osichat/visitors/`; const visitorsSocketUrl = `${visitors_ws_scheme}://${my_domain}/ws/osichat/visitors/`;
const getClientIPAndCountry = (callback) => { // Function to fetch client IP and country from the API
fetch('https://ipinfo.io/json?token=YOUR_IPINFO_TOKEN') function fetchClientData() {
return fetch('https://osina.ositcom.com/get-client-ip/')
.then(response => response.json()) .then(response => response.json())
.then(data => { .then(data => ({
callback({ client_ip: data.ip,
ip: data.ip, client_country: data.country
country: data.country }))
});
})
.catch(error => { .catch(error => {
console.error('Error fetching IP and country:', error); console.error('Error fetching client data:', error);
callback({ return {
ip: null, client_ip: 'Unknown',
country: 'unknown' client_country: 'Unknown'
}); };
}); });
}; }
const establishWebSocketConnection = (client_ip, client_country) => { function initializeWebSocket() {
const visitorsSocket = new WebSocket(visitorsSocketUrl); const referrer = document.referrer;
visitorsSocket.onopen = () => { // Fetch client data and then initialize WebSocket
console.log('WebSocket connection to visitors established'); fetchClientData().then(({ client_ip, client_country }) => {
const visitorsSocket = new WebSocket(visitorsSocketUrl);
const event_message = {
'event_type': 'new_visitor', visitorsSocket.onopen = () => {
'referrer': referrer, console.log('WebSocket connection to visitors established');
'url': window.location.href,
'client_ip': client_ip, const event_message = {
'client_country': client_country '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 = () => { visitorsSocket.onclose = () => {
console.log('WebSocket connection to visitors closed'); console.log('WebSocket connection to visitors closed');
}; };
visitorsSocket.onerror = (error) => { visitorsSocket.onerror = (error) => {
console.error('WebSocket error:', error); console.error('WebSocket error:', error);
}; };
}; });
}
// Get the client IP and country first, then establish the WebSocket connection initializeWebSocket();
getClientIPAndCountry((client_info) => {
establishWebSocketConnection(client_info.ip, client_info.country);
});

Loading…
Cancel
Save