This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Services\ProxyService;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ProxyController extends Controller
|
||||
{
|
||||
protected $proxyService;
|
||||
|
||||
public function __construct(ProxyService $proxyService)
|
||||
{
|
||||
$this->proxyService = $proxyService;
|
||||
}
|
||||
|
||||
public function home()
|
||||
{
|
||||
return view('proxy.home');
|
||||
}
|
||||
|
||||
public function browse(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'url' => 'required'
|
||||
]);
|
||||
|
||||
try {
|
||||
// Decode the URL if it's base64 encoded
|
||||
$url = $request->url;
|
||||
if (base64_encode(base64_decode($url, true)) === $url) {
|
||||
// Decode base64 and then URL decode
|
||||
$url = urldecode(base64_decode($url));
|
||||
}
|
||||
|
||||
// Log the URL processing
|
||||
\Log::info('Processing URL', [
|
||||
'original' => $request->url,
|
||||
'decoded' => $url
|
||||
]);
|
||||
|
||||
// Validate the decoded URL
|
||||
if (!filter_var($url, FILTER_VALIDATE_URL)) {
|
||||
throw new \Exception('Invalid URL format: ' . $url);
|
||||
}
|
||||
|
||||
$content = $this->proxyService->fetchUrl($url);
|
||||
|
||||
// Get content type from response
|
||||
$contentType = $this->proxyService->getLastContentType();
|
||||
|
||||
// Log the content type for debugging
|
||||
\Log::info('Content type received', [
|
||||
'url' => $url,
|
||||
'content_type' => $contentType,
|
||||
'is_css' => strpos($contentType, 'text/css') !== false ||
|
||||
strpos($contentType, 'application/css') !== false ||
|
||||
strpos($url, '.css') !== false
|
||||
]);
|
||||
|
||||
// If it's a CSS file, serve it with the correct content type
|
||||
if (strpos($contentType, 'text/css') !== false ||
|
||||
strpos($contentType, 'application/css') !== false ||
|
||||
strpos($url, '.css') !== false) {
|
||||
|
||||
\Log::info('Serving CSS file', [
|
||||
'url' => $url,
|
||||
'content_type' => $contentType,
|
||||
'content_length' => strlen($content)
|
||||
]);
|
||||
|
||||
return response($content)
|
||||
->header('Content-Type', 'text/css')
|
||||
->header('Cache-Control', 'public, max-age=3600')
|
||||
->header('Access-Control-Allow-Origin', '*')
|
||||
->header('X-Content-Type-Options', 'nosniff');
|
||||
}
|
||||
|
||||
// For HTML content, return the view
|
||||
return view('proxy.browse', [
|
||||
'url' => $url,
|
||||
'content' => $content
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
\Log::error('Proxy error', [
|
||||
'url' => $request->url,
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString()
|
||||
]);
|
||||
return back()->withErrors(['url' => 'Failed to fetch URL: ' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user