no message

This commit is contained in:
hellcat 2025-04-14 21:02:26 +08:00
parent 3ab633fbf3
commit d1a6817b19
19 changed files with 294 additions and 202 deletions

View File

@ -67,7 +67,7 @@ final class HookController
$aParams['aOption'] = $aOption;
$aParams['aArg'] = $aArg;
$sClassPath = "App\\Services\\TG\\Hook\\".$sClass;
$sClassPath = "App\\Services\\TG\\Hook\\Mod\\".$sClass;
if (method_exists($sClassPath, $sFunc)) {

View File

@ -4,8 +4,7 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Article;
//use App\Services\TomTool\Http;
//use voku\helper\AntiXSS;
use App\Services\TG\Http as TGHttp;
class CronController
{
@ -13,37 +12,61 @@ class CronController
private $sUserName;
private $sProjectName;
private $sProjectPath;
private $sProjectUrl;
// public function __construct()
// {
// $this->sProjectPath = env("BASE_DIR")."github/".$this->sUserName."-".$this->sProjectName;
// $this->sProjectUrl = env("github_url_base").":".$this->sUserName."/".$this->sProjectName.".git";
// }
public function index()
{
$h = date("H");
$i = date("i");
$s = date("s");
//test
$i = 7;
$aConditions = Article::select('cron_condition')
->where('status', 1)
->distinct()
->get()
->toArray();
try {
$h = date("H");
$i = date("i");
$s = date("s");
//test
$i = '03';
// i_in10规则
$iIin10 = $i[1];
$aArticle = Article::where("i_in10", $iIin10)->where("status", 1)->get()->toArray();
$this->run($aArticle);
$aConditions = Article::select('cron_condition')
->where('status', 1)
->distinct()
->get()
->toArray();
foreach ($aConditions as $row) {
$sCondition = $row['cron_condition'];
if (eval("return $sCondition;")) {
foreach ($aConditions as $row) {
$aArticle = Article::where("cron_condition", $sCondition)->where("status", 1)->get()->toArray();
$this->run($aArticle);
$sCondition = $row['cron_condition'];
if (!empty($sCondition) && eval("return $sCondition;")) {
$aArticle = Article::where("cron_condition", $sCondition)->where("status", 1)->get()->toArray();
$this->run($aArticle);
}
}
} catch (\Throwable $e) {
$sMessage = "错误: " . $e->getMessage();
$oTGHttp = new TGHttp();
$oTGHttp->sendToTG($sMessage);
}
}
private function run($aArticle)
@ -51,68 +74,130 @@ class CronController
foreach ($aArticle as $row) {
$this->sUserName = $row["user_name"];
$this->sProjectName = $row["project_name"];
$this->sProjectPath = env("BASE_DIR")."github/".$this->sUserName."-".$this->sProjectName;
$this->sProjectUrl = env("github_url_base").":".$this->sUserName."/".$this->sProjectName.".git";
list($sClass, $sFunc) = explode('#', $row['mod']);
$sClass = "App\\Services\\Github\\File\\".$sClass;
$sClass = "App\\Services\\Github\\Mod\\".$sClass;
if (method_exists($sClass, $sFunc)) {
$oInstans = new $sClass();
$sReadMe = $oInstans->$sFunc($row);
// var_dump($sReadMe);
} else {
// 处理方法不存在的情况(可选)
// $this->tgMessage("Method $sClass does not exist.");
}
// 目录
$sBaseDir = env("BASE_DIR");
$sUserProjectDir = $sBaseDir."github/".$this->sUserName."-".$this->sProjectName;
$sGitShDir = $sBaseDir."github";
// 没有文件夹,创建一个
if (!is_dir($sUserProjectDir)) {
mkdir($sUserProjectDir, 0755, true); // 0755 是权限true 表示递归创建目录
if (!is_dir($this->sProjectPath)) {
mkdir($this->sProjectPath, 0755, true); // 0755 是权限true 表示递归创建目录
}
// 没有仓库,创建一个
if (!is_dir($sUserProjectDir . "/.git")) {
if (!is_dir($this->sProjectPath . "/.git")) {
$sGithubProjectUrl = env("github_url_base").":".$this->sUserName."/".$this->sProjectName.".git";
$this->gitInit();
$output = shell_exec("sh " . $sGitShDir . "/init.sh " . escapeshellarg($sUserProjectDir) . " " . escapeshellarg($sGithubProjectUrl) . " " . escapeshellarg($this->sUserName));
echo "Output from init.sh:\n$output\n";
$output = shell_exec("git config --global --add safe.directory $sUserProjectDir 2>&1");
//
// echo "<br>Output from --add safa.directory:\n$output\n";
}
// 写入内容
file_put_contents($sUserProjectDir."/README.md", $this->formatstr($sReadMe));
file_put_contents($sUserProjectDir."/version.text", time());
file_put_contents($this->sProjectPath."/README.md", $this->formatstr($sReadMe));
file_put_contents($this->sProjectPath."/version.text", time());
// 提交
$output = shell_exec("sh ".$sGitShDir."/push.sh " . escapeshellarg($sUserProjectDir));
echo "Output from push.sh:\n$output\n";
$this->gitPush();
}
}
private function tgMessage($sMsg)
{
if (env("is_loc") == true) {
echo $sMsg;
} else {
echo 11;
}
}
private function formatstr($str)
{
$str = substr($str, 1, -1);
return $str;
}
private function runCommand($command) {
$output = [];
$returnVar = 0;
exec($command, $output, $returnVar);
if ($returnVar !== 0) {
// 将输出数组转换为 JSON 格式
$jsonOutput = json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
$errorMessage = "命令失败: $command\n" . $jsonOutput . "\n";
throw new \Exception($errorMessage);
}
}
private function gitInit()
{
// 进入指定目录
if (!chdir($this->sProjectPath)) {
throw new \Exception("无法进入目录 {$this->sProjectPath}\n");
}
// 初始化 Git 仓库
$this->runCommand('git init');
// 添加安全目录
$this->runCommand("git config --local --add safe.directory \"{$this->sProjectPath}\"");
// 设置用户
$this->runCommand("git config --local user.name \"{$this->sUserName}\"");
$this->runCommand("git config --local user.email \"{$this->sUserName}@tuta.io\"");
// 创建并切换到 main 分支
$this->runCommand('git branch -M main');
// 添加远程仓库
$this->runCommand("git remote add origin \"{$this->sProjectUrl}\"");
echo "操作完成:以初始化仓库。\n";
}
// private function gitPullR()
// {
// // 进入指定目录
// if (!chdir($this->sProjectPath)) {
// echo "无法进入目录 {$this->sProjectPath}\n";
// exit(1);
// }
//
// // 获取远程更新
// $this->runCommand('git fetch origin');
//
// // 硬重置到远程主分支
// $this->runCommand('git reset --hard origin/main');
//
// echo "操作完成:已重置到远程主分支。\n";
// }
private function gitPush()
{
// 进入指定目录
if (!chdir($this->sProjectPath)) {
echo "无法进入目录 {$this->sProjectPath}\n";
exit(1);
}
// 添加更改到暂存区
$this->runCommand('git add .');
// 提交更改,使用当前日期作为提交信息
$commitMessage = date('Y-m-d H:i:s'); // 使用当前日期和时间作为提交信息
$this->runCommand("git commit -m \"$commitMessage\"");
// 推送到远程主分支
$this->runCommand('git push -u origin main'); // 确保将 'main' 替换为你的默认分支名称
echo "操作完成:已提交并推送更改。\n";
}
}

View File

@ -24,7 +24,7 @@ class TestsController
//test
// $s = "\/AticleSet__1115zz__sale_rate 1118.118";
$s = "\/sdjkf";
$s = "\/git#pullR 1";
$oHttp = new Http();
$oHttp->sMethod = "post";

View File

@ -1,5 +1,5 @@
<?php
namespace App\Services\Github\File;
namespace App\Services\Github\Mod;
use App\Services\TomTool\Http;
class FreeNode {
@ -107,9 +107,6 @@ $sContent = "
# 公益免费v2ray节点订阅 每日更新 更新时间 ".date('Y-m-d H:i:s')."
## 节点导入方法
CTRL+A网页全选CTRL+C复制右键点击任务栏v2rayN客户端图标左键点击从剪贴板批量导入URL即可一键导入所有v2ray节点
## 性价比机场推荐:[福云](https://fuuu.cloud)
* 价格低 - 目前七折优惠折后低至8元。

View File

@ -1,29 +0,0 @@
<?php
namespace App\Services\TG\Hook;
use App\Models\Article as ModelArticle;
class Cmd extends Base {
public function List()
{
$aCmdList = [
'优惠码list' => '/SaleCode#List',
'优惠码find' => '/SaleCode#Find xxhh',
'优惠码rate' => '/SaleCode#SetRate__xxzz 6.7',
'文章list' => '/Article#List',
'文章like' => '/Article#Like__field like',
'文章add' => '/Article#Add__name',
'文章del' => '/Article#Del__id',
'文章set' => '/Article#Set__id__fild set',
'添加github的sshhost' => "System#AddSshHostGithub",
];
$this->oTGHttp->sendToTG(json_encode($aCmdList, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
$this->oHttp::response(200, 'ok');
}
}

View File

@ -1,52 +0,0 @@
<?php
namespace App\Services\TG\Hook;
use App\Models\Article as ModelArticle;
class Git extends Base {
public function AddSshHostGithub()
{
// 定义要添加的主机
$host = 'github.com';
// 执行 ssh-keyscan 命令
$key = shell_exec("ssh-keyscan -t ed25519 $host");
// 检查是否成功获取到公钥
if ($key) {
// 获取 known_hosts 文件的路径
$knownHostsFile = '/root/.ssh/known_hosts';
// 检查 known_hosts 文件中是否已经存在该主机的公钥
if (strpos(file_get_contents($knownHostsFile), $host) === false) {
// 将公钥写入 known_hosts 文件
file_put_contents($knownHostsFile, $key, FILE_APPEND);
$sMsg = "公钥已成功添加到 $knownHostsFile\n";
} else {
$sMsg = "公钥已存在于 $knownHostsFile\n";
}
} else {
$sMsg = "无法获取 $host 的公钥\n";
}
$this->oTGHttp->sendToTG($sMsg);
$this->oHttp::response(200, 'ok');
}
// /Git#PullR 3
public function PullR($aParam)
{
$iId = $aParam['aArg'][1];
$aArticle = ModelArticle::find($iId);
tomd($aArticle, 1);
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace App\Services\TG\Hook;
namespace App\Services\TG\Hook\Mod;
use App\Models\Article as ModelArticle;
class Article extends Base {

View File

@ -1,6 +1,6 @@
<?php
namespace App\Services\TG\Hook;
namespace App\Services\TG\Hook\Mod;
//use App\Services\TomTool\Http;
//use App\Models\Article as ModelArticle;
use App\Services\TomTool\Http;

View File

@ -0,0 +1,31 @@
<?php
namespace App\Services\TG\Hook\Mod;
use App\Models\Article as ModelArticle;
class Cmd extends Base {
public function List()
{
$aCmdList = [
'优惠码list' => 'SaleCode#List',
'优惠码find' => 'SaleCode#Find xxhh',
'优惠码rate' => 'SaleCode#SetRate__xxzz 6.7',
'文章list' => 'Article#List',
'文章like' => 'Article#Like__field like',
'文章add' => 'Article#Add__name',
'文章del' => 'Article#Del__id',
'文章set' => 'Article#Set__id__fild set',
// '添加github的sshhost' => "System#AddSshHostGithub",
'文章clone - 没有' => "article#clone id",
'手动pullR - 没有' => "article#clone id",
];
$this->oTGHttp->sendToTG(json_encode($aCmdList, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
$this->oHttp::response(200, 'ok');
}
}

View File

@ -0,0 +1,110 @@
<?php
namespace App\Services\TG\Hook\Mod;
use App\Models\Article as ModelArticle;
class Git extends Base {
public function AddSshHostGithub()
{
// 定义要添加的主机
$host = 'github.com';
// 执行 ssh-keyscan 命令
$key = shell_exec("ssh-keyscan -t ed25519 $host");
// 检查是否成功获取到公钥
if ($key) {
// 获取 known_hosts 文件的路径
$knownHostsFile = '/root/.ssh/known_hosts';
// 检查 known_hosts 文件中是否已经存在该主机的公钥
if (strpos(file_get_contents($knownHostsFile), $host) === false) {
// 将公钥写入 known_hosts 文件
file_put_contents($knownHostsFile, $key, FILE_APPEND);
$sMsg = "公钥已成功添加到 $knownHostsFile\n";
} else {
$sMsg = "公钥已存在于 $knownHostsFile\n";
}
} else {
$sMsg = "无法获取 $host 的公钥\n";
}
$this->oTGHttp->sendToTG($sMsg);
$this->oHttp::response(200, 'ok');
}
public function Clone($aParam)
{
$iId = $aParam['aArg'][1];
// 查找文章
$aArticle = ModelArticle::find($iId);
if ($aArticle) {
// 复制文章
$newArticle = $aArticle->replicate();
// 如果需要,可以修改新文章的某些字段,例如标题或状态
// $newArticle->title = $newArticle->title . ' - Copy'; // 修改标题
// $newArticle->status = 'draft'; // 例如,将状态设置为草稿
// 保存新文章
$newArticle->save();
// 发送新文章的 ID 到 Telegram
$sMsg = "新文章已创建ID: " . $newArticle->id;
$this->oTGHttp->sendToTG($sMsg);
// 返回 HTTP 响应
$this->oHttp::response(200, 'ok');
} else {
throw new \Exception("没有这个id");
}
}
// /Git#PullR 3
public function PullR($aParam)
{
$iId = $aParam['aArg'][1];
$aArticle = ModelArticle::find($iId);
$sProjectPath = env("BASE_DIR")."github/".$aArticle->user_name."-".$aArticle->project_name;
// 进入指定目录
if (!chdir($sProjectPath)) {
echo "无法进入目录 {$sProjectPath}\n";
exit(1);
}
// 获取远程更新
$this->runCommand('git fetch origin');
// 硬重置到远程主分支
$this->runCommand('git reset --hard origin/main');
echo "操作完成:已重置到远程主分支。\n";
}
private function runCommand($command) {
$output = [];
$returnVar = 0;
exec($command, $output, $returnVar);
if ($returnVar !== 0) {
// 将输出数组转换为 JSON 格式
$jsonOutput = json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
$errorMessage = "命令失败: $command\n" . $jsonOutput . "\n";
throw new \Exception($errorMessage);
}
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace App\Services\TG\Hook;
namespace App\Services\TG\Hook\Mod;
use App\Models\Article as ModelArticle;
class SaleCode extends Base {

View File

@ -7,6 +7,12 @@ class Http {
public $sApiToken;
public $sApiChatId;
function __construct()
{
$this->sApiToken = env('telegram_token');
$this->sApiChatId = env('telegram_chatid');
}
public function sendToTG($sMsg)
{

View File

@ -1,35 +0,0 @@
#!/bin/bash
# 检查是否提供了目录和远程仓库地址参数
if [ -z "$1" ] || [ -z "$2" ]; then
echo "请提供要操作的目录和远程仓库地址."
exit 1
fi
# 进入指定目录
cd "$1" || { echo "无法进入目录 $1"; exit 1; }
# 初始化 Git 仓库
git init
# 添加安全目录
git config --local --add safe.directory "$1"
# 设置用户
git config --local user.name "$3"
git config --local user.email "$3"."@tuta.io"
# 添加文件到暂存区
git add .
# 提交第一次更改
git commit -m "first commit"
# 创建并切换到 main 分支
git branch -M main
# 添加远程仓库
git remote add origin "$2"
# 推送到远程仓库
# git push -u origin main

View File

@ -1,7 +0,0 @@
#!/bin/bash
cd $1
git fetch origin
git reset --hard origin/main

View File

@ -1,10 +0,0 @@
#!/bin/bash
cd $1
git add .
git commit -m "$(date)"
git push origin main # 确保将 'main' 替换为你的默认分支名称

@ -1 +0,0 @@
Subproject commit 689aab7911bc0911dc5491ff51167f1016d550ad

@ -1 +0,0 @@
Subproject commit fad2d8f73aea5dbb8783f265b426971af2aafad5

@ -1 +0,0 @@
Subproject commit f76ea021e2dab961b8e727e208dbde6a4ef7876a

@ -1 +0,0 @@
Subproject commit c10878f28055ed1dfd1863870829015c3f9be576