Last active
January 5, 2021 23:03
-
-
Save thewhodidthis/97930d5230adba5788a90421c92b2d73 to your computer and use it in GitHub Desktop.
Simple CORS bypass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$url = filter_input(INPUT_GET, 'url', FILTER_SANITIZE_URL) or exit; | |
// This allows for catching warnings eg. when offline, or `$url` resource missing | |
set_error_handler( | |
function ($severity, $message, $file, $line) { | |
if (error_reporting() & $severity) { | |
throw new ErrorException($message, 0, $severity, $file, $line); | |
} | |
} | |
); | |
try { | |
$content = file_get_contents($url, 0); | |
if (false !== $content) { | |
echo $content; | |
} | |
} catch (Exception $e) { | |
http_response_code(500); | |
} | |
// Back to normal | |
restore_error_handler(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'open-uri' | |
require 'cgi' | |
require 'webrick' | |
server = WEBrick::HTTPServer.new( | |
:Port => 9090, | |
) | |
server.mount_proc '/proxy' do |req, res| | |
params = CGI.parse(req.query_string) | |
target = params['url'].first | |
# TODO: Wrap in try block? | |
feed = URI.open(target) | |
res.header["Access-Control-Allow-Origin"] = "*" | |
res.header["Content-Type"] = feed.content_type | |
res.body = feed.read | |
end | |
server.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment