require 'cibn.tool'; require 'cibn.dictionary'; do local function readconts(filepath) local contents = {}; for ipline in io.lines(filepath) do if ipline ~= nil then contents = {next = contents , value = ipline}; else break; end end return contents; end local function init_ip2zone(conts) local tzone = dictionary.dict(); while conts do if conts.value == nil then break; end iplist = tool.split(tostring(conts.value), ';'); if iplist ~= nil then ipzone = iplist[1]; area = string.sub(iplist[2], 2); ips = tool.split(tostring(ipzone), '/'); if ips ~= nil then ip = ips[1]; mask = ips[2]; bin = tool.ip2bin(ip); if bin ~= nil then binkey = string.sub(bin, 1, tonumber(mask)); tzone.set(binkey, area); end else ngx.say('ipzone = ' .. ipzone .. '
'); break; end else ngx.log(ngx.ERR, 'the config of ip is error!'); os.exit(1); end if conts.next.value ~= nil then conts = conts.next; else break; end end return tzone; end local function search_zone(dd, bip) local lip = bip; local area = dd.get(lip); if area then return area; else nextlen = #lip - 1; if nextlen >= 1 then lip = string.sub(lip, 1, nextlen); return search_zone(dd, lip); else return 'default'; end end end local function init_isp2node(ispconts) local tnode = dictionary.dict(); local tflag = dictionary.dict(); while ispconts do if ispconts.value == nil then break; end ins = tool.split(tostring(ispconts.value) , ':'); if ins ~= nil then isp = ins[1]; istart, iend = string.find(isp, '#'); if istart ~= nil then isp = string.sub(isp, 1, istart - 1); flag = string.upper(string.sub(isp, iend)); if flag == nil then flag = 'P'; end tflag.set(isp, flag); else flag = 'P'; tflag.set(isp, flag); end nodelist = ins[2]; if string.sub(nodelist, -1) == ';' then nodelist = string.sub(nodelist, 1, -2); end tnode.set(isp, nodelist); else ngx.log(ngx.ERR, 'the config of ip is error!'); os.exit(1); end if ispconts.next.value ~= nil then ispconts = ispconts.next; else break; end end return tflag, tnode; end local function search_percent_node(ds, tisp) local nodes = ds.get(tisp); local nnodelist = {}; local nn = dictionary.dict(); local tp = 0; if nodes ~= nil then nstart, nend = string.find(nodes, ';'); if nstart ~= nil then nlist = tool.split(nodes, ';'); for i = 1, table.getn(nlist) do nnstart, nnend = string.find(nlist[i], '#'); if nnstart ~= nil then nnode = string.sub(nlist[i], 1, nnstart - 1); npercent = tonumber(string.sub(nlist[i], nnend + 1)); if nnode == nil then ngx.log(ngx.ERR, 'the config file of isp2node is error. at ' .. nlist[i]); ngx.exit(509); elseif npercent == nil then npercent = 1; end else nnode = nlist[i]; npercent = 1; end pzone = tostring(tp) .. '-' .. tostring(tp + npercent); nn.set(nnode, pzone); tp = tp + npercent; table.insert(nnodelist, nnode); end --ngx.say('tp = ' .. tp .. '
'); math.randomseed(os.time()); rp = math.random(1, tp); --ngx.say('rp = ' .. rp .. '
'); for i = 1, table.getn(nnodelist) do pz = nn.get(nnodelist[i]); ps = tool.split(pz, '-'); if tonumber(ps[1]) < rp and rp <= tonumber(ps[2]) then node = nnodelist[i]; break; end end return node; else nnstart, nnend = string.find(nodes, '#'); if nnstart ~= nil then node = string.sub(nodes, nnstart - 1); else node = nodes; end end return node; else return 'default'; end end local function init_slist(slconts) local sloop = dictionary.dict(); local single = dictionary.dict(); local sflag = dictionary.dict(); local spoint = dictionary.dict(); local sring = dictionary.dict(); while slconts do if slconts.value == nil then break; end local aring = dictionary.dict(); local apoint = {}; servers = tool.split(tostring(slconts.value) , '#'); if servers ~= nil then node = servers[1]; sslist = tostring(servers[2]); if string.sub(sslist, -1) == ';' then sslist = string.sub(sslist, 1, #sslist - 1); end f_start, f_end = string.find(sslist, ':'); if f_start ~= nil then ss = tool.split(sslist, ':'); if ss[1] == nil then ss[1] = 'l'; end if ss[2] == nil then ngx.log(ngx.ERR, 'the config of serverlist is error! at ' .. sslist); os.exit(1); end flag = ss[1]; sslist = ss[2]; sflag.set(node, flag); else ngx.log(ngx.ERR, 'the config of serverlist is error! search servers without flag.'); os.exit(1); end t_start, t_end = string.find(sslist, ';'); if t_start ~= nil then if flag == 'h' then hosts = tool.split(sslist, ';'); for k,v in ipairs(hosts) do for i = 1, 500 do seed = string.format('%s-%03d', v, i); point = ring.str2point(seed); aring.set(point, v); table.insert(apoint, point); end end table.sort(apoint); sring.set(node, aring); spoint.set(node, apoint); elseif flag == 'l' then hosts = tool.split(sslist, ';'); sloop.set(node, hosts); end else flag = 's'; sflag.set(node, flag); single.set(node, sslist); end end if slconts.next.value ~= nil then slconts = slconts.next; else break; end end return sflag, spoint, sring, single, sloop; end local function init_spec(conts) local domain2node = dictionary.dict(); while conts do if conts.value == nil then break; end tstart, tend = string.find(conts.value, ':'); if tstart == nil then ngx.log(ngx.ERR, 'the config file of specdomain is error. at ' .. conts.value); os.exit(1); end ninfo = tool.split(conts.value, ':'); if ninfo ~= nil then tnode = ninfo[1]; dstart, dend = string.find(ninfo[2], '#'); if dstart == nil then ngx.log(ngx.ERR, 'the config file of specdomain is error. at ' .. ninfo[2]); os.exit(1); end d2n = tool.split(ninfo[2], '#'); domain2node.set(string.lower(d2n[1]), string.lower(d2n[2])); end if conts.next.value ~= nil then conts = conts.next; else break; end end if domain2node.get('default') == nil then ngx.log(ngx.ERR, 'the config file of specdomain is error. the default domain must be set.'); os.exit(1); end return tnode, domain2node; end base = { readconts = readconts, init_ip2zone = init_ip2zone, search_zone = search_zone, init_isp2node = init_isp2node, search_percent_node = search_percent_node, init_slist = init_slist, init_spec = init_spec, } end