付智勇

no message

{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}/bin/www"
}
]
}
\ No newline at end of file
... ...
const router = require('koa-router')()
var userContoller = require('../controller/userController')
var status = require('../util/resTemplate')
const inspectMediaChannelKey = require('../util/inspectMediaChannelKey')
router.prefix('/users')
... ... @@ -106,7 +108,21 @@ router.put('/updatePwByTelphone',async(ctx, next)=>{
})
/**
* 加密token
*/
router.post('/encryptionToken',async (ctx, next) => {
try{
let body = ctx.request.body;
let inspectMediaChannel = await inspectMediaChannelKey(body.appID,body.appCertificate,body.channel,body.uid)
ctx.response.status= 200;
ctx.response.body= inspectMediaChannel
}catch(e){
console.log(e)
status.catchError(ctx,400,e.message);
}
})
module.exports = router
... ...
module.exports = ['/',"/users/addUser","/users/login",'/users/addUser?time=1',]
\ No newline at end of file
module.exports = ['/',"/users/addUser","/users/login",'/users/addUser?time=1','/users/encryptionToken']
\ No newline at end of file
... ...
var crypto = require('crypto');
var appID = "970ca35de60c44645bbae8a215061b33";//传参
var appCertificate = "5cfd2fd1755d40ecb72977518be15d3b";//传参
var channel = "7d72365eb983485397e3e3f9d460bdda"; //传参
var ts = 1446455472;
var r = 58964981;
var uid=2882341273; //传参
var expiredTs=1446455471;
var version = "005";
var noUpload = "0";
var audioVideoUpload = "3";
var encodeHMac = function(key, message) {
return crypto.createHmac('sha1', key).update(message).digest('hex').toUpperCase();
};
var hexDecode = function(str) {
return new Buffer(str, 'hex');
};
var Message = function(options) {
options.pack = function() {
var out = ByteBuf();
return out.putUint16(options.serviceType)
.putBytes(options.appID)
.putUint32(options.unixTs)
.putUint32(options.salt)
.putString(options.channelName)
.putUint32(options.uid)
.putUint32(options.expiredTs)
.putTreeMap(options.extra)
.pack();
}
return options;
}
// InChannelPermissionKey
var ALLOW_UPLOAD_IN_CHANNEL = 1;
// Service Type
var MEDIA_CHANNEL_SERVICE = 1;
var RECORDING_SERVICE = 2;
var PUBLIC_SHARING_SERVICE = 3;
var IN_CHANNEL_PERMISSION = 4;
var inspectMediaChannelKey = function async(appID, appCertificate, channelName, unixTs, randomInt, uid, expiredTs) {
var rawAppID = hexDecode(appID);
console.log('App ID:\t\t\t ' + rawAppID.toString('hex').toUpperCase());
var rawAppCertificate = hexDecode(appCertificate);
console.log('App Certificate:\t ' + rawAppCertificate.toString('hex').toUpperCase());
var serviceType = MEDIA_CHANNEL_SERVICE;
var extra = null;
var ByteBuf = function() {
var that = {
buffer: new Buffer(1024)
, position: 0
};
that.buffer.fill(0);
that.pack = function() {
var out = new Buffer(that.position);
that.buffer.copy(out, 0, 0, out.length);
return out;
};
that.putUint16 = function(v) {
try {
console.log(typeof v)
that.buffer.writeUInt16LE(v, that.position);
that.position += 2;
} catch (error) {
console.log('buffer',error)
}
return that;
};
that.putUint32 = function(v) {
that.buffer.writeUInt32LE(v, that.position);
that.position += 4;
return that;
};
that.putBytes = function(bytes) {
that.putUint16(bytes.length);
bytes.copy(that.buffer, that.position);
that.position += bytes.length;
return that;
};
that.putString = function(str) {
return that.putBytes(new Buffer(str));
};
that.putTreeMap = function(map) {
if (!map) {
that.putUint16(0);
return that;
}
that.putUint16(Object.keys(map).length);
for (var key in map) {
that.putUint16(key);
that.putString(map[key]);
}
return that;
};
return that;
}
var Message = function(options) {
console.log('s生成buffer')
options.pack = function() {
var out = ByteBuf();
return out.putUint16(options.serviceType)
.putBytes(options.appID)
.putUint32(options.unixTs)
.putUint32(options.salt)
.putString(options.channelName)
.putUint32(options.uid)
.putUint32(options.expiredTs)
.putTreeMap(options.extra)
.pack();
}
return options;
}
var m = Message({
serviceType: serviceType
, appID: rawAppID
, unixTs: unixTs
, salt: randomInt
, channelName: channelName
, uid: uid
, expiredTs: expiredTs
, extra: extra
});
var toSign = m.pack();
console.log("Message to sign:\t " + toSign.toString('hex').toUpperCase());
var signature = encodeHMac(rawAppCertificate, toSign);
console.log("Signature:\t\t " + signature.toString('hex').toUpperCase());
var DynamicKey5Content = function(options) {
options.pack = function() {
var out = ByteBuf();
return out.putUint16(options.serviceType)
.putString(options.signature)
.putBytes(options.appID)
.putUint32(options.unixTs)
.putUint32(options.salt)
.putUint32(options.expiredTs)
.putTreeMap(options.extra)
.pack();
}
return options;
}
var content = DynamicKey5Content({
serviceType: serviceType
, signature: signature
, appID: hexDecode(appID)
, unixTs: unixTs
, salt: randomInt
, expiredTs: expiredTs
, extra: extra}).pack();
console.log("Content to encode:\t " + content.toString('hex').toUpperCase());
var channelKey = version + content.toString('base64');
console.log("Channel key:\t\t " + channelKey);
return channelKey;
};
var expected = "005AQAoAEJERTJDRDdFNkZDNkU0ODYxNkYxQTYwOUVFNTM1M0U5ODNCQjFDNDQQAJcMo13mDERkW7roohUGGzOwKDdW9buDA68oN1YAAA==";
//var result = inspectMediaChannelKey(appID, appCertificate, channel, ts, r, uid, expiredTs);
//console.log( (expected == result) ? "ok" : "failed");
module.exports = async(appIDa, appCertificatea, channela,uida)=>{
let keys = await inspectMediaChannelKey(appIDa, appCertificatea, channela, ts, r, uida, expiredTs);
if(expected == keys){
return {
code:200,
channelKey:keys,
uid:uida
}
}else {
return {
code:400,
msg:"failed"
}
}
}
... ...