Repository navigation
Expand file tree
/
Copy pathplatform_windows.go
More file actions
292 lines (261 loc) · 7.91 KB
/
Copy pathplatform_windows.go
File metadata and controls
292 lines (261 loc) · 7.91 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//go:build windows
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"time"
"unsafe"
)
const (
helperArgument = "__keepawake"
autoMonitorTaskName = "nosleep-agent-monitor"
esContinuous = 0x80000000
esSystemRequired = 0x00000001
esDisplayRequired = 0x00000002
processSynchronize = 0x00100000
processQueryLimitedInformation = 0x1000
stillActive = 259
)
func autoMonitorPollInterval() time.Duration {
return 10 * time.Second
}
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
setThreadExecutionState = kernel32.NewProc("SetThreadExecutionState")
getExitCodeProcess = kernel32.NewProc("GetExitCodeProcess")
queryFullProcessImageName = kernel32.NewProc("QueryFullProcessImageNameW")
moveFileEx = kernel32.NewProc("MoveFileExW")
)
const moveFileReplaceExisting = 0x1
func runPlatformHelper(args []string) (bool, error) {
if len(args) == 1 && args[0] == agentMonitorArgument {
return true, runAgentMonitor()
}
if len(args) == 0 || args[0] != helperArgument {
return false, nil
}
if len(args) > 2 {
return true, errors.New("invalid internal helper arguments")
}
var duration time.Duration
if len(args) == 2 {
seconds, err := strconv.ParseInt(args[1], 10, 64)
if err != nil || seconds < 1 {
return true, errors.New("invalid internal helper duration")
}
duration = time.Duration(seconds) * time.Second
}
// Windows tracks execution requirements for the calling thread. Keep this
// goroutine on that thread for the lifetime of the helper process.
runtime.LockOSThread()
defer runtime.UnlockOSThread()
result, _, callErr := setThreadExecutionState.Call(esContinuous | esSystemRequired | esDisplayRequired)
if result == 0 {
return true, fmt.Errorf("SetThreadExecutionState failed: %w", callErr)
}
defer setThreadExecutionState.Call(esContinuous)
if duration > 0 {
time.Sleep(duration)
} else {
select {}
}
return true, nil
}
func isSupportedPlatform() bool {
return true
}
func replaceFile(source string, destination string) error {
sourcePointer, err := syscall.UTF16PtrFromString(source)
if err != nil {
return err
}
destinationPointer, err := syscall.UTF16PtrFromString(destination)
if err != nil {
return err
}
result, _, callErr := moveFileEx.Call(
uintptr(unsafe.Pointer(sourcePointer)),
uintptr(unsafe.Pointer(destinationPointer)),
moveFileReplaceExisting,
)
if result == 0 {
return callErr
}
return nil
}
func sleepPreventionCommand(duration *durationState) (*exec.Cmd, []string, error) {
executable, err := os.Executable()
if err != nil {
return nil, nil, err
}
args := []string{helperArgument}
if duration != nil {
args = append(args, strconv.FormatInt(duration.Seconds, 10))
}
return exec.Command(executable, args...), args, nil
}
func configureDetachedProcess(cmd *exec.Cmd) {
const (
createNewProcessGroup = 0x00000200
detachedProcess = 0x00000008
createNoWindow = 0x08000000
)
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: createNewProcessGroup | detachedProcess | createNoWindow,
HideWindow: true,
}
}
func openProcess(access uint32, pid int) (syscall.Handle, error) {
handle, err := syscall.OpenProcess(access, false, uint32(pid))
if err != nil {
return 0, err
}
return handle, nil
}
func isPIDAlive(pid int) bool {
handle, err := openProcess(processSynchronize|processQueryLimitedInformation, pid)
if err != nil {
return false
}
defer syscall.CloseHandle(handle)
var exitCode uint32
result, _, _ := getExitCodeProcess.Call(uintptr(handle), uintptr(unsafe.Pointer(&exitCode)))
return result != 0 && exitCode == stillActive
}
func isSleepPreventionPID(pid int) bool {
handle, err := openProcess(processQueryLimitedInformation, pid)
if err != nil {
return false
}
defer syscall.CloseHandle(handle)
buffer := make([]uint16, 32768)
size := uint32(len(buffer))
result, _, _ := queryFullProcessImageName.Call(
uintptr(handle),
0,
uintptr(unsafe.Pointer(&buffer[0])),
uintptr(unsafe.Pointer(&size)),
)
if result == 0 {
return false
}
runningPath := syscall.UTF16ToString(buffer[:size])
executable, err := os.Executable()
if err != nil {
return false
}
return strings.EqualFold(filepath.Clean(runningPath), filepath.Clean(executable))
}
func terminateState(state *sessionState) error {
process, err := os.FindProcess(state.PID)
if err != nil {
return removeState()
}
if err := process.Kill(); err != nil {
return removeState()
}
for attempt := 0; attempt < 20; attempt++ {
time.Sleep(100 * time.Millisecond)
if !isPIDAlive(state.PID) {
break
}
}
return removeState()
}
func findRunningAgents() ([]detectedAgent, error) {
const script = `ConvertTo-Json -InputObject @(Get-CimInstance Win32_Process -Filter "Name = 'claude.exe' OR Name = 'codex.exe'" | Select-Object ProcessId,ExecutablePath,Name,CommandLine) -Compress`
output, err := exec.Command("powershell.exe", "-NoProfile", "-NonInteractive", "-Command", script).Output()
if err != nil {
return nil, fmt.Errorf("inspect running processes: %w", err)
}
var processes []struct {
ProcessID int `json:"ProcessId"`
ExecutablePath string `json:"ExecutablePath"`
Name string `json:"Name"`
CommandLine string `json:"CommandLine"`
}
if len(strings.TrimSpace(string(output))) == 0 {
return nil, nil
}
if err := json.Unmarshal(output, &processes); err != nil {
return nil, fmt.Errorf("decode running processes: %w", err)
}
agents := make([]detectedAgent, 0, len(processes))
for _, process := range processes {
executable := process.ExecutablePath
if executable == "" {
executable = process.Name
}
if name := classifyAgent(executable, process.CommandLine); name != "" {
agents = append(agents, detectedAgent{Name: name, PID: process.ProcessID})
}
}
return agents, nil
}
func runAgentMonitor() error {
// SetThreadExecutionState applies to the calling thread, so the monitor loop
// must remain on the same OS thread while its automatic assertion is active.
runtime.LockOSThread()
defer runtime.UnlockOSThread()
start := func() (int, error) {
result, _, callErr := setThreadExecutionState.Call(esContinuous | esSystemRequired | esDisplayRequired)
if result == 0 {
return 0, fmt.Errorf("start automatic sleep prevention: %w", callErr)
}
return 0, nil
}
stop := func() error {
result, _, callErr := setThreadExecutionState.Call(esContinuous)
if result == 0 {
return fmt.Errorf("stop automatic sleep prevention: %w", callErr)
}
return nil
}
return runAgentMonitorLoop(nil, start, stop)
}
func installAutoMonitor() error {
executable, err := os.Executable()
if err != nil {
return err
}
executable, err = filepath.Abs(executable)
if err != nil {
return err
}
taskCommand := fmt.Sprintf(`"%s" %s`, executable, agentMonitorArgument)
command := exec.Command(
"schtasks.exe", "/Create",
"/TN", autoMonitorTaskName,
"/TR", taskCommand,
"/SC", "ONLOGON",
"/RL", "LIMITED",
"/F",
)
if output, err := command.CombinedOutput(); err != nil {
return fmt.Errorf("install automatic agent monitor: %w: %s", err, strings.TrimSpace(string(output)))
}
command = exec.Command("schtasks.exe", "/Run", "/TN", autoMonitorTaskName)
if output, err := command.CombinedOutput(); err != nil {
return fmt.Errorf("start automatic agent monitor: %w: %s", err, strings.TrimSpace(string(output)))
}
return nil
}
func uninstallAutoMonitor() error {
_ = exec.Command("schtasks.exe", "/End", "/TN", autoMonitorTaskName).Run()
command := exec.Command("schtasks.exe", "/Delete", "/TN", autoMonitorTaskName, "/F")
if output, err := command.CombinedOutput(); err != nil {
// A missing task is already in the desired disabled state.
if !strings.Contains(strings.ToLower(string(output)), "cannot find") {
return fmt.Errorf("remove automatic agent monitor: %w: %s", err, strings.TrimSpace(string(output)))
}
}
return nil
}