fix:更新已知bug,优化代码

This commit is contained in:
Ying
2022-11-28 19:11:12 +08:00
parent f6aee95cfc
commit 9445b206a2
1378 changed files with 53759 additions and 20789 deletions

View File

@@ -38,7 +38,7 @@ return [
'dispatch_success' => app_path() . '/admin/view/public/jumptpl.html',
'exception_tpl' => app_path() . '/admin/view/error/500.html',
'error_message' => '页面错误!请稍后再试~',
'version' => 'v1.2.0',
'version' => 'v1.1.5',
'cors_domain' => ['*', '127.0.0.1'],
'api_url' => 'https://api.swiftadmin.net/',
'show_error_msg' => false,

View File

@@ -0,0 +1,4 @@
<?php
return [
'enable' => true,
];

View File

@@ -0,0 +1,37 @@
<?php
use Webman\GatewayWorker\Gateway;
use Webman\GatewayWorker\BusinessWorker;
use Webman\GatewayWorker\Register;
return [
'gateway' => [
'handler' => Gateway::class,
'listen' => 'websocket://0.0.0.0:7272',
'count' => cpu_count(),
'reloadable' => false,
'constructor' => ['config' => [
'lanIp' => '127.0.0.1',
'startPort' => 2300,
'pingInterval' => 25,
'pingData' => '{"type":"ping"}',
'registerAddress' => '127.0.0.1:1236',
'onConnect' => function(){},
]]
],
'worker' => [
'handler' => BusinessWorker::class,
'count' => cpu_count()*2,
'constructor' => ['config' => [
'eventHandler' => app\common\gateway\Events::class,
'name' => 'ChatBusinessWorker',
'registerAddress' => '127.0.0.1:1236',
]]
],
'register' => [
'handler' => Register::class,
'listen' => 'text://0.0.0.0:1236',
'count' => 1, // Must be 1
'constructor' => []
],
];

View File

@@ -1,10 +0,0 @@
<?php
return [
'enable' => true,
'websocket' => 'websocket://0.0.0.0:3131',
'api' => 'http://0.0.0.0:3232',
'app_key' => '6c07d4502768615a45d8c3193b66eb88',
'app_secret' => 'c395efb291683dc01472deb4e9db4bcd',
'channel_hook' => 'http://127.0.0.1:8787/plugin/webman/push/hook',
'auth' => '/plugin/webman/push/auth'
];

View File

@@ -1,21 +0,0 @@
<?php
use Webman\Push\Server;
return [
'server' => [
'handler' => Server::class,
'listen' => config('plugin.webman.push.app.websocket'),
'count' => 1, // 必须是1
'reloadable' => false, // 执行reload不重启
'constructor' => [
'api_listen' => config('plugin.webman.push.app.api'),
'app_info' => [
config('plugin.webman.push.app.app_key') => [
'channel_hook' => config('plugin.webman.push.app.channel_hook'),
'app_secret' => config('plugin.webman.push.app.app_secret'),
],
]
]
]
];

View File

@@ -1,87 +0,0 @@
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use support\Request;
use Webman\Route;
use Webman\Push\Api;
/**
* 推送js客户端文件
*/
Route::get('/plugin/webman/push/push.js', function (Request $request) {
return response()->file(base_path().'/vendor/webman/push/src/push.js');
});
/**
* 私有频道鉴权这里应该使用session辨别当前用户身份然后确定该用户是否有权限监听channel_name
*/
Route::post(config('plugin.webman.push.app.auth'), function (Request $request) {
$pusher = new Api(str_replace('0.0.0.0', '127.0.0.1', config('plugin.webman.push.app.api')), config('plugin.webman.push.app.app_key'), config('plugin.webman.push.app.app_secret'));
$channel_name = $request->post('channel_name');
$session = $request->session();
// 这里应该通过session和channel_name判断当前用户是否有权限监听channel_name
$has_authority = true;
if ($has_authority) {
return response($pusher->socketAuth($channel_name, $request->post('socket_id')));
} else {
return response('Forbidden', 403);
}
});
/**
* 当频道上线以及下线时触发的回调
* 频道上线:是指某个频道从没有连接在线到有连接在线的事件
* 频道下线:是指某个频道的所有连接都断开触发的事件
*/
Route::post(parse_url(config('plugin.webman.push.app.channel_hook'), PHP_URL_PATH), function (Request $request) {
// 没有x-pusher-signature头视为伪造请求
if (!$webhook_signature = $request->header('x-pusher-signature')) {
return response('401 Not authenticated', 401);
}
$body = $request->rawBody();
// 计算签名,$app_secret 是双方使用的密钥,是保密的,外部无从得知
$expected_signature = hash_hmac('sha256', $body, config('plugin.webman.push.app.app_secret'), false);
// 安全校验如果签名不一致可能是伪造的请求返回401状态码
if ($webhook_signature !== $expected_signature) {
return response('401 Not authenticated', 401);
}
// 这里存储这上线 下线的channel数据
$payload = json_decode($body, true);
$channels_online = $channels_offline = [];
foreach ($payload['events'] as $event) {
if ($event['name'] === 'channel_added') {
$channels_online[] = $event['channel'];
} else if ($event['name'] === 'channel_removed') {
$channels_offline[] = $event['channel'];
}
}
// 业务根据需要处理上下线的channel例如将在线状态写入数据库通知其它channel等
// 上线的所有channel
echo 'online channels: ' . implode(',', $channels_online) . "\n";
// 下线的所有channel
echo 'offline channels: ' . implode(',', $channels_offline) . "\n";
return 'OK';
});

View File

@@ -23,6 +23,7 @@ return [
'monitor_dir' => [
app_path(),
config_path(),
base_path() . '/plugin',
base_path() . '/process',
base_path() . '/support',
base_path() . '/resource',

View File

@@ -15,8 +15,8 @@
return [
'default' => [
'host' => '127.0.0.1',
'password' => null,
'password' => getenv('REDIS_PASSWORD'),
'port' => 6379,
'database' => 3,
'database' => 1,
],
];

View File

@@ -10,9 +10,13 @@
// +----------------------------------------------------------------------
use Webman\Route;
use think\facade\Cache;
// 加载自定义路由
// 验证码路由
Route::any('/captcha', [app\BaseController::class, 'captcha']);
/**
* 加载自定义路由 [插件路由]
*/
$defineRoute = require __DIR__ . '/defineRoute.php';
if ($defineRoute && is_array($defineRoute)) {
foreach ($defineRoute as $key => $value) {
@@ -20,73 +24,40 @@ if ($defineRoute && is_array($defineRoute)) {
}
}
/**
* 默认管理员路由
* @var string $manage
*/
Route::any('/manage', function () {
// 登录入口session缓存
request()->session()->set('AdminLogin', ['_security' => request()->buildToken()]);
return redirect('/admin/login');
});
Route::any('/captcha', [app\BaseController::class, 'captcha']);
// 遍历默认应用文件夹
$default_app = config('app.default_app', 'index');
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(root_path('app/' . $default_app), \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
$filePath = str_replace('\\', '/', $file->getPathname());
$fileExt = pathinfo($filePath, PATHINFO_EXTENSION);
// 只处理PHP文件
if (strpos(strtolower($filePath), '/controller/') === false || strtolower($fileExt) !== 'php') {
continue;
}
// 获取路由URL路径
$urlPath = str_replace(['/controller/', '/Controller/'], '/', substr(substr($filePath, strlen(app_path())), 0, -4));
$urlPath = str_replace($default_app . '/', '', $urlPath);
$className = str_replace('/', '\\', substr(substr($filePath, strlen(base_path())), 0, -4));
if (!class_exists($className)) {
continue;
}
$refClass = new \ReflectionClass($className);
$className = $refClass->name;
$methods = $refClass->getMethods(\ReflectionMethod::IS_PUBLIC);
$route = function ($url, $action) {
if (!in_array($url, get_routes()) && !empty($url)) {
$url = strtolower($url);
Route::any($url, $action);
}
};
foreach ($methods as $item) {
$action = $item->name;
$magic = [
'__construct', '__destruct', '__call', '__callStatic', '__get', '__set','__isset', '__unset', '__sleep', '__wakeup', '__toString',
'__invoke', '__set_state', '__clone', '__autoload', '__debugInfo', 'initialize',
];
if (in_array($action,$magic)) {
/**
* 加载插件全局初始化路由
* @var array $pluginRoute
*/
Route::any('/static/system/js/plugin.js', function () {
$plugins = get_plugin_list();
$pluginFiles = [];
foreach ($plugins as $item) {
if (!$item['status']) {
continue;
}
if ($action === 'index') {
if (strtolower(substr($urlPath, -6)) === '/index' && $urlPath === '/Index') {
$route('/', [$className, $action]);
$route(substr($urlPath, 0, -6), [$className, $action]);
}
$route($urlPath, [$className, $action]);
// 是否存在javascript文件
$pluginJs = plugin_path($item['name']) . 'plugin.js';
if (file_exists($pluginJs)) {
$pluginFiles[] = read_file($pluginJs);
}
$route($urlPath . '/' . $action, [$className, $action]);
}
}
return $pluginFiles ? implode(PHP_EOL, $pluginFiles) : '';
});
/**
* 全局404路由fallback
* @var array $request
* @var array $response
*/
Route::fallback(function ($request) {
$pathInfo = parse_url(request()->url());
if (!isset($pathInfo['path'])) {

View File

@@ -18,7 +18,7 @@ use support\view\Blade;
use support\view\ThinkPHP;
return [
'handler' => ThinkPHP::class,
'handler' => \support\Template::class,
'options' => [
'tpl_cache' => true,
'taglib_begin' => '<',