mirror of
https://github.com/crazywhalecc/quick-shell.git
synced 2026-07-04 23:35:43 +08:00
Initial commit
This commit is contained in:
78
src/QuickShell/QuickShellController.php
Normal file
78
src/QuickShell/QuickShellController.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace QuickShell;
|
||||
|
||||
use ZM\Annotation\Http\Controller;
|
||||
use ZM\Annotation\Http\RequestMapping;
|
||||
use ZM\Annotation\Swoole\OnRequestEvent;
|
||||
use ZM\Config\ZMConfig;
|
||||
use ZM\Event\EventDispatcher;
|
||||
use ZM\Exception\InterruptException;
|
||||
|
||||
/**
|
||||
* @Controller("/")
|
||||
*/
|
||||
class QuickShellController
|
||||
{
|
||||
/**
|
||||
* @RequestMapping("/manifest")
|
||||
*/
|
||||
public function manifest()
|
||||
{
|
||||
return json_encode(ZMConfig::get('shell_list'), 128|256);
|
||||
}
|
||||
|
||||
/**
|
||||
* @RequestMapping("/")
|
||||
* @RequestMapping("/index")
|
||||
* @RequestMapping("/list")
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$response = implode("\n", QuickShellProvider::getInstance()->getShellList()) . PHP_EOL;
|
||||
$response .= "执行:\tcurl -s http://shell.zhamao.xin/run/{name} | bash" . PHP_EOL;
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @RequestMapping("/test")
|
||||
* @return string
|
||||
*/
|
||||
public function test()
|
||||
{
|
||||
return cmd('bash -c "$(curl -fsSL https://api.zhamao.xin/tools/env.sh)"');
|
||||
}
|
||||
|
||||
/**
|
||||
* @RequestMapping("/run")
|
||||
*/
|
||||
public function runHelp()
|
||||
{
|
||||
return cmd('echo ""');
|
||||
}
|
||||
|
||||
/**
|
||||
* @RequestMapping("/run/{name}")
|
||||
*
|
||||
* @param $param
|
||||
* @return string
|
||||
*/
|
||||
public function run($param): string
|
||||
{
|
||||
$shell = QuickShellProvider::getInstance()->isShellExists($param['name']);
|
||||
if (!$shell) {
|
||||
return cmd("echo 'shell \"".$param['name']."\" not found'");
|
||||
}
|
||||
return cmd(QuickShellProvider::getInstance()->getShellCommand($param['name']));
|
||||
}
|
||||
|
||||
/**
|
||||
* 阻止 Chrome 自动请求 /favicon.ico 导致的多条请求并发和干扰
|
||||
* @OnRequestEvent(rule="ctx()->getRequest()->server['request_uri'] == '/favicon.ico'",level=200)
|
||||
* @throws InterruptException
|
||||
*/
|
||||
public function onRequest()
|
||||
{
|
||||
EventDispatcher::interrupt();
|
||||
}
|
||||
}
|
||||
35
src/QuickShell/QuickShellProvider.php
Normal file
35
src/QuickShell/QuickShellProvider.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace QuickShell;
|
||||
|
||||
use ZM\Config\ZMConfig;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Utils\SingletonTrait;
|
||||
|
||||
class QuickShellProvider
|
||||
{
|
||||
use SingletonTrait;
|
||||
|
||||
public function getShellList(): array
|
||||
{
|
||||
$ls = [];
|
||||
foreach (ZMConfig::get('shell_list') as $shell_name => $shell_class) {
|
||||
$ls[] = Console::setColor($shell_name, 'green') . ":\t" . $shell_class['description'];
|
||||
}
|
||||
return $ls;
|
||||
}
|
||||
|
||||
public function isShellExists($name)
|
||||
{
|
||||
return array_key_exists($name, ZMConfig::get('shell_list'));
|
||||
}
|
||||
|
||||
public function getShellCommand($name)
|
||||
{
|
||||
$d = ZMConfig::get('shell_list')[$name]['command'] ?? null;
|
||||
if ($d === null) {
|
||||
return 'echo "command not found"';
|
||||
}
|
||||
return $d;
|
||||
}
|
||||
}
|
||||
6
src/QuickShell/global_function.php
Normal file
6
src/QuickShell/global_function.php
Normal file
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
function cmd($cmd): string
|
||||
{
|
||||
return $cmd . PHP_EOL;
|
||||
}
|
||||
Reference in New Issue
Block a user