forked from masterzen/winrm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponse.go
204 lines (170 loc) · 4.47 KB
/
response.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
package winrm
import (
"encoding/base64"
"errors"
"fmt"
"io"
"strconv"
"strings"
"github.com/CalypsoSys/bobwinrm/soap"
"github.com/ChrisTrenkamp/goxpath"
"github.com/ChrisTrenkamp/goxpath/tree"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree"
)
type ExecuteCommandError struct {
Inner error
Body string
}
func (e *ExecuteCommandError) Error() string {
if e.Inner == nil {
return "error"
}
return e.Inner.Error()
}
func (e *ExecuteCommandError) Is(err error) bool {
_, ok := err.(*ExecuteCommandError)
return ok
}
func (b *ExecuteCommandError) Unwrap() error {
return b.Inner
}
func first(node tree.Node, xpath string) (string, error) {
nodes, err := xPath(node, xpath)
if err != nil {
return "", err
}
if len(nodes) < 1 {
return "", err
}
return nodes[0].ResValue(), nil
}
func any(node tree.Node, xpath string) (bool, error) {
nodes, err := xPath(node, xpath)
if err != nil {
return false, err
}
if len(nodes) > 0 {
return true, nil
}
return false, nil
}
func xPath(node tree.Node, xpath string) (tree.NodeSet, error) {
xpExec := goxpath.MustParse(xpath)
nodes, err := xpExec.ExecNode(node, soap.GetAllXPathNamespaces())
if err != nil {
return nil, err
}
return nodes, nil
}
// ParseOpenShellResponse ParseOpenShellResponse
func ParseOpenShellResponse(response string) (string, error) {
if response == "" {
return "", errors.New("empty response")
}
doc, err := xmltree.ParseXML(strings.NewReader(response))
if err != nil {
return "", err
}
shellID, err := first(doc, "//w:Selector[@Name='ShellId']")
if err != nil {
return "", err
}
if shellID != "" {
return shellID, nil
}
return "", errors.New("invalid shell id")
}
// ParseExecuteCommandResponse ParseExecuteCommandResponse
func ParseExecuteCommandResponse(response string) (commandId string, err error) {
defer func() {
if err != nil {
err = &ExecuteCommandError{Inner: err, Body: response}
}
}()
if response == "" {
return "", errors.New("empty response")
}
doc, err := xmltree.ParseXML(strings.NewReader(response))
if err != nil {
return "", fmt.Errorf("parsing xml response: %w", err)
}
action, err := first(doc, "//a:Action")
if err != nil {
return "", fmt.Errorf("getting response action: %w", err)
}
switch action {
case "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandResponse":
commandId, err = first(doc, "//rsp:CommandId")
if err != nil {
return "", fmt.Errorf("finding command id: %w", err)
}
return commandId, nil
default:
return "", fmt.Errorf("unsupported action: %v", action)
}
}
// ParseSlurpOutputErrResponse ParseSlurpOutputErrResponse
func ParseSlurpOutputErrResponse(response string, stdout, stderr io.Writer) (bool, int, error) {
var (
finished bool
exitCode int
)
if response == "" {
return false, 0, nil
}
doc, err := xmltree.ParseXML(strings.NewReader(response))
if err != nil {
return false, 0, err
}
stdouts, _ := xPath(doc, "//rsp:Stream[@Name='stdout']")
for _, node := range stdouts {
content, _ := base64.StdEncoding.DecodeString(node.ResValue())
stdout.Write(content)
}
stderrs, _ := xPath(doc, "//rsp:Stream[@Name='stderr']")
for _, node := range stderrs {
content, _ := base64.StdEncoding.DecodeString(node.ResValue())
stderr.Write(content)
}
ended, _ := any(doc, "//*[@State='http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done']")
if ended {
finished = ended
if exitBool, _ := any(doc, "//rsp:ExitCode"); exitBool {
exit, _ := first(doc, "//rsp:ExitCode")
exitCode, _ = strconv.Atoi(exit)
}
} else {
finished = false
}
return finished, exitCode, err
}
// ParseSlurpOutputResponse ParseSlurpOutputResponse
func ParseSlurpOutputResponse(response string, stream io.Writer, streamType string) (bool, int, error) {
var (
finished bool
exitCode int
)
if response == "" {
return false, 0, nil
}
doc, err := xmltree.ParseXML(strings.NewReader(response))
if err != nil {
return false, 0, err
}
nodes, _ := xPath(doc, fmt.Sprintf("//rsp:Stream[@Name='%s']", streamType))
for _, node := range nodes {
content, _ := base64.StdEncoding.DecodeString(node.ResValue())
_, _ = stream.Write(content)
}
ended, _ := any(doc, "//*[@State='http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done']")
if ended {
finished = ended
if exitBool, _ := any(doc, "//rsp:ExitCode"); exitBool {
exit, _ := first(doc, "//rsp:ExitCode")
exitCode, _ = strconv.Atoi(exit)
}
} else {
finished = false
}
return finished, exitCode, err
}