diff --git a/luci-app-passwall/luasrc/controller/passwall.lua b/luci-app-passwall/luasrc/controller/passwall.lua index e4c4318a6..1712f4dd7 100644 --- a/luci-app-passwall/luasrc/controller/passwall.lua +++ b/luci-app-passwall/luasrc/controller/passwall.lua @@ -95,6 +95,9 @@ function index() --[[Backup]] entry({"admin", "services", appname, "backup"}, call("create_backup")).leaf = true + + --[[geoview]] + entry({"admin", "services", appname, "geo_view"}, call("geo_view")).leaf = true end local function http_write_json(content) @@ -561,3 +564,53 @@ function create_backup() http.write(fs.readfile(tar_file)) fs.remove(tar_file) end + +function geo_view() + local action = luci.http.formvalue("action") + local value = luci.http.formvalue("value") + if not value or value == "" then + http.prepare_content("text/plain") + http.write(i18n.translate("Please enter query content!")) + return + end + local geo_dir = (uci:get(appname, "@global_rules[0]", "v2ray_location_asset") or "/usr/share/v2ray/"):match("^(.*)/") + local geosite_path = geo_dir .. "/geosite.dat" + local geoip_path = geo_dir .. "/geoip.dat" + local geo_type, file_path, cmd + local geo_string = "" + if action == "lookup" then + if api.datatypes.ipaddr(value) or api.datatypes.ip6addr(value) then + geo_type, file_path = "geoip", geoip_path + else + geo_type, file_path = "geosite", geosite_path + end + cmd = string.format("geoview -type %s -action lookup -input '%s' -value '%s'", geo_type, file_path, value) + geo_string = luci.sys.exec(cmd):lower() + if geo_string ~= "" then + local lines = {} + for line in geo_string:gmatch("([^\n]*)\n?") do + if line ~= "" then + table.insert(lines, geo_type .. ":" .. line) + end + end + geo_string = table.concat(lines, "\n") + end + elseif action == "extract" then + local prefix, list = value:match("^(geoip:)(.*)$") + if not prefix then + prefix, list = value:match("^(geosite:)(.*)$") + end + if prefix and list and list ~= "" then + geo_type = prefix:sub(1, -2) + file_path = (geo_type == "geoip") and geoip_path or geosite_path + cmd = string.format("geoview -type %s -action extract -input '%s' -list '%s'", geo_type, file_path, list) + geo_string = luci.sys.exec(cmd) + end + end + http.prepare_content("text/plain") + if geo_string and geo_string ~="" then + http.write(geo_string) + else + http.write(i18n.translate("No results were found!")) + end +end diff --git a/luci-app-passwall/luasrc/model/cbi/passwall/client/rule_list.lua b/luci-app-passwall/luasrc/model/cbi/passwall/client/rule_list.lua index ed099dd2b..15f04bd8a 100644 --- a/luci-app-passwall/luasrc/model/cbi/passwall/client/rule_list.lua +++ b/luci-app-passwall/luasrc/model/cbi/passwall/client/rule_list.lua @@ -313,6 +313,13 @@ end m:append(Template(appname .. "/rule_list/js")) +if api.is_finded("geoview") then + s:tab("geoview", translate("Geo View")) + o = s:taboption("geoview", DummyValue, "_geoview_fieldset") + o.rawhtml = true + o.template = appname .. "/rule_list/geoview" +end + function m.on_before_save(self) m:set("@global[0]", "flush_set", "1") end diff --git a/luci-app-passwall/luasrc/view/passwall/rule_list/geoview.htm b/luci-app-passwall/luasrc/view/passwall/rule_list/geoview.htm new file mode 100644 index 000000000..f5fc86901 --- /dev/null +++ b/luci-app-passwall/luasrc/view/passwall/rule_list/geoview.htm @@ -0,0 +1,81 @@ +<% +local api = require "luci.passwall.api" +-%> + + + +