httpdns.lua 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. --[[
  2. @module httpdns
  3. @summary 使用Http进行域名解析
  4. @version 1.0
  5. @date 2023.07.13
  6. @author wendal
  7. @usage
  8. -- 通过阿里DNS获取结果
  9. local ip = httpdns.ali("air32.cn")
  10. log.info("httpdns", "air32.cn", ip)
  11. -- 通过腾讯DNS获取结果
  12. local ip = httpdns.tx("air32.cn")
  13. log.info("httpdns", "air32.cn", ip)
  14. ]]
  15. local httpdns = {}
  16. --[[
  17. 通过阿里DNS获取结果
  18. @api httpdns.ali(domain_name)
  19. @string 域名
  20. @return string ip地址
  21. @usage
  22. local ip = httpdns.ali("air32.cn")
  23. log.info("httpdns", "air32.cn", ip)
  24. ]]
  25. function httpdns.ali(n)
  26. if n == nil then return end
  27. local code, _, body = http.request("GET", "http://223.5.5.5/resolve?short=1&name=" .. tostring(n)).wait()
  28. if code == 200 and body and #body > 2 then
  29. local jdata = json.decode(body)
  30. if jdata and #jdata > 0 then
  31. return jdata[1]
  32. end
  33. end
  34. end
  35. --[[
  36. 通过腾讯DNS获取结果
  37. @api httpdns.tx(domain_name)
  38. @string 域名
  39. @return string ip地址
  40. @usage
  41. local ip = httpdns.tx("air32.cn")
  42. log.info("httpdns", "air32.cn", ip)
  43. ]]
  44. function httpdns.tx(n)
  45. if n == nil then return end
  46. local code, _, body = http.request("GET", "http://119.29.29.29/d?dn=" .. tostring(n)).wait()
  47. if code == 200 and body and #body > 2 then
  48. local tmp = body:split(",")
  49. if tmp then return tmp[1] end
  50. end
  51. end
  52. return httpdns