You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
4.0 KiB

1 year ago
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\command;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\input\Argument;
  7. use think\console\input\Option;
  8. use think\console\Output;
  9. use think\facade\Db;
  10. use think\facade\Config;
  11. use app\lib\Plugins;
  12. class DecryptFile extends Command
  13. {
  14. protected function configure()
  15. {
  16. $this->setName('decrypt')
  17. ->addArgument('type', Argument::REQUIRED, '文件类型,plugin:插件文件,module:模块文件,classdir:宝塔class目录,all:所有py文件')
  18. ->addArgument('file', Argument::REQUIRED, '文件路径')
  19. ->addArgument('os', Argument::OPTIONAL, '操作系统:Windows/Linux')
  20. ->setDescription('解密宝塔面板python文件');
  21. }
  22. protected function execute(Input $input, Output $output)
  23. {
  24. $type = trim($input->getArgument('type'));
  25. $file = trim($input->getArgument('file'));
  26. if(!file_exists($file)){
  27. $output->writeln('文件不存在');
  28. return;
  29. }
  30. if($type == 'plugin'){
  31. $os = trim($input->getArgument('os'));
  32. try{
  33. if(Plugins::decode_plugin_main_local($file, $os)){
  34. $output->writeln('文件解密成功!');
  35. }else{
  36. $output->writeln('文件解密失败!');
  37. }
  38. }catch(\Exception $e){
  39. $output->writeln($e->getMessage());
  40. }
  41. }elseif($type == 'module'){
  42. try{
  43. $res = Plugins::decode_module_file($file);
  44. if($res == 2){
  45. $output->writeln('文件解密失败!');
  46. }elseif($res == 1){
  47. $output->writeln('文件解密成功!');
  48. }
  49. }catch(\Exception $e){
  50. $output->writeln($e->getMessage());
  51. }
  52. }elseif($type == 'classdir'){
  53. $file = rtrim($file, '/');
  54. if(!file_exists($file.'/common.py')){
  55. $output->writeln('当前路径非宝塔面板class目录');
  56. return;
  57. }
  58. $dirs = glob($file.'/*Model');
  59. foreach($dirs as $dir){
  60. if(!is_dir($dir))continue;
  61. $files = glob($dir.'/*Model.py');
  62. foreach($files as $file){
  63. try{
  64. $res = Plugins::decode_module_file($file);
  65. if($res == 2){
  66. $output->writeln('文件解密失败:'.$file);
  67. }elseif($res == 1){
  68. $output->writeln('文件解密成功:'.$file);
  69. }
  70. }catch(\Exception $e){
  71. $output->writeln($e->getMessage().':'.$file);
  72. }
  73. }
  74. }
  75. }elseif($type == 'all'){
  76. $file = rtrim($file, '/');
  77. $this->scan_all_file($input, $output, $file);
  78. }else{
  79. $output->writeln('未知文件类型');
  80. }
  81. }
  82. private function scan_all_file(Input $input, Output $output, $path) {
  83. $dir = opendir($path);
  84. while(false !== ( $file = readdir($dir)) ) {
  85. if (( $file != '.' ) && ( $file != '..' )) {
  86. $filepath = $path . '/' . $file;
  87. if ( is_dir($filepath) ) {
  88. $this->scan_all_file($input, $output, $filepath);
  89. }
  90. elseif(substr($filepath, -3) == '.py') {
  91. try{
  92. $res = Plugins::decode_module_file($filepath);
  93. if($res == 2){
  94. $output->writeln('文件解密失败:'.$filepath);
  95. }elseif($res == 1){
  96. $output->writeln('文件解密成功:'.$filepath);
  97. }
  98. }catch(\Exception $e){
  99. $output->writeln($e->getMessage().':'.$filepath);
  100. }
  101. }
  102. }
  103. }
  104. closedir($dir);
  105. }
  106. }