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

由于记录的是广告数据,单次计费数据都会上报,全国内约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接口服务的更多相关文章

  1. iOS CommonCrypto 对称加密 AES ecb,cbc

    CommonCrypto 为苹果提供的系统加密接口,支持iOS 和 mac 开发: 不仅限于AES加密,提供的接口还支持其他DES,3DES,RC4,BLOWFISH等算法, 本文章主要讨论AES在i ...

  2. 个人理解c#对称加密 非对称加密 散列算法的应用场景

    c#类库默认实现了一系列加密算法在System.Security.Cryptography; 命名空间下 对称加密 通过同一密匙进行加密和解密.往往应用在内部数据传输情况下.比如公司a程序 和B程序 ...

  3. .NET中的DES对称加密

    DES是一种对称加密(Data Encryption Standard)算法,于1977年得到美国政府的正式许可,是一种用56位密钥来加密64位数据的方法.一般密码长度为8个字节,其中56位加密密钥, ...

  4. C#不对称加密

    对称加密的缺点是双方使用相同的密钥和IV进行加密.解密.由于接收方必须知道密钥和IV才能解密数据,因此发送方需要先将密钥和IV传递给接收方.这就 有一个问题,如果攻击者截获了密钥和IV,也就等于知道了 ...

  5. openssl evp 对称加密(AES_ecb,ccb)

    openssl evp 对称加密(AES_ecb,ccb) evp.h 封装了openssl常用密码学工具,以下主要说对称加密的接口 1. 如下使用 aes_256_ecb 模式的加密解密测试代码 u ...

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

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

  7. DotNet加密方式解析--对称加密

    离过年又近了一天,回家已是近在咫尺,有人欢喜有人愁,因为过几天就得经历每年一度的装逼大戏,亲戚朋友加同学的各方显摆,所以得靠一剂年终奖来装饰一个安稳的年,在这里我想起了一个题目“论装逼的技术性和重要性 ...

  8. Java加密与解密笔记(二) 对称加密

    前面的仅仅是做了编码或者摘要,下面看看真正的加密技术. DES public class DESUtil { static final String ALGORITHM = "DES&quo ...

  9. 使用Aes对称加密解密Web.Config数据库连接串

    现在很多公司开始为了保证数据库的安全性,通常会对Web.Config的数据库连接字符串进行加密.本文将介绍学习使用Aes加密解密数据库连接字符串.本文采用MySql数据库. AES概念简述 AES 是 ...

随机推荐

  1. flask上下文管理相关-LocalStack 对象维护栈

    LocalStack 对象维护栈 模拟 import threading """ storage = { 1232: {stack:[123,456]} } " ...

  2. django 之(三) --- 认证|权限

    用户模块 登陆注册1:Django2.0  [ 1:N ] user/url.py from django.urls import path from user.views0 import UserT ...

  3. 详解Linux开源安全审计和渗透测试工具Lynis

    转载自FreeBuf.COM Lynis是一款Unix系统的安全审计以及加固工具,能够进行深层次的安全扫描,其目的是检测潜在的时间并对未来的系统加固提供建议.这款软件会扫描一般系统信息,脆弱软件包以及 ...

  4. tcp与串口透传(select)

    介绍 tcp作为服务端,监听端口8888,实现串口透传,这里是使用select监听tcp的receive和串口的read,单工通信 -p 指定tcp端口 -s 指定串口 -b 指定波特率 支持4800 ...

  5. mui ajax传参示例

    //加入购物车var data=[];var row1 = {good_id:'1',number:'2',goods_spec_ids:[1,2]};data.push(row1);console. ...

  6. 日历插件 js

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 数据库学习其一 oracle11g数据泵导入导出

    一.检查环境一致性 需检查数据库客户端与服务端字符编码,以避免后续各种各样的问题 查询服务端编码 注意最好用sqlplus查询,用plsql有时候会出现查询不一致问题,如下图同一个语句在plsql和s ...

  8. pymysql连接和操作Mysql数据库

    pymysql 一.概要 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库, 二.PyMySQL 安装 pip install pymysql 三.操作流程 创建c ...

  9. 1.5JdbcTmeplates、Jpa、Mybatis、beatlsql、Druid的使用

    Spring boot 连接数据库整合 -- create table `account`DROP TABLE `account` IF EXISTSCREATE TABLE `account` ( ...

  10. Colossal Fibonacci Numbers! UVA - 11582(快速幂,求解)

    Problem Description The i’th Fibonacci number f(i) is recursively defined in the following way: •f(0 ...