macho/macho-go/internal/wrapper/program_context.go

124 lines
3.0 KiB
Go
Raw Normal View History

2023-05-31 16:17:03 +07:00
package wrapper
import (
"encoding/json"
"io/ioutil"
log "github.com/sirupsen/logrus"
. "ios-wrapper/internal/wrapper/action"
. "ios-wrapper/internal/wrapper/ofile"
"ios-wrapper/pkg/protomodel"
)
type UserConfig struct {
Repack bool `json:"repack"`
Hook bool `json:"hook"`
Emulator bool `json:"emulator"`
Debug bool `json:"debug"`
Root bool `json:"root"`
ServerURL string `json:"server_url"`
ReportType int32 `json:"report_type"`
RemoteConfigURL string `json:"remote_config_url"`
DomainID string `json:"domain_id"`
}
func (uc *UserConfig) Protomodel() *protomodel.Config {
return &protomodel.Config{
Hook: uc.Hook,
Repack: uc.Repack,
Emulator: uc.Emulator,
Debug: uc.Debug,
Root: uc.Root,
ServerUrl: uc.ServerURL,
ReportType: uc.ReportType,
RemoteConfigUrl: uc.RemoteConfigURL,
DomainId: uc.DomainID,
}
}
type ProgramContext struct {
2023-06-26 15:28:16 +07:00
remove_inits bool
2023-05-31 16:17:03 +07:00
remove_codesign bool
2023-06-01 17:29:23 +07:00
remove_imports bool
2023-06-26 15:28:16 +07:00
remove_others bool
remove_exports bool
remove_symbol_table bool
2023-05-31 16:17:03 +07:00
dylib_to_add []string
rpath_to_add []string
outfile string
actions []Action
err error
user_config UserConfig
bcellfile string
signed bool // true: Protobuf<SignedData>; false: Protobuf<BcellFile>
}
func (pc *ProgramContext) ExplainError(err error) {
log.WithFields(log.Fields{
"pc error": pc.err,
"ofile error": err,
}).Error("OFile Process gone wrong")
}
func (pc *ProgramContext) ReadUserConfig(f string) {
json_data, err := ioutil.ReadFile(f)
if err != nil {
log.Panic("User Config file is invalid")
}
json.Unmarshal(json_data, &pc.user_config)
}
func (pc *ProgramContext) AddAction(action Action) {
pc.actions = append(pc.actions, action)
}
func (pc *ProgramContext) dispatchActions(ofile OFile) {
for _, action := range pc.actions {
err := RunAction(action, ofile)
if err != nil {
pc.err = err
break
}
}
}
func (pc *ProgramContext) Process(ofile OFile) {
if pc.remove_codesign {
pc.AddAction(NewRemoveCodeSignatureAction())
}
2023-06-15 10:41:18 +07:00
if pc.remove_inits {
2023-05-31 16:17:03 +07:00
pc.AddAction(NewSaveInitPointerAction())
pc.AddAction(NewRemoveInitPointerAction())
}
2023-06-15 10:41:18 +07:00
if pc.remove_imports {
pc.AddAction(NewSaveImportsAction())
2023-06-26 15:28:16 +07:00
pc.AddAction(NewRemoveImportsAction())
2023-06-15 10:41:18 +07:00
}
2023-06-26 15:28:16 +07:00
if pc.remove_symbol_table {
2023-05-31 16:17:03 +07:00
pc.AddAction(NewRemoveClassicSymbolAction())
2023-06-26 15:28:16 +07:00
}
if pc.remove_exports {
pc.AddAction(NewRemoveExportsAction())
}
2023-06-15 10:41:18 +07:00
ExperimentalFeature("Remove Unnecessary Info", func() {
2023-06-26 15:28:16 +07:00
if pc.remove_others {
pc.AddAction(NewRemoveUnnecessaryInfoAction())
}
2023-05-31 16:17:03 +07:00
})
pc.AddAction(NewAddRpathAction(pc.rpath_to_add))
pc.AddAction(NewAddDylibAction(pc.dylib_to_add))
if pc.bcellfile != "" {
pc.AddAction(NewSaveBcellFileAction(pc.user_config.Protomodel(), pc.bcellfile, pc.signed))
} else {
log.Warn("bcellfile is not set, no output bcellfile")
}
pc.AddAction(NewWriteFileAction(pc.outfile))
pc.dispatchActions(ofile)
}