付智勇

no message

  1 +{
  2 + // Use IntelliSense to learn about possible Node.js debug attributes.
  3 + // Hover to view descriptions of existing attributes.
  4 + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  5 + "version": "0.2.0",
  6 + "configurations": [
  7 + {
  8 + "type": "node",
  9 + "request": "launch",
  10 + "name": "Launch Program",
  11 + "program": "${workspaceRoot}/bin/www"
  12 + }
  13 + ]
  14 +}
1 const router = require('koa-router')() 1 const router = require('koa-router')()
2 var userContoller = require('../controller/userController') 2 var userContoller = require('../controller/userController')
3 var status = require('../util/resTemplate') 3 var status = require('../util/resTemplate')
  4 +const inspectMediaChannelKey = require('../util/inspectMediaChannelKey')
  5 +
4 6
5 router.prefix('/users') 7 router.prefix('/users')
6 8
@@ -106,7 +108,21 @@ router.put('/updatePwByTelphone',async(ctx, next)=>{ @@ -106,7 +108,21 @@ router.put('/updatePwByTelphone',async(ctx, next)=>{
106 }) 108 })
107 109
108 110
109 - 111 +/**
  112 + * 加密token
  113 + */
  114 +router.post('/encryptionToken',async (ctx, next) => {
  115 + try{
  116 + let body = ctx.request.body;
  117 +
  118 + let inspectMediaChannel = await inspectMediaChannelKey(body.appID,body.appCertificate,body.channel,body.uid)
  119 + ctx.response.status= 200;
  120 + ctx.response.body= inspectMediaChannel
  121 + }catch(e){
  122 + console.log(e)
  123 + status.catchError(ctx,400,e.message);
  124 + }
  125 +})
