Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the fuctionality for blocking the proxying to spefic urls #451

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lib/cors-anywhere.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

var httpProxy = require('http-proxy');
var net = require('net');
const { parse } = require('path');
var url = require('url');
var regexp_tld = require('./regexp-top-level-domain');
var getProxyForUrl = require('proxy-from-env').getProxyForUrl;
Expand Down Expand Up @@ -253,12 +254,32 @@ function parseURL(req_url) {
return parsed;
}


function checkUrlMatchList(url,list){
var parsed_list=list.map((el)=> parseURL(el))

var complete_url=url.host+url.pathname

for (var i in parsed_list){
var url = parsed_list[i].host +parsed_list[i].pathname
url=url.replace(".","\.")
url=url.replace("*","[^.]*")
var regrex= RegExp("^"+url+"$")
if (regrex.test(complete_url)){
return true
}
}
return false
}

// Request handler factory
function getHandler(options, proxy) {
var corsAnywhere = {
handleInitialRequest: null, // Function that may handle the request instead, by returning a truthy value.
getProxyForUrl: getProxyForUrl, // Function that specifies the proxy to use
maxRedirects: 5, // Maximum number of redirects to be followed.
requestUrlBlacklist: [], // Requests to these url will be blocked.
requestUrlWhitelist: [], // If non-empty, requests not from an url in this list will be blocked.
originBlacklist: [], // Requests from these origins will be blocked.
originWhitelist: [], // If non-empty, requests not from an origin in this list will be blocked.
checkRateLimit: null, // Function that may enforce a rate-limit by returning a non-empty string.
Expand Down Expand Up @@ -358,6 +379,23 @@ function getHandler(options, proxy) {
return;
}

if (corsAnywhere.requestUrlBlacklist.length>0){
if (checkUrlMatchList(location,corsAnywhere.requestUrlBlacklist)){
res.writeHead(403, 'Forbidden', cors_headers);
res.end('The destination "' + req.url.slice(1) + '" was blocked by the operator of this proxy.');
return ;
}
}

if (corsAnywhere.requestUrlWhitelist.length>0){
if (! checkUrlMatchList(location,corsAnywhere.requestUrlWhitelist)){
res.writeHead(403, 'Forbidden', cors_headers);
res.end('The destination "' + req.url.slice(1) + '" was blocked by the operator of this proxy.');
return;
}
}


var origin = req.headers.origin || '';
if (corsAnywhere.originBlacklist.indexOf(origin) >= 0) {
res.writeHead(403, 'Forbidden', cors_headers);
Expand Down
5 changes: 5 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ var port = process.env.PORT || 8080;
// use originWhitelist instead.
var originBlacklist = parseEnvList(process.env.CORSANYWHERE_BLACKLIST);
var originWhitelist = parseEnvList(process.env.CORSANYWHERE_WHITELIST);
var requestUrlBlacklist= parseEnvList(process.env.CORSANYWHERE_URL_BLACKLIST);
var requestUrlWhitelist = parseEnvList(process.env.CORSANYWHERE_URL_WHITELIST);

function parseEnvList(env) {
if (!env) {
return [];
Expand All @@ -23,6 +26,8 @@ var cors_proxy = require('./lib/cors-anywhere');
cors_proxy.createServer({
originBlacklist: originBlacklist,
originWhitelist: originWhitelist,
requestUrlBlacklist:requestUrlBlacklist,
requestUrlWhitelist:requestUrlWhitelist,
requireHeader: ['origin', 'x-requested-with'],
checkRateLimit: checkRateLimit,
removeHeaders: [
Expand Down