-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathipc_windows.go
More file actions
163 lines (144 loc) · 3.55 KB
/
ipc_windows.go
File metadata and controls
163 lines (144 loc) · 3.55 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
//go:build windows
package main
import (
"bufio"
"errors"
"fmt"
"net"
"os"
"strings"
"time"
"github.com/Microsoft/go-winio"
"golang.org/x/sys/windows"
)
func wincronControlPipeUserPath() string {
sid, err := currentProcessUserSID()
if err != nil || sid == "" {
return `\\.\pipe\wincron_control_user`
}
return `\\.\pipe\wincron_control_` + sid
}
func currentProcessUserSID() (string, error) {
var tok windows.Token
if err := windows.OpenProcessToken(windows.CurrentProcess(), windows.TOKEN_QUERY, &tok); err != nil {
return "", err
}
defer tok.Close()
tu, err := tok.GetTokenUser()
if err != nil {
return "", err
}
if tu == nil || tu.User.Sid == nil {
return "", errors.New("token user sid not available")
}
return tu.User.Sid.String(), nil
}
func pipeSecurityDescriptor(allowAuthenticatedUsers bool) string {
if allowAuthenticatedUsers {
return "D:P(A;;GA;;;SY)(A;;GA;;;BA)(A;;GA;;;AU)"
}
sid, err := currentProcessUserSID()
if err != nil || sid == "" {
return "D:P(A;;GA;;;SY)(A;;GA;;;BA)(A;;GA;;;AU)"
}
return fmt.Sprintf("D:P(A;;GA;;;SY)(A;;GA;;;BA)(A;;GA;;;%s)", sid)
}
func startIPCServer(pipePath string, allowAuthenticatedUsers bool, handler func(ipcRequest) ipcResponse) (stop func(), err error) {
cfg := &winio.PipeConfig{
SecurityDescriptor: pipeSecurityDescriptor(allowAuthenticatedUsers),
}
l, err := winio.ListenPipe(pipePath, cfg)
if err != nil {
l, err = winio.ListenPipe(pipePath, nil)
if err != nil {
return nil, err
}
}
done := make(chan struct{})
go func() {
for {
conn, acceptErr := l.Accept()
if acceptErr != nil {
select {
case <-done:
return
default:
continue
}
}
go func(c net.Conn) {
defer c.Close()
_ = c.SetDeadline(time.Now().Add(5 * time.Second))
r := bufio.NewReaderSize(c, 4096)
line, readErr := r.ReadBytes('\n')
if readErr != nil {
return
}
var req ipcRequest
if err := unmarshalJSONLine(line, &req); err != nil {
resp := ipcResponse{Ok: false, Error: "invalid request"}
if b, mErr := marshalJSONLine(resp); mErr == nil {
_, _ = c.Write(b)
}
return
}
req.Cmd = strings.ToLower(strings.TrimSpace(req.Cmd))
resp := handler(req)
b, mErr := marshalJSONLine(resp)
if mErr != nil {
return
}
_, _ = c.Write(b)
}(conn)
}
}()
stop = func() {
select {
case <-done:
return
default:
close(done)
_ = l.Close()
}
}
return stop, nil
}
func sendIPCRequestToPipe(pipePath string, req ipcRequest) (ipcResponse, error) {
timeout := 2 * time.Second
conn, err := winio.DialPipe(pipePath, &timeout)
if err != nil {
return ipcResponse{}, err
}
defer conn.Close()
_ = conn.SetDeadline(time.Now().Add(4 * time.Second))
b, err := marshalJSONLine(req)
if err != nil {
return ipcResponse{}, err
}
if _, err := conn.Write(b); err != nil {
return ipcResponse{}, err
}
r := bufio.NewReaderSize(conn, 4096)
line, err := r.ReadBytes('\n')
if err != nil {
return ipcResponse{}, err
}
var resp ipcResponse
if err := unmarshalJSONLine(line, &resp); err != nil {
return ipcResponse{}, err
}
return resp, nil
}
func sendIPCRequest(req ipcRequest) (ipcResponse, error) {
return sendIPCRequestToPipe(wincronControlPipeUserPath(), req)
}
func isLikelyPipeNotRunning(err error) bool {
if err == nil {
return false
}
if errors.Is(err, os.ErrNotExist) {
return true
}
msg := strings.ToLower(err.Error())
return strings.Contains(msg, "file not found") || strings.Contains(msg, "cannot find") || strings.Contains(msg, "the system cannot find the file specified")
}