-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebBridge.php
83 lines (65 loc) · 2.72 KB
/
WebBridge.php
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
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
class WebBridge extends Controller
{
private string $ENCRYPTION_KEY;
private int $ADDITIONAL_TIME;
private string $DATETIME_FORMAT;
public function __construct()
{
$this->ENCRYPTION_KEY = "a25ea98b37d4efb661f2cf679e1a9bfd165060a7"; // must match EncryptionKeys::MD5_HASH_KEY in UE5Bridge.cpp
$this->ADDITIONAL_TIME = 123456789; // unix timestamp adder must match EncryptionKeys::TIMESTAMP_ADDER in UE5Bridge.cpp
$this->DATETIME_FORMAT = 'Y.m.d-H.i.s'; // unreal engine time format.
}
public function VerifyRequest(): bool
{
$userAgent = Request()->header('USER_AGENT');
$timeStamp = Request()->header('X-UE5-Timestamp');
$ue5Token = Request()->header('X-UE5-Token');
if (in_array($userAgent, ['UE5-Client', 'UE5-Server', 'UE5-Editor'])) {
// Validate Timestamp and Generate Dynamic Token
$carbonDate = Carbon::createFromFormat($this->DATETIME_FORMAT, $timeStamp);
$carbonTimestamp = $carbonDate->timestamp;
if (!$carbonDate) {
return false;
}
// Add additional timestamp duration to current request timestamp
$dynamicTime = $carbonTimestamp + $this->ADDITIONAL_TIME;
// Convert back to UE5 Timestamp format for comparison
$backToUETimestamp = Carbon::createFromTimestamp($dynamicTime, 'UTC')->format($this->DATETIME_FORMAT);
// Expected encrypted token
$encrypted = $this->encrypt($backToUETimestamp, $this->ENCRYPTION_KEY);
if ($ue5Token !== $encrypted) {
// Hash does not match
return false;
}
// the incoming request can be
// expected to be from the ue5 client.
return true;
}
return false;
}
function encrypt($inputString, $key): string
{
$encryptedData = '';
$keyLength = strlen($key);
for ($i = 0; $i < strlen($inputString); $i++) {
$encryptedByte = ord($inputString[$i]) ^ ord($key[$i % $keyLength]);
$encryptedData .= chr($encryptedByte);
}
// Base64 encode the encrypted data
return base64_encode($encryptedData);
}
function decrypt($encodedString, $key): string
{
$decodedData = base64_decode($encodedString);
$decryptedString = '';
$keyLength = strlen($key);
for ($i = 0; $i < strlen($decodedData); $i++) {
$decryptedByte = ord($decodedData[$i]) ^ ord($key[$i % $keyLength]);
$decryptedString .= chr($decryptedByte);
}
return $decryptedString;
}
}