关于workerman的健壮性的一个问题,connect的时候故意传错一个参数,程序就崩溃了

小七他哥

我在websocket的链接里加了个参数类似下面:

ws://127.0.0.1:8019/?sid=3670549-360dMrtp

然后服务端 onConnect 方法里做了如下验证:

$worker->onConnect = function (\Workerman\Connection\TcpConnection $connection) {
    try {
        $connection->uniqueId = $connection->getLocalIp() . '-' . $connection->getLocalPort() . '-' . $connection->worker->id . '-' . $connection->id;

        $connection->onWebSocketConnect = function (\Workerman\Connection\TcpConnection $connection, $http_header) {
            if (
                !isset($_SERVER['HTTP_ORIGIN']) ||
                ($_SERVER['HTTP_ORIGIN'] != 'http://local.tieba.com:8018' && $_SERVER['HTTP_ORIGIN'] != 'https://tieba.baidu.com')
            ) {
                throw new Exception('链接unique_id=' . $connection->uniqueId . ',链接参数 HTTP_ORIGIN 错误', 1025);
            }

            if (!isset($_GET['sid']) || !$_GET['sid']) {
                throw new Exception('链接unique_id=' . $connection->uniqueId . ',链接参数错误,sid为空', 1026);
            }

            $connection->sid = $_GET['sid'];
        };

        $connection_ip = $connection->getRemoteIp();

        $connection->maxSendBufferSize = 10485760;

        if ($connection_ip) {
            if (in_array($connection_ip, $GLOBALS['ip_black_list'])) {
                throw new Exception('ip=' . $connection_ip . ',unique_id=' . $connection->uniqueId . ' 的用户已被加入到ip黑名单,直接关闭该链接', 1027);
            }
            if (isset($GLOBALS['ip_list'][$connection_ip])) {
                if ($GLOBALS['ip_list'][$connection_ip]['num'] >= ONE_IP_MAX_WEBSOCKET_NUM) {
                    $GLOBALS['ip_black_list'][] = $connection_ip;
                    throw new Exception('ip=' . $connection_ip . ',unique_id=' . $connection->uniqueId . ' 的用户超过了最大链接数,直接关闭该链接', 1029);
                }
                $GLOBALS['ip_list'][$connection_ip]['num'] += 1;
            } else {
                $GLOBALS['ip_list'][$connection_ip]['num'] = 1;
            }
        }
    } catch (Exception $exception) {
        $connection->close_reason = $exception->getMessage() . ':' . $exception->getCode();
        $connection->close("\r\n");
    }

    wlog('new connection ' . $connection->uniqueId . ' from ip ' . $connection_ip, false, 1, $GLOBALS['system_set']);
};

上面的代码没有问题,嘿嘿,但是如果链接改成了:

ws://127.0.0.1:8019/?sidx=3670549-360dMrtp

我原本的意思是没有验证到 sid 参数,应该输出一个log并且关闭这个链接,然后没啥事了,但是现在是程序老是报一大堆的错误,如下,我检查了半天感觉我的业务代码是没有什么问题的,想请大神们帮我看下是不是框架有问题了?

[2021-07-14 15:38:31] new connection 172.17.0.2-8910-0-1 from ip 172.17.0.1
2021-07-14 15:38:31 pid:1569 Exception: 链接unique_id=172.17.0.2-8910-0-1,链接参数错误,sid为空 in /var/www/diary/server/WorkermanWebsocketServer.php:116
Stack trace:
#0 /var/www/diary/lib/Workerman/Protocols/Websocket.php(392): {closure}()
#1 /var/www/diary/lib/Workerman/Protocols/Websocket.php(57): Workerman\Protocols\Websocket::dealHandshake()
#2 /var/www/diary/lib/Workerman/Connection/TcpConnection.php(600): Workerman\Protocols\Websocket::input()
#3 /var/www/diary/lib/Workerman/Events/Select.php(294): Workerman\Connection\TcpConnection->baseRead()
#4 /var/www/diary/lib/Workerman/Worker.php(2419): Workerman\Events\Select->loop()
#5 /var/www/diary/lib/Workerman/Worker.php(1543): Workerman\Worker->run()
#6 /var/www/diary/lib/Workerman/Worker.php(1373): Workerman\Worker::forkOneWorkerForLinux()
#7 /var/www/diary/lib/Workerman/Worker.php(1347): Workerman\Worker::forkWorkersForLinux()
#8 /var/www/diary/lib/Workerman/Worker.php(1680): Workerman\Worker::forkWorkers()
#9 /var/www/diary/lib/Workerman/Worker.php(1629): Workerman\Worker::monitorWorkersForLinux()
#10 /var/www/diary/lib/Workerman/Worker.php(544): Workerman\Worker::monitorWorkers()
#11 /var/www/diary/server/WorkermanWebsocketServer.php(181): Workerman\Worker::runAll()
#12 {main}
2021-07-14 15:38:31 pid:1569 Worker[1569] process terminated

