-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
230 lines (213 loc) · 4.72 KB
/
main.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/pkg/errors"
)
var (
// populate with key management servers.
servers []string = []string{
"kms.srv.crsoo.com",
"cy2617.jios.org",
"kms.digiboy.ir",
"kms.cangshui.net",
"kms.library.hk",
"hq1.chinancce.com",
"kms.loli.beer",
"kms.v0v.bid",
"54.223.212.31",
"kms.jm33.me",
"nb.shenqw.win",
"kms.izetn.cn",
"kms.cin.ink",
"222.184.9.98",
"kms.ijio.net",
"fourdeltaone.net:1688",
"kms.iaini.net",
"kms.cnlic.com",
"kms.51it.wang",
"key.17108.com",
"kms.chinancce.com",
"kms.ddns.net",
"windows.kms.app",
"kms.ddz.red",
"franklv.ddns.net",
"kms.mogeko.me",
"k.zpale.com",
"amrice.top",
"m.zpale.com",
"mvg.zpale.com",
"kms.shuax.com",
"kensol263.imwork.net:1688",
"xykz.f3322.org",
"kms789.com",
"dimanyakms.sytes.net:1688",
"kms8.MSGuides.com",
"kms.03k.org:1688",
"kms.ymgblog.com",
"kms.bige0.com",
"kms9.MSGuides.com",
"kms.cz9.cn",
"kms.lolico.moe",
"kms.ddddg.cn",
"kms.zhuxiaole.org",
"kms.moeclub.org",
"kms.lotro.cc",
"zh.us.to",
"noair.strangled.net:1688",
}
// populate with keys for the windows version you want to activate.
keys map[winVer][]string = map[winVer][]string{
pro: {},
}
)
func main() {
print("Activate Windows 10 All Versions\n", underscore)
if err := uninstallProductKey(); err != nil {
infof("%v", errors.Wrap(err, "uninstalling existing product key"))
}
if err := clearRegistry(); err != nil {
infof("%v", errors.Wrap(err, "clearing registry"))
}
version, err := version()
if err != nil {
failedf("%v", errors.Wrap(err, "detecting windows version"))
}
infof("Activating for %v version\n", version)
keylist, ok := keys[version]
if !ok {
failedf("no configured keys for %v version", version)
}
for _, key := range keylist {
ok, err := installProductKey(key)
if err != nil {
failedf("%v", errors.Wrap(err, "installing product key"))
}
if ok {
break
}
}
var successful bool
for _, server := range servers {
if err := setKeyManagementServer(server); err != nil {
failedf("%v", errors.Wrap(err, "setting key management server"))
}
if err := activateWindows(); err == nil {
successful = true
fmt.Printf("Activated!\n")
break
}
infof("could not connect to %q: %v, trying another\n", server, err)
}
if !successful {
failedf("activation failed.\n")
}
}
const (
underscore = iota
)
func print(msg string, flag int) {
fmt.Print(msg)
if flag == underscore {
if msg[len(msg)-1] != '\n' {
fmt.Printf("\n")
}
for ii := 0; ii < len(msg)-1; ii++ {
fmt.Printf("=")
}
fmt.Printf("\n")
}
}
type winVer string
const (
enterprise winVer = "Enterprise"
pro winVer = "Pro"
home winVer = "Home"
unknown winVer = "Unknown"
)
func version() (winVer, error) {
out, err := exec.Command(`C:\Windows\System32\wbem\wmic.exe`, "os").CombinedOutput()
if err != nil {
return unknown, err
}
stdout := string(out)
switch {
case strings.Contains(stdout, "enterprise"):
return enterprise, nil
case strings.Contains(stdout, "10 Pro"):
return pro, nil
case strings.Contains(stdout, "home"):
return home, nil
default:
return unknown, fmt.Errorf("could not determine Windows version")
}
}
func uninstallProductKey() error {
out, err := slmgr("/upk")
if err != nil {
return errors.Wrap(err, "/upk")
}
if !strings.Contains(out, "successfully") {
return fmt.Errorf(out)
}
return nil
}
func clearRegistry() error {
out, err := slmgr("/cpky")
if err != nil {
return errors.Wrap(err, "/cpky")
}
if !strings.Contains(out, "successfully") {
return fmt.Errorf(out)
}
return nil
}
func installProductKey(key string) (bool, error) {
out, err := slmgr("/ipk", key)
if err != nil {
return false, errors.Wrapf(err, "/ipk %s", key)
}
if !strings.Contains(out, "successfully") {
return false, nil
}
return true, nil
}
func setKeyManagementServer(server string) error {
out, err := slmgr("/skms", server)
if err != nil {
return errors.Wrap(err, "/skms")
}
if !strings.Contains(out, "successfully") {
return fmt.Errorf(out)
}
return nil
}
func activateWindows() error {
out, err := slmgr("/ato")
if err != nil {
return errors.Wrap(err, "/ato")
}
if !strings.Contains(out, "successfully") {
return fmt.Errorf("activation failed")
}
return nil
}
func slmgr(args ...string) (string, error) {
cmd := []string{
"cscript",
"//nologo",
`C:\Windows\System32\slmgr.vbs`,
}
cmd = append(cmd, args...)
out, err := exec.Command(cmd[0], cmd[1:]...).CombinedOutput()
return string(out), err
}
func failedf(format string, v ...interface{}) {
fmt.Printf(fmt.Sprintf("[error] %s", format), v...)
os.Exit(1)
}
func infof(format string, v ...interface{}) {
fmt.Printf(fmt.Sprintf("[info] %v\n", format), v...)
}