1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
// 註冊表地址 HKEY_CURRENT_USER\Software\Typora\IDate
// 用戶設置文件 C:\Users\UserName\AppData\Roaming\Typora\profile.data
// 呃呃 不過profile.data是進行了一次十六進制編碼的json格式文件 需要hex2str後才能修改具體內容
type Config struct {
// C:\Users\Administrator\AppData\Roaming\Typora
ProfilePath string `json:"profile_path"`
// C:\Typora_KeepTrying
InstallDirPath string `json:"install_path"`
// S-1-5-21-2495842453-42734561234-229025492-1006
SID string `json:"security_id"`
}
var DefaultInstall = `C:\Typora_KeepTrying`
var CFG *Config = &Config{}
//....
{
config, err := readProfile(CFG.ProfilePath)
if err != nil {
Log("解析出错...")
Log("Parse failed...", err)
return err
}
Log("解析Profile文件...")
Log("Parsing Profile file...")
config["_iD"] = today
//改写PreVersion 失效
//config["version"] = "9.99.9"
//禁用自动更新
config["enableAutoUpdate"] = false
//禁止发送匿名使用消息
config["send_usage_info"] = false
if err := writeProfile(CFG.ProfilePath, config); err != nil {
Log("写入出错...")
Log("Write failed...", err)
restoreErr := restoreBackup(CFG.ProfilePath)
if restoreErr != nil {
Log("恢复失败,请手动检查 profile.data.bak")
Log("Recovery failed, please manually check profile.data.bak")
} else {
Log("已成功恢复 profile.data.bak")
Log("Successfully restored profile.data.bak")
}
return err
}
if err := updateRegistry(today, CFG.SID); err != nil {
Log("写入注册表出错...")
Log("Failed to write to registry...", err)
return err
}
//....
}
//....
func updateRegistry(today string, sid string) error {
regKey := fmt.Sprintf(`%s\Software\Typora`, sid)
Log(regKey)
//"S-1-5-21-2495842453-42734561234-229025492-1006\\Software\\Typora"
k, _, err := registry.CreateKey(registry.USERS, regKey, registry.SET_VALUE)
if err != nil {
Log("无法打开或创建注册表项: " + err.Error())
Log("Failed to open or create registry key: " + err.Error())
return errors.New("无法打开或创建注册表项: %w")
}
defer k.Close()
err = k.SetStringValue("IDate", today)
if err != nil {
Log("无法设置注册表值: " + err.Error())
Log("Failed to set registry value: " + err.Error())
return errors.New("无法设置注册表值: %w")
}
return nil
}
func readProfile(path string) (map[string]interface{}, error) {
rawHex, err := os.ReadFile(path)
if err != nil {
return nil, err
}
jsonBytes, err := hex.DecodeString(string(rawHex))
if err != nil {
return nil, err
}
var config map[string]interface{}
if err := json.Unmarshal(jsonBytes, &config); err != nil {
return nil, err
}
return config, nil
}
func writeProfile(path string, config map[string]interface{}) error {
jsonBytes, err := json.Marshal(config)
if err != nil {
return err
}
newHex := hex.EncodeToString(jsonBytes)
return os.WriteFile(path, []byte(newHex), 0644)
}
|