Update: 2026-07-07 04:57:50
This commit is contained in:
@@ -17,6 +17,8 @@ use Workerman\Timer;
|
||||
use Workerman\Http\Client as AsyncHttp;
|
||||
use PHPSocketIO\SocketIO;
|
||||
use Predis\Client as RedisClient;
|
||||
use Firebase\JWT\JWT;
|
||||
use Firebase\JWT\Key;
|
||||
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
@@ -58,7 +60,23 @@ loadEnvironment('/home/location/env/.env');
|
||||
// ============================================================
|
||||
// 🔐 مفاتيح الأمان
|
||||
// ============================================================
|
||||
$INTERNAL_KEY = trim((string) @file_get_contents('/home/location/.internal_socket_key'));
|
||||
function getInternalSocketKey(): string {
|
||||
$key = getenv('INTERNAL_SOCKET_KEY');
|
||||
if ($key) return trim($key);
|
||||
$path = getenv('INTERNAL_SOCKET_KEY_PATH') ?: '/home/location/.internal_socket_key';
|
||||
if (file_exists($path)) return trim((string) @file_get_contents($path));
|
||||
return '';
|
||||
}
|
||||
|
||||
$INTERNAL_KEY = getInternalSocketKey();
|
||||
|
||||
function getJwtSecret(): string {
|
||||
$keyPath = getenv('JWT_SECRET_KEY_PATH');
|
||||
if ($keyPath && file_exists($keyPath)) {
|
||||
return trim(file_get_contents($keyPath));
|
||||
}
|
||||
return getenv('JWT_SECRET_KEY') ?: '';
|
||||
}
|
||||
$redisPass = trim((string) @file_get_contents('/home/location/.reds_pass_key'));
|
||||
|
||||
if (empty($INTERNAL_KEY)) logMsg('❌ CRITICAL: Internal key missing!');
|
||||
@@ -530,9 +548,30 @@ $io->on('connection', function ($socket) use ($INTERNAL_KEY) {
|
||||
$query = $socket->handshake['query'] ?? [];
|
||||
$driverId = $query['driver_id'] ?? null;
|
||||
$platform = $query['platform'] ?? 'android';
|
||||
$token = $query['token'] ?? '';
|
||||
$fcmToken = $query['token'] ?? ''; // FCM token
|
||||
$jwtToken = $query['jwt'] ?? ''; // JWT Token for authentication
|
||||
|
||||
if (!$driverId) {
|
||||
if (!$driverId || empty($jwtToken)) {
|
||||
logMsg("🚫 Connection Rejected: Missing driver_id or jwt token");
|
||||
$socket->disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$secretKey = getJwtSecret();
|
||||
if (empty($secretKey)) {
|
||||
logMsg("⚠️ JWT Secret is not configured on the server!");
|
||||
} else {
|
||||
$decoded = JWT::decode($jwtToken, new Key($secretKey, 'HS256'));
|
||||
// Validate that the token belongs to this driver
|
||||
if ((string)$decoded->sub !== (string)$driverId || $decoded->role !== 'driver') {
|
||||
logMsg("🚫 Connection Rejected: Invalid JWT for driver_id=$driverId");
|
||||
$socket->disconnect();
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
logMsg("🚫 Connection Rejected: JWT Verification failed -> " . $e->getMessage());
|
||||
$socket->disconnect();
|
||||
return;
|
||||
}
|
||||
@@ -541,7 +580,7 @@ $io->on('connection', function ($socket) use ($INTERNAL_KEY) {
|
||||
$connectedDrivers[$driverId] = [
|
||||
'conn' => $socket,
|
||||
'platform' => $platform,
|
||||
'token' => $token,
|
||||
'token' => $fcmToken,
|
||||
];
|
||||
|
||||
if (!isset($driverState[$driverId])) {
|
||||
|
||||
Reference in New Issue
Block a user