Skip to content

Commit

Permalink
refactor: 代码重构
Browse files Browse the repository at this point in the history
  • Loading branch information
Ackites committed Jul 31, 2024
1 parent a5053dc commit 78753ef
Show file tree
Hide file tree
Showing 22 changed files with 588 additions and 271 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/gorilla/css v1.0.1
github.com/yosssi/gohtml v0.0.0-20201013000340-ee4748c638f4
golang.org/x/crypto v0.25.0
golang.org/x/net v0.21.0
golang.org/x/net v0.27.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/yosssi/gohtml v0.0.0-20201013000340-ee4748c638f4 h1:0sw0nJM544SpsihWx
github.com/yosssi/gohtml v0.0.0-20201013000340-ee4748c638f4/go.mod h1:+ccdNT0xMY1dtc5XBxumbYfOUhmduiGudqaDgD2rVRE=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
31 changes: 30 additions & 1 deletion internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"path/filepath"
"strings"

"github.com/Ackites/KillWxapkg/internal/restore"
"github.com/Ackites/KillWxapkg/internal/util"

. "github.com/Ackites/KillWxapkg/internal/config"
"github.com/Ackites/KillWxapkg/internal/decrypt"
"github.com/Ackites/KillWxapkg/internal/unpack"
)
Expand Down Expand Up @@ -54,6 +58,14 @@ func DetermineOutputDir(input, appID string) string {
func ProcessFile(inputFile, outputDir, appID string) error {
log.Printf("开始处理文件: %s\n", inputFile)

manager := GetWxapkgManager()

// 初始化 WxapkgInfo
info := &WxapkgInfo{
WxAppId: appID,
IsExtracted: false,
}

// 确定解密后的文件路径
decryptedFilePath := filepath.Join(outputDir, filepath.Base(inputFile))

Expand Down Expand Up @@ -88,17 +100,34 @@ func ProcessFile(inputFile, outputDir, appID string) error {
}
}(tempDir)

err = unpack.UnpackWxapkg(decryptedData, tempDir)
// 包文件列表
var filelist []string

filelist, err = unpack.UnpackWxapkg(decryptedData, tempDir)
if err != nil {
return fmt.Errorf("解包失败: %v", err)
}

// 设置解包状态
info.IsExtracted = true

// 合并解包后的内容到输出目录
err = mergeDirs(tempDir, outputDir)
if err != nil {
return fmt.Errorf("合并目录失败: %v", err)
}

info.WxapkgType = util.GetWxapkgType(filelist)

if restore.IsMainPackage(info) {
info.SourcePath = outputDir
} else if restore.IsSubpackage(info) {
info.SourcePath = filelist[0]
}

// 将包信息添加到管理器中
manager.AddPackage(info.SourcePath, info)

return nil
}

Expand Down
59 changes: 59 additions & 0 deletions internal/config/wxapkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package config

import (
"sync"

"github.com/Ackites/KillWxapkg/internal/enum"
)

// Parser 接口
type Parser interface {
Parse(option WxapkgInfo) error
}

// WxapkgOption 微信小程序解包选项
type WxapkgOption struct {
ViewSource string
AppConfigSource string
ServiceSource string
SetAppConfig bool
}

// WxapkgInfo 保存包的信息
type WxapkgInfo struct {
WxAppId string
WxapkgType enum.WxapkgType
SourcePath string
IsExtracted bool
Option *WxapkgOption
Parsers []Parser // 添加解析器列表
}

// WxapkgManager 管理多个微信小程序包
type WxapkgManager struct {
Packages map[string]*WxapkgInfo
}

var managerInstance *WxapkgManager
var wxapkgOnce sync.Once

// GetWxapkgManager 获取单例的 WxapkgManager 实例
func GetWxapkgManager() *WxapkgManager {
wxapkgOnce.Do(func() {
managerInstance = &WxapkgManager{
Packages: make(map[string]*WxapkgInfo),
}
})
return managerInstance
}

// AddPackage 添加包信息
func (manager *WxapkgManager) AddPackage(id string, info *WxapkgInfo) {
manager.Packages[id] = info
}

// GetPackage 获取包信息
func (manager *WxapkgManager) GetPackage(id string) (*WxapkgInfo, bool) {
info, exists := manager.Packages[id]
return info, exists
}
42 changes: 42 additions & 0 deletions internal/enum/wxapkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package enum

// 定义微信小程序包的文件名常量
const (
App_Config = "app-config.json" // 应用配置文件
App_Service = "app-service.js" // 应用服务文件
PageFrameHtml = "page-frame.html" // 页面框架HTML文件
AppWxss = "app-wxss.js" // 应用样式文件
CommonApp = "common.app.js" // 通用应用文件
AppJson = "app.json" // 应用JSON文件
Workers = "workers.js" // 工作者脚本文件
Page_Frame = "page-frame.js" // 页面框架JS文件
AppService = "appservice.js" // 应用服务文件
PageFrame = "pageframe.js" // 页面框架JS文件
Game = "game.js" // 游戏脚本文件
GameJson = "game.json" // 游戏JSON文件
SubContext = "subContext.js" // 子上下文脚本文件
Plugin = "plugin.js" // 插件脚本文件
PluginJson = "plugin.json" // 插件JSON文件
)

// WxapkgType 定义微信小程序包的类型
type WxapkgType string

