feat: autotransfer
This commit is contained in:
@@ -2,42 +2,58 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use RuntimeException;
|
||||
|
||||
class SettingsService
|
||||
{
|
||||
public const KEYS = [
|
||||
'telegram.bot_token',
|
||||
'telegram.bot_username',
|
||||
'telegram.owner_chat_id',
|
||||
'channels.max_per_agent',
|
||||
'channels.domains',
|
||||
'panel.admin_hosts',
|
||||
'panel.agent_hosts',
|
||||
/**
|
||||
* Admin-editable settings persisted to `.env`.
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
public const ENV_KEYS = [
|
||||
'telegram.bot_username' => 'TELEGRAM_BOT_USERNAME',
|
||||
'telegram.owner_chat_id' => 'TELEGRAM_OWNER_CHAT_ID',
|
||||
'auto_transfer.enabled' => 'AUTO_TRANSFER_ENABLED',
|
||||
'auto_transfer.threshold_usdt' => 'AUTO_TRANSFER_THRESHOLD_USDT',
|
||||
'auto_transfer.threshold_trx' => 'AUTO_TRANSFER_THRESHOLD_TRX',
|
||||
'auto_transfer.threshold_eth' => 'AUTO_TRANSFER_THRESHOLD_ETH',
|
||||
'auto_transfer.threshold_btc' => 'AUTO_TRANSFER_THRESHOLD_BTC',
|
||||
'transfer.fee_address_tron' => 'TRANSFER_FEE_ADDRESS_TRON',
|
||||
'transfer.fee_private_key_tron' => 'TRANSFER_FEE_PRIVATE_KEY_TRON',
|
||||
'transfer.fee_topup_trx' => 'TRANSFER_FEE_TOPUP_TRX',
|
||||
];
|
||||
|
||||
/** @return array<string, string|null> */
|
||||
public function all(): array
|
||||
{
|
||||
$out = [];
|
||||
foreach (self::KEYS as $key) {
|
||||
$out[$key] = $this->get($key);
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
/** Empty form value means keep existing env value. */
|
||||
private const SECRET_KEYS = [
|
||||
'transfer.fee_private_key_tron',
|
||||
];
|
||||
|
||||
public function get(string $key, ?string $default = null): ?string
|
||||
{
|
||||
if (! $this->tableReady()) {
|
||||
$value = match ($key) {
|
||||
'telegram.bot_token' => config('coruna.telegram.bot_token'),
|
||||
'telegram.bot_username' => config('coruna.telegram.bot_username'),
|
||||
'telegram.owner_chat_id' => config('coruna.telegram.owner_chat_id'),
|
||||
'auto_transfer.enabled' => config('coruna.auto_transfer.enabled') ? '1' : '0',
|
||||
'auto_transfer.threshold_usdt' => config('coruna.auto_transfer.threshold_usdt'),
|
||||
'auto_transfer.threshold_trx' => config('coruna.auto_transfer.threshold_trx'),
|
||||
'auto_transfer.threshold_eth' => config('coruna.auto_transfer.threshold_eth'),
|
||||
'auto_transfer.threshold_btc' => config('coruna.auto_transfer.threshold_btc'),
|
||||
'transfer.to_address' => config('coruna.transfer.to_address'),
|
||||
'transfer.to_address_eth' => config('coruna.transfer.to_address_eth'),
|
||||
'transfer.to_address_btc' => config('coruna.transfer.to_address_btc'),
|
||||
'transfer.fee_address_tron' => config('coruna.transfer.fee_address_tron'),
|
||||
'transfer.fee_private_key_tron' => config('coruna.transfer.fee_private_key_tron'),
|
||||
'transfer.fee_topup_trx' => config('coruna.transfer.fee_topup_trx'),
|
||||
default => null,
|
||||
};
|
||||
|
||||
if ($value === null || $value === '') {
|
||||
return $default;
|
||||
}
|
||||
|
||||
$map = $this->cachedMap();
|
||||
|
||||
return array_key_exists($key, $map) ? $map[$key] : $default;
|
||||
return (string) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,82 +61,51 @@ class SettingsService
|
||||
*/
|
||||
public function putMany(array $values): void
|
||||
{
|
||||
$envPairs = [];
|
||||
|
||||
foreach ($values as $key => $value) {
|
||||
if (! in_array($key, self::KEYS, true)) {
|
||||
if (! isset(self::ENV_KEYS[$key])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$str = $value === null ? null : trim((string) $value);
|
||||
Setting::query()->updateOrCreate(
|
||||
['key' => $key],
|
||||
['value' => $str === '' ? null : $str]
|
||||
);
|
||||
if (in_array($key, self::SECRET_KEYS, true) && ($str === null || $str === '')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$str ??= '';
|
||||
$envPairs[self::ENV_KEYS[$key]] = $str;
|
||||
$this->applyRuntimeConfig($key, $str);
|
||||
}
|
||||
|
||||
if ($envPairs !== []) {
|
||||
$this->writeEnv($envPairs);
|
||||
}
|
||||
Cache::forget($this->cacheKey());
|
||||
$this->applyToConfig();
|
||||
}
|
||||
|
||||
/** Kept for boot compatibility; config is loaded from `.env` via config/coruna.php. */
|
||||
public function applyToConfig(): void
|
||||
{
|
||||
if (! $this->tableReady()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$map = $this->cachedMap();
|
||||
|
||||
$bot = $map['telegram.bot_token'] ?? null;
|
||||
if ($bot !== null && $bot !== '') {
|
||||
config([
|
||||
'coruna.telegram.bot_token' => $bot,
|
||||
'nutgram.token' => $bot,
|
||||
]);
|
||||
}
|
||||
$botName = $map['telegram.bot_username'] ?? null;
|
||||
if ($botName !== null) {
|
||||
config(['coruna.telegram.bot_username' => $botName]);
|
||||
}
|
||||
$chat = $map['telegram.owner_chat_id'] ?? null;
|
||||
if ($chat !== null && $chat !== '') {
|
||||
config(['coruna.telegram.owner_chat_id' => $chat]);
|
||||
}
|
||||
|
||||
$max = $map['channels.max_per_agent'] ?? null;
|
||||
if ($max !== null && $max !== '' && is_numeric($max)) {
|
||||
config(['coruna.channels.max_per_agent' => max(1, (int) $max)]);
|
||||
}
|
||||
|
||||
if (array_key_exists('channels.domains', $map) && $map['channels.domains'] !== null) {
|
||||
$domains = self::parseDomains($map['channels.domains']);
|
||||
// Settings override channel hosts only; reporting stays CORUNA_REPORTING_DOMAINS.
|
||||
config([
|
||||
'coruna.channel_domains' => $domains,
|
||||
'coruna.deployment_domains' => $domains,
|
||||
]);
|
||||
}
|
||||
|
||||
// Once saved in settings, DB wins (null/empty = no Host restriction).
|
||||
if (array_key_exists('panel.admin_hosts', $map)) {
|
||||
config(['coruna.panel.admin_hosts' => self::parseDomains($map['panel.admin_hosts'])]);
|
||||
}
|
||||
if (array_key_exists('panel.agent_hosts', $map)) {
|
||||
config(['coruna.panel.agent_hosts' => self::parseDomains($map['panel.agent_hosts'])]);
|
||||
}
|
||||
// no-op: no DB overlay
|
||||
}
|
||||
|
||||
/** Effective values for the settings form (DB overrides env). */
|
||||
/** @return array<string, string> */
|
||||
public function effectiveForForm(): array
|
||||
{
|
||||
$domains = config('coruna.channel_domains', []);
|
||||
$adminHosts = config('coruna.panel.admin_hosts', []);
|
||||
$agentHosts = config('coruna.panel.agent_hosts', []);
|
||||
$feeKeySet = trim((string) config('coruna.transfer.fee_private_key_tron', '')) !== '';
|
||||
|
||||
return [
|
||||
'telegram.bot_token' => (string) (config('coruna.telegram.bot_token') ?: ''),
|
||||
'telegram.bot_username' => (string) (config('coruna.telegram.bot_username') ?: ''),
|
||||
'telegram.owner_chat_id' => (string) (config('coruna.telegram.owner_chat_id') ?: ''),
|
||||
'channels.max_per_agent' => (string) (int) config('coruna.channels.max_per_agent', 5),
|
||||
'channels.domains' => is_array($domains) ? implode("\n", $domains) : '',
|
||||
'panel.admin_hosts' => is_array($adminHosts) ? implode("\n", $adminHosts) : '',
|
||||
'panel.agent_hosts' => is_array($agentHosts) ? implode("\n", $agentHosts) : '',
|
||||
'auto_transfer.enabled' => config('coruna.auto_transfer.enabled') ? '1' : '0',
|
||||
'auto_transfer.threshold_usdt' => (string) (config('coruna.auto_transfer.threshold_usdt') ?? ''),
|
||||
'auto_transfer.threshold_trx' => (string) (config('coruna.auto_transfer.threshold_trx') ?? ''),
|
||||
'auto_transfer.threshold_eth' => (string) (config('coruna.auto_transfer.threshold_eth') ?? ''),
|
||||
'auto_transfer.threshold_btc' => (string) (config('coruna.auto_transfer.threshold_btc') ?? ''),
|
||||
'transfer.fee_address_tron' => (string) config('coruna.transfer.fee_address_tron', ''),
|
||||
'transfer.fee_private_key_tron' => '',
|
||||
'transfer.fee_private_key_tron_set' => $feeKeySet ? '1' : '0',
|
||||
'transfer.fee_topup_trx' => (string) (config('coruna.transfer.fee_topup_trx') ?: '20'),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -136,7 +121,6 @@ class SettingsService
|
||||
$d = trim((string) $d);
|
||||
$d = preg_replace('#^https?://#i', '', $d) ?? $d;
|
||||
$d = rtrim($d, '/');
|
||||
// Host only (drop path / port).
|
||||
$d = explode('/', $d, 2)[0];
|
||||
$d = preg_replace('/:\d+$/', '', $d) ?? $d;
|
||||
|
||||
@@ -144,25 +128,64 @@ class SettingsService
|
||||
}, $parts)));
|
||||
}
|
||||
|
||||
/** @return array<string, string|null> */
|
||||
private function cachedMap(): array
|
||||
private function applyRuntimeConfig(string $key, string $value): void
|
||||
{
|
||||
return Cache::remember($this->cacheKey(), 300, function () {
|
||||
return Setting::query()->pluck('value', 'key')->all();
|
||||
});
|
||||
match ($key) {
|
||||
'telegram.bot_username' => config(['coruna.telegram.bot_username' => $value]),
|
||||
'telegram.owner_chat_id' => config(['coruna.telegram.owner_chat_id' => $value]),
|
||||
'auto_transfer.enabled' => config(['coruna.auto_transfer.enabled' => $value === '1']),
|
||||
'auto_transfer.threshold_usdt' => config(['coruna.auto_transfer.threshold_usdt' => $value]),
|
||||
'auto_transfer.threshold_trx' => config(['coruna.auto_transfer.threshold_trx' => $value]),
|
||||
'auto_transfer.threshold_eth' => config(['coruna.auto_transfer.threshold_eth' => $value]),
|
||||
'auto_transfer.threshold_btc' => config(['coruna.auto_transfer.threshold_btc' => $value]),
|
||||
'transfer.fee_address_tron' => config(['coruna.transfer.fee_address_tron' => $value]),
|
||||
'transfer.fee_private_key_tron' => config(['coruna.transfer.fee_private_key_tron' => $value]),
|
||||
'transfer.fee_topup_trx' => config(['coruna.transfer.fee_topup_trx' => $value !== '' ? $value : '20']),
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
private function cacheKey(): string
|
||||
/**
|
||||
* @param array<string, string> $pairs
|
||||
*/
|
||||
private function writeEnv(array $pairs): void
|
||||
{
|
||||
return 'coruna.settings.map';
|
||||
}
|
||||
$path = base_path('.env');
|
||||
if (! is_file($path)) {
|
||||
throw new RuntimeException('.env not found');
|
||||
}
|
||||
if (! is_writable($path)) {
|
||||
throw new RuntimeException('.env is not writable');
|
||||
}
|
||||
|
||||
private function tableReady(): bool
|
||||
{
|
||||
try {
|
||||
return Schema::hasTable('settings');
|
||||
} catch (\Throwable) {
|
||||
return false;
|
||||
$content = (string) file_get_contents($path);
|
||||
foreach ($pairs as $name => $value) {
|
||||
$line = $name.'='.$this->formatEnvValue($value);
|
||||
if (preg_match('/^'.preg_quote($name, '/').'=/m', $content) === 1) {
|
||||
$content = preg_replace('/^'.preg_quote($name, '/').'=.*$/m', $line, $content, 1) ?? $content;
|
||||
} else {
|
||||
$content = rtrim($content, "\r\n")."\n".$line."\n";
|
||||
}
|
||||
|
||||
putenv($name.'='.$value);
|
||||
$_ENV[$name] = $value;
|
||||
$_SERVER[$name] = $value;
|
||||
}
|
||||
|
||||
if (file_put_contents($path, $content) === false) {
|
||||
throw new RuntimeException('Failed to write .env');
|
||||
}
|
||||
}
|
||||
|
||||
private function formatEnvValue(string $value): string
|
||||
{
|
||||
if ($value === '') {
|
||||
return '';
|
||||
}
|
||||
if (preg_match('/[\s#"\'\\\\]/', $value) === 1) {
|
||||
return '"'.str_replace(['\\', '"'], ['\\\\', '\\"'], $value).'"';
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user