ProtoLogin.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Jiaruan;
  3. class ProtoLogin {
  4. public function proc( $req ){
  5. //检查签名
  6. if($req->sign != $this->sign_req($req) ){
  7. log_error('check sign failed ' . json_encode($req));
  8. return $this->create_resp($req,1);
  9. }
  10. else{
  11. log_debug('check sign success ' . json_encode($req));
  12. return $this->create_resp($req,0);
  13. }
  14. }
  15. private function sign_req( $req ){
  16. $str = $req->type;
  17. $str.= $req->msgid;
  18. $str.= $req->model;
  19. $str.= $req->mac;
  20. $str.= $req->sn;
  21. $str.= $req->version;
  22. $str.= $req->wip;
  23. $str.= $req->nmode;
  24. $str.= $req->rssi;
  25. $str.= $req->serialnum;
  26. $str.= $req->uptime;
  27. $str.= C('协议签名密钥');
  28. return strtoupper(md5(urlencode($str)));
  29. }
  30. private function sign_resp( $resp ){
  31. $str = $resp['type'];
  32. $str.= $resp['msgid'];
  33. //$str.= $resp['result'];
  34. $str.= C('协议签名密钥');
  35. return strtoupper(md5(urlencode($str)));
  36. }
  37. public function create_resp( $req, $result ){
  38. $resp = array(
  39. 'type' => 2,
  40. 'msgid' => $req->msgid,
  41. 'mac' => $req->mac,
  42. 'sign' => '',
  43. 'result' => $result,
  44. );
  45. $resp['sign'] = $this->sign_resp($resp);
  46. return $resp;
  47. }
  48. }