Lua脚本解析web端发送的数据与浏览器的useragent
博客专区 > -九天- 的博客 > 博客详情
Lua脚本解析web端发送的数据与浏览器的useragent
-九天- 发表于8个月前
Lua脚本解析web端发送的数据与浏览器的useragent
  • 发表于 8个月前
  • 阅读 13
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】买域名送云解析+SSL证书+建站!>>>   

local cjson = require("cjson")
local cjson_safe = require("cjson.safe")
local logger = require("logger")
local uuid4= require("uuid4")
local args   = {}
local request_method = ngx.var.request_method
function string.split(str, delimiter)
	if str==nil or str=='' or delimiter==nil then
		return nil
	end
    local result = {}
    for match in (str..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match)
    end
    return result
end


function string.trim (s) return (string.gsub(s, "^%s*(.-)%s*$", "%1")) end

function getUAField(t)
	local separator=';'
	local tab={}

	local android=string.find(t,"Android")
        local iphone=string.find(t,"Mac")
	local windows=string.find(t,"Windows")

	--android手机
	if android  then
		tab["platform"]="android"
    		-- tab["number"]=android
    		tab["ismobiledevice"]="true"
		local startIndex =string.find(t,"Build")
		if startIndex then
			local res=string.sub(t,0,startIndex-1)
			local rtable=string.split(res,separator)
			local devicename=string.trim(rtable[#rtable])
			local name=string.find(t,"XiaoMi")
			if name then
				local name1 = string.sub(t,name,name+6)
				tab["number"]=name
				if devicename~=nil then
					tab["devicename"]=name1 .. "/" .. devicename
				end
			else
				if devicename~=nil then
					tab["devicename"]=devicename
				end
			end
		end

		local osvTable=string.split(string.sub(t,android) ,separator)
		if osvTable then
			local osvTab=string.split(osvTable[1] ,' ')
			tab["os_version"]=string.trim(osvTab[#osvTab])
		end
	end

	--iphone手机
	if iphone then
		local ipho = string.find(t,"iPhone")
		local mac = string.find(t,"Macintosh")
		if ipho then
			tab["platform"]="iOS"
			tab["devicename"]="iphone"
			tab["number"]=ipho	
			tab["ismobiledevice"]="true"
 			local vs=string.find(t,"CPU")	
			if vs then
			    local osvTable=string.sub(t,vs)
			    local osvTable1 = string.split(osvTable,' ')
		            local osversion = string.trim(osvTable1[4])
			    tab["os_version"]=osversion
		        end	
		end
		--mac电脑
		if mac then
			tab["platform"]="Mac"
			tab["number"]=mac
			tab["devicename"]="PC"
			tab["ismobiledevice"]="flase"
			local osvers = string.sub(t,mac)
			local osversi= string.split(osvers,separator)
			if osversi then
				local osvTab = string.split(osversi[2],' ')
				table["os_version"]=string.trim(osvTab[5])
			end
		end
	end
 	--windows电脑
    if windows  then
		tab["platform"]="Windows"
		tab["ismobiledevice"]="flase"
		local osvTable=string.split(string.sub(t,windows) ,separator)
		if osvTable then
			local osvTab=string.split(osvTable[1] ,' ')
			tab["os_version"]=string.trim(osvTab[#osvTab])
			tab["devicename"]="PC"
		end
	end

	if  next(tab)  == nil then
		tab["platform"]=""
		tab["devicename"]=""
		tab["os_version"]=""
		tab["number"]=""
		tab["ismobiledevice"]=""
	end

	return tab

end

--获取参数的值
if "GET" == request_method then
    args = ngx.req.get_uri_args()
elseif "POST" == request_method then
    ngx.req.read_body()
    args = ngx.req.get_post_args()
end

function getTimeStamp(t)
    return os.date("%Y-%m-%d %H:%M:%S",t)
end

if next(args) ~= nil then
	response = '{"flag":"-1", "msg":"content error"}'
	local UATable,err=getUAField(ngx.var.http_user_agent)



	--cd
	cd_arr = {}
	cd_arr["flag"] = "-1"
	cd_arr["havebt"] = "true"
	cd_arr["havegravity"] = "true"
	cd_arr["havewifi"] = "true"
	cd_arr["havegps"] = "true"
	cd_arr["devicename"]=UATable["devicename"]
	cd_arr["os_version"]=UATable["os_version"]
	cd_arr["platform"]=UATable["platform"]
    cd_arr["number"]=UATable["number"]
    cd_arr["ismobiledevice"]=UATable["ismobiledevice"]
	cd_arr["version"]="1.0.0"
	cd_arr["isjailbreak"]="flase"
	cd_arr["modulename"]=""
	cd_arr["phonenumber"]=""
	cd_arr["network"]=""
	cd_arr["latitude"]=""
	cd_arr["imsi"]=""
	cd_arr["lac"]=""
	cd_arr["wifimac"]=""
	cd_arr["longitude"]=""
	cd_arr["mccmnc "]=""
	cd_arr["cellid"]=""

	--event
	event_arr={}
	event_arr["version"]="1.0.0"
	event_arr["event_identifier"] = "-1"

	--usinglog
	usinglog_arr={}
	usinglog_arr["devicename"]=UATable["devicename"]
	usinglog_arr["version"]="1.0.0"
	usinglog_arr["duration"] = "-1"


	for key, val in pairs(args) do

		if key == "appkey" then
			event_arr["appkey"]=val
			usinglog_arr["appkey"]=val
			cd_arr["appkey"]=val
		elseif key == "acc" then
			event_arr["acc"]=val
		elseif key == "activity" then
			event_arr["activity"]=val
			usinglog_arr["activity"]=val
		elseif key == "session_id" then
			event_arr["session_id"]=val
			usinglog_arr["session_id"]=val
		elseif key == "label" then
			event_arr["label"]=val
		elseif key == "deviceid" then
			event_arr["deviceid"]=val
			usinglog_arr["deviceid"]=val
			cd_arr["deviceid"]=val
		elseif key == "time" then
			event_arr["time"]=val
			cd_arr["time"]=val
		elseif key == "event_identifier" then
			event_arr["event_identifier"]=val
			cd_arr["event_identifier"]=val
		elseif key == "start_millis" then
			usinglog_arr["start_millis"]=val
		elseif key == "duration" then
			usinglog_arr["duration"]=val
		elseif key == "end_millis" then
			usinglog_arr["end_millis"]=val
		elseif key == "lang" then
			cd_arr["language"]=val
		elseif key=="flag" then
			cd_arr["flag"]=val
		elseif key=="hw" then
			cd_arr["resolution"]=val
		end

		response = '{"flag":"1", "msg":"OK"}'
	end


	if event_arr["event_identifier"]  ~= "-1" then
		if event_arr["time"] ~= nil then
			event_arr["time"] = getTimeStamp(event_arr["time"]/1000)
		end
		event = {}
		event["data"] = event_arr
		log_line, err = cjson_safe.encode(event)
		log_file = ngx.var.log_file_event .. "." .. os.date(ngx.var.date_fmt, ngx.time())
		logger.info(log_file, log_line)
	elseif cd_arr["flag"] ~= "-1" then
		if cd_arr["time"] ~= nil then
			cd_arr["time"] = getTimeStamp(cd_arr["time"]/1000)
		end
		cd = {}
		cd["data"] = cd_arr
		log_line, err = cjson_safe.encode(cd)
		log_file = ngx.var.log_file_cd .. "." .. os.date(ngx.var.date_fmt, ngx.time())
		logger.info(log_file, log_line)
	elseif usinglog_arr["duration"]  ~= "-1" then
		if usinglog_arr["start_millis"] ~=nil and usinglog_arr["end_millis"] ~=nil then
			usinglog_arr["end_millis"]=getTimeStamp(usinglog_arr["end_millis"]/1000)
			usinglog_arr["start_millis"]=getTimeStamp(usinglog_arr["start_millis"]/1000)
		end
		usinglog = {}
		usinglog["data"] = usinglog_arr
		log_line, err = cjson_safe.encode(usinglog)
		log_file = ngx.var.log_file_usinglog .. "." .. os.date(ngx.var.date_fmt, ngx.time())
		logger.info(log_file, log_line)

		
	else
		response = '{"flag":"-1", "msg":"' .. err .. '"}'
	end

	ngx.say(response)
else
  ngx.say('{"flag":"-1", "msg":"No post data found"}')

end


  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 4
博文 58
码字总数 47309
×
-九天-
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: