记录后端接收日志的流程;

由于记录的是广告数据,单次计费数据都会上报,全国内约10几万终端上报。

终端上报:Android电视端Apk上报

接收终端:Openresty(Nginx+lua)利用nginx非阻塞io来缓解服务器压力

数据处理:为了提高处理效率避免队列写死,采用go语言分析数据并入库

贴代码:生成uuid参数,获取客户端访问接口获取uuid,带着uuid参数上报

getuuid.lua:

--获取body数据,含get,post数据
--获取body数据,含get,post数据
local GET = {}
local POST = {}
ngx.req.read_body()
local  args_get= ngx.req.get_uri_args()
local args_post = ngx.req.get_post_args()
for k,v in pairs(args_get) doGET[k]=v
end
for k,v in pairs(args_post) doPOST[k]=v
end
--生成加密的密钥
function unlock_mac (mac,password)local  int_iv = 0local  mac_len = string.len(mac)for  i = 1,mac_len doint_iv = int_iv+string.byte(mac,i)endlocal mac_md5 = ngx.md5(mac)iv_byte = string.sub(mac_md5,1,1)..string.sub(mac_md5,3,5)..int_iv..ngx.md5(password)return string.sub(iv_byte,1,16)
end--AES解密
function unaes(key,data)local aes = require "resty.aes"local str = require "resty.string"local hash = {iv = "fedcba9876543210",method = nil
}
local salt = "0123456789abcdef"
local aes_128_cbc, err = aes:new(key, salt, aes.cipher(128,"cbc"), hash)
return aes_128_cbc:decrypt(data)
end--随机数
function CreateUUID()local template ="xxxxxxxxxxxx"d = io.open("/dev/urandom", "r"):read(4)math.randomseed(os.time() + d:byte(1) + (d:byte(2) * 256) + (d:byte(3) * 65536) + (d:byte(4) * 4294967296))return string.gsub(template, "x", function (c)local v = (c == "x") and math.random(0, 0xf) or math.random(8, 0xb)return string.format("%x", v)end)
end--生成uuid
function guid()local seed={'e','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}local tb={}for i=1,32 dotable.insert(tb,seed[math.random(1,16)])endlocal sid=table.concat(tb)return string.format('%s-%s-%s-%s-%s',string.sub(sid,1,8),string.sub(sid,9,12),string.sub(sid,13,16),string.sub(sid,17,20),string.sub(sid,21,32))
end--获取解密key
local  mac = GET["mac"]
local password = "(&*87-=KLJHuywe~s.,m-="
local key = unlock_mac(mac,password)
--获取post数据进行解析local post_val = POST["data"]
local unaes_val,err = unaes(key,ngx.decode_base64(post_val))
if  unaes_val == nil thenlocal json = require("cjson")json.encode_empty_table_as_object(false)local str = {code=10000,error_message="unaes err"}ngx.say(json.encode(str))ngx.log(ngx.ERR, " unaes_val:", unaes_val)return
endif GET["type"] == "app" or GET["type"] == "cd"thenuidmac = GET["type"].."_uuid_"..mac
elseuidmac = "uuid_"..mac
endlocal redis = require "resty.redis_iresty"
local red = redis:new()
redis:auth("password")
redis:del(uidmac)uidtable = {}
local s=0
while s<50 dos=s+1print(CreateUUID())local uuid = guid()local ok, err = redis:hset(uidmac,uuid,"1")if not ok thenlocal str = {code=10000,error_message="failed to set lbs"}ngx.say(json.encode(str))ngx.log(ngx.ERR,"setuidmac:",err)returnenduidtable[s] = uuid
end
local json = require("cjson")--ngx.say(uidtable)datatable = {data = {list =uidtable},code = 20000}ngx.say(json.encode(datatable))ngx.exit(200)  

接收日志,存入队列:

--获取body数据,get,post数据
local GET = {}
local POST = {}
ngx.req.read_body()
local  args_get= ngx.req.get_uri_args()
local args_post = ngx.req.get_post_args()
for k,v in pairs(args_get) doGET[k]=v
end
for k,v in pairs(args_post) doPOST[k]=v
end--生成加密的密钥
function unlock_mac (mac,password)local  int_iv = 0local  mac_len = string.len(mac)for  i = 1,mac_len doint_iv = int_iv+string.byte(mac,i)endlocal mac_md5 = ngx.md5(mac)iv_byte = string.sub(mac_md5,1,1)..string.sub(mac_md5,3,5)..int_iv..ngx.md5(password)return string.sub(iv_byte,1,16)
end--AES解密
function unaes(key,data)local aes = require "resty.aes"local str = require "resty.string"local hash = {iv = "fedcba9876543210",method = nil
}
local salt = "0123456789abcdef"
local aes_128_cbc, err = aes:new(key, salt, aes.cipher(128,"cbc"), hash)
return aes_128_cbc:decrypt(data)
end--获取mac并验证
if next(GET) ~=nil and string.len(GET["mac"]) == 12 thenmac = GET["mac"]
elsengx.say("Mac illegal")return
end
local key = unlock_mac(mac,"(&*87-=KLJHuywe~s.,m-=")--验证data数据
if next(POST) ~= nil thendata = POST["data"]
elsengx.say("data is nil")return
end-- ngx.say(key)
-- ngx.say(data)
local json = require("cjson")
local unaes_val,err = unaes(key,ngx.decode_base64(data))
--ngx.say(unaes_val)
if  unaes_val == nil thenlocal json = require("cjson")json.encode_empty_table_as_object(false)errdata = {data = {list ={}},code = 10000}ngx.say (json.encode(errdata))return
elselocal dataObj = json.decode(unaes_val)local redis = require "resty.redis_iresty"local red = redis:new()redis:auth("password")uidmac = "uuid_"..macuuid = dataObj.uuidlocal ok= redis:hget(uidmac,uuid)if not ok thenngx.say("failed to get uidmac: ", err)ngx.log(ngx.ERR,"getuidmac:",err)returnelseStrmd5 = ngx.md5(dataObj.data)if "lbs" ~= dataObj.type thenreturnend--ngx.say(dataObj.data)if Strmd5 == dataObj.md5 thenlocal ok, err = redis:lpush('lbs_data_queue',unaes_val)if not ok thenngx.say("failed to push lbs_data_queue: ", err)ngx.log(ngx.ERR,"lbs_data_queue:",err)returnendngx.say("ok")elsengx.say("md5 check err")ngx.log(ngx.ERR,"checkmd5","lbs_data_queue")endend
end

  

转载于:https://www.cnblogs.com/liuquan/p/10417231.html

对称加密实现重要日志上报Openresty接口服务相关推荐

  1. 编写ATL工程实现ActiveX控件调用cryptoAPI接口(三)------------AES对称加密与解密

    注:下面的代码中用了Map,Base64,log,Result等都为自定义类型,太长就不一一贴出. [cpp]  view plain  copy  print ? /* * * * 文件名称:Enc ...

  2. Java和.NET使用DES对称加密的区别

    Java和.NET的系统类库里都有封装DES对称加密的实现方式,但是对外暴露的接口却各不相同,甚至有时会让自己难以解决其中的问题,比如Java加密后的结果在.NET中解密不出来等,由于最近项目有跨Ja ...

  3. .NET中的密码学--对称加密

    作者:未知 介绍 在.NET之前,使用非托管的Win32APIs加密解密数据是一件非常痛苦的事情.为了这个加密解密的目的,.NET配置了一组类(和命名空间).现在你有很多类可以使用每种不同的算法保护你 ...

  4. golang中的对称加密

    概念 按位异或 第一步需要将数据转换为二进制 按位异或操作符: ^ 两个标志位进行按位异或操作: 相同为0, 不同为1 举例: 1 0 0 0 ----> 8 1 0 1 1 ----> ...

  5. Android安全加密:对称加密

    Android安全加密专题文章索引 Android安全加密:对称加密 Android安全加密:非对称加密 Android安全加密:消息摘要Message Digest Android安全加密:数字签名 ...

  6. JWT对称加密非对称加密

    JWT JWT原理图: JWT的数据结构 jwt头:JWT头部分是一个描述JWT元数据的JSON对象 有效载荷:七个默认字段+自定义私有字段 签名=HMACSHA256(base64UrlEncode ...

  7. Android+Java中使用Aes对称加密的工具类与使用

    场景 Android+Java中使用RSA加密实现接口调用时的校验功能: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/11146 ...

  8. 对称密钥加密算法 对称轮数_选择Java加密算法第2部分–单密钥对称加密

    对称密钥加密算法 对称轮数 抽象 这是涵盖Java加密算法的三部分博客系列的第2部分. 该系列涵盖如何实现以下功能: 使用SHA–512散列 AES–256 RSA–4096 这第二篇文章详细介绍了如 ...

  9. 选择Java加密算法第2部分–单密钥对称加密

    抽象 这是涵盖Java加密算法的三部分博客系列的第2部分. 本系列介绍如何实现以下目标: 使用SHA–512散列 AES–256 RSA–4096 这第二篇文章详细介绍了如何实现单密钥对称AES-25 ...

最新文章

  1. C++:随笔9----模板/内联函数/容器
  2. Re-ID新方法VPM
  3. matlab温度数据怎么滤波_卡尔曼滤波算法思想理解 Kalman filter 第一篇
  4. python音频聚类_Python实现聚类算法AP
  5. UA MATH567 高维统计II 随机向量4 Frame、凸性与各向同性
  6. 20行Python代码教你让视频字符化
  7. 大学物理质点动力学思维导图_生理学 | 思维导图
  8. ASP.NET数据库编程入门
  9. NanShan作者:风景未变人已变
  10. 股票买卖(信息学奥赛一本通-T1302)
  11. 吴恩达机器学习之单变量线性回归理论部分
  12. 随机森林的原理分析及Python代码实现
  13. 兔子生兔子java_用Java编程计算兔子生兔子的问题
  14. 电磁波传播matlab程序,电磁波在不同介质中传播的 MATLAB 仿真教学实践论文
  15. word里边页眉下面的下划线怎么去掉
  16. 半导体车间净化工程的空气洁净度划分等级
  17. int, long, long long类型的范围
  18. STP协议 与 STP树的生成
  19. [算法]Morse Code(摩斯密码)
  20. Big Sur 2K显示器HiDPI修改

热门文章

  1. Java 8 - Optional全解
  2. MySQL - 共享锁和排它锁初探
  3. JVM-09自动内存管理机制【内存分配和回收策略】
  4. Spring-AOP 混合使用各种切面类型及不同切面总结
  5. CSS基础_Day03
  6. java set spliterator_Java TreeSet spliterator()方法
  7. 学习笔记Kafka(六)—— Kafka Consumer API及开发实例
  8. 为什么用redis?
  9. docker 安装nginx 配置目录挂载
  10. php网站添加cnzz,cnzz代码添加元素到页面