Skip to content

Commit

Permalink
Added CORS proxy source with startup instraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamikoto-sama committed Jun 16, 2021
1 parent 0c915ab commit 30eedbe
Show file tree
Hide file tree
Showing 10 changed files with 656 additions and 0 deletions.
1 change: 1 addition & 0 deletions CORS-proxy/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node server.js
21 changes: 21 additions & 0 deletions CORS-proxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
1. Установить [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli#download-and-install) и [залогиниться](https://devcenter.heroku.com/articles/heroku-cli#getting-started).
2. В папке с прокси в консноли выполнить:
```
heroku create [*название приложения]
```
*если не указывать, сгенерируется случайно
```
git init
heroku git:remote -a [*название приложения]
```
*сгенерированное будет тут
![img1](img.png)
```
git add .
git commit -m "init"
git push heroku master
```
3. Вставить урл приложения **(без слеша на конце!)** в `src/Constants.ts` в `CORS_PROXY`
![img2](img_1.png)
Binary file added CORS-proxy/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CORS-proxy/img_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
461 changes: 461 additions & 0 deletions CORS-proxy/lib/cors-anywhere.js

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions CORS-proxy/lib/help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
This API enables cross-origin requests to anywhere.

Usage:

/ Shows help
/iscorsneeded This is the only resource on this host which is served without CORS headers.
/<url> Create a request to <url>, and includes CORS headers in the response.

If the protocol is omitted, it defaults to http (https if port 443 is specified).

Cookies are disabled and stripped from requests.

Redirects are automatically followed. For debugging purposes, each followed redirect results
in the addition of a X-CORS-Redirect-n header, where n starts at 1. These headers are not
accessible by the XMLHttpRequest API.
After 5 redirects, redirects are not followed any more. The redirect response is sent back
to the browser, which can choose to follow the redirect (handled automatically by the browser).

The requested URL is available in the X-Request-URL response header.
The final URL, after following all redirects, is available in the X-Final-URL response header.


To prevent the use of the proxy for casual browsing, the API requires either the Origin
or the X-Requested-With header to be set. To avoid unnecessary preflight (OPTIONS) requests,
it's recommended to not manually set these headers in your code.


Demo : https://robwu.nl/cors-anywhere.html
Source code : https://github.com/Rob--W/cors-anywhere/
Documentation : https://github.com/Rob--W/cors-anywhere/#documentation
74 changes: 74 additions & 0 deletions CORS-proxy/lib/rate-limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';
module.exports = function createRateLimitChecker(CORSANYWHERE_RATELIMIT) {
// Configure rate limit. The following format is accepted for CORSANYWHERE_RATELIMIT:
// <max requests per period> <period in minutes> <non-ratelimited hosts>
// where <non-ratelimited hosts> is a space-separated list of strings or regexes (/.../) that
// matches the whole host (ports have to be listed explicitly if applicable).
// <period in minutes> cannot be zero.
//
// Examples:
// - Allow any origin to make one request per 5 minutes:
// 1 5
//
// - Allow example.com to make an unlimited number of requests, and the others 1 per 5 minutes.
// 1 5 example.com
//
// - Allow example.com, or any subdomain to make any number of requests and block the rest:
// 0 1 /(.*\.)?example\.com/
//
// - Allow example.com and www.example.com, and block the rest:
// 0 1 example.com www.example.com
var rateLimitConfig = /^(\d+) (\d+)(?:\s*$|\s+(.+)$)/.exec(CORSANYWHERE_RATELIMIT);
if (!rateLimitConfig) {
// No rate limit by default.
return function checkRateLimit() {};
}
var maxRequestsPerPeriod = parseInt(rateLimitConfig[1]);
var periodInMinutes = parseInt(rateLimitConfig[2]);
var unlimitedPattern = rateLimitConfig[3]; // Will become a RegExp or void.
if (unlimitedPattern) {
var unlimitedPatternParts = [];
unlimitedPattern.trim().split(/\s+/).forEach(function(unlimitedHost, i) {
var startsWithSlash = unlimitedHost.charAt(0) === '/';
var endsWithSlash = unlimitedHost.slice(-1) === '/';
if (startsWithSlash || endsWithSlash) {
if (unlimitedHost.length === 1 || !startsWithSlash || !endsWithSlash) {
throw new Error('Invalid CORSANYWHERE_RATELIMIT. Regex at index ' + i +
' must start and end with a slash ("/").');
}
unlimitedHost = unlimitedHost.slice(1, -1);
// Throws if the pattern is invalid.
new RegExp(unlimitedHost);
} else {
// Just escape RegExp characters even though they cannot appear in a host name.
// The only actual important escape is the dot.
unlimitedHost = unlimitedHost.replace(/[$()*+.?[\\\]^{|}]/g, '\\$&');
}
unlimitedPatternParts.push(unlimitedHost);
});
unlimitedPattern = new RegExp('^(?:' + unlimitedPatternParts.join('|') + ')$', 'i');
}

var accessedHosts = Object.create(null);
setInterval(function() {
accessedHosts = Object.create(null);
}, periodInMinutes * 60000);

var rateLimitMessage = 'The number of requests is limited to ' + maxRequestsPerPeriod +
(periodInMinutes === 1 ? ' per minute' : ' per ' + periodInMinutes + ' minutes') + '. ' +
'Please self-host CORS Anywhere if you need more quota. ' +
'See https://github.com/Rob--W/cors-anywhere#demo-server';

return function checkRateLimit(origin) {
var host = origin.replace(/^[\w\-]+:\/\//i, '');
if (unlimitedPattern && unlimitedPattern.test(host)) {
return;
}
var count = accessedHosts[host] || 0;
++count;
if (count > maxRequestsPerPeriod) {
return rateLimitMessage;
}
accessedHosts[host] = count;
};
};
8 changes: 8 additions & 0 deletions CORS-proxy/lib/regexp-top-level-domain.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions CORS-proxy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "cors-anywhere",
"version": "0.4.3",
"description": "CORS Anywhere is a reverse proxy which adds CORS headers to the proxied request. Request URL is taken from the path",
"license": "MIT",
"author": "Rob Wu <rob@robwu.nl>",
"repository": {
"type": "git",
"url": "https://github.com/Rob--W/cors-anywhere.git"
},
"bugs": {
"url": "https://github.com/Rob--W/cors-anywhere/issues/",
"email": "rob@robwu.nl"
},
"keywords": [
"cors",
"cross-domain",
"http-proxy",
"proxy",
"heroku"
],
"main": "./lib/cors-anywhere.js",
"files": [
"lib/",
"test/",
"Procfile",
"demo.html",
"server.js"
],
"dependencies": {
"http-proxy": "1.11.1",
"proxy-from-env": "0.0.1"
},
"devDependencies": {
"coveralls": "^2.11.6",
"eslint": "^2.2.0",
"istanbul": "^0.4.2",
"lolex": "^1.5.0",
"mocha": "^3.4.2",
"nock": "^8.2.1",
"supertest": "^2.0.1"
},
"scripts": {
"start": "node server.js",
"lint": "eslint .",
"test": "mocha ./test/test*.js --reporter spec",
"test-coverage": "istanbul cover ./node_modules/.bin/_mocha -- test/test.js test/test-ratelimit.js --reporter spec"
},
"engines": {
"node": ">=0.10.0"
}
}
9 changes: 9 additions & 0 deletions CORS-proxy/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const host = process.env.HOST || '0.0.0.0';
const port = process.env.PORT || 81;

const cors_proxy = require('./lib/cors-anywhere');
cors_proxy.createServer({
redirectSameOrigin: true,
}).listen(port, host, function () {
console.log('Running CORS Anywhere on ' + host + ':' + port);
});

0 comments on commit 30eedbe

Please sign in to comment.