small-package/luci-app-vssr/root/usr/share/lua/vssrutil.lua

102 lines
20 KiB
Lua
Raw Normal View History

2021-09-05 16:50:22 +08:00
#!/usr/bin/lua
------------------------------------------------
-- This file is converter ip to country iso code
-- @author Jerryk <jerrykuku@qq.com>
------------------------------------------------
2022-04-21 12:14:00 +08:00
2021-09-05 16:50:22 +08:00
local _M = {}
2022-04-21 12:14:00 +08:00
2021-09-05 16:50:22 +08:00
-- Get country iso code with remark or host
-- Return String:iso_code
function _M.get_flag(remark, host)
2022-04-21 12:14:00 +08:00
local nixio = require 'nixio'
2022-01-08 14:28:09 +08:00
local json = require('cjson')
local json_string =
'[{"code":"AC","regx":["🇦🇨","AC","Ascension Island"]},{"code":"AD","regx":["安道尔","🇦🇩","AD","Andorra"]},{"code":"AE","regx":["阿联酋","🇦🇪","AE","United Arab Emirates"]},{"code":"AF","regx":["阿富汗","🇦🇫","AF","Afghanistan"]},{"code":"AG","regx":["安提瓜和巴布达","🇦🇬","AG","Antigua & Barbuda"]},{"code":"AI","regx":["安圭拉","🇦🇮","AI","Anguilla"]},{"code":"AL","regx":["阿尔巴尼亚","🇦🇱","AL","Albania"]},{"code":"AM","regx":["亚美尼亚","🇦🇲","AM","Armenia"]},{"code":"AO","regx":["安哥拉","🇦🇴","AO","Angola"]},{"code":"AQ","regx":["南极洲","🇦🇶","AQ","Antarctica"]},{"code":"AR","regx":["阿根廷","🇦🇷","AR","Argentina"]},{"code":"AS","regx":["美属萨摩亚","🇦🇸","AS","American Samoa"]},{"code":"AT","regx":["奥地利","🇦🇹","AT","Austria"]},{"code":"AU","regx":["澳大利亚","🇦🇺","AU","Australia"]},{"code":"AW","regx":["阿鲁巴","🇦🇼","AW","Aruba"]},{"code":"AX","regx":["奥兰群岛","🇦🇽","AX","Åland Islands"]},{"code":"AZ","regx":["阿塞拜疆","🇦🇿","AZ","Azerbaijan"]},{"code":"BA","regx":["波黑","🇧🇦","BA","Bosnia & Herzegovina"]},{"code":"BB","regx":["巴巴多斯","🇧🇧","BB","Barbados"]},{"code":"BD","regx":["孟加拉国","🇧🇩","BD","Bangladesh"]},{"code":"BE","regx":["比利时","🇧🇪","BE","Belgium"]},{"code":"BF","regx":["布基纳法索","🇧🇫","BF","Burkina Faso"]},{"code":"BG","regx":["保加利亚","🇧🇬","BG","Bulgaria"]},{"code":"BH","regx":["巴林","🇧🇭","BH","Bahrain"]},{"code":"BI","regx":["布隆迪","🇧🇮","BI","Burundi"]},{"code":"BJ","regx":["贝宁","🇧🇯","BJ","Benin"]},{"code":"BL","regx":["圣巴泰勒米岛","🇧🇱","BL","St. Barthélemy"]},{"code":"BM","regx":["百慕大","🇧🇲","BM","Bermuda"]},{"code":"BN","regx":["文莱","🇧🇳","BN","Brunei"]},{"code":"BO","regx":["玻利维亚","🇧🇴","BO","Bolivia"]},{"code":"BQ","regx":["荷兰加勒比区","🇧🇶","BQ","Caribbean Netherlands"]},{"code":"BR","regx":["巴西","🇧🇷","BR","Brazil"]},{"code":"BS","regx":["巴哈马","🇧🇸","BS","Bahamas"]},{"code":"BT","regx":["不丹","🇧🇹","BT","Bhutan"]},{"code":"BV","regx":["布韦岛","🇧🇻","BV","Bouvet Island"]},{"code":"BW","regx":["博茨瓦纳","🇧🇼","BW","Botswana"]},{"code":"BY","regx":["白俄罗斯","🇧🇾","BY","Belarus"]},{"code":"BZ","regx":["伯利兹","🇧🇿","BZ","Belize"]},{"code":"CA","regx":["加拿大","🇨🇦","CA","Canada"]},{"code":"CC","regx":["科科斯群岛","🇨🇨","CC","Cocos (Keeling) Islands"]},{"code":"CD","regx":["刚果(金)","🇨🇩","CD","Congo - Kinshasa"]},{"code":"CF","regx":["中非","🇨🇫","CF","Central African Republic"]},{"code":"CG","regx":["刚果(布)","🇨🇬","CG","Congo - Brazzaville"]},{"code":"CH","regx":["瑞士","🇨🇭","CH","Switzerland"]},{"code":"CI","regx":["科特迪瓦","🇨🇮","CI","Côte dIvoire"]},{"code":"CK","regx":["库克群岛","🇨🇰","CK","Cook Islands"]},{"code":"CL","regx":["智利","🇨🇱","CL","Chile"]},{"code":"CM","regx":["喀麦隆","🇨🇲","CM","Cameroon"]},{"code":"CN","regx":["中国;\r\n內地","🇨🇳","CN","China"]},{"code":"CO","regx":["哥伦比亚","🇨🇴","CO","Colombia"]},{"code":"CP","regx":["🇨🇵","CP","Clipperton Island"]},{"code":"CR","regx":["哥斯达黎加","🇨🇷","CR","Costa Rica"]},{"code":"CU","regx":["古巴","🇨🇺","CU","Cuba"]},{"code":"CV","regx":["佛得角","🇨🇻","CV","Cape Verde"]},{"code":"CW","regx":["库拉索","🇨🇼","CW","Curaçao"]},{"code":"CX","regx":["圣诞岛","🇨🇽","CX","Christmas Island"]},{"code":"CY","regx":["塞浦路斯","🇨🇾","CY","Cyprus"]},{"code":"CZ","regx":["捷克","🇨🇿","CZ","Czechia"]},{"code":"DE","regx":["德国","🇩🇪","DE","Germany"]},{"code":"DG","regx":["🇩🇬","DG","Diego Garcia"]},{"code":"DJ","regx":["吉布提","🇩🇯","DJ","Djibouti"]},{"code":"DK","regx":["丹麦","🇩🇰","DK","Denmark"]},{"code":"DM","regx":["多米尼克","🇩🇲","DM","
2021-09-05 16:50:22 +08:00
2022-01-08 14:28:09 +08:00
local search_table = json.decode(json_string)
2021-09-05 16:50:22 +08:00
local iso_code = nil
2022-04-21 12:14:00 +08:00
local delete_table = {'%b[]', 'networks', 'test', 'game', 'gaming', 'tls', 'iepl', 'aead', 'hgc', 'hkbn', 'netflix', 'disney', 'hulu', 'hinet','Sb','az','aws','cn'}
2021-09-05 16:50:22 +08:00
if (remark ~= nil) then
2022-01-08 14:28:09 +08:00
-- 过滤
2022-04-21 12:14:00 +08:00
remark = string.lower(remark)
for i, v in pairs(delete_table) do
remark = string.gsub(remark, v, '')
end
2022-01-08 14:28:09 +08:00
for i, v in pairs(search_table) do
for s, t in pairs(v.regx) do
2022-04-21 12:14:00 +08:00
if (string.find(remark, string.lower(t)) ~= nil) then
2022-01-08 14:28:09 +08:00
iso_code = string.lower(v.code)
break
end
2021-09-05 16:50:22 +08:00
end
end
end
2022-01-08 14:28:09 +08:00
if (iso_code == nil) then
2022-04-21 12:14:00 +08:00
if (host ~= '') then
local ret = nixio.getaddrinfo(_M.trim(host), 'any')
2022-01-08 14:28:09 +08:00
if (ret == nil) then
2022-04-21 12:14:00 +08:00
iso_code = 'un'
2021-09-05 16:50:22 +08:00
else
local hostip = ret[1].address
2022-01-08 14:28:09 +08:00
local status, code = pcall(_M.get_iso, hostip)
if (status) then
2021-09-05 16:50:22 +08:00
iso_code = code
else
2022-04-21 12:14:00 +08:00
iso_code = 'un'
2021-09-05 16:50:22 +08:00
end
end
else
2022-04-21 12:14:00 +08:00
iso_code = 'un'
2021-09-05 16:50:22 +08:00
end
end
return string.gsub(iso_code, '\n', '')
end
function _M.get_iso(ip)
local mm = require 'maxminddb'
local db = mm.open('/usr/share/vssr/GeoLite2-Country.mmdb')
local res = db:lookup(ip)
2022-04-21 12:14:00 +08:00
return string.lower(res:get('country', 'iso_code'))
2021-09-05 16:50:22 +08:00
end
function _M.get_cname(ip)
local mm = require 'maxminddb'
local db = mm.open('/usr/share/vssr/GeoLite2-Country.mmdb')
local res = db:lookup(ip)
2022-04-21 12:14:00 +08:00
return string.lower(res:get('country', 'names', 'zh-CN'))
2021-09-05 16:50:22 +08:00
end
-- Get status of conncet to any site with host and port
-- Return String:true or nil
function _M.check_site(host, port)
2022-04-21 12:14:00 +08:00
local nixio = require 'nixio'
local socket = nixio.socket('inet', 'stream')
socket:setopt('socket', 'rcvtimeo', 2)
socket:setopt('socket', 'sndtimeo', 2)
2021-09-05 16:50:22 +08:00
local ret = socket:connect(host, port)
socket:close()
return ret
end
function _M.trim(text)
2022-04-21 12:14:00 +08:00
if not text or text == '' then
return ''
end
return (string.gsub(text, '^%s*(.-)%s*$', '%1'))
2021-09-05 16:50:22 +08:00
end
function _M.wget(url)
2022-04-21 12:14:00 +08:00
local sys = require 'luci.sys'
local stdout =
sys.exec(
'wget-ssl -q --user-agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36" --no-check-certificate -t 3 -T 10 -O- "' .. url .. '"'
)
2021-09-05 16:50:22 +08:00
return _M.trim(stdout)
end
return _M