first commit

This commit is contained in:
浪子小新
2020-03-22 12:20:07 +08:00
commit bf9e9a2dfd
85 changed files with 17363 additions and 0 deletions
@@ -0,0 +1,83 @@
<?php
/**
* This file is part of workerman.
*
* 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<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* 用于检测业务代码死循环或者长时间阻塞等问题
* 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
* 然后观察一段时间workerman.log看是否有process_timeout异常
*/
//declare(ticks=1);
use \GatewayWorker\Lib\Gateway;
use Workerman\Connection\AsyncTcpConnection;
/**
* 主逻辑
* 主要是处理 onConnect onMessage onClose 三个方法
* onConnect 和 onClose 如果不需要可以不用实现并删除
*/
class Events
{
/**
* 当客户端连接时触发
* 如果业务不需此回调可以删除onConnect
*
* @param int $client_id 连接id
*/
public static function onConnect($client_id)
{
// 向当前client_id发送数据
// Gateway::sendToClient($client_id, "Hello $client_id\r\n");
// 向所有人发送
// Gateway::sendToAll("$client_id login\r\n");
}
/**
* 当客户端发来消息时触发
* @param int $client_id 连接id
* @param mixed $message 具体消息
*/
public static function onMessage($client_id, $message)
{
try {
$task_connection = new AsyncTcpConnection('text://127.0.0.1:16345');
$task_connection->onConnect = function ($task_connection) use ($message) {
$task_connection->send($message);
};
$task_connection->onMessage = function ($task_connection, $data) use ($client_id) {
if($data == "true") {
$task_connection->close();
}else{
$data && Gateway::sendToClient($client_id, $data . "\r\n");
}
};
$task_connection->connect();
} catch (\Throwable $e) {
}
}
/**
* 当用户断开连接时触发
* @param int $client_id 连接id
*/
public static function onClose($client_id)
{
// 向所有人发送
GateWay::sendToAll("$client_id logout\r\n");
}
}
@@ -0,0 +1,37 @@
<?php
/**
* This file is part of workerman.
*
* 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<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use \Workerman\Worker;
use \Workerman\WebServer;
use \GatewayWorker\Gateway;
use \GatewayWorker\BusinessWorker;
use \Workerman\Autoloader;
// 自动加载类
require_once __DIR__ . '/../../vendor/autoload.php';
// bussinessWorker 进程
$worker = new BusinessWorker();
// worker名称
$worker->name = 'YourAppBusinessWorker';
// bussinessWorker进程数量
$worker->count = 4;
// 服务注册地址
$worker->registerAddress = '127.0.0.1:1238';
// 如果不是在根目录启动,则运行runAll方法
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
@@ -0,0 +1,76 @@
<?php
/**
* This file is part of workerman.
*
* 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<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use \Workerman\Worker;
use \Workerman\WebServer;
use \GatewayWorker\Gateway;
use \GatewayWorker\BusinessWorker;
use \Workerman\Autoloader;
// 自动加载类
require_once __DIR__ . '/../../vendor/autoload.php';
$context = array(
// 更多ssl选项请参考手册 http://php.net/manual/zh/context.ssl.php
'ssl' => array(
// 请使用绝对路径
'local_cert' => '/usr/local/nginx/conf/3598272_g.widora.cn.pem', // 也可以是crt文件
'local_pk' => '/usr/local/nginx/conf/3598272_g.widora.cn.key',
'verify_peer' => false,
// 'allow_self_signed' => true, //如果是自签名证书需要开启此选项
)
);
// gateway 进程,这里使用Text协议,可以用telnet测试
$gateway = new Gateway("websocket://0.0.0.0:8848",$context);
// 开启SSLwebsocket+SSL 即wss
$gateway->transport = 'ssl';
// gateway名称,status方便查看
$gateway->name = 'YourAppGateway';
// gateway进程数
$gateway->count = 4;
// 本机ip,分布式部署时使用内网ip
$gateway->lanIp = '127.0.0.1';
// 内部通讯起始端口,假如$gateway->count=4,起始端口为4000
// 则一般会使用4000 4001 4002 4003 4个端口作为内部通讯端口
$gateway->startPort = 2900;
// 服务注册地址
$gateway->registerAddress = '127.0.0.1:1238';
// 心跳间隔
$gateway->pingInterval = 10;
// 心跳数据
$gateway->pingData = '{"type":"ping"}';
Worker::$daemonize = true;
/*
// 当客户端连接上来时,设置连接的onWebSocketConnect,即在websocket握手时的回调
$gateway->onConnect = function($connection)
{
$connection->onWebSocketConnect = function($connection , $http_header)
{
// 可以在这里判断连接来源是否合法,不合法就关掉连接
// $_SERVER['HTTP_ORIGIN']标识来自哪个站点的页面发起的websocket链接
if($_SERVER['HTTP_ORIGIN'] != 'http://kedou.workerman.net')
{
$connection->close();
}
// onWebSocketConnect 里面$_GET $_SERVER是可用的
// var_dump($_GET, $_SERVER);
};
};
*/
// 如果不是在根目录启动,则运行runAll方法
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
@@ -0,0 +1,28 @@
<?php
/**
* This file is part of workerman.
*
* 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<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use \Workerman\Worker;
use \GatewayWorker\Register;
// 自动加载类
require_once __DIR__ . '/../../vendor/autoload.php';
// register 必须是text协议
$register = new Register('text://0.0.0.0:1238');
// 如果不是在根目录启动,则运行runAll方法
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
@@ -0,0 +1,123 @@
<?php
use \Workerman\Worker;
$task_worker = new Worker('text://127.0.0.1:16345');
// task进程数可以根据需要多开一些
$task_worker->count = 10;
$task_worker->name = 'TaskWorker_github';
$datatime = date("Ymd");
$webDir = dirname(dirname(dirname(dirname(__FILE__)))) . "/staticweb/source/".$datatime;
$task_worker->onMessage = function ($connection, $task_data) use ($webDir,$datatime) {
$dataArr = json_decode($task_data, true);
if(!$dataArr){
return;
}
if(isset($dataArr['type'])){
switch($dataArr['type']){
case "1":
$pdo = new PDO('sqlite:' . dirname(dirname(dirname(__FILE__))) . '/sql');
$pdo->exec("UPDATE statistics SET down_num=down_num+1 where id=1");
$connection->send("true");
$pdo = null;
return true;
break;
case "2":
$pdo = new PDO('sqlite:' . dirname(dirname(dirname(__FILE__))) . '/sql');
$data = $pdo->query("select down_num from statistics where id=1")->fetch();
$down_num = $data[0];
$connection->send(json_encode([
'status' => 0,
'msg' =>"完成",
'data' =>[
'sta' => 4,
'down_num' => $down_num
]
],JSON_UNESCAPED_UNICODE) . "\r\n");
$pdo = null;
$connection->send("true");
return true;
break;
}
}
//&& !preg_match("/^.*(?<=(\.gz|\.tar\.gz|\.bz2|\.tar|\.tar\.bz2|\.zip|\.tar\.xz|\.tar\.z|\.rpm|\.deb|\.rar))$/ix",$dataArr['url'])
if (!preg_match("/^((https|http):\/\/)?([\w\.\/\-\_^]+?)\.([tar\.gz|gz|tar\.bz2|bz2|tar|zip|tar\.xz|tar\.z|rpm|deb|rar]+?)$/iu",$dataArr['url'],$ext) && !preg_match("/^https:\/\/github.com\/([\w\/\-\.\_]+)$/iu", $dataArr['url'])
) {
$connection->send(json_encode(["status" => 1, "msg" => "请输入正确的github地址:仅支持https协议 或<br> gz|tar.gz|bz2|tar|tar.bz2|zip|tar.xz|tar.z|rpm|deb|rar)格式的压缩包下载链接"]) . "\r\n");
$connection->send("true");
return;
}
if (!is_dir($webDir)) {
mkdir($webDir, 0777);
}
if(stristr($dataArr['url'],'github') && !stristr($dataArr['url'],'.zip')) {
//github的检出、打包
$connection->send(json_encode([
'status' => 0,
'msg' => "正在检出代码",
'data' => [
'sta' => 1,
]
], JSON_UNESCAPED_UNICODE) . "\r\n");
$git_dir_path = md5($dataArr['url']);
if (!is_dir($webDir . "/" . $git_dir_path) && !is_file($webDir . "/{$git_dir_path}.tar")) {
system('cd ' . $webDir . ';git clone --depth=50 "' . $dataArr['url'] . '" ' . $git_dir_path);
}
$connection->send(json_encode([
'status' => 0,
'msg' => "正在打包,请稍后",
'data' => [
'sta' => 2,
]
], JSON_UNESCAPED_UNICODE) . "\r\n");
if (!is_file($webDir . "/{$git_dir_path}.tar")) {
system('cd ' . $webDir . ";tar -cvf {$git_dir_path}.tar {$git_dir_path} ");
system("rm -rf " . $webDir . "/" . $git_dir_path);
}
$pdo = new PDO('sqlite:' . dirname(dirname(dirname(__FILE__))) . '/sql');
$pdo->exec("insert into file_info ('path', 'addtime') VALUES ('" . $webDir . "/{$git_dir_path}.tar" . "','" . time() . "')");
$pdo = null;
$connection->send(json_encode([
'status' => 0,
'msg' => "下载压缩包",
'data' => [
'sta' => 3,
'url' => '/source/' . $datatime . "/$git_dir_path.tar",
]
], JSON_UNESCAPED_UNICODE) . "\r\n");
$connection->send("true");
}else{
//普通的压缩包下载处理
$connection->send(json_encode([
'status' => 0,
'msg' => "准备中,请耐心等待~☕️",
'data' => [
'sta' => 2,
]
], JSON_UNESCAPED_UNICODE) . "\r\n");
$fileExt = end($ext);
$fileName = md5($dataArr['url']) . ".{$fileExt}";
system('cd ' . $webDir . '; wget "'.$dataArr['url'] . '" -O '.$fileName);
$pdo = new PDO('sqlite:' . dirname(dirname(dirname(__FILE__))) . '/sql');
$pdo->exec("insert into file_info ('path', 'addtime') VALUES ('" . $webDir . "/{$fileName}" . "','" . time() . "')");
$pdo = null;
$connection->send(json_encode([
'status' => 0,
'msg' => "下载压缩包",
'data' => [
'sta' => 3,
'url' => '/source/' . $datatime . "/{$fileName}",
]
], JSON_UNESCAPED_UNICODE) . "\r\n");
$connection->send("true");
}
};
if (!defined('GLOBAL_START')) {
Worker::runAll();
}