/
request.go
115 lines (94 loc) · 4.61 KB
/
request.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
package winrm
import (
"encoding/base64"
"github.com/nu7hatch/gouuid"
"github.com/sneal/go-winrm/soap"
)
func genUUID() string {
uuid, _ := uuid.NewV4()
return "uuid:" + uuid.String()
}
func defaultHeaders(message *soap.SoapMessage, params *Parameters) (h *soap.SoapHeader) {
h = message.Header()
h.To(params.url).ReplyTo("http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous").MaxEnvelopeSize(params.EnvelopeSize).Id(genUUID()).Locale(params.Locale).Timeout(params.Timeout)
return
}
func NewOpenShellRequest(uri string, params *Parameters) (message *soap.SoapMessage) {
if params == nil {
params = DefaultParameters()
}
params.url = uri
message = soap.NewMessage()
defaultHeaders(message, params).Action("http://schemas.xmlsoap.org/ws/2004/09/transfer/Create").ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").AddOption(soap.NewHeaderOption("WINRS_NOPROFILE", "FALSE")).AddOption(soap.NewHeaderOption("WINRS_CODEPAGE", "437")).Build()
body := message.CreateBodyElement("Shell", soap.NS_WIN_SHELL)
input := message.CreateElement(body, "InputStreams", soap.NS_WIN_SHELL)
input.SetContent("stdin")
output := message.CreateElement(body, "OutputStreams", soap.NS_WIN_SHELL)
output.SetContent("stdout stderr")
return
}
func NewDeleteShellRequest(uri string, shellId string, params *Parameters) (message *soap.SoapMessage) {
if params == nil {
params = DefaultParameters()
}
params.url = uri
message = soap.NewMessage()
defaultHeaders(message, params).Action("http://schemas.xmlsoap.org/ws/2004/09/transfer/Delete").ShellId(shellId).ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").Build()
message.NewBody()
return
}
func NewExecuteCommandRequest(uri string, shellId string, command string, params *Parameters) (message *soap.SoapMessage) {
if params == nil {
params = DefaultParameters()
}
params.url = uri
message = soap.NewMessage()
defaultHeaders(message, params).Action("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command").ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").ShellId(shellId).AddOption(soap.NewHeaderOption("WINRS_CONSOLEMODE_STDIN", "TRUE")).AddOption(soap.NewHeaderOption("WINRS_SKIP_CMD_SHELL", "FALSE")).Build()
// ensure special characters like & don't mangle the request XML
command = "<![CDATA[" + command + "]]>"
body := message.CreateBodyElement("CommandLine", soap.NS_WIN_SHELL)
commandElement := message.CreateElement(body, "Command", soap.NS_WIN_SHELL)
commandElement.SetContent(command)
return
}
func NewGetOutputRequest(uri string, shellId string, commandId string, streams string, params *Parameters) (message *soap.SoapMessage) {
if params == nil {
params = DefaultParameters()
}
params.url = uri
message = soap.NewMessage()
defaultHeaders(message, params).Action("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive").ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").ShellId(shellId).Build()
receive := message.CreateBodyElement("Receive", soap.NS_WIN_SHELL)
desiredStreams := message.CreateElement(receive, "DesiredStream", soap.NS_WIN_SHELL)
desiredStreams.SetAttr("CommandId", commandId)
desiredStreams.SetContent(streams)
return
}
func NewSendInputRequest(uri string, shellId string, commandId string, input []byte, params *Parameters) (message *soap.SoapMessage) {
if params == nil {
params = DefaultParameters()
}
params.url = uri
message = soap.NewMessage()
defaultHeaders(message, params).Action("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Send").ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").ShellId(shellId).Build()
content := base64.StdEncoding.EncodeToString(input)
send := message.CreateBodyElement("Send", soap.NS_WIN_SHELL)
streams := message.CreateElement(send, "Stream", soap.NS_WIN_SHELL)
streams.SetAttr("Name", "stdin")
streams.SetAttr("CommandId", commandId)
streams.SetContent(content)
return
}
func NewSignalRequest(uri string, shellId string, commandId string, params *Parameters) (message *soap.SoapMessage) {
if params == nil {
params = DefaultParameters()
}
params.url = uri
message = soap.NewMessage()
defaultHeaders(message, params).Action("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Signal").ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").ShellId(shellId).Build()
signal := message.CreateBodyElement("Signal", soap.NS_WIN_SHELL)
signal.SetAttr("CommandId", commandId)
code := message.CreateElement(signal, "Code", soap.NS_WIN_SHELL)
code.SetContent("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/signal/terminate")
return
}