简体   繁体   中英

Node GCM with proxy - port 443 disabled

I am using the plugin https://www.npmjs.com/package/node-gcm

It works fine with port 443 enabled without any proxy set up. But I need it to work in an RHEL environment with 443 disabled.

Node GCM plugin behind a proxy. Below is the code

var message = new gcm.Message();

            // ... or some given values 
            var message = new gcm.Message({
              collapseKey: 'demo',
              priority: 'high',
              contentAvailable: true,
              delayWhileIdle: true,
              //timeToLive: 10000,
              restrictedPackageName: '',
              dryRun: false
            });


            var notification_title = config_file.gcm.gcm_title ? config_file.gcm.gcm_title.replace(/\\'/g,"'") : '';
            var message_content = config_file.gcm.gcm_content ? config_file.gcm.gcm_content.replace(/\\'/g,"'") : '';
            // as object 
            message.addNotification({
                title: notification_title,
                body: '',
                icon: 'notification',
                tag: false,
                sound: true
            });



            // Set up the sender with you API key 
            var requestOptions = {
                proxy: 'http://aws-public-ip:8000',
                timeout: 1000
                // strictSSL: false,
                // method: 'POST'
            };
            var sender = new gcm.Sender(config_file.gcm.gcm_api, requestOptions);

            // Add the registration tokens of the devices you want to send to 
            var tokens = [];
            for(var t = 0; t < token.length; t++)
            {
                tokens.push(token[t].gcm_token);
            }

            // Max devices per request    
            var batchLimit = 1000;

            // Batches will be added to this array
            var tokenBatches = [];

            // Traverse tokens and split them up into batches of 1,000 devices each  
            for ( var start = 0; start < tokens.length; start += batchLimit )
            {
                // Get next 1,000 tokens
                var slicedTokens = tokens.splice(start, start + batchLimit);

                // Add to batches array
                tokenBatches.push(slicedTokens);
            }
            // You can now send a push to each batch of devices, in parallel, using the caolan/async library
            async.each( tokenBatches, function( batch, callback )
            {
                // Assuming you already set up the sender and message
                sender.send(message, { registrationTokens: batch }, function(err, response) {  

                    // Push failed?
                    if (err)
                    {
                        // Stops executing other batches
                        console.log('ds');
                        console.log(err);
                    }
                    else {
                        console.log('ts');
                        console.log(response);
                    }
                    // Done with batch
                    callback();
                });
            },
            function( err )
            {
                // Log the error to console
                if ( err )
                {
                    console.log(err);
                }
            });

Also I have enabled the outgoing port for 8000. Also below commands to enable the proxy in aws and npm

export HTTP_PROXY="http://aws-public-ip:8000"
export HTTPS_PROXY="http://aws-public-ip:8000"
export NO_PROXY="169.254.169.254"
export http_proxy="http://aws-public-ip:8000"
export https_proxy="http://aws-public-ip:8000"
export no_proxy="169.254.169.254"
npm config set proxy http://aws-public-ip:8000
npm config set https-proxy http://aws-public-ip:8000
npm config set strict-ssl false
npm config set registry http://registry.npmjs.org
sudo npm config set proxy http://aws-public-ip:8000 -g

I get the error { [Error: tunneling socket could not be established, cause=socket hang up] code: 'ECONNRESET' }

TL DR: I'm not sure if I have set the proxy in aws correctly with the above commands. Also, I am not sure if the plugin will work with 443 port being disabled.

My Mistake. The proxy address should have been a correct ip, which accepts proxy and has connection to 443 port open.

I was using the same ip of aws as proxy not knowing what proxy really is.

I created another instance in aws with ubuntu OS and installed tiny proxy. Opened required ports and provided the new instance ip as proxy in the my app and it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM