Icecast 用户身份验证和网络音频 API

Icecast User Auth and Web Audio API

我 运行 一个 Icecast2 服务器,可以使用网络音频从我的网站获得流 API。我想使用 Icecast User Basic Auth 设置私有流。可以使用以下方式访问这些流:http://Username:Password@example.com/stream.

我面临的问题是我想将 URL 作为 http://example.com/stream 传递给 WEB 音频 API 并使用 XMLHTTPRequest 进行身份验证,如果可能的话;但是,该请求未通过 CORS 预检,我不确定我是否正确设置了 headers.

请注意,我还尝试在不使用任何请求的情况下向 URL 提供用户名和密码,并收到消息:The HTMLMediaElement passed to createMediaElementSource has a cross-origin resource, the node will output silence. 所以我想无论如何我都需要发送请求。

我目前正在本地网络上对此进行测试。 Icecast 服务器在 linux 上 运行ning,我正在测试的网页在 windows 上使用 IIS 运行ning。 Icecast ip 是 192.168.1.30:6048 并且 IIS 是 127.0.0.1:80

下面是我的 Icecast 配置文件和我正在使用的 XMLHTTPRequest 的相关部分。我目前还在测试中,在 Icecast 配置中关闭了全局 headers:

<mount>
    <mount-name>/test-stream.ogg</mount-name>
    <authentication type="htpasswd">
        <option name="filename" value="/usr/share/icecast2/user_auth"/>
        <option name="allow_duplicate_users" value="0"/>
    </authentication>
    <http-headers>
        <header name="Access-Control-Allow-Credentials" value="true" />
        <header name="Access-Control-Allow-Origin" value="http://127.0.0.1" />
        <header name="Access-Control-Allow-Headers" value="Authorization" />
        <header name="Access-Control-Allow-Methods" value="GET, OPTIONS" />
    </http-headers>
</mount>
window.onload = function() {
    let username = "User1";
    let password = "Pass1";

    let xhr = new XMLHttpRequest();
    xhr.open('GET', 'http://192.168.1.30:6048/test-stream.ogg', true);
    xhr.withCredentials = true;
    xhr.setRequestHeader('Origin','http://127.0.0.1');
    xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
                
    xhr.onload = function() {
        // Do Stuff...
    }
    xhr.send(null);
}

尝试将它添加到您的 Icecast 配置文件的顶部(这会启用 * CORS)

<icecast>
    <http-headers>
        <header name="Access-Control-Allow-Origin" value="*" />
        <header name="Access-Control-Allow-Headers" value="Origin, Accept, X-Requested-With, Content-Type, If-Modified-Since" />
        <header name="Access-Control-Allow-Methods" value="GET, OPTIONS, HEAD" />
    </http-headers>
    <limits>
...

经过一系列设置和进一步研究后,我发现尝试在本地主机上的网络服务器上使用 Icecast 身份验证时似乎存在一些未指明的问题。

我通过将所有内容移动到 public 服务器并将 SSL 添加到 Icecast 来解决问题。