2013-12-12 21 views
8

Hiện tại, tôi đang sử dụng khung sails dựa trên expressj và sử dụng passportjs (http://passportjs.org/) để thực hiện xác thực yammer.Dù sao để đặt cài đặt proxy trong hộ chiếu?

Tôi gặp sự cố khi triển khai ứng dụng nút trên máy chủ nằm phía sau proxy của công ty. Nó không thể kết nối với yammer để xác thực OAuth2.

Lỗi này là như sau:

 
error: failed to obtain access token (Error: connect ETIMEDOUT) 
    at /root/rlps/node_modules/passport-yammer/node_modules/passport-oauth/lib/passport-oauth/strategies/oauth2.js:125:38 
    at /root/rlps/node_modules/passport-yammer/lib/passport-yammer/strategy.js:72:20 
    at ClientRequest. (/root/rlps/node_modules/passport-yammer/node_modules/passport-oauth/node_modules/oauth/lib/oauth2.js:129:5) 
    at ClientRequest.EventEmitter.emit (events.js:95:17) 
    at CleartextStream.socketErrorListener (http.js:1547:9) 
    at CleartextStream.EventEmitter.emit (events.js:95:17) 
    at Socket.onerror (tls.js:1437:17) 
    at Socket.EventEmitter.emit (events.js:117:20) 
    at net.js:441:14 
    at process._tickDomainCallback (node.js:459:13) 

Tôi tin rằng đó là vì các proxy được chặn đường đi. Tôi đã cố gắng để thiết lập tất cả mọi thứ trong giá trị môi trường (như http_proxy và https_proxy) nhưng dường như mã không thừa nhận chúng và cần phải cấu hình nó ở đâu đó trong hộ chiếu thay thế.

Vì vậy, có cách nào tốt để đặt cài đặt proxy trong hộ chiếu hoặc giải quyết vấn đề proxy này trong nodejs?

+0

Chúng tôi cần thêm chi tiết. Bạn đang gặp phải lỗi gì và nó bắt nguồn từ đâu? Bạn có thể cách ly mã liên quan và đăng nó không? Đây có phải là vấn đề với cookie an toàn không và sự cố có chỉ hiển thị khi máy chủ ở sau proxy không? Nếu vậy, và nếu bạn đang sử dụng Express, hãy kiểm tra cài đặt 'trust proxy' trong Express [docs] (http://expressjs.com/api.html#app-settings). –

+0

Nếu tôi có thể tiếp tục với giả định rằng bạn đang sử dụng Express, hãy xem [Cách đặt cookie an toàn bằng cách sử dụng heroku + node.js + express?] (Http://stackoverflow.com/questions/14463972/how-to-set -secure-cookie-using-heroku-node-js-express) –

+0

xin lỗi vì câu hỏi không rõ ràng. Tôi sẽ bổ sung thêm chi tiết. Đã thêm –

Trả lời

6

Node.js không sử dụng các biến số http_proxyhttps_proxy theo mặc định.

Bạn cần phải tinh chỉnh các thông số agent cho yêu cầu nhưng vì bạn không có quyền kiểm soát trên thư viện mà bạn có thể thay đổi toàn cầu như thế này:

npm i tunnel --save 

tạo setup_proxy.js:

var env = process.env; 

if (!env['http_proxy']) return; 

var localUrls = [ 
    'http://some-internal-url.mycompany.local', 
]; 

var url = require('url'); 
var tunnel = require('tunnel'); 
var proxy = url.parse(env['http_proxy']); 

var tunnelingAgent = tunnel.httpsOverHttp({ 
    proxy: { 
    host: proxy.hostname, 
    port: proxy.port 
    } 
}); 

var https = require('https'); 
var http = require('http'); 

var oldhttpsreq = https.request; 
https.request = function (options, callback) { 

    if (localUrls.some(function (u) { 
    return ~u.indexOf(options.host); 
    })){ 
    return oldhttpsreq.apply(https, arguments); 
    } 

    options.agent = tunnelingAgent; 
    return oldhttpsreq.call(null, options, callback); 
}; 

var oldhttpreq = http.request; 
http.request = function (options, callback) { 

    if (localUrls.some(function (u) { 
    return ~u.indexOf(options.host); 
    })){ 
    return oldhttpreq.apply(http, arguments); 
    } 

    options.agent = tunnelingAgent; 
    return oldhttpreq.call(null, options, callback); 
}; 

yêu cầu điều này ngay từ đầu require('./setup_proxy').

Lưu ý rằng điều này sử dụng cùng một biến số http_proxy env cho lưu lượng truy cập http và https, nhưng mã dễ làm theo nếu bạn cần thay đổi.

+0

Tại sao bạn sử dụng 'var oldhttpreq = https.request;' trước 'http.request'?Tôi cũng nhận được một vòng lặp vô hạn + 'Maximium Stack Size Exceeded ' –

+0

@AndiGiga, chúng tôi đang lồng ghép hàm và chúng ta cần giữ một tham chiếu đến hàm ban đầu. –

+0

Nhưng tại sao 'var oldhttpreq = https.request;' không 'var oldhttpreq = http.request;' 'http' vs' https' –

Các vấn đề liên quan