// 定义微信小程序包的类型常量
const (
App_V1 WxapkgType = "APP_V1" // 应用类型 V1
App_V2 WxapkgType = "APP_V2" // 应用类型 V2
App_V3 WxapkgType = "APP_V3" // 应用类型 V3
App_V4 WxapkgType = "APP_V4" // 应用类型 V4

APP_SUBPACKAGE_V1 WxapkgType = "APP_SUBPACKAGE_V1" // 应用子包类型 V1
APP_SUBPACKAGE_V2 WxapkgType = "APP_SUBPACKAGE_V2" // 应用子包类型 V2

APP_PLUGIN_V1 WxapkgType = "APP_PLUGIN_V1" // 应用插件类型 V1

GAME WxapkgType = "GAME" // 游戏类型
GAME_SUBPACKAGE WxapkgType = "GAME_SUBPACKAGE" // 游戏子包类型
GAME_PLUGIN WxapkgType = "GAME_PLUGIN" // 游戏插件类型

FRAMEWORK WxapkgType = "FRAMEWORK" // 框架类型
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
159 changes: 159 additions & 0 deletions internal/restore/decompiler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package restore

import (
"fmt"
"log"
"path/filepath"

"github.com/Ackites/KillWxapkg/internal/unpack"

"github.com/Ackites/KillWxapkg/internal/util"

"github.com/Ackites/KillWxapkg/internal/config"
"github.com/Ackites/KillWxapkg/internal/enum"
)

type WxapkgDecompiler struct {
}

func isParserV1(wxapkg *config.WxapkgInfo) bool {
switch wxapkg.WxapkgType {
case enum.App_V1, enum.App_V2, enum.APP_SUBPACKAGE_V1:
return true
default:
return false
}
}

func isParserV3(wxapkg *config.WxapkgInfo) bool {
switch wxapkg.WxapkgType {
case enum.App_V3, enum.App_V4, enum.APP_SUBPACKAGE_V2, enum.APP_PLUGIN_V1:
return true
default:
return false
}
}

// IsMainPackage 是否主包
func IsMainPackage(wxapkg *config.WxapkgInfo) bool {
switch wxapkg.WxapkgType {
case enum.App_V1, enum.App_V2, enum.App_V3, enum.App_V4, enum.GAME:
return true
default:
return false
}
}

// IsSubpackage 是否分包
func IsSubpackage(wxapkg *config.WxapkgInfo) bool {
switch wxapkg.WxapkgType {
case enum.APP_SUBPACKAGE_V1, enum.APP_SUBPACKAGE_V2, enum.GAME_SUBPACKAGE:
return true
default:
return false
}
}

// 是否小程序插件
func isAppPlugin(wxapkg *config.WxapkgInfo) bool {
switch wxapkg.WxapkgType {
case enum.APP_PLUGIN_V1:
return true
default:
return false
}

}

// 是否游戏插件
func isGamePlugin(wxapkg *config.WxapkgInfo) bool {
switch wxapkg.WxapkgType {
case enum.GAME_PLUGIN:
return true
default:
return false
}
}

// 是否插件
func isPlugin(wxapkg *config.WxapkgInfo) bool {
return isAppPlugin(wxapkg) || isGamePlugin(wxapkg)
}

// OutputDir 输出目录
var OutputDir string

func (d *WxapkgDecompiler) Decompile(outputDir string) {
// 设置输出目录
OutputDir = outputDir

wxapkgManager := config.GetWxapkgManager()
for _, wxapkg := range wxapkgManager.Packages {
switch wxapkg.WxapkgType {
case enum.App_V1, enum.App_V4:
wxapkg.Option = &config.WxapkgOption{
ViewSource: filepath.Join(wxapkg.SourcePath, enum.PageFrameHtml),
SetAppConfig: true,
}
setApp(wxapkg)
case enum.App_V2, enum.App_V3:
wxapkg.Option = &config.WxapkgOption{
SetAppConfig: true,
}
setApp(wxapkg)
case enum.APP_SUBPACKAGE_V1, enum.APP_SUBPACKAGE_V2:
wxapkg.Option = &config.WxapkgOption{
ViewSource: filepath.Join(wxapkg.SourcePath, enum.Page_Frame),
SetAppConfig: false,
}
setApp(wxapkg)
case enum.APP_PLUGIN_V1:
wxapkg.Option = &config.WxapkgOption{
ViewSource: filepath.Join(wxapkg.SourcePath, enum.PageFrame),
ServiceSource: filepath.Join(wxapkg.SourcePath, enum.AppService),
SetAppConfig: false,
}
setApp(wxapkg)
case enum.GAME:
case enum.GAME_SUBPACKAGE:
case enum.GAME_PLUGIN:
}
}
}

func setApp(wxapkg *config.WxapkgInfo) {
// 如果未解压,则不进行解析
if !wxapkg.IsExtracted {
return
}

if wxapkg.Option == nil {
wxapkg.Option = &config.WxapkgOption{}
}

if wxapkg.Option.ServiceSource == "" {
wxapkg.Option.ServiceSource = filepath.Join(wxapkg.SourcePath, enum.App_Service)
}

if wxapkg.Option.ViewSource == "" {
wxapkg.Option.ViewSource = filepath.Join(wxapkg.SourcePath, enum.AppWxss)
}

wccVersion := util.GetWccVersion(wxapkg.Option.ViewSource)
if wccVersion != "" {
log.Printf(fmt.Sprintf("The package %s wcc version is: [%s]", wxapkg.SourcePath, wccVersion))
}

if wxapkg.Option.SetAppConfig {
if wxapkg.Option.AppConfigSource == "" {
wxapkg.Option.AppConfigSource = filepath.Join(wxapkg.SourcePath, enum.App_Config)
}
wxapkg.Parsers = append(wxapkg.Parsers, &unpack.ConfigParser{})
}

wxapkg.Parsers = append(wxapkg.Parsers, &unpack.JavaScriptParser{OutputDir: OutputDir})
}

func cleanApp() {

}
32 changes: 0 additions & 32 deletions internal/restore/javascript.go

This file was deleted.

Loading

0 comments on commit 78753ef

Please sign in to comment.