--[[ @object-name: rtty @object-desc: Rtty 配置 --]] local utils = require "oui.utils" local rpc = require "oui.rpc" local ubus = require "oui.ubus" local uci = require "uci" local M = {} --[[ @method-type: call @method-name: get_config @method-desc: 查询当前配置 @out bool ssh_enabled 是否允许 ssh @out bool web_enabled 是否允许 web @in-example: {"jsonrpc":"2.0","id":1,"method":"call","params":["","rtty","get_config"]} @out-example: {"jsonrpc": "2.0", "id": 1, "result": {"ssh_enabled": true, "web_enabled": true}} --]] M.get_config = function() local c = uci.cursor() local ssh_enabled = c:get("rtty", "general", "ssh_en") == "1" local web_enabled = c:get("rtty", "general", "web_en") == "1" return { ssh_enabled = ssh_enabled, web_enabled = web_enabled } end --[[ @method-type: call @method-name: set_config @method-desc: 设置 rtty 功能 @in bool ssh_enable 是否允许 ssh @in bool web_enable 是否允许 web @out bool ssh_enabled 是否允许 ssh @out bool web_enabled 是否允许 web @in-example: {"jsonrpc":"2.0","id":1,"method":"call","params":["","rtty","set_config", {"ssh_enable": true, "web_enable": true}]} @out-example: {"jsonrpc": "2.0", "id": 1, "result": {"ssh_enabled": true, "web_enabled": true}} --]] M.set_config = function(params) local ssh_en = params.ssh_enable and 1 or 0 local web_en = params.web_enable and 1 or 0 local c = uci.cursor() c:set("rtty", "general", "ssh_en", ssh_en) c:set("rtty", "general", "web_en", web_en) return M.get_config() end --[[ @method-type: call @method-name: run @method-desc: 运行 rtty @in string ?token token @in string host 服务器地址 @in number port 服务器端口 @in bool ?ssl=false 服务器是否开启了 ssl @out number ?err_code 错误码: 1: 设备无有效的 DDNS ID, 2: rtty 连接服务器失败 @out number ?err_msg 错误信息 @in-example: {"jsonrpc":"2.0","id":1,"method":"call","params":["","rtty","run", {"token":"sfsedtretger124233as", "host": "192.168.3.1", "port":5912, "ssl": true}]} @out-example: {"jsonrpc": "2.0", "id": 1, "result": null} --]] M.run = function(params) local token = params.token local host = params.host local port = params.port local ssl = params.ssl or false if type(host) ~= "string" or type(port) ~= "number" then return rpc.ERROR_CODE_INVALID_PARAMS end local last_host_port = utils.readfile("/tmp/rtty-host-port", "*l") local need_kill = false if last_host_port ~= host .. port .. tostring(ssl or false) then need_kill = true end if need_kill then ubus.call("service", "delete", { name = "rtty" }) end local s = ubus.call("service", "list", { name = "rtty" }) if s and s["rtty"] and s["rtty"]["instances"] and s["rtty"]["instances"]["instance1"] and s["rtty"]["instances"]["instance1"].running then return end local ddns = utils.readfile("/proc/gl-hw-info/device_ddns", "l") if not ddns or ddns:match("[^%a%d_-]+") then return { err_code = 1, err_msg = "no valid ddns" } end local command = {"/usr/sbin/rtty", "-I", ddns, "-h", host, "-p", tostring(port), "-v"} if token then command[#command + 1] = "-t" command[#command + 1] = token end if ssl then command[#command + 1] = "-sx" end ubus.call("service", "add", { name = "rtty", instances = { instance1 = { command = command } } }) ngx.sleep(0.5) for i = 1, 5 do local s = ubus.call("service", "list", { name = "rtty" }) if s and s["rtty"] and s["rtty"]["instances"] and s["rtty"]["instances"]["instance1"] and s["rtty"]["instances"]["instance1"].running then utils.writefile("/tmp/rtty-host-port", host .. port .. tostring(ssl or false)) return end ngx.sleep(0.5) end return { err_code = 2, err_msg = "connect failed" } end --[[ @method-type: call @method-name: stop @method-desc: 停止 rtty @in-example: {"jsonrpc":"2.0","id":1,"method":"call","params":["","rtty","stop"]} @out-example: {"jsonrpc": "2.0", "id": 1, "result": {}} --]] M.stop = function() ubus.call("service", "delete", { name = "rtty" }) end return M