47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
from django.shortcuts import render
|
|
from rest_framework import status
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
|
|
from dotenv import load_dotenv
|
|
import os
|
|
import requests
|
|
|
|
load_dotenv()
|
|
|
|
|
|
def home(request):
|
|
return render(request, 'home.html')
|
|
|
|
|
|
def robots(request):
|
|
return render(request, 'robots.txt')
|
|
|
|
|
|
class Doxme(APIView):
|
|
def get_client_ip(self, request):
|
|
"""Get the user's IP from the request headers"""
|
|
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
|
|
if x_forwarded_for:
|
|
ip = x_forwarded_for.split(',')[0] # First IP in the chain
|
|
else:
|
|
ip = request.META.get('REMOTE_ADDR')
|
|
return ip
|
|
|
|
def get(self, request):
|
|
client_ip = self.get_client_ip(request)
|
|
|
|
ipinfo_token = os.getenv('IPINFO_TOKEN')
|
|
ipinfo_url = f'http://api.ipinfo.io/lite/{client_ip}?token={ipinfo_token}'
|
|
ip_info = requests.get(ipinfo_url)
|
|
|
|
|
|
ipis_token = os.getenv('IPIS_TOKEN')
|
|
ipis_url = f'https://api.ipapi.is?q={client_ip}&key={ipis_token}'
|
|
ipis_info = requests.get(ipis_url)
|
|
|
|
if ip_info.ok or ipis_info.ok:
|
|
print('ipinfo:',ip_info)
|
|
return Response({'method': 'get', 'ip_info': ip_info.json(),'ipis':ipis_info.json()}, status=status.HTTP_200_OK)
|
|
return Response({'message': 'error at ipinfo'}, status=status.HTTP_400_BAD_REQUEST)
|