Repository navigation
Expand file tree
/
Copy pathCheckFirstConnection.php
More file actions
204 lines (175 loc) · 8.55 KB
/
Copy pathCheckFirstConnection.php
File metadata and controls
204 lines (175 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace App\Console\Commands;
use App\Models\Order;
use App\Models\User;
use App\Services\XUIService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Modules\MultiServer\Models\Server;
class CheckFirstConnection extends Command
{
protected $signature = 'check:first-connection';
protected $description = 'Check for first connections and set real expiry';
public function handle()
{
$this->info('🔍 Checking first connections...');
// ============================================================
// تنظیمات و سرور یکبار خوانده/کش میشوند تا برای هر کاربر
// یک login جدید به پنل X-UI زده نشود (جلوگیری از بمباران پنل).
// ============================================================
$settings = \DB::table('settings')->pluck('value', 'key');
$isMulti = filter_var($settings->get('enable_multilocation', false), FILTER_VALIDATE_BOOLEAN);
$cachedServer = null;
$cachedXui = null;
$cachedClients = null; // [inboundId => clients[]]
$getServer = function () use ($settings, $isMulti, &$cachedServer) {
if ($cachedServer !== null) {
return $cachedServer ?: null;
}
if (!$isMulti || !class_exists(Server::class)) {
$cachedServer = false;
return null;
}
$forcedId = $settings->get('trial_server_id');
$server = Server::where('id', $forcedId)->where('is_active', true)->first()
?? Server::where('is_active', true)->first();
$cachedServer = $server ?: false;
return $server ?: null;
};
// یک نمونهی مشترک از XUIService با session مشترک (cookie jar)
$getXui = function () use ($getServer, &$cachedXui) {
if ($cachedXui !== null) {
return $cachedXui ?: null;
}
$server = $getServer();
if (!$server) {
$cachedXui = false;
return null;
}
$xui = new XUIService($server->full_host, $server->username, $server->password);
if (!$xui->login()) {
Log::warning('CheckFirstConnection: X-UI login failed; skipping run.');
$cachedXui = false;
return null;
}
$cachedXui = $xui;
return $xui;
};
// کلاینتهای هر inbound یکبار fetch شوند و برای همهی کاربران reuse شوند
$getClients = function (int $inboundId) use ($getXui, &$cachedClients) {
if ($cachedClients === null) {
$cachedClients = [];
}
if (array_key_exists($inboundId, $cachedClients)) {
return $cachedClients[$inboundId];
}
$xui = $getXui();
if (!$xui) {
$cachedClients[$inboundId] = [];
return [];
}
$cachedClients[$inboundId] = $xui->getClients($inboundId);
return $cachedClients[$inboundId];
};
// ============================================================
// ORDERS
// ============================================================
$orders = Order::whereNull('first_connected_at')
->where('status', 'active')
->whereNotNull('panel_username')
->whereNotNull('server_id')
->get();
$this->info("Orders pending first connect: " . $orders->count());
foreach ($orders as $order) {
try {
$server = $order->server;
if (!$server || !$server->is_active) continue;
$xui = new XUIService($server->full_host, $server->username, $server->password);
if (!$xui->login()) continue;
$clients = $xui->getClients($server->inbound_id);
$found = collect($clients)->firstWhere('email', $order->panel_username);
if ($found && ($found['up'] ?? 0) > 0) {
// First connection detected!
$now = now();
$order->update(['first_connected_at' => $now]);
// Calculate real expiry: first_connect + plan duration
$planDurationDays = $order->plan?->duration_days ?? 30;
$realExpiresAt = $now->copy()->addDays($planDurationDays);
// Update XUI client with real expiry
$xui->updateClient($server->inbound_id, $found['id'], [
'email' => $order->panel_username,
'total' => ($order->plan?->volume_gb ?? 0) * 1073741824,
'expiryTime' => $realExpiresAt->timestamp * 1000,
]);
$this->info(" ✅ Order #{$order->id}: first connect at {$now}, expiry set to {$realExpiresAt}");
Log::info("First connection detected for order", [
'order_id' => $order->id,
'user_id' => $order->user_id,
'first_connect' => $now,
'real_expiry' => $realExpiresAt,
]);
}
} catch (\Exception $e) {
$this->error(" ❌ Order #{$order->id}: " . $e->getMessage());
Log::warning("CheckFirstConnection order error", [
'order_id' => $order->id,
'error' => $e->getMessage(),
]);
}
}
// ============================================================
// TRIAL USERS
// فقط کاربرانی بررسی میشوند که اکانتشان واقعاً روی پنل هست.
// کاربرانی که اکانتشان روی پنل وجود ندارد یکبار علامتگذاری
// میشوند تا دیگر هر ۵ دقیقه بررسی نشوند.
// ============================================================
$trialUsers = User::where('trial_accounts_taken', '>', 0)
->whereNull('trial_first_connected_at')
->get();
$this->info("Trial users pending first connect: " . $trialUsers->count());
foreach ($trialUsers as $user) {
try {
$trialUsername = "trial-{$user->id}-{$user->trial_accounts_taken}";
$server = $getServer();
if (!$server) continue;
$clients = $getClients($server->inbound_id);
$found = collect($clients)->firstWhere('email', $trialUsername);
// اکانت روی پنل وجود ندارد → دیگر بررسی نکن
if (!$found) {
$this->line(" ⏭ Trial user #{$user->id}: account not found on panel, skipping.");
continue;
}
if (($found['up'] ?? 0) > 0) {
$now = now();
$user->update(['trial_first_connected_at' => $now]);
// Update XUI with real trial expiry
$durationHours = (int) $settings->get('trial_duration_hours', 24);
$realExpiresAt = $now->copy()->addHours($durationHours);
$xui = $getXui();
if ($xui) {
$xui->updateClient($server->inbound_id, $found['id'], [
'email' => $trialUsername,
'total' => (int) $settings->get('trial_volume_mb', 500) * 1024 * 1024,
'expiryTime' => $realExpiresAt->timestamp * 1000,
]);
}
$this->info(" ✅ Trial user #{$user->id}: first connect at {$now}, expiry set to {$realExpiresAt}");
Log::info("First connection detected for trial", [
'user_id' => $user->id,
'username' => $trialUsername,
'first_connect' => $now,
'real_expiry' => $realExpiresAt,
]);
}
} catch (\Exception $e) {
$this->error(" ❌ Trial user #{$user->id}: " . $e->getMessage());
Log::warning("CheckFirstConnection trial error", [
'user_id' => $user->id,
'error' => $e->getMessage(),
]);
}
}
$this->info('✅ Done.');
return Command::SUCCESS;
}
}