forked from CalypsoSys/bobwinrm
/
response.go
168 lines (142 loc) · 3.71 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
package winrm
import (
"encoding/base64"
"errors"
"fmt"
"io"
"strconv"
"strings"
"github.com/ibrahimtopbasi/winrm/soap"
"github.com/ChrisTrenkamp/goxpath"
"github.com/ChrisTrenkamp/goxpath/tree"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree"
)
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) (string, error) {
if response == "" {
return "", errors.New("empty response")
}
doc, err := xmltree.ParseXML(strings.NewReader(response))
if err != nil {
return "", err
}
commandID, err := first(doc, "//rsp:CommandId")
if err != nil {
return "", err
}
if commandID != "" {
return commandID, nil
}
return "", errors.New("invalid command id")
}
//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
}