|
@@ -11,8 +11,8 @@ from Service.CommonService import CommonService
|
|
from Object.IPWeatherObject import IPQuery
|
|
from Object.IPWeatherObject import IPQuery
|
|
|
|
|
|
|
|
|
|
-#确认设备所在地区
|
|
|
|
class ConfirmRegion(TemplateView):
|
|
class ConfirmRegion(TemplateView):
|
|
|
|
+ # 设备根据ip获取域名
|
|
@method_decorator(csrf_exempt)
|
|
@method_decorator(csrf_exempt)
|
|
def dispatch(self, *args, **kwargs):
|
|
def dispatch(self, *args, **kwargs):
|
|
return super(ConfirmRegion, self).dispatch(*args, **kwargs)
|
|
return super(ConfirmRegion, self).dispatch(*args, **kwargs)
|
|
@@ -21,34 +21,38 @@ class ConfirmRegion(TemplateView):
|
|
response = ResponseObject()
|
|
response = ResponseObject()
|
|
request.encoding = 'utf-8'
|
|
request.encoding = 'utf-8'
|
|
try:
|
|
try:
|
|
- ip = request.GET.get('ip', None)
|
|
|
|
|
|
+ ip = CommonService.get_ip_address(request)
|
|
device_domain_data = {'ip': ip}
|
|
device_domain_data = {'ip': ip}
|
|
- ip_addr_qs = IPAddr.objects.filter(ip=ip).values("country_code")
|
|
|
|
|
|
+
|
|
|
|
+ ip_addr_qs = IPAddr.objects.filter(ip=ip).values('country_code')
|
|
if ip_addr_qs.exists():
|
|
if ip_addr_qs.exists():
|
|
country_code = ip_addr_qs[0]['country_code']
|
|
country_code = ip_addr_qs[0]['country_code']
|
|
else:
|
|
else:
|
|
ip_qs = IPQuery(ip)
|
|
ip_qs = IPQuery(ip)
|
|
country_code = ip_qs.country_id
|
|
country_code = ip_qs.country_id
|
|
|
|
+
|
|
logger = logging.getLogger('info')
|
|
logger = logging.getLogger('info')
|
|
logger.info('设备获取域名---ip:{},country_code:{}'.format(ip, country_code))
|
|
logger.info('设备获取域名---ip:{},country_code:{}'.format(ip, country_code))
|
|
if country_code:
|
|
if country_code:
|
|
device_domain_data['country_name'] = country_code
|
|
device_domain_data['country_name'] = country_code
|
|
- device_request_url = CountryModel.objects.filter(country_code=country_code).values("region__api")
|
|
|
|
|
|
+ device_request_url = CountryModel.objects.filter(country_code=country_code).\
|
|
|
|
+ values('region__api', 'region__push_api')
|
|
if device_request_url.exists():
|
|
if device_request_url.exists():
|
|
api = device_request_url[0]['region__api']
|
|
api = device_request_url[0]['region__api']
|
|
|
|
+ push_api = device_request_url[0]['region__push_api']
|
|
device_domain_data['api'] = api
|
|
device_domain_data['api'] = api
|
|
DeviceDomainModel.objects.create(**device_domain_data)
|
|
DeviceDomainModel.objects.create(**device_domain_data)
|
|
- return response.json(0, {'request_api_url': api})
|
|
|
|
|
|
+ return response.json(0, {'request_api_url': api, 'push_api': push_api})
|
|
|
|
|
|
# 不存在默认返回美洲地区api。
|
|
# 不存在默认返回美洲地区api。
|
|
- api = RegionModel.objects.filter(continent_code='NA').values("api")
|
|
|
|
|
|
+ api = RegionModel.objects.filter(continent_code='NA').values('api', 'push_api')
|
|
device_domain_data['country_name'] = 'NA'
|
|
device_domain_data['country_name'] = 'NA'
|
|
device_domain_data['api'] = api[0]['api']
|
|
device_domain_data['api'] = api[0]['api']
|
|
DeviceDomainModel.objects.create(**device_domain_data)
|
|
DeviceDomainModel.objects.create(**device_domain_data)
|
|
- return response.json(0, {'request_api_url': api[0]['api']})
|
|
|
|
|
|
+ return response.json(0, {'request_api_url': api[0]['api'], 'push_api': api[0]['push_api']})
|
|
except Exception as e:
|
|
except Exception as e:
|
|
print(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': 'https://push.dvema.com'})
|
|
|
|
|
|
|
|
|
|
# 根据设备的ip返回域名和地区id
|
|
# 根据设备的ip返回域名和地区id
|
|
@@ -107,16 +111,15 @@ class ConfirmRegionV2(TemplateView):
|
|
region_id = region_qs[0]['id']
|
|
region_id = region_qs[0]['id']
|
|
return api, region_id
|
|
return api, region_id
|
|
|
|
|
|
-#确认设备所在地区
|
|
|
|
|
|
+
|
|
class Device_Region(object):
|
|
class Device_Region(object):
|
|
|
|
|
|
def get_device_region(self, ip):
|
|
def get_device_region(self, ip):
|
|
-
|
|
|
|
ipInfo = CommonService.getIpIpInfo(ip, "CN")
|
|
ipInfo = CommonService.getIpIpInfo(ip, "CN")
|
|
-
|
|
|
|
- #暂时测试国外
|
|
|
|
|
|
+ # 暂时测试国外
|
|
if ipInfo['country_code']:
|
|
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():
|
|
if device_request_url.exists():
|
|
return device_request_url[0]['region__id']
|
|
return device_request_url[0]['region__id']
|
|
# 不存在默认返回美洲地区api
|
|
# 不存在默认返回美洲地区api
|
|
@@ -155,7 +158,8 @@ class StatisticsIpRegion(TemplateView):
|
|
# 已存在数据,更新p2p请求次数和relay请求次数
|
|
# 已存在数据,更新p2p请求次数和relay请求次数
|
|
p2p_request_times = p2p_ip_qs[0]['p2p_request_times'] + p2p_request_times
|
|
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
|
|
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:
|
|
else:
|
|
# 根据ip确定位置信息
|
|
# 根据ip确定位置信息
|
|
ip_info = CommonService.getIpIpInfo(ip, 'CN')
|
|
ip_info = CommonService.getIpIpInfo(ip, 'CN')
|
|
@@ -169,13 +173,15 @@ class StatisticsIpRegion(TemplateView):
|
|
region_name = ip_info['region_name']
|
|
region_name = ip_info['region_name']
|
|
city_name = ip_info['city_name']
|
|
city_name = ip_info['city_name']
|
|
P2PIpModel.objects.create(uid=uid, ip=ip, continent_name=continent_name, country_name=country_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)
|
|
return response.json(0)
|
|
except Exception as e:
|
|
except Exception as e:
|
|
print(e)
|
|
print(e)
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
|
|
+
|
|
def confirm_country_with_ip(request):
|
|
def confirm_country_with_ip(request):
|
|
'''
|
|
'''
|
|
根据ip统计设备所在国家的程序,
|
|
根据ip统计设备所在国家的程序,
|