65 lines
7.9 KiB
PHP
65 lines
7.9 KiB
PHP
<?php
|
|
declare(strict_types=1);umask(0077);ini_set('display_errors','0');
|
|
$root='/var/www/wwwroot/fuc-xb-test/current';$dir='/var/www/wwwroot/fuc-xb-test/private/launch-ready-20261105';
|
|
$stateFile=$dir.'/panel-state.json';$manifestFile=$dir.'/node-manifest.json';
|
|
putenv('XB_INSTANCE=fuc-xb-test');$_SERVER['XB_INSTANCE']=$_ENV['XB_INSTANCE']='fuc-xb-test';
|
|
require $root.'/vendor/autoload.php';$app=require $root.'/bootstrap/app.php';$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
|
|
use Illuminate\Support\Facades\{DB,Cache};use App\Models\{User,Plan,Server,ServerMachine,ServerGroup};
|
|
if(DB::connection()->getDatabaseName()!=='fuc_xb_test'||!is_link('/var/www/wwwroot/fuc-xb-test/current'))throw new RuntimeException('Wrong staging instance');
|
|
$lock=fopen($dir.'/panel.lock','c');if(!flock($lock,LOCK_EX|LOCK_NB))throw new RuntimeException('Another FUC preparation is running');
|
|
$action=$argv[1]??'';
|
|
function saveState(array $s):void{global $stateFile;$p=$stateFile.'.tmp';file_put_contents($p,json_encode($s,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR));chmod($p,0600);rename($p,$stateFile);}
|
|
function newQA(int $gid,string $phase):User{return User::create(['email'=>'qa-launch-'.$phase.'[email protected]','password'=>password_hash(bin2hex(random_bytes(20)),PASSWORD_BCRYPT),'uuid'=>(string)Illuminate\Support\Str::uuid(),'token'=>bin2hex(random_bytes(16)),'group_id'=>$gid,'transfer_enable'=>104857600,'expired_at'=>time()+3600,'balance'=>0,'banned'=>0,'u'=>0,'d'=>0,'speed_limit'=>30,'device_limit'=>2,'remind_expire'=>0,'remind_traffic'=>0]);}
|
|
if($action==='prepare'){
|
|
if(is_file($stateFile)||Server::count()||ServerMachine::count())throw new RuntimeException('Existing preparation; inspect before retry');
|
|
$manifest=json_decode(file_get_contents($manifestFile),true,512,JSON_THROW_ON_ERROR);
|
|
if($manifest['owner']!=='fuc-xb-launch-20261105'||count($manifest['nodes'])!==22)throw new RuntimeException('Wrong frozen batch');
|
|
$catalog=json_decode(file_get_contents(__DIR__.'/catalog.json'),true,512,JSON_THROW_ON_ERROR);
|
|
if(count($catalog['products'])!==12||$catalog['reset_traffic_method']!==2)throw new RuntimeException('Wrong catalog');
|
|
$qa=json_decode(file_get_contents('/var/www/wwwroot/fuc-xb-test/private/qa-credentials.json'),true,512,JSON_THROW_ON_ERROR);
|
|
$state=['owner'=>$manifest['owner'],'manifest_sha256'=>$manifest['manifest_sha256'],'before'=>['users'=>User::count(),'wallet'=>(int)User::sum('balance'),'plans'=>Plan::count(),'groups'=>ServerGroup::count()],'nodes'=>[],'plans'=>[],'users'=>[],'phase'=>'preparing'];
|
|
file_put_contents($dir.'/plans-before.json',DB::table('v2_plan')->orderBy('id')->get()->toJson());chmod($dir.'/plans-before.json',0600);
|
|
DB::transaction(function()use(&$state,$catalog,$manifest,$qa){
|
|
$g=new ServerGroup();$g->name='FUC-XB launch restricted QA';$g->save();$state['qa_group_id']=$g->id;
|
|
$sales=new ServerGroup();$sales->name='FUC-XB 正式销售权限';$sales->save();$state['sales_group_id']=$sales->id;
|
|
Plan::whereKey($qa['plan_id'])->update(['show'=>false]);
|
|
foreach($catalog['products']as$i=>$p){$plan=Plan::create(['name'=>$p['name'],'group_id'=>$sales->id,'transfer_enable'=>$p['quota_gb'],'speed_limit'=>null,'device_limit'=>null,'display_device_limit'=>null,'capacity_limit'=>null,'show'=>true,'sell'=>true,'renew'=>true,'prices'=>[$p['period']=>$p['price_yuan']],'sort'=>$i+1,'reset_traffic_method'=>2,'tags'=>['fuc-series:'.$p['series']],'content'=>'']);$state['plans'][$p['code']]=$plan->id;}
|
|
foreach($manifest['nodes']as$i=>$row){
|
|
$machine=ServerMachine::create(['name'=>'fuc-xb-'.$row['key'],'token'=>ServerMachine::generateToken(),'is_active'=>true,'notes'=>$state['owner'].'; isolated QA until cutover']);
|
|
$node=Server::create(['name'=>$row['name'],'type'=>'anytls','host'=>$row['relay_host'],'port'=>(string)$row['relay_port'],'server_port'=>'24445','group_ids'=>[(string)$g->id],'route_ids'=>[],'tags'=>[$state['owner']],'rate'=>$row['rate'],'show'=>true,'enabled'=>true,'machine_id'=>$machine->id,'sort'=>$i+1,'protocol_settings'=>['tls'=>['server_name'=>$row['sni'],'allow_insecure'=>false]],'cert_config'=>['cert_mode'=>'file','domain'=>$row['sni'],'cert_file'=>$row['certificate'],'key_file'=>$row['private_key']]]);
|
|
$state['nodes'][$row['key']]=['node_id'=>$node->id,'machine_id'=>$machine->id,'spec'=>$row];
|
|
}
|
|
$user=newQA($g->id,'banned');$state['users'][]=$user->id;$state['current_user_id']=$user->id;$state['qa_phase']='banned';
|
|
$state['settings_before']=DB::table('v2_settings')->whereIn('name',['server_pull_interval','server_push_interval'])->get()->map(fn($r)=>(array)$r)->all();
|
|
app(App\Support\Setting::class)->save(['server_pull_interval'=>5,'server_push_interval'=>5]);
|
|
$state['phase']='prepared';saveState($state);
|
|
});
|
|
echo json_encode(['prepared'=>true,'sales_plans'=>count($state['plans']),'nodes'=>count($state['nodes']),'old_users_on_new_nodes'=>0,'qa_group_isolated'=>true,'manifest_sha256'=>$state['manifest_sha256']]).PHP_EOL;exit;
|
|
}
|
|
$state=json_decode(file_get_contents($stateFile),true,512,JSON_THROW_ON_ERROR);if($state['owner']!=='fuc-xb-launch-20261105')throw new RuntimeException('Foreign state');
|
|
if($action==='export-machine'){
|
|
$key=$argv[2]??'';$row=$state['nodes'][$key]??null;if(!$row)throw new RuntimeException('Unknown node');$m=ServerMachine::findOrFail($row['machine_id']);
|
|
if($m->name!=='fuc-xb-'.$key)throw new RuntimeException('Machine ownership changed');
|
|
// Dedicated SSH consumer only; never print this action in the tool UI.
|
|
echo json_encode(['owner'=>$state['owner'],'source'=>'https://fuc-xb-test.gemcook.net','machine_id'=>$m->id,'machine_token'=>$m->token,'node'=>$row['spec']]);exit;
|
|
}
|
|
if($action==='status'){
|
|
$nodes=[];foreach($state['nodes']as$key=>$row){$n=Server::findOrFail($row['node_id']);$m=ServerMachine::findOrFail($row['machine_id']);$nodes[$key]=['id'=>$n->id,'group_ids'=>$n->group_ids,'available_users'=>App\Services\ServerService::getAvailableUsers($n)->count(),'upload'=>$n->u,'download'=>$n->d,'last_check_at'=>$n->last_check_at,'last_push_at'=>$n->last_push_at,'machine_last_seen'=>$m->last_seen_at];}
|
|
echo json_encode(['phase'=>$state['phase'],'qa_phase'=>$state['qa_phase'],'nodes'=>$nodes,'users'=>User::count(),'native_orders'=>App\Models\Order::count(),'sales_plans'=>Plan::where('show',true)->where('sell',true)->count(),'automatic_mail_policy'=>App\Services\MailFlow\AutomaticMailPolicy::state()]);exit;
|
|
}
|
|
if($action==='qa-revoke'){
|
|
$u=User::findOrFail($state['current_user_id']);if($u->group_id!==$state['qa_group_id'])throw new RuntimeException('QA identity changed');
|
|
if($state['qa_phase']==='banned')$u->update(['banned'=>1]);elseif($state['qa_phase']==='expired')$u->update(['expired_at'=>time()-1]);elseif($state['qa_phase']==='exhausted')$u->update(['u'=>$u->transfer_enable]);else throw new RuntimeException('Invalid phase');
|
|
echo json_encode(['revoked'=>true,'phase'=>$state['qa_phase']]);exit;
|
|
}
|
|
if($action==='qa-bound'){
|
|
$u=User::findOrFail($state['current_user_id']);if($u->group_id!==$state['qa_group_id']||!str_ends_with($u->email,'@fuc-xb.invalid')||$u->balance!==0)throw new RuntimeException('Not this QA');
|
|
$u->update(['transfer_enable'=>104857600,'expired_at'=>min((int)$u->expired_at,time()+3600)]);echo json_encode(['restricted'=>true,'quota_bytes'=>104857600,'maximum_seconds'=>3600]);exit;
|
|
}
|
|
if($action==='qa-next'){
|
|
$u=User::findOrFail($state['current_user_id']);foreach($state['nodes']as$row)if(App\Services\ServerService::getAvailableUsers(Server::findOrFail($row['node_id']))->count())throw new RuntimeException('Revoke all QA access first');
|
|
$phase=match($state['qa_phase']){'banned'=>'expired','expired'=>'exhausted',default=>throw new RuntimeException('No remaining phase')};
|
|
$u->update(['banned'=>1]);$new=newQA($state['qa_group_id'],$phase);$state['users'][]=$new->id;$state['current_user_id']=$new->id;$state['qa_phase']=$phase;saveState($state);echo json_encode(['new_identity'=>true,'phase'=>$phase]);exit;
|
|
}
|
|
throw new RuntimeException('Unsupported operation');
|