workman服务端开发模式-应用开发-gateway的onWebSocketConnect开发

一、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安全。

相关推荐
iPadiPhone1 小时前
分布式架构的“润滑剂”:RabbitMQ 核心原理与大厂面试避坑指南
分布式·后端·面试·架构·rabbitmq
ꟼ ꟼ✚19226385 小时前
基于 FPGA 的 16QAM 调制解调系统功能说明文档
gateway
Irene19916 小时前
JavaScript脚本加载的两种方式:defer/async 的区别
前端·javascript·php
luanma1509806 小时前
PHP vs C#:30字秒懂两大语言核心差异
android·开发语言·python·php·laravel
wanhengidc6 小时前
云手机与模拟器的关系
大数据·运维·服务器·分布式·智能手机
fresh hacker7 小时前
【Linux系统】通用的“系统排障”
linux·运维·服务器·网络·php
luanma1509807 小时前
Laravel 8.X重磅特性全解析
前端·javascript·vue.js·php·lua
luanma1509807 小时前
Laravel 9.x重磅升级:十大新特性解析
php·laravel
zhangren024687 小时前
PHP vs C++:从Web脚本到系统编程的终极对比
开发语言·c++·php
iPadiPhone7 小时前
万亿级流量的基石:Kafka 核心原理、大厂面试题解析与实战
分布式·后端·面试·kafka