|
@@ -1,52 +1,66 @@
|
|
|
-import logging
|
|
|
import time
|
|
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
+from django.views import View
|
|
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
-from django.views.generic import TemplateView
|
|
|
-from Model.models import CountryModel, RegionModel, P2PIpModel, DeviceDomainModel, DeviceDomainRegionModel
|
|
|
+from Model.models import CountryModel, RegionModel, P2PIpModel, DeviceDomainModel, DeviceDomainRegionModel, IPAddr
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Service.CommonService import CommonService
|
|
|
+from Object.IPWeatherObject import IPQuery
|
|
|
|
|
|
|
|
|
-#确认设备所在地区
|
|
|
-class ConfirmRegion(TemplateView):
|
|
|
+class ConfirmRegion(View):
|
|
|
+ # 设备根据ip获取域名
|
|
|
@method_decorator(csrf_exempt)
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
return super(ConfirmRegion, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
- def get(self, request, *args, **kwargs):
|
|
|
+ @staticmethod
|
|
|
+ def get(request, *args, **kwargs):
|
|
|
response = ResponseObject()
|
|
|
request.encoding = 'utf-8'
|
|
|
try:
|
|
|
ip = CommonService.get_ip_address(request)
|
|
|
- device_domain_data = {'ip': ip}
|
|
|
- ipInfo = CommonService.getIpIpInfo(ip, 'CN')
|
|
|
- logger = logging.getLogger('info')
|
|
|
- logger.info('设备获取域名---ip:{},country_code:{}'.format(ip, ipInfo['country_code']))
|
|
|
- if ipInfo['country_code']:
|
|
|
- device_domain_data['country_name'] = ipInfo['country_code']
|
|
|
- device_request_url = CountryModel.objects.filter(country_code=ipInfo['country_code']).values("region__api")
|
|
|
- if device_request_url.exists():
|
|
|
- api = device_request_url[0]['region__api']
|
|
|
- device_domain_data['api'] = api
|
|
|
- DeviceDomainModel.objects.create(**device_domain_data)
|
|
|
- return response.json(0, {'request_api_url': api})
|
|
|
-
|
|
|
- # 不存在默认返回美洲地区api。
|
|
|
- api = RegionModel.objects.filter(continent_code='NA').values("api")
|
|
|
- device_domain_data['country_name'] = 'NA'
|
|
|
- device_domain_data['api'] = api[0]['api']
|
|
|
- DeviceDomainModel.objects.create(**device_domain_data)
|
|
|
- return response.json(0, {'request_api_url': api[0]['api']})
|
|
|
+ device_domain_qs = DeviceDomainModel.objects.filter(ip=ip)
|
|
|
+
|
|
|
+ # 获取国家编码
|
|
|
+ ip_addr_qs = IPAddr.objects.filter(ip=ip).values('country_code')
|
|
|
+ if ip_addr_qs.exists():
|
|
|
+ country_code = ip_addr_qs[0]['country_code']
|
|
|
+ else:
|
|
|
+ ip_qs = IPQuery(ip)
|
|
|
+ country_code = ip_qs.country_id
|
|
|
+
|
|
|
+ if country_code:
|
|
|
+ country_qs = CountryModel.objects.filter(country_code=country_code).\
|
|
|
+ values('region__api', 'region__push_api')
|
|
|
+ if country_qs.exists():
|
|
|
+ api = country_qs[0]['region__api']
|
|
|
+ push_api = country_qs[0]['region__push_api']
|
|
|
+
|
|
|
+ if not device_domain_qs.exists():
|
|
|
+ DeviceDomainModel.objects.create(ip=ip, country_name=country_code, api=api)
|
|
|
+
|
|
|
+ return response.json(0, {'request_api_url': api, 'push_api_url': push_api})
|
|
|
+
|
|
|
+ # 不存在则返回美洲域名
|
|
|
+ region_qs = RegionModel.objects.filter(continent_code='NA').values('api', 'push_api')
|
|
|
+ api = region_qs[0]['api']
|
|
|
+ push_api = region_qs[0]['push_api']
|
|
|
+
|
|
|
+ if not device_domain_qs.exists():
|
|
|
+ DeviceDomainModel.objects.create(ip=ip, country_name='NA', api=api)
|
|
|
+
|
|
|
+ return response.json(0, {'request_api_url': api, 'push_api_url': push_api})
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
- return response.json(0, {'request_api_url': 'https://www.dvema.com'})
|
|
|
+ return response.json(0, {'request_api_url': 'https://www.dvema.com',
|
|
|
+ 'push_api_url': 'https://push.dvema.com'})
|
|
|
|
|
|
|
|
|
-# 根据设备的ip返回域名和地区id
|
|
|
-class ConfirmRegionV2(TemplateView):
|
|
|
+class ConfirmRegionV2(View):
|
|
|
+ # 设备根据ip获取域名V2接口
|
|
|
@method_decorator(csrf_exempt)
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
return super(ConfirmRegionV2, self).dispatch(*args, **kwargs)
|
|
@@ -60,57 +74,78 @@ class ConfirmRegionV2(TemplateView):
|
|
|
data_dict = {'serial_number': serial_number}
|
|
|
device_domain_region_qs = DeviceDomainRegionModel.objects.filter(serial_number=serial_number)
|
|
|
|
|
|
- # 根据请求ip确认地区
|
|
|
+ # 获取ip
|
|
|
request.encoding = 'utf-8'
|
|
|
ip = CommonService.get_ip_address(request)
|
|
|
data_dict['ip'] = ip
|
|
|
- ipInfo = CommonService.getIpIpInfo(ip, 'CN')
|
|
|
- country_code = ipInfo['country_code']
|
|
|
+
|
|
|
+ # 获取国家编码
|
|
|
+ ip_addr_qs = IPAddr.objects.filter(ip=ip).values('country_code')
|
|
|
+ if ip_addr_qs.exists():
|
|
|
+ country_code = ip_addr_qs[0]['country_code']
|
|
|
+ else:
|
|
|
+ ip_qs = IPQuery(ip)
|
|
|
+ country_code = ip_qs.country_id
|
|
|
|
|
|
if country_code:
|
|
|
- data_dict['country_name'] = ipInfo['country_name']
|
|
|
+ data_dict['country_code'] = country_code
|
|
|
country_qs = CountryModel.objects.filter(country_code=country_code).\
|
|
|
- values('region__api', 'region__id')
|
|
|
+ values('region__api', 'region__push_api', 'region__id')
|
|
|
if country_qs.exists():
|
|
|
api = country_qs[0]['region__api']
|
|
|
+ push_api = country_qs[0]['region__push_api']
|
|
|
region_id = country_qs[0]['region__id']
|
|
|
- else: # 默认返回美洲地区api
|
|
|
- api, region_id = self.get_default_api()
|
|
|
+ else:
|
|
|
+ # 返回美洲域名
|
|
|
+ data_dict['country_code'] = 'NA'
|
|
|
+ api, push_api, region_id = self.get_default_api()
|
|
|
else:
|
|
|
- # 默认返回美洲地区api
|
|
|
- api, region_id = self.get_default_api()
|
|
|
+ # 返回美洲域名
|
|
|
+ data_dict['country_code'] = 'NA'
|
|
|
+ api, push_api, region_id = self.get_default_api()
|
|
|
|
|
|
# 更新或创建设备域名数据
|
|
|
- data_dict['api'] = api
|
|
|
data_dict['region_id'] = region_id
|
|
|
if device_domain_region_qs.exists():
|
|
|
device_domain_region_qs.update(**data_dict)
|
|
|
else:
|
|
|
device_domain_region_qs.create(**data_dict)
|
|
|
- res = {'request_api_url': api, 'region_id': region_id}
|
|
|
+
|
|
|
+ # 响应数据
|
|
|
+ res = {
|
|
|
+ 'request_api_url': api,
|
|
|
+ 'push_api_url': push_api,
|
|
|
+ 'region_id': region_id
|
|
|
+ }
|
|
|
return response.json(0, res)
|
|
|
except Exception as e:
|
|
|
- print(e)
|
|
|
- return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+ print(repr(e))
|
|
|
+ api, push_api, region_id = self.get_default_api()
|
|
|
+ res = {
|
|
|
+ 'request_api_url': api,
|
|
|
+ 'push_api_url': push_api,
|
|
|
+ 'region_id': region_id
|
|
|
+ }
|
|
|
+ return response.json(0, res)
|
|
|
|
|
|
# 获取区域表美洲的相关数据
|
|
|
@staticmethod
|
|
|
def get_default_api():
|
|
|
- region_qs = RegionModel.objects.filter(continent_code='NA').values('api', 'id')
|
|
|
+ region_qs = RegionModel.objects.filter(continent_code='NA').values('api', 'push_api', 'id')
|
|
|
api = region_qs[0]['api']
|
|
|
+ push_api = region_qs[0]['push_api']
|
|
|
region_id = region_qs[0]['id']
|
|
|
- return api, region_id
|
|
|
+ return api, push_api, region_id
|
|
|
+
|
|
|
|
|
|
-#确认设备所在地区
|
|
|
class Device_Region(object):
|
|
|
|
|
|
def get_device_region(self, ip):
|
|
|
-
|
|
|
ipInfo = CommonService.getIpIpInfo(ip, "CN")
|
|
|
-
|
|
|
- #暂时测试国外
|
|
|
+ # 暂时测试国外
|
|
|
if ipInfo['country_code']:
|
|
|
- device_request_url = CountryModel.objects.filter(country_code=ipInfo['country_code']).values("region__api","region__id")
|
|
|
+ device_request_url = CountryModel.objects.filter(country_code=ipInfo['country_code']).\
|
|
|
+ values("region__api", "region__id")
|
|
|
if device_request_url.exists():
|
|
|
return device_request_url[0]['region__id']
|
|
|
# 不存在默认返回美洲地区api
|
|
@@ -119,7 +154,7 @@ class Device_Region(object):
|
|
|
|
|
|
|
|
|
# 根据p2p的ip统计设备所在地区
|
|
|
-class StatisticsIpRegion(TemplateView):
|
|
|
+class StatisticsIpRegion(View):
|
|
|
@method_decorator(csrf_exempt)
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
return super(StatisticsIpRegion, self).dispatch(*args, **kwargs)
|
|
@@ -149,7 +184,8 @@ class StatisticsIpRegion(TemplateView):
|
|
|
# 已存在数据,更新p2p请求次数和relay请求次数
|
|
|
p2p_request_times = p2p_ip_qs[0]['p2p_request_times'] + p2p_request_times
|
|
|
relay_request_times = p2p_ip_qs[0]['relay_request_times'] + relay_request_times
|
|
|
- p2p_ip_qs.update(p2p_request_times=p2p_request_times, relay_request_times=relay_request_times, update_time=now_time)
|
|
|
+ p2p_ip_qs.update(p2p_request_times=p2p_request_times, relay_request_times=relay_request_times,
|
|
|
+ update_time=now_time)
|
|
|
else:
|
|
|
# 根据ip确定位置信息
|
|
|
ip_info = CommonService.getIpIpInfo(ip, 'CN')
|
|
@@ -163,13 +199,15 @@ class StatisticsIpRegion(TemplateView):
|
|
|
region_name = ip_info['region_name']
|
|
|
city_name = ip_info['city_name']
|
|
|
P2PIpModel.objects.create(uid=uid, ip=ip, continent_name=continent_name, country_name=country_name,
|
|
|
- region_name=region_name, city_name=city_name, p2p_request_times=p2p_request_times,
|
|
|
- relay_request_times=relay_request_times, add_time=now_time, update_time=now_time)
|
|
|
+ region_name=region_name, city_name=city_name,
|
|
|
+ p2p_request_times=p2p_request_times, relay_request_times=relay_request_times,
|
|
|
+ add_time=now_time, update_time=now_time)
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
+
|
|
|
def confirm_country_with_ip(request):
|
|
|
'''
|
|
|
根据ip统计设备所在国家的程序,
|