730
5
刚才只花了十几分钟,几句prompt,就让chatgpt O1生成了一堆基于laravel框架的代码,它说这些代码搞起来就是一个域名监控系统,大佬试试能不能运行起来
#### **AuthController.php**
```php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AuthController extends Controller
{
public function showLogin()
{
return view('auth.login');
}
public function login(Request $request)
{
$credentials = $request->validate([
'username' => 'required',
'password' => 'required',
]);
if (Auth::attempt($credentials)) {
return redirect()->route('domains.index');
}
return back()->withErrors(['message' => 'Invalid credentials']);
}
public function logout()
{
Auth::logout();
return redirect()->route('login');
}
}
```
#### **DomainController.php**
```php
<?php
namespace App\Http\Controllers;
use App\Models\Domain;
use App\Models\WhoisRecord;
use Illuminate\Http\Request;
class DomainController extends Controller
{
public function index()
{
$domains = Domain::where('user_id', auth()->id())->get();
return view('domains.index', compact('domains'));
}
public function create()
{
return view('domains.create');
}
public function store(Request $request)
{
$validated = $request->validate([
'domain' => 'required|url',
'start_time' => 'required|date',
'interval' => 'required|integer|min:1',
'keyword' => 'required|string',
]);
Domain::create([
'user_id' => auth()->id(),
...$validated,
'status' => 'monitoring',
]);
return redirect()->route('domains.index');
}
public function show(Domain $domain)
{
$records = WhoisRecord::where('domain_id', $domain->id)->latest()->get();
return view('domains.show', compact('domain', 'records'));
}
public function update(Request $request, Domain $domain)
{
$validated = $request->validate([
'start_time' => 'date',
'interval' => 'integer|min:1',
'keyword' => 'string',
'status' => 'in:monitoring,paused',
]);
$domain->update($validated);
return back();
}
public function destroy(Domain $domain)
{
$domain->delete();
return redirect()->route('domains.index');
}
}
```
#### **User.php**
```php
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
protected $fillable = ['username', 'password'];
}
```
#### **Domain.php**
```php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Domain extends Model
{
use HasFactory;
protected $fillable = [
'user_id', 'domain', 'start_time', 'interval', 'keyword', 'status'
];
public function records()
{
return $this->hasMany(WhoisRecord::class);
}
}
```
#### **WhoisRecord.php**
```php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class WhoisRecord extends Model
{
use HasFactory;
protected $fillable = ['domain_id', 'data', 'is_deleted'];
}
```
#### **login.blade.php**
```blade
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<form method="POST" action="{{ route('login') }}">
@csrf
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit">Login</button>
</form>
</body>
</html>
```
#### **domains/index.blade.php**
```blade
@extends('layouts.app')
@section('content')
<h1>Monitored Domains</h1>
<a href="{{ route('domains.create') }}">Add Domain</a>
<table>
<tr>
<th>Domain</th>
<th>Start Time</th>
<th>Interval (hours)</th>
<th>Keyword</th>
<th>Status</th>
<th>Actions</th>
</tr>
@foreach ($domains as $domain)
<tr>
<td>{{ $domain->domain }}</td>
<td>{{ $domain->start_time }}</td>
<td>{{ $domain->interval }}</td>
<td>{{ $domain->keyword }}</td>
<td>{{ $domain->status }}</td>
<td>
<a href="{{ route('domains.show', $domain) }}">View</a>
<form action="{{ route('domains.destroy', $domain) }}" method="POST">
@csrf @method('DELETE')
<button type="submit">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
@endsection
```
#### **CheckDomains.php (Command)**
```php
<?php
namespace App\Console\Commands;
use App\Models\Domain;
use App\Notifications\DomainDeleted;
use App\Services\WhoisService;
use Illuminate\Console\Command;
class CheckDomains extends Command
{
protected $signature = 'domains:check';
protected $description = 'Check domain statuses';
public function handle()
{
$domains = Domain::where('status', 'monitoring')
->where('start_time', '<=', now())
->get();
foreach ($domains as $domain) {
$lastCheck = $domain->last_check_time ?? $domain->start_time;
if (now()->diffInHours($lastCheck) >= $domain->interval) {
$this->checkDomain($domain);
}
}
}
private function checkDomain(Domain $domain)
{
$whoisData = (new WhoisService())->query($domain->domain);
$isDeleted = !str_contains($whoisData, $domain->keyword);
$domain->records()->create([
'data' => $whoisData,
'is_deleted' => $isDeleted,
]);
$domain->update(['last_check_time' => now()]);
if ($isDeleted) {
$domain->user->notify(new DomainDeleted($domain));
}
}
}
```
#### **WhoisService.php**
```php
<?php
namespace App\Services;
class WhoisService
{
public function query($domain)
{
$domain = escapeshellarg($domain);
return shell_exec("whois $domain");
}
}
```
#### **DomainDeleted.php (Notification)**
```php
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class DomainDeleted extends Notification
{
use Queueable;
protected $domain;
public function __construct($domain)
{
$this->domain = $domain;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->line("The domain {$this->domain->domain} has been deleted.");
}
}
```
#### **routes/web.php**
```php
<?php
use App\Http\Controllers\AuthController;
use App\Http\Controllers\DomainController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return redirect()->route('domains.index');
})->middleware('auth');
Route::get('login', [AuthController::class, 'showLogin'])->name('login');
Route::post('login', [AuthController::class, 'login']);
Route::post('logout', [AuthController::class, 'logout'])->name('logout');
Route::middleware('auth')->group(function () {
Route::resource('domains', DomainController::class)->except(['edit']);
});
```
#### **Migrations**
**create_domains_table.php**
```php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateDomainsTable extends Migration
{
public function up()
{
Schema::create('domains', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->string('domain');
$table->dateTime('start_time');
$table->integer('interval');
$table->string('keyword');
$table->string('status')->default('monitoring');
$table->dateTime('last_check_time')->nullable();
$table->timestamps();
});
}
}
```
**create_whois_records_table.php**
```php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateWhoisRecordsTable extends Migration
{
public function up()
{
Schema::create('whois_records', function (Blueprint $table) {
$table->id();
$table->foreignId('domain_id')->constrained();
$table->text('data');
$table->boolean('is_deleted');
$table->timestamps();
});
}
}
```
#### **Kernel.php**
```php
protected function schedule(Schedule $schedule)
{
$schedule->command('domains:check')->everyMinute();
}
```
---
### 3. **Implementation Instructions**
1. **Database Setup**:
- Run `php artisan migrate` to create tables.
2. **Create User**:
- Manually insert your credentials into the `users` table:
```php
User::create([
'username' => 'your_username',
'password' => bcrypt('your_password'),
]);
```
3. **Task Scheduling**:
- Add the cron entry `* * * * * cd /path-to-project && php artisan schedule:run >> /dev/null 2>&1`.
4. **Email Configuration**:
- Update `.env` with your SMTP details.
5. **Testing**:
- Log in, add a domain, and verify the cron job checks domains.
这家伙太懒了,什么也没留下。