63 lines
1.8 KiB
PHP
63 lines
1.8 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Support\Facades\Log;
|
||
|
|
|
||
|
|
class PortScannerController extends Controller
|
||
|
|
{
|
||
|
|
public function index()
|
||
|
|
{
|
||
|
|
return view('port-scanner.index');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function scan(Request $request)
|
||
|
|
{
|
||
|
|
$request->validate([
|
||
|
|
'ip' => 'required|ip',
|
||
|
|
'port' => 'required|integer|min:1|max:65535',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$ip = $request->input('ip');
|
||
|
|
$port = $request->input('port');
|
||
|
|
$timeout = 2; // Timeout in seconds
|
||
|
|
|
||
|
|
try {
|
||
|
|
$startTime = microtime(true);
|
||
|
|
$connection = @fsockopen($ip, $port, $errno, $errstr, $timeout);
|
||
|
|
$endTime = microtime(true);
|
||
|
|
$responseTime = round(($endTime - $startTime) * 1000, 2); // Convert to milliseconds
|
||
|
|
|
||
|
|
if ($connection) {
|
||
|
|
fclose($connection);
|
||
|
|
$status = 'open';
|
||
|
|
$message = "Port $port is open on $ip";
|
||
|
|
} else {
|
||
|
|
$status = 'closed';
|
||
|
|
$message = "Port $port is closed on $ip";
|
||
|
|
}
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'status' => $status,
|
||
|
|
'message' => $message,
|
||
|
|
'response_time' => $responseTime,
|
||
|
|
'ip' => $ip,
|
||
|
|
'port' => $port
|
||
|
|
]);
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
Log::error('Port scan error', [
|
||
|
|
'ip' => $ip,
|
||
|
|
'port' => $port,
|
||
|
|
'error' => $e->getMessage()
|
||
|
|
]);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => false,
|
||
|
|
'message' => 'An error occurred while scanning the port',
|
||
|
|
'error' => $e->getMessage()
|
||
|
|
], 500);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|