--[[ @object-name: ipv6 @object-desc: ipv6设置 --]] local uci = require "uci" local rpc = require 'oui.rpc' local M = {} --[[ @method-type: call @method-name: get_ipv6 @method-desc: 获取ipv6的配置信息 @out bool enable 启用或禁用IPv6 @out string lan_mode LAN接口模式为 native/relay/nat6/static [ relay 在前端显示为 Passthrough ] . @out string lan_ip 当lan_mode为static时,为LAN接口的IPv6地址. @out bool lan_dns_mode LAN接口DNS服务器的模式,auto为true, manual为false. @out string lan_dns1 当lan_dnsmode为manual时,LAN接口的第一个DNS服务器地址. @out string lan_dns2 当lan_dnsmode为manual时,LAN接口的第二个DNS服务器地址. @out number ?err_code Error code,-1:获取信息失败. @out string ?err_msg Error message @in-example: {"jsonrpc":"2.0","id":1,"method":"call","params":["","ipv6","get_ipv6"]} @out-example: {"jsonrpc": "2.0", "id": 1, "result": {"enable": false, "lan_dns_mode": true, "lan_mode": "nat6"}} --]] function M.get_ipv6() local c = uci.cursor() local enable = c:get("glipv6", "globals", "enabled") == "1" local lan_mode = c:get("glipv6", "lan", "mode") local lan_ip if lan_mode == "static" then lan_ip = c:get("glipv6", "lan", "ip6addr") end local lan_dns_mode = c:get("glipv6", "lan", "dnsmode") local lan_dns = c:get("glipv6", "lan", "dns") local ipv6 = { enable = enable, lan_mode = lan_mode, lan_ip = lan_ip, lan_dns_mode = lan_dns_mode == "auto" } if lan_dns and lan_dns_mode ~= "auto" then if type(string.find(lan_dns,"#")) ~= "nil" then ipv6.lan_dns1, ipv6.lan_dns2 = lan_dns:match("([^#]+)#([^#]+)") else ipv6.lan_dns1 = lan_dns ipv6.lan_dns2 = "" end end if type(enable) == "nil" or type(lan_mode) ~= "string" or type(lan_dns_mode) == "nil" then return rpc.ERROR_CODE_INVALID_PARAMS end return ipv6 end --[[ @method-type: call @method-name: set_ipv6 @method-desc: 设置ipv6 @in bool enable 启用或禁用IPv6.[启用ipv6后,对应也会开启Repeater,Tethering,Modem 的ipv6] @in string lan_mode LAN接口模式为 native/relay/nat6/static [ relay 在前端显示为 Passthrough ]. @in string lan_ip 当lan_mode为static时,为LAN接口的IPv6地址. @in bool lan_dns_mode LAN接口DNS服务器的模式,auto为true, manual为false. @in string lan_dns1 当lan_dnsmode为manual时,LAN接口的第一个DNS服务器地址. @in string lan_dns2 当lan_dnsmode为manual时,LAN接口的第二个DNS服务器地址. @out number ?err_code Error code. @out string ?err_msg Error message @in-example: {"jsonrpc":"2.0","method":"call","params":["","ipv6","set_ipv6",{"enable":true,"lan_mode":"static","lan_ip":"fdab:6a57:e8f5:10:d14f:d19b:a63c:d7a5","lan_dns_mode":true,"lan_dns1":"fdab:6a57:e8f5:10::1","lan_dns2":"fdab:6a57:e8f5:10::2"}],"id":1} @out-example: {"jsonrpc": "2.0", "id": 1, "result": null} --]] function M.set_ipv6(params) local c = uci.cursor() local enable = params.enable local lan_mode = params.lan_mode local lan_ip = params.lan_ip local lan_dns_mode = params.lan_dns_mode local lan_dns1 = params.lan_dns1 local lan_dns2 = params.lan_dns2 local lan_dns if type(enable) == "boolean" and enable then c:set("glipv6", "globals", "enabled", "1") c:set("glipv6", "lan", "interface") if lan_mode == "native" then c:set("glipv6", "lan", "mode", "native") elseif lan_mode == "relay" then c:set("glipv6", "lan", "mode", "relay") elseif lan_mode == "nat6" then c:set("glipv6", "lan", "mode", "nat6") elseif lan_mode == "static" then c:set("glipv6", "lan", "mode", "static") if type(lan_ip) == "string" then c:set("glipv6", "lan", "ip6addr", lan_ip) else return rpc.ERROR_CODE_INVALID_PARAMS end else return rpc.ERROR_CODE_INVALID_PARAMS end if type(lan_dns_mode) == "boolean" and not lan_dns_mode then c:set("glipv6", "lan", "dnsmode", "manual") if lan_dns2 then if lan_dns1:gsub("%s+", "") ~= "" and lan_dns2:gsub("%s+", "") ~= "" then lan_dns = lan_dns1.."#"..lan_dns2 c:set("glipv6", "lan", "dns", lan_dns) elseif lan_dns2:gsub("%s+", "") ~= "" then c:set("glipv6", "lan", "dns", lan_dns2) end elseif lan_dns1:gsub("%s+", "") ~= "" then c:set("glipv6", "lan", "dns", lan_dns1) else return rpc.ERROR_CODE_INVALID_PARAMS end elseif type(lan_dns_mode) == "boolean" and lan_dns_mode then c:set("glipv6", "lan", "dnsmode", "auto") c:delete("glipv6", "lan", "dns") else return rpc.ERROR_CODE_INVALID_PARAMS end c:commit("glipv6") ngx.timer.at(1.0, function() ngx.pipe.spawn({"/etc/init.d/gl_ipv6", "restart"}):wait() ngx.pipe.spawn({"/etc/init.d/shortcut-fe", "restart"}):wait() end) else c:set("glipv6", "globals", "enabled", "0") c:commit("glipv6") ngx.timer.at(1.0, function() ngx.pipe.spawn({"/etc/init.d/gl_ipv6", "stop"}):wait() ngx.pipe.spawn({"/etc/init.d/shortcut-fe", "restart"}):wait() end) end end return M