2012-07-04 77 views
1

我正在創建一個連接到Proxmox 2.1的REST api的NodeJS服務。NodeJS https客戶端錯誤 - 400

我的腳本:

// module dependencies 
var http = require('http'), 
    url  = require('url'), 
    https = require('https'), 
    querystring = require('querystring'), 
    fs  = require('fs'); 

exports.urlReq = function(reqUrl, options, cb){ 
    if(typeof options === "function"){ cb = options; options = {}; }// incase no options passed in 

    // parse url to chunks 
    reqUrl = url.parse(reqUrl); 

    // http.request settings 
    var settings = { 
     host: reqUrl.hostname, 
     port: reqUrl.port || 80, 
     path: reqUrl.pathname, 
     headers: options.headers || {}, 
     method: options.method || 'GET', 
     requestCert: false 
    }; 

    // if there are params: 
    if(options.params){ 
     options.params = querystring.stringify(options.params); 
     settings.headers['Content-Length'] = options.params.length; 
    }; 


    // MAKE THE REQUEST 
    var req = https.request(settings); 

    req.on('error', function(err) { 
     console.log(err); 
    }); 

    // when the response comes back 
    req.on('response', function(res){ 
     res.body = ''; 

     console.log("statusCode: ", res.statusCode); 
     console.log("headers: ", res.headers); 

     res.setEncoding('utf-8'); 

     // concat chunks 
     res.on('data', function(chunk){ res.body += chunk }); 

     res.on('error', function(err){ 
      throw err; 
     }) 

     // when the response has finished 
     res.on('end', function(){ 

      // fire callback 
      cb(res.body, res); 
     }); 
    }); 

    // if there are params: write them to the request 
    if(options.params){ req.write(options.params) }; 

    // end the request 
    req.end(); 
} 

該腳本GET請求,雖然這樣做POST請求時,它死。它不會拋出任何錯誤,它只是默默地失敗。

當控制檯登錄響應,這裏是res.connection對象:

connection: 
     { pair: [Object], 
     writable: true, 
     readable: true, 
     _paused: false, 
     _needDrain: false, 
     _pending: [], 
     _pendingCallbacks: [], 
     _pendingBytes: 0, 
     socket: [Object], 
     encrypted: [Object], 
     authorized: false, 
     _controlReleased: true, 
     _events: [Object], 
     _pool: <Buffer 48 54 54 50 2f 31 2e 31 20 34 30 30 20 50 61 72 61 6d 65 74 65 72 20 76 65 72 69 66 69 63 61 74 69 6f 6e 20 66 61 69 6c 65 64 20 2d 20 64 75 70 6c 69 63 ...>, 
     _poolStart: 332, 
     _poolEnd: 65536, 
     parser: [Object], 
     _httpMessage: [Circular], 
     ondata: [Function: socketOnData], 
     onend: [Function: socketOnEnd], 
     npnProtocol: undefined, 
     authorizationError: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' }, 

服務器使用自簽名證書的SSL。

任何幫助將不勝感激,

謝謝!

回答

1

authorized: falseauthorizationError: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'明確指出這是證書問題,通常是由於證書未被Root Certification Authority正確簽名。

我不知道您是否可以在Proxmox服務器上設置正確簽名的證書,但管理它的人應該如此。 如果它的一些的軟件,你維持或管理,那麼你有兩個選擇(請注意以下內容適用於其中一個HTTPS客戶端需要信任的HTTPS服務器每一種情況):

  1. 安裝正確簽名的證書在服務器上(購買它,或者告訴管理它的人);或
  2. 讓您的客戶信任無法驗證的證書。

如果你打算做第一件事,那麼在客戶端沒有別的事情要做。

那麼,接踵而來的是如何讓你的NodeJS HTTPS客戶端信任從一個特定的服務器使用自簽名證書的簡要說明(請注意,它只會增加特定證書的信任鏈,不所有自簽名證書):

  1. 使用Web瀏覽器訪問目標URL,並在本地保存證書。 Mozilla Firefox支持以PEM格式導出證書。
  2. 如果您的瀏覽器無法導出PEM格式的證書,請使用OpenSSLconvert它。
  3. 最後,通過TLS服務器選項對象指示HTTPS代理信任證書。詳細信息請參見nodejs tls.connect() documentation
3

這是比較舊,但最近我遇到類似的問題與Proxmox,並希望有助於防止其他人看到這個問題。

要解決「authorizationError:‘UNABLE_TO_VERIFY_LEAF_SIGNATURE’」的錯誤,你可以指示的node.js接受自簽名證書(它拒絕在默認情況下),把這個在你的腳本的頂部:

處理。env ['NODE_TLS_REJECT_UNAUTHORIZED'] ='0';

此外,我不能告訴你的代碼是否設置了Content-Type頭,但它必須設置爲應用程序/ x-www-form-urlencoded用於POST到Proxmox API。也許你已經在options.headers中設置了它,但是當你指定Content-Length時,我不確定。 POST也需要CSRFPreventionToken,所以應該作爲標題的一部分傳遞。

+0

這樣做和使用普通的未加密的HTTP幾乎相同。請注意,通過遵循此建議,您正在創建安全漏洞。 https://security.stackexchange.com/search?q=self+signed+certificates –