Giter Club home page Giter Club logo

local-cors-proxy's Introduction

Local CORS Proxy

Simple proxy to bypass CORS issues. This was built as a local dev only solution to enable prototyping against existing APIs without having to worry about CORS.

This module was built to solve the issue of getting this error:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disable

Getting Started

npm install -g local-cors-proxy

Simple Example

API endpoint that we want to request that has CORS issues:

https://www.yourdomain.ie/movies/list

Start Proxy:

lcp --proxyUrl https://www.yourdomain.ie

Then in your client code, new API endpoint:

http://localhost:8010/proxy/movies/list

End result will be a request to https://www.yourdomain.ie/movies/list without the CORS issues!

Alternatively you can install the package locally and add a script to your project:

 "scripts": {
   "proxy": "lcp --proxyUrl https://www.yourdomain.ie"
 }

Options

Option Example Default
--proxyUrl https://www.google.ie
--proxyPartial foo proxy
--port 8010 8010
--credentials (no value needed) false
--origin http://localhost:4200 *

local-cors-proxy's People

Contributors

acailly avatar garmeeh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

local-cors-proxy's Issues

Unhandled stream error in pipe

I have updated to node 12.0. Now I am facing following error.

internal/streams/legacy.js:59`
      throw er; // Unhandled stream error in pipe.
      ^

Error: write EPROTO 22796:error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol:c:\ws\deps\openssl\openssl\ssl\statem\statem_lib.c:1922:

    at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:83:16)

Blocked due to CORS Policy (redirected from)

Am trying to use this library to bypass CORS on a client side request to a public API.

However, instead of getting the normal CORS error, I am instead getting the same error with note that it was redirected.

This is the error in Chrome

Access to XMLHttpRequest at 'https://www.zoho.com/creator/' (redirected from 'http://localhost:8010/proxy/oauth/v2/token?client_id=#&client_secret=#&refresh_token=#&grant_type=refresh_token') from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Secure Proxy

Hi, thanks for this project!

Is there a way to make the proxy secure? passing a certificate or something?
I am getting a "Blocked loading mixed active content" error on the browser because I'm calling the proxy using https.

Could you suggest how to get started if this is not already built in?

Way to also proxy static files

I want to proxy an entire page but when I do the static files 404.

http://localhost:8010/public/font/v1/400.woff

Is there any way to also proxy those files? Thanks

Handle invalid connection

Thanks for the package - very useful.

Currently, the server throws and exits when it can't connect to the server. I think it would be better for proxy to continue operation when it can't connect to the server, since it's a very common case. Sometimes the other server is still booting up - may be temporarily down - or you forgot to turn it on. It currently throws:

internal/streams/legacy.js:61
      throw er; // Unhandled stream error in pipe.
      ^

Error: connect ECONNREFUSED 127.0.0.1:8080
  at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 8080
}
lcp --proxyUrl http://localhost:8080 exited with code 1

And also a nice error message would be good too something like "Cannot connect to http://localhost:8080"

need exposedHeaders config

Hi, I need to expose some header like 'content-disposition', which is not included by default.

so can you add exposedHeaders config from cros?

'lcp' is not recognized as an internal or external command...

Hi All,

When trying to run proxy, I do receive below response. Running on VS Code on Company Windows 10 OS. Can you please suggest some solution?
system variable "npm" set to ...AppData\Roaming\npm
lcp installed globally as https://www.npmjs.com/package/local-cors-proxy

Thanks in an advance
Stepan

> npm run proxy
> [email protected] proxy C:\Users\stanist3\FSSRepository\portal-public
> lcp --proxyUrl http://X.X.X.X:8080
2020-02-16T16_22_14_228Z-debug.log

'lcp' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] proxy: lcp --proxyUrl http://X.X.X.X:8080
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] proxy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\stanist3\AppData\Roaming\npm-cache_logs\2020-02-16T16_22_14_228Z-debug.log

Seeking Community Input: Is There Still Interest in Maintaining local-cors-proxy?

Hello everyone,

I've found this tool extremely useful and noticed that it hasn't been updated for a while. Before I decide to maintain a fork of this project, I would like to know if there is still interest in this library from the community. If you find this tool valuable and would like to see it updated and maintained, please let me know either by responding to this issue or giving a thumbs up.

Your feedback will help determine the future of this project, and if there's enough interest, I'll proceed with creating a new repository and addressing the current issues, updating dependencies, and ensuring compatibility with newer technologies. I welcome any suggestions or contributions from all of you!

Thank you!

Suppose I need to set proxy for multiple(plural) URLs

Hi ,
I have try the below comment but I got the error
lcp --proxyUrl https://www.google.co.in https://www.google.co.uk/

I got below error

(UNKNOWN_VALUE: Unknown value: https://www.google.co.uk
    at commandLineArgs (C:\Users\gvenunathan\AppData\Roaming\npm\node_modules\local-cors-proxy\node_modules\command-line-args\dist\index.js:1342:21)
    at Object.<anonymous> (C:\Users\gvenunathan\AppData\Roaming\npm\node_modules\local-cors-proxy\bin\lcp.js:19:17)
    at Module._compile (internal/modules/cjs/loader.js:1256:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1277:10)
    at Module.load (internal/modules/cjs/loader.js:1105:32)
    at Function.Module._load (internal/modules/cjs/loader.js:967:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47 {
  value: 'https://www.google.co.uk'
})

I have tried with twice --proxyUrl

lcp --proxyUrl https://www.google.co.in --proxyUrl https://www.google.co.uk

ALREADY_SET: Singular option already set [proxyUrl=https://www.google.co.in]
    at Option._set (C:\Users\gvenunathan\AppData\Roaming\npm\node_modules\local-cors-proxy\node_modules\command-line-args\dist\index.js:1173:21)
    at Option.set (C:\Users\gvenunathan\AppData\Roaming\npm\node_modules\local-cors-proxy\node_modules\command-line-args\dist\index.js:1157:10)
    at commandLineArgs (C:\Users\gvenunathan\AppData\Roaming\npm\node_modules\local-cors-proxy\node_modules\command-line-args\dist\index.js:1365:14)
    at Object.<anonymous> (C:\Users\gvenunathan\AppData\Roaming\npm\node_modules\local-cors-proxy\bin\lcp.js:19:17)
    at Module._compile (internal/modules/cjs/loader.js:1256:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1277:10)
    at Module.load (internal/modules/cjs/loader.js:1105:32)
    at Function.Module._load (internal/modules/cjs/loader.js:967:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47 {
  value: null,
  optionName: 'proxyUrl'
}

Pls help on that to resolve

thank you.

Man. This has virtually saved lives. (at least mine) (or my sanity, or whatever).
I'm very impressed.

thank you and empty proxyPartial

Hi

Just to say thank you, very nice package

Personally I am using it with an empty proxyPartial
lcp --proxyUrl https://example.com --proxyPartial '' --port 8000

Reflect origin

Certain requests require an explicit origin in the 'Access-Control-Allow-Origin' header. A wildcard like '*' is not allowed. Therefore, you should add the option or better change the default value to enable origin reflection in the cors package.

See here: https://expressjs.com/en/resources/middleware/cors.html#configuration-options

origin: Configures the Access-Control-Allow-Origin CORS header. Possible values:

  • Boolean - set origin to true to reflect the request origin, as defined by req.header('Origin'), or set it to false to disable CORS.

Example implementation

Remove the default ('*') in here:

{ name: 'origin', type: String, defaultValue: '*' }

and add the boolean true as fallback for an undefined value here:

proxy.options('*', cors({credentials: credentials, origin: origin}));

like so:

proxy.options('*', cors({
  credentials: credentials, 
  origin: origin || true // true (boolean): enables origin reflection
}));

error when setting credentials to true from package.json

When running lcp locally from a script setting credentials to true throws and error (below)

> lcp --proxyUrl https://sub.domain.com
--port 8000 --credentials true --origin http://localhost:8080

UNKNOWN_VALUE: Unknown value: true
    at commandLineArgs (D:\repo\recipe-svelte\node_modules\command-line-args\dist\index.js:1369:21)
    at Object.<anonymous> (D:\repo\recipe-svelte\node_modules\local-cors-proxy\bin\lcp.js:19:17)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)       
    at Module.load (internal/modules/cjs/l    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as 
runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47 {
  value: 'true'
}

A work around for the moment is to set the default to true in the optionDefinitions variable in /bin/lcp.js. Obviously that's not a permanent solution.

SSL error

Hi,

Thank you for publishing this package!

I am getting the following:

Request Proxied -> /api/v1/[...] // Replaced
Request Proxied -> /api/v1/[...] // Replaced
internal/streams/legacy.js:61
throw er; // Unhandled stream error in pipe.
^

Error: write EPROTO 139838356379520:error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol:../deps/openssl/openssl/ssl/statem/statem_lib.c:1942:

at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:92:16) {

errno: -71,
code: 'EPROTO',
syscall: 'write'
}

Could it be related to using self-signed certificates?

Thanks,
Andrea

Handle response cookies

Is there a way to redirect response cookies to localhost from the proxied host?

I have calls running perfectly from localhost during development, but the target returns a cookie that needs to be passed in future calls for authentication. Since I'm proxying the call, I imagine the response cookie domain is set to the proxy and not localhost so doesn't appear in the browser.

I'm having some content encoding issues

It's a bit above my head, but do you know if you can make it more resilient to content encoding? Binary data and stuff as I think these are causing issues for me?

Not succeeding, still getting the 'No 'Access-Control-Allow-Origin' header is present on the requested resource.' error

I'm trying to test endpoints of my Node.js app running on localhost:3000 making requests from my Flutter web app running on localhost:5000 and I get the infamous 'No 'Access-Control-Allow-Origin' header is present on the requested resource.' error. So I'm trying to use local-core-proxy..
In my package.json scripts I added "proxy": "lcp --proxyUrl http://localhost --port 3000" .. and if I got right this should generate localhost:3000/proxy domain which I 'd use for my request as:

final Uri uri = Uri.http('localhost:3000',
        '/proxy/api/products');

But when making the request I still get the error:

Access to XMLHttpRequest at 'http://localhost:3000/proxy/api/products' from origin 'http://localhost:5000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Am I missing to set something?
Many thanks for this package.

Thank you

Just wanted to thank you for this simple solution, another projects has many stars and it's complex and didn't work for me.

Thank you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.