【问题标题】:SRS (RTMP) authentification with PHP error带有 PHP 错误的 SRS (RTMP) 身份验证
【发布时间】:2020-08-06 21:23:42
【问题描述】:

所以我最近开始与 SRS (https://github.com/ossrs/srs) 合作,但在弄清楚如何使 FFMpeg 工作方面我已经遇到了很多问题。

但现在不同了,我需要进行基本身份验证 (https://github.com/ossrs/srs/wiki/v3_EN_HTTPCallback)。

所以我想到了编写一个 PHP 代码,当用户正确时返回代码 200 和值 0,当它不起作用时返回 404。

对我来说,它不起作用,我现在无法在这个 repo 中获得帮助,这里有没有人使用过它或者对我应该如何做有任何想法?

这是我的 PHP 代码(目前是一个示例):

<?php
$username = $_POST["name"]; # in our current example, this will be 'john'
$password = $_POST["psk"]; # in our current example, this will be 'supersecret'
$postdata = file_get_contents("php://input");

$valid_users = array("john" => "supersecret",
                     "winnie" => "thepooh",
                                         "batman" => "nananananananana");
$fp = fopen('test.txt', 'w');
fwrite($fp, "test $username $password posted data $postdata");
fclose($fp);
header('HTTP/1.0 404 Not Found');
if ($valid_users[$username] == $password) {
  http_response_code(200); # return 201 "Created"
        echo intval(false);
} else {
  http_response_code(404); # return 404 "Not Found"
}
?>

这是我尝试使用它时遇到的错误。

[2020-04-23 09:35:16.634][Trace][3921][473] connect app, tcUrl=rtmp://192.168.1.10:1935/live/livestream?name=john&psk=supersecret, pageUrl=, swfUrl=rtmp://192.168.1.10:1935/live/livestream?name=john&psk=supersecret, schema=rtmp, vhost=195.201.153.98, port=1935, app=live/livestream, args=null
[2020-04-23 09:35:16.634][Trace][3921][473] protocol in.buffer=0, in.ack=0, out.ack=0, in.chunk=4096, out.chunk=128
[2020-04-23 09:35:16.724][Trace][3921][473] client identified, type=fmle-publish, vhost=195.201.153.98, app=live/livestream, stream=, param=?name=john&psk=supersecret, duration=0ms
[2020-04-23 09:35:16.726][Warn][3921][473][11] use public address as ip: 192.168.1.10
[2020-04-23 09:35:16.728][Warn][3921][473][11] http: ignore on_close failed, client_id=473, url=http://195.201.153.97/test.php, request={"action":"on_close","client_id":473,"ip":"178.211.242.54","vhost":"__defaultVhost__","app":"live/livestream","send_bytes":3655,"recv_bytes":3430}, response=
, code=404, ret=4005
[2020-04-23 09:35:16.728][Error][3921][473][11] serve error code=4005 : service cycle : rtmp: stream service : check vhost : rtmp: callback on connect : rtmp on_connect http://192.168.1.10/test.php : http: on_connect failed, client_id=473, url=http://192.168.1.10/test.php, request={"action":"on_connect","client_id":473,"ip":"192.168.1.11","vhost":"__defaultVhost__","app":"live/livestream","tcUrl":"rtmp://192.168.1.10:1935/live/livestream?name=john&psk=supersecret","pageUrl":""}, response=
, code=404 : http: status 404
thread [3921][473]: do_cycle() [src/app/srs_app_rtmp_conn.cpp:210][errno=11]
thread [3921][473]: service_cycle() [src/app/srs_app_rtmp_conn.cpp:399][errno=11]
thread [3921][473]: stream_service_cycle() [src/app/srs_app_rtmp_conn.cpp:462][errno=11]
thread [3921][473]: check_vhost() [src/app/srs_app_rtmp_conn.cpp:586][errno=11]
thread [3921][473]: http_hooks_on_connect() [src/app/srs_app_rtmp_conn.cpp:1243][errno=11]
thread [3921][473]: on_connect() [src/app/srs_app_http_hooks.cpp:83][errno=11]
thread [3921][473]: do_post() [src/app/srs_app_http_hooks.cpp:504][errno=11](Resource temporarily unavailable)
[2020-04-23 09:38:40.582][Trace][3921][474] API server client, ip=187.8.182.21
[2020-04-23 09:38:40.582][Trace][3921][474] HTTP API GET http://192.168.1.10:9090/setup/index.jsp, content-length=-1, chunked=0/0
[2020-04-23 09:38:40.984][Warn][3921][474][104] client disconnect peer. ret=1007

也许你比我更懂日志,但我真的不知道我做错了什么。

有什么想法吗?

【问题讨论】:

    标签: php http-live-streaming rtmp simple-realtime-server


    【解决方案1】:

    是的,如果成功,你必须回复something来识别成功,否则SRS会拒绝客户端,从而可以拒绝非法客户端,请阅读Callback Error Code

    注意:on_publish 回调也可以用作高级安全性,通过其 IP 或请求 url 中的令牌或客户端的任何其他信息对 allowdeny 客户端。

    something 的意思是:

    • HTTP/200,即 HTTP 成功。
    • AND 响应和 int 值 0,或字段代码为 0 的 JSON 对象。

    像这样:

    HTTP/1.1 200 OK
    Content-Length: 1
    0
    

    或者:

    HTTP/1.1 200 OK
    Content-Length: 11
    {"code": 0}
    

    您可以通过以下方式运行示例 HTTP 回调服务器:

    cd srs/trunk
    python research/api-server/server.py 8085
    

    你会找出right的回复是什么。

    【讨论】:

      【解决方案2】:

      所以在挖掘了很多小时之后。如果找到,这就是解决方案(但我没有找到正确拒绝的方法),因此如果您输入除 0 以外的任何内容,服务器将出错(而不是通过崩溃)并简单地拒绝有效但不是的连接干净。

      <?php
      echo "0";
      $code = 200;
          // clear the old headers
          header_remove();
          // set the actual code
          http_response_code($code);
          // set the header to make sure cache is forced
          header("Cache-Control: no-transform,public,max-age=300,s-maxage=900");
          // treat this as json
          header('Content-Type: application/json');
          $status = array(
              200 => '200 OK',
              400 => '400 Bad Request',
              422 => 'Unprocessable Entity',
              500 => '500 Internal Server Error'
              );
          // ok, validation error, or failure
          header('Status: '.$status[$code]);
          // return the encoded json
          return json_encode(array(
              'status' => $code < 300, // success or not?
              'message' => $message
              ));
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-30
        • 1970-01-01
        • 1970-01-01
        • 2017-10-13
        • 1970-01-01
        • 1970-01-01
        • 2020-09-11
        • 1970-01-01
        相关资源
        最近更新 更多