完成变量名混淆打包

This commit is contained in:
2022-11-26 17:17:24 +08:00
parent e67c5bbf23
commit 9c170cf8b1
2 changed files with 82 additions and 38 deletions

View File

@@ -174,11 +174,11 @@ class Dist extends Command
$this->log('编译所有代码以全命名空间路径调用'); $this->log('编译所有代码以全命名空间路径调用');
$this->packClassUseName(); $this->packClassUseName();
$this->log('编译混淆变量名');
$this->packFakeVar();
return; return;
$this->log('编译混淆变量名');
// 根据配置pack_var扫描编译
$this->log('编译标准类库'); $this->log('编译标准类库');
@@ -329,14 +329,13 @@ class Dist extends Command
$pretty_printer = new PrettyPrinterTools(); $pretty_printer = new PrettyPrinterTools();
$target_include = $this->getAllInclude();
$target_exclude = $this->getAllExclude();
foreach ($list_file as $item_file) { foreach ($list_file as $item_file) {
$path = $item_file['path']; $path = $item_file['path'];
$target_include = $this->getAllInclude();
$target_exclude = $this->getAllExclude();
if (!$this->checkPregMatchPhp($target_include, $item_file) || $this->checkPregMatch($target_exclude, $path, false)) { if (!$this->checkPregMatchPhp($target_include, $item_file) || $this->checkPregMatch($target_exclude, $path, false)) {
continue; continue;
@@ -397,20 +396,18 @@ class Dist extends Command
protected function getAllInclude() protected function getAllInclude()
{ {
return array_merge( return array_merge(
Config::get('dist.include_path', []),
Config::get('dist.pack_app.include_path', []), Config::get('dist.pack_app.include_path', []),
Config::get('dist.pack_vars.include_path', []),
Config::get('dist.pack_config.include_path', []), Config::get('dist.pack_config.include_path', []),
Config::get('dist.pack_env.include_path', []),
); );
} }
protected function getAllExclude() protected function getAllExclude()
{ {
return array_merge( return array_merge(
Config::get('dist.exclude_path', []),
Config::get('dist.pack_app.exclude_path', []), Config::get('dist.pack_app.exclude_path', []),
Config::get('dist.pack_vars.exclude_path', []),
Config::get('dist.pack_config.exclude_path', []), Config::get('dist.pack_config.exclude_path', []),
Config::get('dist.pack_env.exclude_path', []),
); );
} }
@@ -423,12 +420,13 @@ class Dist extends Command
$pretty_printer = new PrettyPrinterTools(); $pretty_printer = new PrettyPrinterTools();
$target_include = $this->getAllInclude();
$target_exclude = $this->getAllExclude();
foreach ($list_file as $item_file) { foreach ($list_file as $item_file) {
$path = $item_file['path']; $path = $item_file['path'];
$target_include = $this->getAllInclude();
$target_exclude = $this->getAllExclude();
if (!$this->checkPregMatchPhp($target_include, $item_file) || $this->checkPregMatch($target_exclude, $path, false)) { if (!$this->checkPregMatchPhp($target_include, $item_file) || $this->checkPregMatch($target_exclude, $path, false)) {
continue; continue;
@@ -590,13 +588,13 @@ class Dist extends Command
$pretty_printer = new PrettyPrinterTools(); $pretty_printer = new PrettyPrinterTools();
$target_include = $this->getAllInclude();
$target_exclude = $this->getAllExclude();
foreach ($list_files as $item_file) { foreach ($list_files as $item_file) {
$path = $item_file['path']; $path = $item_file['path'];
$target_include = $this->getAllInclude();
$target_exclude = $this->getAllExclude();
if (!$this->checkPregMatchPhp($target_include, $item_file) || $this->checkPregMatch($target_exclude, $path, false)) { if (!$this->checkPregMatchPhp($target_include, $item_file) || $this->checkPregMatch($target_exclude, $path, false)) {
continue; continue;
@@ -627,6 +625,58 @@ class Dist extends Command
$this->tempFilesystem->delete('.env'); $this->tempFilesystem->delete('.env');
} }
} }
public function packFakeVar()
{
$pack_vars_mode = Config::get('dist.pack_vars.pack_vars_mode');
if ($pack_vars_mode == 1) {
return;
}
$list_files = $this->tempFilesystem->listContents('', true);
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$pretty_printer = new PrettyPrinterTools();
$target_include = $this->getAllInclude();
$target_exclude = $this->getAllExclude();
if ($pack_vars_mode == 2) {
$target_exclude = array_merge($target_exclude, Config::get('dist.pack_vars.controller_path', []));
}
foreach ($list_files as $item_file) {
$path = $item_file['path'];
if (!$this->checkPregMatchPhp($target_include, $item_file) || $this->checkPregMatch($target_exclude, $path, false)) {
continue;
}
$this->debug('编译: ' . $path);
$file_content = $this->tempFilesystem->read($path);
$file_stmts = $parser->parse($file_content);
$fake_var_traverser = new NodeTraverser;
$fake_var_traverser->addVisitor(
new NodeFakeVarVisitorTools()
);
$file_stmts = $fake_var_traverser->traverse($file_stmts);
// 生成代码
$result_content = $pretty_printer->prettyPrintFile($file_stmts);
$this->tempFilesystem->put($path, $result_content);
}
}

View File

@@ -11,6 +11,15 @@ return [
'/^runtime/', '/^runtime/',
], ],
// 基础的加载规则
'include_path' => [
'/^app/',
'/^extend/',
'/^database/',
'/^config/',
'/^route/',
],
'pack_app' => [ 'pack_app' => [
// 应当是标准的thinkphp类库文件比如控制器、模型等 // 应当是标准的thinkphp类库文件比如控制器、模型等
'include_path' => [ 'include_path' => [
@@ -20,18 +29,6 @@ return [
// 应当是thinkphp的其他非类库文件比如配置、中间件配置、自定义加载类、函数库、路由等 // 应当是thinkphp的其他非类库文件比如配置、中间件配置、自定义加载类、函数库、路由等
'exclude_path' => [], 'exclude_path' => [],
], ],
'pack_vars' => [
// 实际上任何代码都可以,但是尽量只编译业务代码
'include_path' => [
"/^app/",
"/^config/",
"/^database/",
"/^extend/",
"/^route/",
],
// 基本不需要排除
'exclude_path' => [],
],
'pack_config' => [ 'pack_config' => [
// 主要是config、middleware等直接return的文件会自动判断是否return。自定义的return的php文件也会编译 // 主要是config、middleware等直接return的文件会自动判断是否return。自定义的return的php文件也会编译
'include_path' => [ 'include_path' => [
@@ -51,17 +48,14 @@ return [
], ],
'pack_env' => [ 'pack_env' => [
// 0:base64方式处理,1:明文打包,3:不要编译env配置 // 0:base64方式处理,1:明文打包,3:不要编译env配置
'pack_env_mode' => 0, 'pack_env_mode' => 0
// 实际上任何代码都可以,但是尽量只编译业务代码 ],
'include_path' => [ 'pack_vars' => [
"/^app/", // 0:全部编译1关闭编译2不编译controller的方法
"/^config/", 'pack_vars_mode' => 0,
"/^database/", 'controller_path' => [
"/^extend/", '/\/controller\//',
"/^route/", ]
],
// 基本不需要排除
'exclude_path' => [],
], ],
// 全局函数库,否则无论是否以/开头,都以项目根目录开头定位,如果有其他的文件,在这里声明 // 全局函数库,否则无论是否以/开头,都以项目根目录开头定位,如果有其他的文件,在这里声明
// 不支持项目以外的位置定义 // 不支持项目以外的位置定义