Initial commit

This commit is contained in:
crazywhalecc
2022-04-17 02:26:11 +08:00
commit fe8d9ed81b
14 changed files with 1676 additions and 0 deletions

View 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();
}
}

View 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;
}
}

View File

@@ -0,0 +1,6 @@
<?php
function cmd($cmd): string
{
return $cmd . PHP_EOL;
}