This commit is contained in:
flucont
2023-07-21 17:46:50 +08:00
commit aa293b66a9
127 changed files with 14478 additions and 0 deletions

230
app/lib/Btapi.php Normal file
View File

@@ -0,0 +1,230 @@
<?php
namespace app\lib;
use Exception;
class Btapi
{
private $BT_KEY; //接口密钥
private $BT_PANEL; //面板地址
public function __construct($bt_panel, $bt_key){
$this->BT_PANEL = $bt_panel;
$this->BT_KEY = $bt_key;
}
//获取面板配置信息
public function get_config(){
$url = $this->BT_PANEL.'/config?action=get_config';
$p_data = $this->GetKeyData();
$result = $this->curl($url,$p_data);
$data = json_decode($result,true);
return $data;
}
//获取已登录用户信息
public function get_user_info(){
$url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=get_user_info';
$p_data = $this->GetKeyData();
$result = $this->curl($url,$p_data);
$data = json_decode($result,true);
return $data;
}
//从云端获取插件列表
public function get_plugin_list(){
$url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=get_plugin_list';
$p_data = $this->GetKeyData();
$result = $this->curl($url,$p_data);
$data = json_decode($result,true);
return $data;
}
//下载插件包,返回文件路径
public function get_plugin_filename($plugin_name, $version){
$url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=download_plugin';
$p_data = $this->GetKeyData();
$p_data['plugin_name'] = $plugin_name;
$p_data['version'] = $version;
$result = $this->curl($url,$p_data);
$data = json_decode($result,true);
return $data;
}
//下载插件主程序文件,返回文件路径
public function get_plugin_main_filename($plugin_name, $version){
$url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=download_plugin_main';
$p_data = $this->GetKeyData();
$p_data['plugin_name'] = $plugin_name;
$p_data['version'] = $version;
$result = $this->curl($url,$p_data);
$data = json_decode($result,true);
return $data;
}
//解密插件主程序py代码返回文件路径
public function get_decode_plugin_main($plugin_name, $version){
$url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=decode_plugin_main';
$p_data = $this->GetKeyData();
$p_data['plugin_name'] = $plugin_name;
$p_data['version'] = $version;
$result = $this->curl($url,$p_data);
$data = json_decode($result,true);
return $data;
}
//下载插件其他文件,返回文件路径
public function get_plugin_other_filename($fname){
$url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=download_plugin_other';
$p_data = $this->GetKeyData();
$p_data['fname'] = $fname;
$result = $this->curl($url,$p_data);
$data = json_decode($result,true);
return $data;
}
//下载文件
public function download($filename, $localpath){
$url = $this->BT_PANEL.'/download';
$p_data = $this->GetKeyData();
$p_data['filename'] = $filename;
$result = $this->curl_download($url.'?'.http_build_query($p_data), $localpath);
return $result;
}
//获取文件base64
public function get_file($filename){
$url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=get_file';
$p_data = $this->GetKeyData();
$p_data['filename'] = $filename;
$result = $this->curl($url,$p_data);
$data = json_decode($result,true);
return $data;
}
//购买第三方插件
public function create_plugin_other_order($pid){
$url = $this->BT_PANEL.'/auth?action=create_plugin_other_order';
$p_data = $this->GetKeyData();
$p_data['pid'] = $pid;
$p_data['cycle'] = '999';
$p_data['type'] = '0';
$result = $this->curl($url,$p_data);
$data = json_decode($result,true);
return $data;
}
//获取一键部署列表
public function get_deplist(){
$url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=get_deplist';
$p_data = $this->GetKeyData();
$result = $this->curl($url,$p_data);
$data = json_decode($result,true);
return $data;
}
private function GetKeyData(){
$now_time = time();
$p_data = array(
'request_token' => md5($now_time.''.md5($this->BT_KEY)),
'request_time' => $now_time
);
return $p_data;
}
private function curl($url, $data = null, $timeout = 60)
{
//定义cookie保存位置
$cookie_file=app()->getRuntimePath().md5($this->BT_PANEL).'.cookie';
if(!file_exists($cookie_file)){
touch($cookie_file);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if($data){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
private function curl_download($url, $localpath, $timeout = 300)
{
//定义cookie保存位置
$cookie_file=app()->getRuntimePath().md5($this->BT_PANEL).'.cookie';
if(!file_exists($cookie_file)){
touch($cookie_file);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$fp = fopen($localpath, 'w+');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
if (curl_errno($ch)) {
$message = curl_error($ch);
curl_close($ch);
fclose($fp);
throw new Exception('下载文件失败:'.$message);
}
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($httpcode>299){
curl_close($ch);
fclose($fp);
throw new Exception('下载文件失败HTTPCODE-'.$httpcode);
}
curl_close($ch);
fclose($fp);
return true;
}
}

347
app/lib/Plugins.php Normal file
View File

@@ -0,0 +1,347 @@
<?php
namespace app\lib;
use Exception;
use ZipArchive;
class Plugins
{
private static function get_btapi($os){
if($os == 'Windows'){
$bt_url = config_get('wbt_url');
$bt_key = config_get('wbt_key');
}else{
$bt_url = config_get('bt_url');
$bt_key = config_get('bt_key');
}
if(!$bt_url || !$bt_key) throw new Exception('请先配置好宝塔面板接口信息');
$btapi = new Btapi($bt_url, $bt_key);
return $btapi;
}
//刷新插件列表
public static function refresh_plugin_list($os = 'Linux'){
$btapi = self::get_btapi($os);
$result = $btapi->get_plugin_list();
if($result && isset($result['list']) && isset($result['type'])){
if(empty($result['list']) || empty($result['type'])){
throw new Exception('获取插件列表失败:插件列表为空');
}
self::save_plugin_list($result, $os);
}else{
throw new Exception('获取插件列表失败:'.(isset($result['msg'])?$result['msg']:'面板连接失败'));
}
}
//保存插件列表
private static function save_plugin_list($data, $os){
$data['ip'] = '127.0.0.1';
$data['serverid'] = '';
$data['beta'] = 0;
$data['uid'] = 1;
$data['skey'] = '';
$list = [];
foreach($data['list'] as $plugin){
if(isset($plugin['endtime'])) $plugin['endtime'] = 0;
$list[] = $plugin;
}
$data['list'] = $list;
$data['ltd'] = strtotime('+10 year');
$json_file = get_data_dir($os).'config/plugin_list.json';
if(!file_put_contents($json_file, json_encode($data))){
throw new Exception('保存插件列表失败,文件无写入权限');
}
}
//获取插件列表
public static function get_plugin_list($os = 'Linux'){
$json_file = get_data_dir($os).'config/plugin_list.json';
if(file_exists($json_file)){
$data = file_get_contents($json_file);
$json_arr = json_decode($data, true);
if($json_arr){
return $json_arr;
}
}
return false;
}
//获取一个插件信息
public static function get_plugin_info($name, $os = 'Linux'){
$json_arr = self::get_plugin_list($os);
if(!$json_arr) return null;
foreach($json_arr['list'] as $plugin){
if($plugin['name'] == $name){
return $plugin;
}
}
return null;
}
//下载插件(自动判断是否第三方)
public static function download_plugin($plugin_name, $version, $os = 'Linux'){
$plugin_info = Plugins::get_plugin_info($plugin_name, $os);
if(!$plugin_info) throw new Exception('未找到该插件信息');
if($plugin_info['type'] == 10 && isset($plugin_info['versions'][0]['download'])){
if($plugin_info['price'] == 0){
$btapi = self::get_btapi($os);
$btapi->create_plugin_other_order($plugin_info['id']);
}
$fname = $plugin_info['versions'][0]['download'];
$filemd5 = $plugin_info['versions'][0]['md5'];
Plugins::download_plugin_other($fname, $filemd5, $os);
if(isset($plugin_info['min_image']) && strpos($plugin_info['min_image'], 'fname=')){
$fname = substr($plugin_info['min_image'], strpos($plugin_info['min_image'], '?fname=')+7);
Plugins::download_plugin_other($fname, null, $os);
}
}else{
Plugins::download_plugin_package($plugin_name, $version, $os);
}
}
//下载插件包
public static function download_plugin_package($plugin_name, $version, $os = 'Linux'){
$filepath = get_data_dir($os).'plugins/package/'.$plugin_name.'-'.$version.'.zip';
$btapi = self::get_btapi($os);
$result = $btapi->get_plugin_filename($plugin_name, $version);
if($result && isset($result['status'])){
if($result['status'] == true){
$filename = $result['filename'];
self::download_file($btapi, $filename, $filepath);
if(file_exists($filepath)){
$zip = new ZipArchive;
if ($zip->open($filepath) === true)
{
$zip->extractTo(get_data_dir($os).'plugins/folder/'.$plugin_name.'-'.$version);
$zip->close();
$main_filepath = get_data_dir($os).'plugins/folder/'.$plugin_name.'-'.$version.'/'.$plugin_name.'/'.$plugin_name.'_main.py';
if(file_exists($main_filepath) && filesize($main_filepath)>10){
if(!strpos(file_get_contents($main_filepath), 'import ')){ //加密py文件需要解密
self::decode_plugin_main($plugin_name, $version, $main_filepath, $os);
self::noauth_plugin_main($main_filepath);
$zip->open($filepath, ZipArchive::CREATE);
$zip->addFile($main_filepath, $plugin_name.'/'.$plugin_name.'_main.py');
$zip->close();
}
}
}else{
throw new Exception('插件包解压缩失败');
}
return true;
}else{
throw new Exception('下载插件包失败,本地文件不存在');
}
}else{
throw new Exception('下载插件包失败:'.($result['msg']?$result['msg']:'未知错误'));
}
}else{
throw new Exception('下载插件包失败,接口返回错误');
}
}
//下载插件主程序文件
public static function download_plugin_main($plugin_name, $version, $os = 'Linux'){
$filepath = get_data_dir($os).'plugins/main/'.$plugin_name.'-'.$version.'.dat';
$btapi = self::get_btapi($os);
$result = $btapi->get_plugin_main_filename($plugin_name, $version);
if($result && isset($result['status'])){
if($result['status'] == true){
$filename = $result['filename'];
self::download_file($btapi, $filename, $filepath);
if(file_exists($filepath)){
return true;
}else{
throw new Exception('下载插件主程序文件失败,本地文件不存在');
}
}else{
throw new Exception('下载插件主程序文件失败:'.($result['msg']?$result['msg']:'未知错误'));
}
}else{
throw new Exception('下载插件主程序文件失败,接口返回错误');
}
}
//解密并下载插件主程序文件
public static function decode_plugin_main($plugin_name, $version, $main_filepath, $os = 'Linux'){
if(self::decode_plugin_main_local($main_filepath, $os)) return true;
$btapi = self::get_btapi($os);
$result = $btapi->get_decode_plugin_main($plugin_name, $version);
if($result && isset($result['status'])){
if($result['status'] == true){
$filename = $result['filename'];
self::download_file($btapi, $filename, $main_filepath);
return true;
}else{
throw new Exception('解密插件主程序文件失败:'.($result['msg']?$result['msg']:'未知错误'));
}
}else{
throw new Exception('解密插件主程序文件失败,接口返回错误');
}
}
//本地解密插件主程序文件
public static function decode_plugin_main_local($main_filepath, $os = 'Linux'){
$btapi = self::get_btapi($os);
$userinfo = $btapi->get_user_info();
if(isset($userinfo['uid'])){
$src = file_get_contents($main_filepath);
if($src===false)throw new Exception('文件打开失败');
if(!$src || strpos($src, 'import ')!==false)return true;
$uid = $userinfo['uid'];
$serverid = $userinfo['serverid'];
$key = md5(substr($serverid, 10, 16).$uid.$serverid);
$iv = md5($key.$serverid);
$key = substr($key, 8, 16);
$iv = substr($iv, 8, 16);
$data_arr = explode("\n", $src);
$de_text = '';
foreach($data_arr as $data){
$data = trim($data);
if(!empty($data) && strlen($data)!=24){
$tmp = openssl_decrypt($data, 'aes-128-cbc', $key, 0, $iv);
if($tmp) $de_text .= $tmp;
}
}
if(!empty($de_text) && strpos($de_text, 'import ')!==false){
file_put_contents($main_filepath, $de_text);
return true;
}
return false;
}else{
throw new Exception('解密插件主程序文件失败,获取用户信息失败');
}
}
public static function decode_module_file($filepath){
$src = file_get_contents($filepath);
if($src===false)throw new Exception('文件打开失败');
if(!$src || strpos($src, 'import ')!==false)return 0;
$key = 'Z2B87NEAS2BkxTrh';
$iv = 'WwadH66EGWpeeTT6';
$data_arr = explode("\n", $src);
$de_text = '';
foreach($data_arr as $data){
$data = trim($data);
if(!empty($data)){
$tmp = openssl_decrypt($data, 'aes-128-cbc', $key, 0, $iv);
if($tmp) $de_text .= $tmp;
}
}
if(!empty($de_text) && strpos($de_text, 'import ')!==false){
file_put_contents($filepath, $de_text);
return 1;
}
return 2;
}
//去除插件主程序文件授权校验
public static function noauth_plugin_main($main_filepath){
$data = file_get_contents($main_filepath);
if(!$data) return false;
$data = str_replace('\'http://www.bt.cn/api/panel/get_soft_list_test', 'public.GetConfigValue(\'home\')+\'/api/panel/get_soft_list_test', $data);
$data = str_replace('\'https://www.bt.cn/api/panel/get_soft_list_test', 'public.GetConfigValue(\'home\')+\'/api/panel/get_soft_list_test', $data);
$data = str_replace('\'http://www.bt.cn/api/panel/get_soft_list', 'public.GetConfigValue(\'home\')+\'/api/panel/get_soft_list', $data);
$data = str_replace('\'https://www.bt.cn/api/panel/get_soft_list', 'public.GetConfigValue(\'home\')+\'/api/panel/get_soft_list', $data);
$data = str_replace('\'http://www.bt.cn/api/panel/notpro', 'public.GetConfigValue(\'home\')+\'/api/panel/notpro', $data);
$data = str_replace('\'https://www.bt.cn/api/panel/notpro', 'public.GetConfigValue(\'home\')+\'/api/panel/notpro', $data);
$data = str_replace('\'http://www.bt.cn/api/wpanel/get_soft_list_test', 'public.GetConfigValue(\'home\')+\'/api/wpanel/get_soft_list_test', $data);
$data = str_replace('\'https://www.bt.cn/api/wpanel/get_soft_list_test', 'public.GetConfigValue(\'home\')+\'/api/wpanel/get_soft_list_test', $data);
$data = str_replace('\'http://www.bt.cn/api/wpanel/get_soft_list', 'public.GetConfigValue(\'home\')+\'/api/wpanel/get_soft_list', $data);
$data = str_replace('\'https://www.bt.cn/api/wpanel/get_soft_list', 'public.GetConfigValue(\'home\')+\'/api/wpanel/get_soft_list', $data);
$data = str_replace('\'http://www.bt.cn/api/wpanel/notpro', 'public.GetConfigValue(\'home\')+\'/api/wpanel/notpro', $data);
$data = str_replace('\'https://www.bt.cn/api/wpanel/notpro', 'public.GetConfigValue(\'home\')+\'/api/wpanel/notpro', $data);
file_put_contents($main_filepath, $data);
}
//下载插件其他文件
public static function download_plugin_other($fname, $filemd5 = null, $os = 'Linux'){
$filepath = get_data_dir().'plugins/other/'.$fname;
@mkdir(dirname($filepath), 0777, true);
$btapi = self::get_btapi($os);
$result = $btapi->get_plugin_other_filename($fname);
if($result && isset($result['status'])){
if($result['status'] == true){
$filename = $result['filename'];
self::download_file($btapi, $filename, $filepath);
if(file_exists($filepath)){
if($filemd5 && md5_file($filepath) != $filemd5){
$msg = filesize($filepath) < 300 ? file_get_contents($filepath) : '插件文件MD5校验失败';
@unlink($filepath);
throw new Exception($msg);
}
return true;
}else{
throw new Exception('下载插件文件失败,本地文件不存在');
}
}else{
throw new Exception('下载插件文件失败:'.($result['msg']?$result['msg']:'未知错误'));
}
}else{
throw new Exception('下载插件文件失败,接口返回错误');
}
}
//下载文件
private static function download_file($btapi, $filename, $filepath){
try{
$btapi->download($filename, $filepath);
}catch(Exception $e){
@unlink($filepath);
//宝塔bug小文件下载失败改用base64下载
$result = $btapi->get_file($filename);
if($result && isset($result['status']) && $result['status']==true){
$filedata = base64_decode($result['data']);
if(strlen($filedata) < 4096 && substr($filedata,0,1)=='{' && substr($filedata,-1,1)=='}'){
$arr = json_decode($filedata, true);
if($arr){
throw new Exception('获取文件失败:'.($arr['msg']?$arr['msg']:'未知错误'));
}
}
if(!$filedata){
throw new Exception('获取文件失败:文件内容为空');
}
file_put_contents($filepath, $filedata);
}elseif($result){
throw new Exception('获取文件失败:'.($result['msg']?$result['msg']:'未知错误'));
}else{
throw new Exception('获取文件失败:未知错误');
}
}
}
//刷新一键部署列表
public static function refresh_deplist($os = 'Linux'){
$btapi = self::get_btapi($os);
$result = $btapi->get_deplist();
if($result && isset($result['list']) && isset($result['type'])){
if(empty($result['list']) || empty($result['type'])){
throw new Exception('获取一键部署列表失败:一键部署列表为空');
}
$json_file = get_data_dir($os).'config/deployment_list.json';
if(!file_put_contents($json_file, json_encode($result))){
throw new Exception('保存一键部署列表失败,文件无写入权限');
}
}else{
throw new Exception('获取一键部署列表失败:'.(isset($result['msg'])?$result['msg']:'面板连接失败'));
}
}
//获取一键部署列表
public static function get_deplist($os = 'Linux'){
$json_file = get_data_dir($os).'config/deployment_list.json';
if(file_exists($json_file)){
$data = file_get_contents($json_file);
$json_arr = json_decode($data, true);
if($json_arr){
return $json_arr;
}
}
return false;
}
}