ndg63276 / smartathome

GNU General Public License v3.0
50 stars 15 forks source link

Cors anywhere rate limit #2

Closed ndg63276 closed 3 years ago

ndg63276 commented 3 years ago

The smart life and sonoff sites need CORS-anywhere (https://github.com/Rob--W/cors-anywhere) to work in a browser, as CORS are disabled on their servers. I have been using the demo CORS anywhere server at https://cors-anywhere.herokuapp.com/, but this is limited to 200 requests per hour.

Need to run my own CORS anywhere server.

ndg63276 commented 3 years ago

Easy enough to set one up on an EC2 server. Use root to allow port 80.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install node
npm install http-proxy
npm install proxy-from-env
git clone https://github.com/Rob--W/cors-anywhere.git
cd cors-anywhere/
export PORT=80
node server.js

Make sure to enable that inbound rule in EC2 Management Console under Security Groups.

Should now see Cors Anywhere help at http://ec2-xxx-xxx-xxx-xxx.eu-west-1.compute.amazonaws.com/, but not secure.

ndg63276 commented 3 years ago

To set up the node to run in a forked process, and then tell it to run on server startup, use:

sudo /bin/bash
cd
npm install pm2 -g
pm2 start server.js
pm2 startup
pm2 ecosystem

This will set up an ecosystem.config.js file. Edit the apps part to have:

module.exports = {
  apps : [{
    name: 'cors-anywhere',
    script: '/root/cors-anywhere/server.js',
    watch: '.',
    env: {
      'PORT': '80',
      'CORSANYWHERE_WHITELIST': 'https://smartathome.co.uk,' +
                                'https://www.smartathome.co.uk,' +
                                'http://192.168.1.93:8000,' +
                                'https://192.168.1.93:4443'
    }
  }],

  deploy : {
    production : {
      user : 'SSH_USERNAME',
      host : 'SSH_HOSTMACHINE',
      ref  : 'origin/master',
      repo : 'GIT_REPOSITORY',
      path : 'DESTINATION_PATH',
      'pre-deploy-local': '',
      'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production',
      'pre-setup': ''
    }
  }
};

Stop all and restart by

pm2 stop all
pm2 start ecosystem.config.js
ndg63276 commented 3 years ago

Cloudfront set up

Set up a cloudfront instance. Under General, make sure to set the CNAME eg cors.smartathome.co.uk. Use the same custom SSL certificate as the main smartathome.co.uk website. Under Origins, set the Origin Protocol Policy as HTTP only, and the HTTP port as 80. Under Behaviours, set the Origin to the EC2 instance. Set the Viewer Protocol Policy to HTTPS Only. Set Allowed HTTP Methods to include OPTIONS, PUT, POST, etc. Use Legacy Cache Settings. Cache Based on Selected Request Headers: Whitelist. Add Origin and Authorization to Whitelist. (https://github.com/ndg63276/sonoff/issues/2) Use Origin Cache Headers. Query String Forwarding and Caching set to "Forward All, Cache Based on All".

Should now see Cors Anywhere help at https://xxxxxxxxxxx.cloudfront.net/, secure but not very friendly.

ndg63276 commented 3 years ago

Route 53 set up

In the smartathome hosted zone, set up a new record. Record name = cors.smartathome.co.uk Route traffic to = Alias to cloudfront distribution, choose the Cloudfront from above. Record type = A

Now see Cors Anywhere help at https://cors.smartathome.co.uk/