Warning: (null)(): supplied resource is not a valid stream resource in Unknown on line 0

Call Stack:
    0.0004    1382960   1. {main}() /var/www/diary/server/WorkermanWebsocketServer.php:0
    0.0172    2878392   2. Workerman\Worker::runAll() /var/www/diary/server/WorkermanWebsocketServer.php:181
    0.0486    2980704   3. Workerman\Worker::monitorWorkers() /var/www/diary/lib/Workerman/Worker.php:544
    0.0486    2980704   4. Workerman\Worker::monitorWorkersForLinux() /var/www/diary/lib/Workerman/Worker.php:1629
  108.0958    2982184   5. Workerman\Worker::forkWorkers() /var/www/diary/lib/Workerman/Worker.php:1680
  108.0958    2982184   6. Workerman\Worker::forkWorkersForLinux() /var/www/diary/lib/Workerman/Worker.php:1347
  108.0958    2982184   7. Workerman\Worker::forkOneWorkerForLinux() /var/www/diary/lib/Workerman/Worker.php:1373
  108.0980    2981432   8. Workerman\Worker->run() /var/www/diary/lib/Workerman/Worker.php:1543
  108.1004    3034448   9. Workerman\Events\Select->loop() /var/www/diary/lib/Workerman/Worker.php:2419
  118.1001    3203544  10. Workerman\Connection\TcpConnection->baseRead() /var/www/diary/lib/Workerman/Events/Select.php:294
  118.1001    3204184  11. Workerman\Protocols\Websocket::input() /var/www/diary/lib/Workerman/Connection/TcpConnection.php:600
  118.1002    3204184  12. Workerman\Protocols\Websocket::dealHandshake() /var/www/diary/lib/Workerman/Protocols/Websocket.php:57

Warning: (null)(): supplied resource is not a valid stream resource in Unknown on line 0

Call Stack:
    0.0004    1382960   1. {main}() /var/www/diary/server/WorkermanWebsocketServer.php:0
    0.0172    2878392   2. Workerman\Worker::runAll() /var/www/diary/server/WorkermanWebsocketServer.php:181
    0.0486    2980704   3. Workerman\Worker::monitorWorkers() /var/www/diary/lib/Workerman/Worker.php:544
    0.0486    2980704   4. Workerman\Worker::monitorWorkersForLinux() /var/www/diary/lib/Workerman/Worker.php:1629
  108.0958    2982184   5. Workerman\Worker::forkWorkers() /var/www/diary/lib/Workerman/Worker.php:1680
  108.0958    2982184   6. Workerman\Worker::forkWorkersForLinux() /var/www/diary/lib/Workerman/Worker.php:1347
  108.0958    2982184   7. Workerman\Worker::forkOneWorkerForLinux() /var/www/diary/lib/Workerman/Worker.php:1373
  108.0980    2981432   8. Workerman\Worker->run() /var/www/diary/lib/Workerman/Worker.php:1543
  108.1004    3034448   9. Workerman\Events\Select->loop() /var/www/diary/lib/Workerman/Worker.php:2419
  118.1001    3203544  10. Workerman\Connection\TcpConnection->baseRead() /var/www/diary/lib/Workerman/Events/Select.php:294
  118.1001    3204184  11. Workerman\Protocols\Websocket::input() /var/www/diary/lib/Workerman/Connection/TcpConnection.php:600
  118.1002    3204184  12. Workerman\Protocols\Websocket::dealHandshake() /var/www/diary/lib/Workerman/Protocols/Websocket.php:57

2021-07-14 15:38:31 pid:1011 worker[diaryWorkermanWebsocketServer:1569] exit with status 64000
[2021-07-14 15:38:31] Worker 0 started
[2021-07-14 15:38:41] new connection 172.17.0.2-8910-0-1 from ip 172.17.0.1
2021-07-14 15:38:41 pid:1570 Exception: 链接unique_id=172.17.0.2-8910-0-1,链接参数错误,sid为空 in /var/www/diary/server/WorkermanWebsocketServer.php:116
Stack trace:
#0 /var/www/diary/lib/Workerman/Protocols/Websocket.php(392): {closure}()
#1 /var/www/diary/lib/Workerman/Protocols/Websocket.php(57): Workerman\Protocols\Websocket::dealHandshake()
#2 /var/www/diary/lib/Workerman/Connection/TcpConnection.php(600): Workerman\Protocols\Websocket::input()
#3 /var/www/diary/lib/Workerman/Events/Select.php(294): Workerman\Connection\TcpConnection->baseRead()
#4 /var/www/diary/lib/Workerman/Worker.php(2419): Workerman\Events\Select->loop()
#5 /var/www/diary/lib/Workerman/Worker.php(1543): Workerman\Worker->run()
#6 /var/www/diary/lib/Workerman/Worker.php(1373): Workerman\Worker::forkOneWorkerForLinux()
#7 /var/www/diary/lib/Workerman/Worker.php(1347): Workerman\Worker::forkWorkersForLinux()
#8 /var/www/diary/lib/Workerman/Worker.php(1680): Workerman\Worker::forkWorkers()
#9 /var/www/diary/lib/Workerman/Worker.php(1629): Workerman\Worker::monitorWorkersForLinux()
#10 /var/www/diary/lib/Workerman/Worker.php(544): Workerman\Worker::monitorWorkers()
#11 /var/www/diary/server/WorkermanWebsocketServer.php(181): Workerman\Worker::runAll()
#12 {main}
2021-07-14 15:38:41 pid:1570 Worker[1570] process terminated

Warning: (null)(): supplied resource is not a valid stream resource in Unknown on line 0

Call Stack:
    0.0004    1382960   1. {main}() /var/www/diary/server/WorkermanWebsocketServer.php:0
    0.0172    2878392   2. Workerman\Worker::runAll() /var/www/diary/server/WorkermanWebsocketServer.php:181
    0.0486    2980704   3. Workerman\Worker::monitorWorkers() /var/www/diary/lib/Workerman/Worker.php:544
    0.0486    2980704   4. Workerman\Worker::monitorWorkersForLinux() /var/www/diary/lib/Workerman/Worker.php:1629
  118.1110    2982184   5. Workerman\Worker::forkWorkers() /var/www/diary/lib/Workerman/Worker.php:1680
  118.1110    2982184   6. Workerman\Worker::forkWorkersForLinux() /var/www/diary/lib/Workerman/Worker.php:1347
  118.1110    2982184   7. Workerman\Worker::forkOneWorkerForLinux() /var/www/diary/lib/Workerman/Worker.php:1373
  118.1130    2981432   8. Workerman\Worker->run() /var/www/diary/lib/Workerman/Worker.php:1543
  118.1157    3034448   9. Workerman\Events\Select->loop() /var/www/diary/lib/Workerman/Worker.php:2419
  128.1156    3203544  10. Workerman\Connection\TcpConnection->baseRead() /var/www/diary/lib/Workerman/Events/Select.php:294
  128.1156    3204184  11. Workerman\Protocols\Websocket::input() /var/www/diary/lib/Workerman/Connection/TcpConnection.php:600
  128.1156    3204184  12. Workerman\Protocols\Websocket::dealHandshake() /var/www/diary/lib/Workerman/Protocols/Websocket.php:57

Warning: (null)(): supplied resource is not a valid stream resource in Unknown on line 0

Call Stack:
    0.0004    1382960   1. {main}() /var/www/diary/server/WorkermanWebsocketServer.php:0
    0.0172    2878392   2. Workerman\Worker::runAll() /var/www/diary/server/WorkermanWebsocketServer.php:181
    0.0486    2980704   3. Workerman\Worker::monitorWorkers() /var/www/diary/lib/Workerman/Worker.php:544
    0.0486    2980704   4. Workerman\Worker::monitorWorkersForLinux() /var/www/diary/lib/Workerman/Worker.php:1629
  118.1110    2982184   5. Workerman\Worker::forkWorkers() /var/www/diary/lib/Workerman/Worker.php:1680
  118.1110    2982184   6. Workerman\Worker::forkWorkersForLinux() /var/www/diary/lib/Workerman/Worker.php:1347
  118.1110    2982184   7. Workerman\Worker::forkOneWorkerForLinux() /var/www/diary/lib/Workerman/Worker.php:1373
  118.1130    2981432   8. Workerman\Worker->run() /var/www/diary/lib/Workerman/Worker.php:1543
  118.1157    3034448   9. Workerman\Events\Select->loop() /var/www/diary/lib/Workerman/Worker.php:2419
  128.1156    3203544  10. Workerman\Connection\TcpConnection->baseRead() /var/www/diary/lib/Workerman/Events/Select.php:294
  128.1156    3204184  11. Workerman\Protocols\Websocket::input() /var/www/diary/lib/Workerman/Connection/TcpConnection.php:600
  128.1156    3204184  12. Workerman\Protocols\Websocket::dealHandshake() /var/www/diary/lib/Workerman/Protocols/Websocket.php:57

2021-07-14 15:38:41 pid:1011 worker[diaryWorkermanWebsocketServer:1570] exit with status 64000
[2021-07-14 15:38:41] Worker 0 started
[2021-07-14 15:38:51] new connection 172.17.0.2-8910-0-1 from ip 172.17.0.1
2021-07-14 15:38:51 pid:1571 Exception: 链接unique_id=172.17.0.2-8910-0-1,链接参数错误,sid为空 in /var/www/diary/server/WorkermanWebsocketServer.php:116
Stack trace:
#0 /var/www/diary/lib/Workerman/Protocols/Websocket.php(392): {closure}()
#1 /var/www/diary/lib/Workerman/Protocols/Websocket.php(57): Workerman\Protocols\Websocket::dealHandshake()
#2 /var/www/diary/lib/Workerman/Connection/TcpConnection.php(600): Workerman\Protocols\Websocket::input()
#3 /var/www/diary/lib/Workerman/Events/Select.php(294): Workerman\Connection\TcpConnection->baseRead()
#4 /var/www/diary/lib/Workerman/Worker.php(2419): Workerman\Events\Select->loop()
#5 /var/www/diary/lib/Workerman/Worker.php(1543): Workerman\Worker->run()
#6 /var/www/diary/lib/Workerman/Worker.php(1373): Workerman\Worker::forkOneWorkerForLinux()
#7 /var/www/diary/lib/Workerman/Worker.php(1347): Workerman\Worker::forkWorkersForLinux()
#8 /var/www/diary/lib/Workerman/Worker.php(1680): Workerman\Worker::forkWorkers()
#9 /var/www/diary/lib/Workerman/Worker.php(1629): Workerman\Worker::monitorWorkersForLinux()
#10 /var/www/diary/lib/Workerman/Worker.php(544): Workerman\Worker::monitorWorkers()
#11 /var/www/diary/server/WorkermanWebsocketServer.php(181): Workerman\Worker::runAll()
#12 {main}
2021-07-14 15:38:51 pid:1571 Worker[1571] process terminated

Warning: (null)(): supplied resource is not a valid stream resource in Unknown on line 0

Call Stack:
    0.0004    1382960   1. {main}() /var/www/diary/server/WorkermanWebsocketServer.php:0
    0.0172    2878392   2. Workerman\Worker::runAll() /var/www/diary/server/WorkermanWebsocketServer.php:181
    0.0486    2980704   3. Workerman\Worker::monitorWorkers() /var/www/diary/lib/Workerman/Worker.php:544
    0.0486    2980704   4. Workerman\Worker::monitorWorkersForLinux() /var/www/diary/lib/Workerman/Worker.php:1629
  128.1293    2982184   5. Workerman\Worker::forkWorkers() /var/www/diary/lib/Workerman/Worker.php:1680
  128.1293    2982184   6. Workerman\Worker::forkWorkersForLinux() /var/www/diary/lib/Workerman/Worker.php:1347
  128.1294    2982184   7. Workerman\Worker::forkOneWorkerForLinux() /var/www/diary/lib/Workerman/Worker.php:1373
  128.1310    2981432   8. Workerman\Worker->run() /var/www/diary/lib/Workerman/Worker.php:1543
  128.1338    3034448   9. Workerman\Events\Select->loop() /var/www/diary/lib/Workerman/Worker.php:2419
  138.1337    3203544  10. Workerman\Connection\TcpConnection->baseRead() /var/www/diary/lib/Workerman/Events/Select.php:294
  138.1338    3204184  11. Workerman\Protocols\Websocket::input() /var/www/diary/lib/Workerman/Connection/TcpConnection.php:600
  138.1338    3204184  12. Workerman\Protocols\Websocket::dealHandshake() /var/www/diary/lib/Workerman/Protocols/Websocket.php:57

Warning: (null)(): supplied resource is not a valid stream resource in Unknown on line 0

Call Stack:
    0.0004    1382960   1. {main}() /var/www/diary/server/WorkermanWebsocketServer.php:0
    0.0172    2878392   2. Workerman\Worker::runAll() /var/www/diary/server/WorkermanWebsocketServer.php:181
    0.0486    2980704   3. Workerman\Worker::monitorWorkers() /var/www/diary/lib/Workerman/Worker.php:544
    0.0486    2980704   4. Workerman\Worker::monitorWorkersForLinux() /var/www/diary/lib/Workerman/Worker.php:1629
  128.1293    2982184   5. Workerman\Worker::forkWorkers() /var/www/diary/lib/Workerman/Worker.php:1680
  128.1293    2982184   6. Workerman\Worker::forkWorkersForLinux() /var/www/diary/lib/Workerman/Worker.php:1347
  128.1294    2982184   7. Workerman\Worker::forkOneWorkerForLinux() /var/www/diary/lib/Workerman/Worker.php:1373
  128.1310    2981432   8. Workerman\Worker->run() /var/www/diary/lib/Workerman/Worker.php:1543
  128.1338    3034448   9. Workerman\Events\Select->loop() /var/www/diary/lib/Workerman/Worker.php:2419
  138.1337    3203544  10. Workerman\Connection\TcpConnection->baseRead() /var/www/diary/lib/Workerman/Events/Select.php:294
  138.1338    3204184  11. Workerman\Protocols\Websocket::input() /var/www/diary/lib/Workerman/Connection/TcpConnection.php:600
  138.1338    3204184  12. Workerman\Protocols\Websocket::dealHandshake() /var/www/diary/lib/Workerman/Protocols/Websocket.php:57

2021-07-14 15:38:51 pid:1011 worker[diaryWorkermanWebsocketServer:1571] exit with status 64000
[2021-07-14 15:38:51] Worker 0 started
2183 4 0
4个回答

小七他哥

我看代码应该是这段出的问题:

try {
                    \call_user_func(isset($connection->onWebSocketConnect)?$connection->onWebSocketConnect:$connection->worker->onWebSocketConnect, $connection, $buffer);
                } catch (\Exception $e) {
                    Worker::log($e);
                    exit(250);
                } catch (\Error $e) {
                    Worker::log($e);
                    exit(250);
                }

因为我代码中抛出了异常导致的,但是我改成抛出我自定义的异常类

throw new NotDoException('链接unique_id=' . $connection->uniqueId . ',链接参数错误,sid为空', 1026);

还是会触发这里的异常处理,请问一下这种情况该怎么处理呢?难道只能放弃我自己的抛出异常的想法吗?

  • 暂无评论
phpcreeper

1、你query传的是sidx,程序里判断却用的是sid,要细心啊;
2、workerman正是因为健壮才在我们写代码不严谨或者遇到异常时抛出执行堆栈等等措施方便我们排查问题;

  • 小七他哥 2021-07-14

    大哥,我是故意这样写的啊,我正确传没问题,但是如果传错了就报一大堆错,这样别人如果攻击我的话,那岂不是很容易高一大堆错误?

  • phpcreeper 2021-07-14

    @8083: 一句话: 就你这种场景的测报错才正常; 题外话: 任何外来的数据从来都不要相信、不论你这个场景是不是测试、这个问题的定性在我看来它就是就是人为的BUG;至于workerman框架提供的或者PHP内置的报错机制那是必然要有的、目的就是为了方便开发者排错;至于攻击我觉得是另外一个层面的安全大问题了、没有绝对的安全、只有相对一层层的安全措施加持、就好比你这段代码扔生产环境之前是不是起码也得经过层层的检验呢、换句话: 我们接纳有意义的报错、但是我们必须尽可能在最后一道上线关口之前将报错消灭到最小危害程度。

静默

正常情况发生notice错误,workerman不会抛出这个异常,进程不会退出重启。

你的程序不断退出,是因为你业务捕获了notice错误并抛出异常,但是你业务又没去捕获这个异常,导致异常传递到workerman框架,workerman能做的也只能是记录异常重启当前进程。workerman这么做是没问题的。如果不这样做,那么你可能都无法发现这个异常,以为程序正常,实际上是有问题的。

如果你不想进程因为notice重启,你就不要捕获notice后抛出异常,或者抛出异常后你自己捕获下。最根本的方法就是测试好程序,不要有bug。

  • 小七他哥 2021-07-14

    我代码没有notice错误,只是抛出了异常。

小七他哥

问题解决了,确实是我自己的问题,产生了notice错误,打扰大家了。

  • 暂无评论
年代过于久远,无法发表回答
×
🔝