Просмотр исходного кода

add: 添加httpdns库,支持阿里dns和腾讯dns

https://gitee.com/openLuat/LuatOS/issues/I6YO4O
Wendal Chen 2 лет назад
Родитель
Сommit
e6e2dab3b5
2 измененных файлов с 141 добавлено и 0 удалено
  1. 81 0
      demo/httpdns/main.lua
  2. 60 0
      script/libs/httpdns.lua

+ 81 - 0
demo/httpdns/main.lua

@@ -0,0 +1,81 @@
+
+-- LuaTools需要PROJECT和VERSION这两个信息
+PROJECT = "httpdnsdemo"
+VERSION = "1.0.0"
+
+--[[
+本demo需要http库, 大部分能联网的设备都具有这个库
+http也是内置库, 无需require
+]]
+
+-- sys库是标配
+_G.sys = require("sys")
+--[[特别注意, 使用http库需要下列语句]]
+_G.sysplus = require("sysplus")
+
+httpdns = require "httpdns"
+
+
+-- Air780E的AT固件默认会为开机键防抖, 导致部分用户刷机很麻烦
+if rtos.bsp() == "EC618" and pm and pm.PWK_MODE then
+    pm.power(pm.PWK_MODE, false)
+end
+
+
+sys.taskInit(function()
+    -----------------------------
+    -- 统一联网函数, 可自行删减
+    ----------------------------
+    if rtos.bsp():startsWith("ESP32") then
+        -- wifi 联网, ESP32系列均支持
+        local ssid = "uiot"
+        local password = "12345678"
+        log.info("wifi", ssid, password)
+        -- TODO 改成esptouch配网
+        LED = gpio.setup(12, 0, gpio.PULLUP)
+        wlan.init()
+        wlan.setMode(wlan.STATION)
+        wlan.connect(ssid, password, 1)
+        local result, data = sys.waitUntil("IP_READY", 30000)
+        log.info("wlan", "IP_READY", result, data)
+        device_id = wlan.getMac()
+    elseif rtos.bsp() == "AIR105" then
+        -- w5500 以太网, 当前仅Air105支持
+        w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00)
+        w5500.config() --默认是DHCP模式
+        w5500.bind(socket.ETH0)
+        LED = gpio.setup(62, 0, gpio.PULLUP)
+        sys.wait(1000)
+        -- TODO 获取mac地址作为device_id
+    elseif rtos.bsp() == "EC618" then
+        -- Air780E/Air600E系列
+        --mobile.simid(2)
+        LED = gpio.setup(27, 0, gpio.PULLUP)
+        device_id = mobile.imei()
+        log.info("ipv6", mobile.ipv6(true))
+        sys.waitUntil("IP_READY", 30000)
+    end
+    log.info("已联网")
+    sys.publish("net_ready")
+end)
+
+sys.taskInit(function()
+    sys.waitUntil("net_ready")
+    while 1 do
+        sys.wait(1000)
+        -- 通过阿里DNS获取结果
+        local ip = httpdns.ali("air32.cn")
+        log.info("httpdns", "air32.cn", ip)
+
+
+        -- 通过腾讯DNS获取结果
+        local ip = httpdns.tx("openluat.com")
+        log.info("httpdns", "openluat.com", ip)
+    end
+end)
+
+
+-- 用户代码已结束---------------------------------------------
+-- 结尾总是这一句
+sys.run()
+-- sys.run()之后后面不要加任何语句!!!!!

+ 60 - 0
script/libs/httpdns.lua

@@ -0,0 +1,60 @@
+--[[
+@module httpdns
+@summary 使用Http进行域名解析
+@version 1.0
+@date    2023.07.13
+@author  wendal
+@usage
+-- 通过阿里DNS获取结果
+local ip = httpdns.ali("air32.cn")
+log.info("httpdns", "air32.cn", ip)
+
+-- 通过腾讯DNS获取结果
+local ip = httpdns.tx("air32.cn")
+log.info("httpdns", "air32.cn", ip)
+]]
+
+local httpdns = {}
+
+--[[
+通过阿里DNS获取结果
+@api httpdns.ali(domain_name)
+@string 域名
+@return string ip地址
+@usage
+local ip = httpdns.ali("air32.cn")
+log.info("httpdns", "air32.cn", ip)
+]]
+function httpdns.ali(n)
+    if n == nil then return end
+    local code, _, body = http.request("GET", "http://223.5.5.5/resolve?short=1&name=" .. tostring(n)).wait()
+    if code == 200 and body and #body > 2 then
+        local jdata = json.decode(body)
+        if jdata and #jdata > 0 then
+            return jdata[1]
+        end
+    end
+end
+
+
+--[[
+通过腾讯DNS获取结果
+@api httpdns.tx(domain_name)
+@string 域名
+@return string ip地址
+@usage
+local ip = httpdns.tx("air32.cn")
+log.info("httpdns", "air32.cn", ip)
+]]
+function httpdns.tx(n)
+    if n == nil then return end
+    local code, _, body = http.request("GET", "http://119.29.29.29/d?dn=" .. tostring(n)).wait()
+    if code == 200 and body and #body > 2 then
+        local tmp = body:split(",")
+        if tmp then return tmp[1] end
+    end
+end
+
+return httpdns
+
+