IP 攝影機的反向代理

IP 攝影機的反向代理

我正在使用 Amcrest 相機,它需要基本身份驗證才能渲染其串流(文件- 第 17 頁)。相機是透過 存取的http://admin:password@IP_CAMERA/cgi-bin/mjpg/video.cgi

當我嘗試點擊 時:56700,系統會提示我進行身份驗證,即使它是硬編碼的(如下)。即使我輸入正確的憑證,它也會失敗。我究竟做錯了什麼?

    server {
        listen 56700;
        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://IP_CAMERA/cgi-bin/mjpg/video.cgi;
            proxy_set_header Authorization "Basic xxx";
        }
    }

我也嘗試過proxy_pass_header Authorization;按照描述添加這裡

答案1

正如上面所說,可以透過以下方式解決

proxy_set_header Authorization "Basic dXNlcjpwYXNzd29yZA==";

其中dXNlcjpwYXNzd29yZA==是命令的結果:echo -n "user:password" | base64

我想你已經嘗試過了

不管怎樣,我相信正確的位置看起來像

location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://IP_CAMERA;
    proxy_set_header Authorization "Basic dXNlcjpwYXNzd29yZA==";
    proxy_set_header Authorization "Basic $http_authorization"; # For interactive mode
}

沒有,/cgi-bin/mjpg/video.cgi因為它會在瀏覽器或 http 用戶端傳遞

或者如果它不適合您,則可能與此案例有關 https://stackoverflow.com/questions/14839712/nginx-reverse-proxy-passthrough-basic-authenication

如果相機有一些領域檢查,您可以透過「標題」標籤 - 回應標題部分的開發工具來了解預期的領域。

但是你需要編譯 nginxheaders-more-nginx-module

你可以用這個腳本來實現

cd /usr/src
NGINXFILE=$(wget -qO- http://nginx.org/en/download.html | tr ' ' '\n' | egrep -o 'nginx.+?tar.gz' | head -1)
wget http://nginx.org/download/${NGINXFILE}
tar zxvf ${NGINXFILE}
cd ${NGINXFILE%.*.*}

cp -r /etc/nginx /root/nginx_$(date +%F) #Backup current nginx configs

cd /usr/src
git clone https://github.com/openresty/headers-more-nginx-module.git

./configure --add-module=/usr/src/headers-more-nginx-module --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
make
make install

完成後你可以嘗試這樣的配置:

location / {
  proxy_http_version      1.1;
  proxy_pass_request_headers on;
  proxy_set_header        Host            $host;
  proxy_set_header        X-Real-IP       $remote_addr;
  proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

  more_set_input_headers  'Authorization: Basic dXNlcjpwYXNzd29yZA=='; 
  #more_set_input_headers  'Authorization: $http_authorization'; # For interactive mode
  proxy_set_header  Accept-Encoding  "";

  proxy_pass              http://IP_CAMERA;
  proxy_redirect          default;
  more_set_headers        -s 401 'www-authenticate: Basic realm="Authentication Required"';
}

www-authenticate: Basic realm="Authentication Required"該標頭的實際數據在哪裡

我檢查了這兩種情況,它對我有用,我在自訂燒瓶應用程式上測試了它。不幸的是我沒有這樣的相機用於個人調試

答案2

使用 Amcrest 特定解決方案進行編輯

在某個時候(2017 年左右?)Amcrest發布了一個韌體更新刪除了基本驗證來自他們的 IP 攝影機,將摘要式身份驗證作為唯一的選擇。

這個堆疊溢位答案可能是您像使用 FastCGI 和 nginx 一樣剝離摘要式驗證的最佳選擇。

或者您可以找到一種方法來降級相機上的韌體以再次支援基本身份驗證。


上一個答案

您是否對使用者名稱和密碼進行 Base64 編碼並在其間使用冒號?

為了HTTP 基本驗證,您需要執行以下操作:

proxy_set_header Authorization "Basic xxx";

將 替換xxxBase64(<username>:<password>).也就是說,找到一個 Base-64 編碼器,輸入使用者名稱、冒號 ( :) 字元和密碼,然後將其替換xxx為結果字串。

例如,如果使用者名稱是admin,密碼是hunter2,我們可以在 Bash 提示字元下執行以下命令:

printf %s 'admin:hunter2' | base64

並將結果字串放入標題中,如下所示:

proxy_set_header Authorization "Basic YWRtaW46aHVudGVyMg==";

答案3

在黑暗中拍攝

我知道有時如果文件權限設定不正確,您會得到一個登入框,在某些情況下,該登入框適用於文件權限級別,而不是連接級別(如果有意義的話)。也許這就是 Web 伺服器端發生的某種檔案權限問題?您最終可能會得到兩種不同的憑證,一種是基於連線/服務的憑證,如果連線/服務沒有檔案權限,則為檔案層級的憑證。

相關內容