httpCommon.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. 'use strict';
  2. var F = require('../common/function');
  3. var C = require('../config/index');
  4. var _ = require('underscore');
  5. _.str = require('underscore.string');
  6. _.v = require('validator');
  7. var co = require('co');
  8. module.exports = function (app) {
  9. var mgr_map = app.common_mgr.mgr_map;
  10. var model_map = app.model_mgr.model_map;
  11. var port = C.port + 1;
  12. var back_svr = _.str.sprintf("http://%s:%s", C.inner_host, port);
  13. var http_app = app.app;
  14. var that = this;
  15. var allow_url_list = [
  16. "/member/*/index",
  17. "/imroom/*/updateMemberInfo",
  18. "/imroom/*/createRoom",
  19. "/imroom/*/updateRoomInfo",
  20. "/imroom/*/pushRoomMsg",
  21. "/imroom/*/pushAllRoomMsg",
  22. "/imroom/*/createStream",
  23. "/imroom/*/closeStream",
  24. "/test/*/test",
  25. "/test/*/test2",
  26. "/user/v4/get",
  27. "/iminnerapi/*/sendMsg",
  28. "/iminnerapi/*/addTimerJob",
  29. "/iminnerapi/*/kickoff",
  30. ];
  31. var not_decrypted_url_list = [];
  32. let url_dic = {};
  33. for (let key in allow_url_list) {
  34. url_dic[allow_url_list[key]] = '';
  35. }
  36. http_app.processDesDecode = function* (ctx) {
  37. try {
  38. if ("ed" in ctx.I && not_decrypted_url_list.indexOf(ctx.pattern_url) == -1) {
  39. let decrypted = F.decipher(ctx.I.ed);
  40. decrypted = _.str.trim(decrypted, ' ');
  41. if (decrypted[0] == "{") { //json
  42. let req_ed_js = JSON.parse(decrypted);
  43. ctx.I = Object.assign(ctx.I, req_ed_js);
  44. // F.addOtherLogs('httpdecode/httpdecode',["decrypted:",req_ed_js]);
  45. } else if (decrypted[0] == "[") { // json 数组先不支持
  46. F.addErrLogs(["not support this encode type", decrypted]);
  47. F.throwErr("not support this encode type");
  48. } else { // url encode
  49. let param_list = decrypted.split("&");
  50. for (let i = 0; i < param_list.length; i++) {
  51. let kv = param_list[i].split("=");
  52. if (kv.length != 2) {
  53. F.addErrLogs(["urlencode format not right:", param_list[i], decrypted])
  54. F.throwErr("urlencode format not right");
  55. }
  56. ctx.I[kv[0]] = decodeURIComponent(kv[1]);
  57. }
  58. }
  59. ctx.hasEncode = 1;
  60. delete ctx.I.ed;
  61. }
  62. } catch (e) {
  63. F.addErrLogs(["decrypted err:", e.stack, ctx.I])
  64. }
  65. }
  66. //im 统一入口拦截器
  67. http_app.beforeCallback = function* (ctx) {
  68. ctx.start_time = new Date().getTime();
  69. ctx.I = yield F.Init(ctx);
  70. let Env = ctx;
  71. let I = ctx.I;
  72. F.addOtherLogs("imrw/imrw",["##http receive:",Env.req.url,"clientip",Env.clientip,I,Env.session.userid,Env.request.req.headers['cookie']]);
  73. yield http_app.processDesDecode(ctx);
  74. var req_url = ctx.pattern_url;
  75. console.log([req_url, ctx.pattern_url]);
  76. if (req_url in url_dic) return true;
  77. return true;
  78. };
  79. // 拦截器,作用类似php析构函数
  80. http_app.afterCallback = function* (ctx) {
  81. if (!F.isNull(ctx.res_list) && ctx.res_list.length > 0) {
  82. if (!F.isNull(ctx.hasEncode)) { // 客户端加密了
  83. let raw_msg = JSON.stringify(ctx.res_list[ctx.res_list.length - 1]);
  84. let encode_msg = F.cipher(raw_msg);
  85. ctx.jsonp = {"ed": encode_msg};
  86. } else { // 客户端没加密
  87. ctx.jsonp = ctx.res_list[ctx.res_list.length - 1];
  88. }
  89. }
  90. ctx.end_time = new Date().getTime();
  91. if (ctx.end_time > ctx.start_time + C.slow_log_delta) {
  92. let usetime = ctx.end_time - ctx.start_time;
  93. mgr_map.logs.addLogs("slow/slow", [`usetime:${usetime}`, "req:", ctx.request.url, ctx.I]);
  94. }
  95. ctx.set('ut', ctx.end_time - ctx.start_time);
  96. ctx.set('Cache-Control', 'no-cache');
  97. ctx.set('P3P', 'CP=CAO PSA OUR');
  98. };
  99. // 异常处理
  100. http_app.catchException = function* (ctx, err) {
  101. ctx.set('P3P', 'CP=CAO PSA OUR');
  102. ctx.set('Cache-Control', 'no-cache');
  103. var istr = JSON.stringify(ctx.I);
  104. var err_str = F.log("err:", "#####%s; req:%s; catch a err:%s ,code:%s", [ctx.originalUrl, istr, err.message, err.code]);
  105. F.addErrLogs([err_str, err.stack]);
  106. let err_code = F.isNull(err.code) || !F.isInt(err.code) ? 10003 : err.code;
  107. var res = {
  108. "errno": err_code,
  109. "errmsg": F.isNull(err.use_msg) ? C.err_msg[err_code.toString()] : err.message
  110. }
  111. if (!F.isNull(err.data)) {
  112. res.data = err.data;
  113. res.errdata = err.data;
  114. }
  115. //res = F.replaceCreditUnitStr(res, ctx.credit_type);
  116. if (!F.isNull(ctx.hasEncode)) { // 客户端加密了
  117. let encode_msg = F.cipher(JSON.stringify(res));
  118. ctx.jsonp = {"ed": encode_msg};
  119. } else { // 客户端没加密
  120. ctx.jsonp = res;
  121. }
  122. };
  123. http_app.regGet = function (router, processer) {
  124. http_app.get(router, function* () {
  125. try {
  126. var ret = yield http_app.beforeCallback(this);
  127. if (!ret) return;
  128. yield processer(this);
  129. if (http_app.afterCallback) yield http_app.afterCallback(this);
  130. } catch (e) {
  131. if (http_app.catchException) {
  132. yield http_app.catchException(this, e);
  133. }
  134. F.addErrLogs(e.stack);
  135. }
  136. });
  137. };
  138. http_app.regPost = function (router, processer) {
  139. http_app.post(router, function* () {
  140. try {
  141. var ret = yield http_app.beforeCallback(this);
  142. if (!ret) return;
  143. yield processer(this);
  144. if (http_app.afterCallback) yield http_app.afterCallback(this);
  145. } catch (e) {
  146. if (http_app.catchException) {
  147. yield http_app.catchException(this, e);
  148. }
  149. F.addErrLogs(e.stack);
  150. }
  151. });
  152. };
  153. http_app.regAll = function (router, processer) {
  154. http_app.all(router, function* () {
  155. try {
  156. var ret = yield http_app.beforeCallback(this);
  157. if (!ret) return;
  158. yield processer(this);
  159. if (http_app.afterCallback) yield http_app.afterCallback(this);
  160. } catch (e) {
  161. if (http_app.catchException) {
  162. yield http_app.catchException(this, e);
  163. }
  164. F.addErrLogs(e.stack);
  165. }
  166. });
  167. };
  168. http_app.regOptions = function (router, processer) {
  169. http_app.options(router, function* () {
  170. try {
  171. var ret = yield http_app.beforeCallback(this);
  172. if (!ret) return;
  173. yield processer(this);
  174. if (http_app.afterCallback) yield http_app.afterCallback(this);
  175. } catch (e) {
  176. if (http_app.catchException) {
  177. yield http_app.catchException(this, e);
  178. }
  179. F.addErrLogs(e.stack);
  180. }
  181. });
  182. };
  183. http_app.regRequest = function (router, processer) {
  184. http_app.post(router, function* () {
  185. try {
  186. var ret = yield http_app.beforeCallback(this);
  187. if (!ret) return;
  188. yield processer(this);
  189. if (http_app.afterCallback) yield http_app.afterCallback(this);
  190. } catch (e) {
  191. if (http_app.catchException) {
  192. yield http_app.catchException(this, e);
  193. }
  194. F.addLogs(["error:", e.stack]);
  195. }
  196. });
  197. http_app.get(router, function* () {
  198. try {
  199. var ret = yield http_app.beforeCallback(this);
  200. if (!ret) return;
  201. yield processer(this);
  202. if (http_app.afterCallback) yield http_app.afterCallback(this);
  203. } catch (e) {
  204. if (http_app.catchException) {
  205. yield http_app.catchException(this, e);
  206. }
  207. F.addErrLogs(["error:", e.stack]);
  208. }
  209. });
  210. };
  211. };