forked from DrDonk/unlocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmw_windows.go
234 lines (203 loc) · 5.75 KB
/
vmw_windows.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
231
232
233
234
// SPDX-FileCopyrightText: © 2014-2022 David Parsons
// SPDX-License-Identifier: MIT
//go:build windows
// +build windows
package vmwpatch
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"time"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/mgr"
)
var manager *mgr.Mgr
func IsAdmin() bool {
// Always return true as now has embedded manifest to runas Administrator
return true
}
func VMWStart(v *VMwareInfo) {
fmt.Println()
fmt.Println("Starting VMware services and tasks...")
svcStart(v.AuthD)
svcStart(v.USBD)
svcStart(v.HostD)
err := manager.Disconnect()
if err != nil {
fmt.Println("Disconnect from SCM failed")
// Not stopping the process over this one
}
cwd, _ := os.Getwd()
os.Chdir("C:\\Windows\\SysWOW64\\")
taskStart(filepath.Join(v.InstallDir, v.Tray))
os.Chdir(cwd)
}
func VMWStop(v *VMwareInfo) {
fmt.Println()
fmt.Println("Stopping VMware services and tasks...")
var err error
manager, err = mgr.Connect()
if err != nil {
panic("SCM connection failed")
}
svcStop(v.HostD)
svcStop(v.USBD)
svcStop(v.AuthD)
taskStop(v.ShellExt) // No Need to re-exec this, it's part of a registered shell extension
taskStop(v.Tray)
}
func VMWInfo() *VMwareInfo {
v := &VMwareInfo{}
// Store known service names
v.AuthD = "VMAuthdService"
v.HostD = "VMwareHostd"
v.USBD = "VMUSBArbService"
// Access registry for version, build and installation path
var access uint32
access = registry.QUERY_VALUE
if runtime.GOARCH == "amd64" {
access = access | registry.WOW64_32KEY
}
regKey, err := registry.OpenKey(registry.LOCAL_MACHINE,
`SOFTWARE\VMware, Inc.\VMware Player`, access)
if err != nil {
fmt.Println("Failed to open VMware registry key")
return v
}
//goland:noinspection GoUnhandledErrorResult
defer regKey.Close()
v.ProductVersion, _, err = regKey.GetStringValue("ProductVersion")
if err != nil {
fmt.Println("Failed to locate registry key ProductVersion")
return v
}
v.BuildNumber, _, err = regKey.GetStringValue("BuildNumber")
if err != nil {
fmt.Println("Failed to locate registry key BuildNumber")
return v
}
v.InstallDir, _, err = regKey.GetStringValue("InstallPath")
if err != nil {
fmt.Println("Failed to locate registry key InstallPath")
return v
}
// Construct needed filenames from reg settings
v.BasePath = getBaseDir()
v.InstallDir64 = filepath.Join(v.InstallDir, "x64")
v.Player = "vmplayer.exe"
v.Workstation = "vmware.exe"
v.KVM = "vmware-kvm.exe"
v.REST = "vmrest.exe"
v.ShellExt = "vmware-shell-ext-thunker.exe"
v.Tray = "vmware-tray.exe"
v.VMXDefault = "vmware-vmx.exe"
v.VMXDebug = "vmware-vmx-debug.exe"
v.VMXStats = "vmware-vmx-stats.exe"
v.VMwareBase = "vmwarebase.dll"
v.PathVMXDefault = filepath.Join(v.InstallDir64, v.VMXDefault)
v.PathVMXDebug = filepath.Join(v.InstallDir64, v.VMXDebug)
v.PathVMXStats = filepath.Join(v.InstallDir64, v.VMXStats)
v.PathVMwareBase = filepath.Join(v.InstallDir, v.VMwareBase)
v.BackDir = filepath.Join(v.BasePath, "backup", v.ProductVersion)
v.BackVMXDefault = filepath.Join(v.BackDir, v.VMXDefault)
v.BackVMXDebug = filepath.Join(v.BackDir, v.VMXDebug)
v.BackVMXStats = filepath.Join(v.BackDir, v.VMXStats)
v.BackVMwareBase = filepath.Join(v.BackDir, v.VMwareBase)
v.SrcISOMacOSX = filepath.Join(v.BasePath, "iso", "darwinPre15.iso")
v.SrcISOmacOS = filepath.Join(v.BasePath, "iso", "darwin.iso")
v.DstISOMacOSX = filepath.Join(v.InstallDir, "darwinPre15.iso")
v.DstISOmacOS = filepath.Join(v.InstallDir, "darwin.iso")
return v
}
func setCTime(path string, ctime time.Time) error {
//setCTime will set the creation time on a file. On Windows, this requires
//calling SetFileTime and explicitly including the creation time.
ctimespec := windows.NsecToTimespec(ctime.UnixNano())
pathp, e := windows.UTF16PtrFromString(path)
if e != nil {
return e
}
h, e := windows.CreateFile(pathp,
windows.FILE_WRITE_ATTRIBUTES, windows.FILE_SHARE_WRITE, nil,
windows.OPEN_EXISTING, windows.FILE_FLAG_BACKUP_SEMANTICS, 0)
if e != nil {
return e
}
//goland:noinspection GoUnhandledErrorResult
defer windows.CloseHandle(h)
c := windows.NsecToFiletime(windows.TimespecToNsec(ctimespec))
return windows.SetFileTime(h, &c, nil, nil)
}
func svcState(s *mgr.Service) svc.State {
status, err := s.Query()
if err != nil {
panic(fmt.Sprintf("Query(%s) failed: %s", s.Name, err))
}
return status.State
}
func svcWaitState(s *mgr.Service, want svc.State) {
state := make(chan svc.State, 1)
defer close(state)
t := time.NewTimer(3 * time.Second)
for {
select {
case <-t.C:
panic(fmt.Sprintf("%s state change timeout", s.Name))
case currentState := <-state:
if currentState == want {
t.Stop()
return
}
case <-time.After(300 * time.Millisecond):
state <- svcState(s)
}
}
}
func svcStart(name string) {
s, err := manager.OpenService(name)
if err != nil {
return
}
fmt.Println("Starting service ", name)
//goland:noinspection ALL
defer s.Close()
if svcState(s) == svc.Stopped {
err = s.Start()
if err != nil {
panic(fmt.Sprintf("Control(%s) failed: %s", name, err))
}
svcWaitState(s, svc.Running)
}
}
func svcStop(name string) {
s, err := manager.OpenService(name)
if err != nil {
return
}
fmt.Println("Stopping service ", name)
//goland:noinspection ALL
defer s.Close()
if svcState(s) == svc.Running {
_, err = s.Control(svc.Stop)
if err != nil {
panic(fmt.Sprintf("Control(%s) failed: %s", name, err))
}
svcWaitState(s, svc.Stopped)
}
}
func taskStart(filename string) {
fmt.Println("Starting task ", filename)
c := exec.Command(filename)
_ = c.Start()
}
func taskStop(name string) {
if TaskRunning(name) != 0 {
fmt.Println("Stopping task ", name)
c := exec.Command("taskkill.exe", "/F", "/IM", name)
_ = c.Run()
}
}