对称加密实现重要日志上报Openresty接口服务
记录后端接收日志的流程;
由于记录的是广告数据,单次计费数据都会上报,全国内约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) do
GET[k]=v
end
for k,v in pairs(args_post) do
POST[k]=v
end
--生成加密的密钥
function unlock_mac (mac,password)
local int_iv = 0
local mac_len = string.len(mac)
for i = 1,mac_len do
int_iv = int_iv+string.byte(mac,i)
end
local 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 do
table.insert(tb,seed[math.random(1,16)])
end
local 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 then
local 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
end if GET["type"] == "app" or GET["type"] == "cd"
then
uidmac = GET["type"].."_uuid_"..mac
else
uidmac = "uuid_"..mac
end local redis = require "resty.redis_iresty"
local red = redis:new()
redis:auth("password")
redis:del(uidmac) uidtable = {}
local s=0
while s<50 do
s=s+1
print(CreateUUID())
local uuid = guid()
local ok, err = redis:hset(uidmac,uuid,"1")
if not ok then
local str = {code=10000,error_message="failed to set lbs"}
ngx.say(json.encode(str))
ngx.log(ngx.ERR,"setuidmac:",err)
return
end
uidtable[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) do
GET[k]=v
end
for k,v in pairs(args_post) do
POST[k]=v
end --生成加密的密钥
function unlock_mac (mac,password)
local int_iv = 0
local mac_len = string.len(mac)
for i = 1,mac_len do
int_iv = int_iv+string.byte(mac,i)
end
local 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 then
mac = GET["mac"]
else
ngx.say("Mac illegal")
return
end
local key = unlock_mac(mac,"(&*87-=KLJHuywe~s.,m-=") --验证data数据
if next(POST) ~= nil then
data = POST["data"]
else
ngx.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 then
local json = require("cjson")
json.encode_empty_table_as_object(false)
errdata = {data = {list ={}},code = 10000}
ngx.say (json.encode(errdata))
return
else
local dataObj = json.decode(unaes_val)
local redis = require "resty.redis_iresty"
local red = redis:new()
redis:auth("password")
uidmac = "uuid_"..mac
uuid = dataObj.uuid
local ok= redis:hget(uidmac,uuid)
if not ok then
ngx.say("failed to get uidmac: ", err)
ngx.log(ngx.ERR,"getuidmac:",err)
return
else Strmd5 = ngx.md5(dataObj.data)
if "lbs" ~= dataObj.type then
return
end
--ngx.say(dataObj.data)
if Strmd5 == dataObj.md5 then
local ok, err = redis:lpush('lbs_data_queue',unaes_val)
if not ok then
ngx.say("failed to push lbs_data_queue: ", err)
ngx.log(ngx.ERR,"lbs_data_queue:",err)
return
end
ngx.say("ok")
else
ngx.say("md5 check err")
ngx.log(ngx.ERR,"checkmd5","lbs_data_queue")
end end
end
对称加密实现重要日志上报Openresty接口服务的更多相关文章
- iOS CommonCrypto 对称加密 AES ecb,cbc
CommonCrypto 为苹果提供的系统加密接口,支持iOS 和 mac 开发: 不仅限于AES加密,提供的接口还支持其他DES,3DES,RC4,BLOWFISH等算法, 本文章主要讨论AES在i ...
- 个人理解c#对称加密 非对称加密 散列算法的应用场景
c#类库默认实现了一系列加密算法在System.Security.Cryptography; 命名空间下 对称加密 通过同一密匙进行加密和解密.往往应用在内部数据传输情况下.比如公司a程序 和B程序 ...
- .NET中的DES对称加密
DES是一种对称加密(Data Encryption Standard)算法,于1977年得到美国政府的正式许可,是一种用56位密钥来加密64位数据的方法.一般密码长度为8个字节,其中56位加密密钥, ...
- C#不对称加密
对称加密的缺点是双方使用相同的密钥和IV进行加密.解密.由于接收方必须知道密钥和IV才能解密数据,因此发送方需要先将密钥和IV传递给接收方.这就 有一个问题,如果攻击者截获了密钥和IV,也就等于知道了 ...
- openssl evp 对称加密(AES_ecb,ccb)
openssl evp 对称加密(AES_ecb,ccb) evp.h 封装了openssl常用密码学工具,以下主要说对称加密的接口 1. 如下使用 aes_256_ecb 模式的加密解密测试代码 u ...
- Java和.NET使用DES对称加密的区别
Java和.NET的系统类库里都有封装DES对称加密的实现方式,但是对外暴露的接口却各不相同,甚至有时会让自己难以解决其中的问题,比如Java加密后的结果在.NET中解密不出来等,由于最近项目有跨Ja ...
- DotNet加密方式解析--对称加密
离过年又近了一天,回家已是近在咫尺,有人欢喜有人愁,因为过几天就得经历每年一度的装逼大戏,亲戚朋友加同学的各方显摆,所以得靠一剂年终奖来装饰一个安稳的年,在这里我想起了一个题目“论装逼的技术性和重要性 ...
- Java加密与解密笔记(二) 对称加密
前面的仅仅是做了编码或者摘要,下面看看真正的加密技术. DES public class DESUtil { static final String ALGORITHM = "DES&quo ...
- 使用Aes对称加密解密Web.Config数据库连接串
现在很多公司开始为了保证数据库的安全性,通常会对Web.Config的数据库连接字符串进行加密.本文将介绍学习使用Aes加密解密数据库连接字符串.本文采用MySql数据库. AES概念简述 AES 是 ...
随机推荐
- Ubuntu 18.04 更换阿里源
1.备份 sudo cp /etc/apt/source.list /etc/apa/source.list.bak 2.编辑 sudo vim /etc/apt/source.list 3.修改内容 ...
- Vue触发隐藏input file的方法
1.使用input透明覆盖法 将input的z-index设置为1以上的数字并覆盖到需点击的内容上,将input的样式opacity设置为0(即为透明度为0),这样通过绑定在input上的change ...
- 【DSP开发】DSP能用VS2010生成的链接库文件吗?
[DSP开发]DSP能用VS2010生成的链接库文件吗? 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 说明:可能这个问题让行家看上去就会莞尔一笑,但是很多 ...
- Charles系列三:Charles打断点(包含修改请求,修改返回的内容),模拟慢速网络(弱网测试),域名映射,过滤请求,接口调试,打压测试
一:Charles断点的使用(包含修改请求,修改返回的数据) 设置断点来修改请求和返回的数据,在开发过程中可以模拟多种响应.步骤如下: 1.添加断点方法有两种: 方法1:找到Charles中菜单项Pr ...
- 顺序表的基本操作【c语言】【创建、插入、删除、输出】
作为数据结构初学者,上课时对一些知识点掌握得不是很透彻,所以利用课余时间通过微博平台总结所学知识,加深对知识的见解,记录学习历程便于后需要时参考. #include<stdio.h> #i ...
- ESXi 虚拟机 提示 无法打开本地虚拟机的 xxx.vmx 的本地管道的 问题解决.
1. 今天同事与我联系, 说一个虚拟机出现连不上, vcenter控制台关闭虚拟机之后 再次打开报错: 2. 自己最开始的解决方法 移除虚拟机, 进入服务器的datastore 重新注册, 结果发现问 ...
- vue中使用第三方插件animate.css实现动画效果
vue中使用第三方插件animate.css实现动画效果1.首先先引入第三方类animated.css2.将你所需要动画的标签用包裹起来3.在transition元素中添加enter-active-c ...
- SQL优化记录
2019.06.19记录: 1.SQL优化的原因: 原因:性能低,执行时间太长,等待时间太长,SQL语句欠佳(尤其连接查询),索引失效,服务器参数设置的不合理(如:缓冲区,线程等) a.SQL: 编写 ...
- Kubernetes-Service(服务)
⒈引用 在Kubernetes中,pod通常需要对来自集群内部的其他pod或来自集群外部的客户端的HTTP请求做出响应.pod需要一种寻找其他pod的方法来使用其他pod提供的服务,不像在没有Kube ...
- 关于日志slf4j+logback&logback.xml配置
1.maven依赖 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <!-- <dependen ...