Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse Error when using REST client behind proxy #16

Closed
super132 opened this issue Feb 13, 2014 · 11 comments
Closed

Parse Error when using REST client behind proxy #16

super132 opened this issue Feb 13, 2014 · 11 comments

Comments

@super132
Copy link

Hi,

I have experienced an error throwing from Socket.socketOnData method when calling REST client behind a proxy. Here is the code I used:

var RestClient = require('node-rest-client').Client, 
    client = new RestClient({
        proxy:{
            host:"proxyDomain",
            port: 8080,
            user:"username",
            password:"password"
        }
    });

client.registerMethod("jsonMethod", "http://ogcef.one.gov.hk/event-psi/json/calendar/20140215.json", "GET");

client.methods.jsonMethod(function (data, response) {...});

After checking up the REST client code a bit, I found that it will try to connect to proxy via 'CONNECT' method using Node.js http client and then making another connection after this call returned successfully. The issue is that if you use Node.js http client to trigger a call using 'CONNECT' method, the above exception is thrown.

May I suggest to use something similiar below so that REST client can connect to the target URL successfully behind a proxy environment? I have tested in my local and it works:

var request = http.request({
    host: 'proxy',
    port: '8080',
    method: 'GET',
    path: 'http://ogcef.one.gov.hk/event-psi/json/calendar/20140215.json',
    headers: {
        'host': 'ogcef.one.gov.hk',
        'Proxy-Authentication': 'Basic ' + new Buffer(['username','password'].join(":")).toString("base64")
    },
    agent: false
}); 

Is it possible to change ConnectManager.proxy function to use the approach above instead of making a CONNECT call?

Thanks and this package is an awesome package for Node.js :)

Sincerely,
Jason

@aacerox
Copy link
Owner

aacerox commented Feb 13, 2014

Hi

First of all many thanks for your support words, they're very kind ;)

I think I've an easy solution to your problem and most important - at least to me - that won't break compatibility with previous node-rest-client versions.

I'll try to apply it in the next few days (probably on next monday it will be ready), keep an eye.

@aacerox
Copy link
Owner

aacerox commented Feb 19, 2014

Hi as you can see it has not been fixed "on next monday".... :(

I'll try to fix it asap

sorry for the delay.

@super132
Copy link
Author

No need to sorry, thanks :)
Actually I can help fix it and send you a pull request if you don't mind

@aacerox
Copy link
Owner

aacerox commented Feb 20, 2014

I'm already fixing the issue, but of course you can do a pull request whenever you want. Surely it will help me.

just one question, the request that works and you mention in your previous comment was done directly with http client for node.js and not using node-rest-client?

Thanks

@aacerox
Copy link
Owner

aacerox commented Feb 20, 2014

Well I think I've fix it. As you can see in the new version of the docs there's a new parameter in proxy options "tunnel:true | false". So if you want to use direct request to proxy instead of tunneling you must instantiate the client with "tunnel:false" (to maintain backward compatibility I couldn't set direct request to proxy as default proxy connection method)

var RestClient = require('node-rest-client').Client, 
    client = new RestClient({
        proxy:{
            host:"proxyDomain",
            port: 8080,
            user:"username",
            password:"password",
            tunnel:false // use direct request to proxy
        }
    });

client.registerMethod("jsonMethod", "http://ogcef.one.gov.hk/event-psi/json/calendar/20140215.json", "GET");

client.methods.jsonMethod(function (data, response) {...});

I've tested and for me it's working, please let me know whether it works for you too.
Bye

@aacerox aacerox closed this as completed Feb 20, 2014
aacerox added a commit that referenced this issue Feb 20, 2014
new direct request to proxy connection
@super132
Copy link
Author

Thanks. It works like a charm. :)

Would you please release it to NPM repository also? Thanks a lot.

@aacerox
Copy link
Owner

aacerox commented Feb 21, 2014

That's what I'm trying since yesterday, but I'm having unexpected authentication problems with npm registry (?!??).

Hope that it'll be fixed soon

@aacerox
Copy link
Owner

aacerox commented Feb 21, 2014

I've had to reinstall node and npm and now everything seems to work again, but please if you don't mind let me know whether it works for you.

Thanks.

@toddzebert
Copy link

Hey guys, thanks so much for this module - it's made my first forray into Node a lot easier.

But, I was running 0.7.7 and I happened to be combining projects and ran npm install in a new directory, and got version 0.7.9 (had "node-rest-client" : "~0.7.7"), and I noticed the commit associated with this issue.

And I got this error:

$ nodemon server.js
21 Feb 14:50:44 - [nodemon] v1.0.15
21 Feb 14:50:44 - [nodemon] to restart at any time, enter `rs`
21 Feb 14:50:44 - [nodemon] watching: *.*
21 Feb 14:50:44 - [nodemon] starting `node server.js`
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0

...live/node_modules/node-rest-client/lib/node-rest-client.js:14
    self.useProxyTunnel = self.options.proxy.tunnel===undefined?true:self.options
                                            ^
TypeError: Cannot read property 'tunnel' of undefined
    at new exports.Client (.../live/node_modules/node-rest-client/lib/node-rest-client.js:14:42)
    at Object.<anonymous> (.../live/server/routes.js:10:10)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (.../live/server.js:39:1)
    at Module._compile (module.js:456:26)
21 Feb 14:50:52 - [nodemon] app crashed - waiting for file changes before starting...

I reverted to 0.7.7 and it worked.

server.js:39 is:

require('./server/routes.js')(app, passport);

routes.js:10 is the client = line below:

var    passport = require('passport'),
       Client   = require('node-rest-client').Client;

var host = "https://my.host";

var options_auth = {user    : "my-user",
                    password: "my-pw"
                    };

client = new Client(options_auth);

Hopefully this is helpful.

Thanks.

@super132
Copy link
Author

Yes, direct proxy connection works for me. Thanks.

@HarshaJagadish
Copy link

HarshaJagadish commented Aug 22, 2017

Hello @aacerox ,
I know its been a long time since this issue was fixes but I am still facing issue. The client is unable to complete the request when inside a proxy. The SSL hand shake fails to connect to the server. below is my set up for the proxy
options_proxy = { proxy: { host: "<My ip>", port: 8888, tunnel: true } }; globalproxySet = true; client = new Client(options_proxy);

I have the client response as follows after the set up

EventEmitter { options: { proxy: { host: '<my local IP>', port: 8888, tunnel: true } }, useProxy: true, useProxyTunnel: true, proxy: { host: '<My IP>', port: 8888, tunnel: true }, connection: {}, mimetypes: {}, requestConfig: {}, responseConfig: {}, methods: {}, parsers: { add: [Function], remove: [Function], find: [Function], getAll: [Function], getDefault: [Function], clean: [Function] }, serializers: { add: [Function], remove: [Function], find: [Function], getAll: [Function], getDefault: [Function], clean: [Function] }, get: [Function], post: [Function], put: [Function], delete: [Function], patch: [Function], registerMethod: [Function], unregisterMethod: [Function], addCustomHttpMethod: [Function] }

I am using Charles for proxy settings. Please let me know what am I doing wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants