ip_local_util.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # -*- coding: utf-8 -*-
  2. import re
  3. import httpx
  4. from app.core.logger import log
  5. class IpLocalUtil:
  6. """
  7. 获取IP归属地工具类
  8. """
  9. @classmethod
  10. def is_valid_ip(cls, ip: str) -> bool:
  11. """
  12. 校验IP格式是否合法。
  13. 参数:
  14. - ip (str): IP地址。
  15. 返回:
  16. - bool: 是否合法。
  17. """
  18. ip_pattern = r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
  19. return bool(re.match(ip_pattern, ip))
  20. @classmethod
  21. def is_private_ip(cls, ip: str) -> bool:
  22. """
  23. 判断是否为内网IP。
  24. 参数:
  25. - ip (str): IP地址。
  26. 返回:
  27. - bool: 是否为内网IP。
  28. """
  29. priv_pattern = r'^(127\.|10\.|172\.(1[6-9]|2[0-9]|3[01])\.|192\.168\.)'
  30. return bool(re.match(priv_pattern, ip))
  31. @classmethod
  32. async def get_ip_location(cls, ip: str) -> str | None:
  33. """
  34. 获取IP归属地信息。
  35. 参数:
  36. - ip (str): IP地址。
  37. 返回:
  38. - str | None: IP归属地信息,失败时返回"未知"或None。
  39. """
  40. # 校验IP格式
  41. if not cls.is_valid_ip(ip):
  42. log.error(f"IP格式不合法: {ip}")
  43. return "未知"
  44. # 内网IP直接返回
  45. if cls.is_private_ip(ip):
  46. return '内网IP'
  47. try:
  48. # 使用ip-api.com API获取IP归属地信息
  49. async with httpx.AsyncClient(timeout=10.0) as client:
  50. # 尝试使用 ip9.com.cn API
  51. url = f'https://ip9.com.cn/get?ip={ip}'
  52. response = await cls._make_api_request(client, url)
  53. if response and response.json().get('ret') == 200:
  54. result = response.json().get('data', {})
  55. return f"{result.get('country','')}-{result.get('prov','')}-{result.get('city','')}-{result.get('area','')}-{result.get('isp','')}"
  56. # 尝试使用百度 API
  57. url = f'https://qifu-api.baidubce.com/ip/geo/v1/district?ip={ip}'
  58. response = await cls._make_api_request(client, url)
  59. if response and response.json().get('code') == "Success":
  60. data = response.json().get('data', {})
  61. # 修正原代码中的格式错误
  62. return f"{data.get('country','')}-{data.get('prov','')}-{data.get('city','')}-{data.get('district','')}-{data.get('isp','')}"
  63. except Exception as e:
  64. log.error(f"获取IP归属地失败: {e}")
  65. return "未知"
  66. @classmethod
  67. async def _make_api_request(cls, client, url):
  68. """
  69. 单独的 API 请求方法,包含重试机制。
  70. 参数:
  71. - client (AsyncClient): httpx 异步客户端。
  72. - url (str): 请求 URL。
  73. 返回:
  74. - Response | None: 响应对象,失败时返回None。
  75. """
  76. max_retries = 3
  77. for attempt in range(max_retries):
  78. try:
  79. response = await client.get(url, timeout=10)
  80. if response.status_code == 200:
  81. return response
  82. except Exception as e:
  83. if attempt < max_retries - 1:
  84. continue
  85. log.error(f"请求 {url} 失败: {e}")
  86. return None