一、onWebSocketConnect安全开发一
为什么叫安全开发一,是因为还有两种情况没有考虑好,需要实测中进行修改。第一种情况:个人如果打开两个窗口,访问同一个系统或个人如果打开两种浏览器,两个窗口同时访问一个系统,改怎么解决。第二种情况:如果有人用工具的情况下,怎么才能断开连接。安全一的代码如下:
/*** 专门处理安全的* User: 龙哥·三年风水* Date: 2024/12/16* Time: 17:03* @ param $client_id* @ param $data*/public static function onWebSocketConnect($client_id,$data){// 没有指定参数的情况下if(!isset($data['get']['token']) || !isset($data['get']['extremity_type']) || !isset($data['get']['login_time'])){//释放当前连接Gateway::destoryClient($client_id);exit;}$token = $data['get']['token'];$extremityType = $data['get']['extremity_type'];$loginTime = $data['get']['login_time'];// 有参数且参数值不合法的情况下if(empty($token) || !in_array($extremityType,[1,2,3]) || preg_match('/^\d{10}$/', $loginTime) == false){//释放当前连接Gateway::destoryClient($client_id);exit;}// 读取Redis配置及Redis连接$config = parse_ini_file('conf.ini', true);$redis = new Client("redis://{$config['redis']['host']}:{$config['redis']['port']}");$redis->auth($config['redis']['password']);// 平台端验证if($extremityType == 1){$redis->select((int)$config['redis']['login_ip']);// 验证IP是否合法$redis->get($token,function($clientIp) use ($client_id,$data){if(empty($clientIp)){//释放当前连接Gateway::destoryClient($client_id);exit;}if($clientIp != $data['server']['HTTP_X_REAL_IP']){//释放当前连接Gateway::destoryClient($client_id);exit;}});// 验证是否登录$redis->select((int)$config['redis']['token_platform']);$authorization = sha1(sha1($token).$loginTime);$redis->get('token_'.$authorization,function ($tokenValue) use ($client_id,$config){if(empty($tokenValue)){//释放当前连接Gateway::destoryClient($client_id);exit;}Gateway::bindUid($client_id,$tokenValue);Gateway::joinGroup($client_id,'platform'.$config['redis']['token_platform']);});}}
二、提前说明
明天将完成流程图及规则说明后再次开发websocket安全。