110 126
111 127
112 module.exports = router 128 module.exports = router
1 -module.exports = ['/',"/users/addUser","/users/login",'/users/addUser?time=1',]  
  1 +module.exports = ['/',"/users/addUser","/users/login",'/users/addUser?time=1','/users/encryptionToken']
  1 +var crypto = require('crypto');
  2 +
  3 +var appID = "970ca35de60c44645bbae8a215061b33";//传参
  4 +var appCertificate = "5cfd2fd1755d40ecb72977518be15d3b";//传参
  5 +var channel = "7d72365eb983485397e3e3f9d460bdda"; //传参
  6 +var ts = 1446455472;
  7 +var r = 58964981;
  8 +var uid=2882341273; //传参
  9 +var expiredTs=1446455471;
  10 +
  11 +var version = "005";
  12 +var noUpload = "0";
  13 +var audioVideoUpload = "3";
  14 +
  15 +var encodeHMac = function(key, message) {
  16 + return crypto.createHmac('sha1', key).update(message).digest('hex').toUpperCase();
  17 +};
  18 +
  19 +var hexDecode = function(str) {
  20 + return new Buffer(str, 'hex');
  21 +};
  22 +
  23 +
  24 +
  25 +
  26 +
  27 +var Message = function(options) {
  28 + options.pack = function() {
  29 + var out = ByteBuf();
  30 + return out.putUint16(options.serviceType)
  31 + .putBytes(options.appID)
  32 + .putUint32(options.unixTs)
  33 + .putUint32(options.salt)
  34 + .putString(options.channelName)
  35 + .putUint32(options.uid)
  36 + .putUint32(options.expiredTs)
  37 + .putTreeMap(options.extra)
  38 + .pack();
  39 + }
  40 +
  41 + return options;
  42 +}
  43 +
  44 +// InChannelPermissionKey
  45 +var ALLOW_UPLOAD_IN_CHANNEL = 1;
  46 +
  47 +// Service Type
  48 +var MEDIA_CHANNEL_SERVICE = 1;
  49 +var RECORDING_SERVICE = 2;
  50 +var PUBLIC_SHARING_SERVICE = 3;
  51 +var IN_CHANNEL_PERMISSION = 4;
  52 +
  53 +var inspectMediaChannelKey = function async(appID, appCertificate, channelName, unixTs, randomInt, uid, expiredTs) {
  54 + var rawAppID = hexDecode(appID);
  55 + console.log('App ID:\t\t\t ' + rawAppID.toString('hex').toUpperCase());
  56 +
  57 + var rawAppCertificate = hexDecode(appCertificate);
  58 + console.log('App Certificate:\t ' + rawAppCertificate.toString('hex').toUpperCase());
  59 +
  60 + var serviceType = MEDIA_CHANNEL_SERVICE;
  61 + var extra = null;
  62 + var ByteBuf = function() {
  63 + var that = {
  64 + buffer: new Buffer(1024)
  65 + , position: 0
  66 + };
  67 +
  68 + that.buffer.fill(0);
  69 +
  70 + that.pack = function() {
  71 + var out = new Buffer(that.position);
  72 + that.buffer.copy(out, 0, 0, out.length);
  73 + return out;
  74 + };
  75 +
  76 + that.putUint16 = function(v) {
  77 + try {
  78 + console.log(typeof v)
  79 + that.buffer.writeUInt16LE(v, that.position);
  80 + that.position += 2;
  81 + } catch (error) {
  82 + console.log('buffer',error)
  83 + }
  84 +
  85 + return that;
  86 + };
  87 +
  88 + that.putUint32 = function(v) {
  89 + that.buffer.writeUInt32LE(v, that.position);
  90 + that.position += 4;
  91 + return that;
  92 + };
  93 +
  94 + that.putBytes = function(bytes) {
  95 + that.putUint16(bytes.length);
  96 + bytes.copy(that.buffer, that.position);
  97 + that.position += bytes.length;
  98 + return that;
  99 + };
  100 +
  101 + that.putString = function(str) {
  102 + return that.putBytes(new Buffer(str));
  103 + };
  104 +
  105 + that.putTreeMap = function(map) {
  106 + if (!map) {
  107 + that.putUint16(0);
  108 + return that;
  109 + }
  110 +
  111 + that.putUint16(Object.keys(map).length);
  112 + for (var key in map) {
  113 + that.putUint16(key);
  114 + that.putString(map[key]);
  115 + }
  116 +
  117 + return that;
  118 + };
  119 +
  120 + return that;
  121 + }
  122 + var Message = function(options) {
  123 + console.log('s生成buffer')
  124 +
  125 + options.pack = function() {
  126 + var out = ByteBuf();
  127 + return out.putUint16(options.serviceType)
  128 + .putBytes(options.appID)
  129 + .putUint32(options.unixTs)
  130 + .putUint32(options.salt)
  131 + .putString(options.channelName)
  132 + .putUint32(options.uid)
  133 + .putUint32(options.expiredTs)
  134 + .putTreeMap(options.extra)
  135 + .pack();
  136 + }
  137 +
  138 + return options;
  139 + }
  140 +
  141 + var m = Message({
  142 + serviceType: serviceType
  143 + , appID: rawAppID
  144 + , unixTs: unixTs
  145 + , salt: randomInt
  146 + , channelName: channelName
  147 + , uid: uid
  148 + , expiredTs: expiredTs
  149 + , extra: extra
  150 + });
  151 +
  152 + var toSign = m.pack();
  153 + console.log("Message to sign:\t " + toSign.toString('hex').toUpperCase());
  154 +
  155 + var signature = encodeHMac(rawAppCertificate, toSign);
  156 + console.log("Signature:\t\t " + signature.toString('hex').toUpperCase());
  157 + var DynamicKey5Content = function(options) {
  158 + options.pack = function() {
  159 + var out = ByteBuf();
  160 + return out.putUint16(options.serviceType)
  161 + .putString(options.signature)
  162 + .putBytes(options.appID)
  163 + .putUint32(options.unixTs)
  164 + .putUint32(options.salt)
  165 + .putUint32(options.expiredTs)
  166 + .putTreeMap(options.extra)
  167 + .pack();
  168 + }
  169 +
  170 + return options;
  171 + }
  172 + var content = DynamicKey5Content({
  173 + serviceType: serviceType
  174 + , signature: signature
  175 + , appID: hexDecode(appID)
  176 + , unixTs: unixTs
  177 + , salt: randomInt
  178 + , expiredTs: expiredTs
  179 + , extra: extra}).pack();
  180 + console.log("Content to encode:\t " + content.toString('hex').toUpperCase());
  181 +
  182 + var channelKey = version + content.toString('base64');
  183 + console.log("Channel key:\t\t " + channelKey);
  184 + return channelKey;
  185 +};
  186 +
  187 +var expected = "005AQAoAEJERTJDRDdFNkZDNkU0ODYxNkYxQTYwOUVFNTM1M0U5ODNCQjFDNDQQAJcMo13mDERkW7roohUGGzOwKDdW9buDA68oN1YAAA==";
  188 +//var result = inspectMediaChannelKey(appID, appCertificate, channel, ts, r, uid, expiredTs);
  189 +//console.log( (expected == result) ? "ok" : "failed");
  190 +module.exports = async(appIDa, appCertificatea, channela,uida)=>{
  191 + let keys = await inspectMediaChannelKey(appIDa, appCertificatea, channela, ts, r, uida, expiredTs);
  192 + if(expected == keys){
  193 + return {
  194 + code:200,
  195 + channelKey:keys,
  196 + uid:uida
  197 + }
  198 + }else {
  199 + return {
  200 + code:400,
  201 + msg:"failed"
  202 + }
  203 + }
  204 +}