emile 10 months ago
parent e29ab2c80c
commit 0997f656c1

Binary file not shown.

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

@ -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})

@ -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 = [

@ -1,27 +1,29 @@
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) => {
function initializeWebSocket() {
const referrer = document.referrer;
// Fetch client data and then initialize WebSocket
fetchClientData().then(({ client_ip, client_country }) => {
const visitorsSocket = new WebSocket(visitorsSocketUrl);
visitorsSocket.onopen = () => {
@ -44,9 +46,7 @@ const establishWebSocketConnection = (client_ip, client_country) => {
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();

Loading…
Cancel
Save