Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

CORS Anywhere is a NodeJS reverse proxy which adds CORS headers to the proxied request.

License

NotificationsYou must be signed in to change notification settings

Go-phie/cors-anywhere

 
 

Repository files navigation

Build StatusCoverage Status

CORS Anywhere is a NodeJS proxy which adds CORS headers to the proxied request.

The url to proxy is literally taken from the path, validated and proxied. The protocolpart of the proxied URI is optional, and defaults to "http". If port 443 is specified,the protocol defaults to "https".

This package does not put any restrictions on the http methods or headers, except forcookies. Requestinguser credentials is disallowed.The app can be configured to require a header for proxying a request, for example to avoida direct visit from the browser.

Example

// Listen on a specific host via the HOST environment variablevarhost=process.env.HOST||'0.0.0.0';// Listen on a specific port via the PORT environment variablevarport=process.env.PORT||8080;varcors_proxy=require('cors-anywhere');cors_proxy.createServer({originWhitelist:[],// Allow all originsrequireHeader:['origin','x-requested-with'],removeHeaders:['cookie','cookie2']}).listen(port,host,function(){console.log('Running CORS Anywhere on '+host+':'+port);});

Request examples:

  • http://localhost:8080/http://google.com/ - Google.com with CORS headers
  • http://localhost:8080/google.com - Same as previous.
  • http://localhost:8080/google.com:443 - Proxieshttps://google.com/
  • http://localhost:8080/ - Shows usage text, as defined inlibs/help.txt
  • http://localhost:8080/favicon.ico - Replies 404 Not found

Live examples:

Documentation

Client

To use the API, just prefix the URL with the API URL. Take a look atdemo.html for an example.A concise summary of the documentation is provided atlib/help.txt.

Note: as of February 2021, access to the demo server requires an opt-in,see:https://github.com/Rob--W/cors-anywhere/issues/301

If you want to automatically enable cross-domain requests when needed, use the following snippet:

(function(){varcors_api_host='cors-anywhere.herokuapp.com';varcors_api_url='https://'+cors_api_host+'/';varslice=[].slice;varorigin=window.location.protocol+'//'+window.location.host;varopen=XMLHttpRequest.prototype.open;XMLHttpRequest.prototype.open=function(){varargs=slice.call(arguments);vartargetOrigin=/^https?:\/\/([^\/]+)/i.exec(args[1]);if(targetOrigin&&targetOrigin[0].toLowerCase()!==origin&&targetOrigin[1]!==cors_api_host){args[1]=cors_api_url+args[1];}returnopen.apply(this,args);};})();

If you're using jQuery, you can also use the following codeinstead of the previous one:

jQuery.ajaxPrefilter(function(options){if(options.crossDomain&&jQuery.support.cors){options.url='https://cors-anywhere.herokuapp.com/'+options.url;}});

Server

The module exportscreateServer(options), which creates a server that handlesproxy requests. The following options are supported:

  • functiongetProxyForUrl - If set, specifies which intermediate proxy to use for a given URL.If the return value is void, a direct request is sent. The default implementation isproxy-from-env, which respects the standard proxyenvironment variables (e.g.https_proxy,no_proxy, etc.).
  • array of stringsoriginBlacklist - If set, requests whose origin is listed are blocked.
    Example:['https://bad.example.com', 'http://bad.example.com']
  • array of stringsoriginWhitelist - If set, requests whose origin is not listed are blocked.
    If this list is empty, all origins are allowed.Example:['https://good.example.com', 'http://good.example.com']
  • functionhandleInitialRequest - If set, it is called with the request, response and a parsedURL of the requested destination (null if unavailable). If the function returns true, the requestwill not be handled further. Then the function is responsible for handling the request.This feature can be used to passively monitor requests, for example for logging (return false).
  • functioncheckRateLimit - If set, it is called with the origin (string) of the request. If thisfunction returns a non-empty string, the request is rejected and the string is send to the client.
  • booleanredirectSameOrigin - If true, requests to URLs from the same origin will not be proxied but redirected.The primary purpose for this option is to save server resources by delegating the request to the client(since same-origin requests should always succeed, even without proxying).
  • array of stringsrequireHeader - If set, the request must include this header or the API will refuse to proxy.
    Recommended if you want to prevent users from using the proxy for normal browsing.
    Example:['Origin', 'X-Requested-With'].
  • array of lowercase stringsremoveHeaders - Exclude certain headers from being included in the request.
    Example:["cookie"]
  • dictionary of lowercase stringssetHeaders - Set headers for the request (overwrites existing ones).
    Example:{"x-powered-by": "CORS Anywhere"}
  • numbercorsMaxAge - If set, an Access-Control-Max-Age request header with this value (in seconds) will be added.
    Example:600 - Allow CORS preflight request to be cached by the browser for 10 minutes.
  • stringhelpFile - Set the help file (shown at the homepage).
    Example:"myCustomHelpText.txt"

For advanced users, the following options are also provided.

  • httpProxyOptions - Under the hood,http-proxyis used to proxy requests. Use this option if you really need to pass optionsto http-proxy. The documentation for these options can be foundhere.
  • httpsOptions - If set, ahttps.Server will be created. The given options are passed to thehttps.createServer method.

For even more advanced usage (building upon CORS Anywhere),see the sample code intest/test-examples.js.

Demo server

A public demo of CORS Anywhere is available athttps://cors-anywhere.herokuapp.com. This server isonly provided so that you can easily and quickly try out CORS Anywhere. To ensure that the servicestays available to everyone, the number of requests per period is limited, except for requests fromsome explicitly whitelisted origins.

Note: as of February 2021, access to the demo server requires an opt-in,see:https://github.com/Rob--W/cors-anywhere/issues/301

If you expect lots of traffic, please host your own instance of CORS Anywhere, and make sure thatthe CORS Anywhere server only whitelists your site to prevent others from using your instance ofCORS Anywhere as an open proxy.

For instance, to run a CORS Anywhere server that accepts any request from some example.com sites onport 8080, use:

export PORT=8080export CORSANYWHERE_WHITELIST=https://example.com,http://example.com,http://example.com:8080node server.js

This application can immediately be run on Heroku, seehttps://devcenter.heroku.com/articles/nodejsfor instructions. Note that theirAcceptable Use Policy forbidsthe use of Heroku for operating an open proxy, so make sure that you either enforce a whitelist asshown above, or severly rate-limit the number of requests.

For example, to blacklist abuse.example.com and rate-limit everything to 50 requests per 3 minutes,except for my.example.com and my2.example.com (which may be unlimited), use:

export PORT=8080export CORSANYWHERE_BLACKLIST=https://abuse.example.com,http://abuse.example.comexport CORSANYWHERE_RATELIMIT='50 3 my.example.com my2.example.com'node server.js

License

Copyright (C) 2013 - 2021 Rob Wurob@robwu.nl

Permission is hereby granted, free of charge, to any person obtaining a copy ofthis software and associated documentation files (the "Software"), to deal inthe Software without restriction, including without limitation the rights touse, copy, modify, merge, publish, distribute, sublicense, and/or sell copiesof the Software, and to permit persons to whom the Software is furnished to doso, subject to the following conditions:

The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.

About

CORS Anywhere is a NodeJS reverse proxy which adds CORS headers to the proxied request.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript97.0%
  • HTML2.9%
  • Makefile0.1%

[8]ページ先頭

©2009-2025 Movatter.jp