-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelevation.go
More file actions
150 lines (130 loc) · 2.62 KB
/
elevation.go
File metadata and controls
150 lines (130 loc) · 2.62 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
package main
import (
"bufio"
"context"
"encoding/json"
"fmt"
"net"
"sync"
"time"
"github.com/google/uuid"
)
type elevationManager struct {
mu sync.Mutex
initMu sync.Mutex
ln net.Listener
conn net.Conn
pipeID string
}
var globalElevator = &elevationManager{}
var (
startElevatedHelperFunc = startElevatedHelper
helperAcceptTimeout = 60 * time.Second
)
func (m *elevationManager) ensureHelper() error {
m.initMu.Lock()
defer m.initMu.Unlock()
m.mu.Lock()
if m.conn != nil {
m.mu.Unlock()
return nil
}
m.mu.Unlock()
pipeID := "wintui-" + uuid.New().String()
ln, err := startElevatedHelperFunc(context.Background(), pipeID)
if err != nil {
return err
}
m.mu.Lock()
m.pipeID = pipeID
m.ln = ln
m.mu.Unlock()
// Wait for helper to connect
// Set a timeout so we don't hang forever if UAC is cancelled
errChan := make(chan error, 1)
go func() {
conn, err := ln.Accept()
if err != nil {
errChan <- err
return
}
m.mu.Lock()
m.conn = conn
m.mu.Unlock()
errChan <- nil
}()
select {
case err := <-errChan:
return err
case <-time.After(helperAcceptTimeout):
m.mu.Lock()
if m.ln != nil {
m.ln.Close()
m.ln = nil
}
m.mu.Unlock()
return fmt.Errorf("timeout waiting for elevated helper (UAC cancelled?)")
}
}
// shutdown closes the helper connection and listener, causing the
// elevated helper process to exit cleanly.
func (m *elevationManager) shutdown() {
m.mu.Lock()
defer m.mu.Unlock()
if m.conn != nil {
m.conn.Close()
m.conn = nil
}
if m.ln != nil {
m.ln.Close()
m.ln = nil
}
}
func (m *elevationManager) runCommandElevated(args ...string) (<-chan string, <-chan error, error) {
if err := m.ensureHelper(); err != nil {
return nil, nil, err
}
outChan := make(chan string)
errChan := make(chan error, 1)
go func() {
defer close(outChan)
defer close(errChan)
m.mu.Lock()
conn := m.conn
m.mu.Unlock()
req := helperRequest{
Action: "winget",
Args: args,
NonInt: false,
}
b, _ := json.Marshal(req)
conn.Write(b)
conn.Write([]byte("\n"))
reader := bufio.NewReader(conn)
for {
line, err := reader.ReadString('\n')
if err != nil {
errChan <- fmt.Errorf("helper connection lost: %w", err)
m.mu.Lock()
m.conn = nil // Reset so we can restart it
m.mu.Unlock()
return
}
var resp helperResponse
if err := json.Unmarshal([]byte(line), &resp); err != nil {
continue
}
switch resp.Type {
case "line":
outChan <- resp.Data
case "done":
errChan <- nil
return
case "error":
errChan <- fmt.Errorf("%s", resp.Data)
return
}
}
}()
return outChan, errChan, nil
}