-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport.go
138 lines (121 loc) · 3.9 KB
/
transport.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
package winrm
import (
"launchpad.net/gwacl/fork/http"
"encoding/xml"
"strings"
"launchpad.net/gwacl/fork/tls"
"fmt"
"bytes"
"errors"
)
type CertificateCredentials struct {
Cert string
Key string
CA string
}
type SoapRequest struct {
Endpoint string
AuthType string
Username string
Passwd string
HttpInsecure bool
CertAuth *CertificateCredentials
HttpClient *http.Client
}
func (conf *SoapRequest) SendMessage(envelope *Envelope) (*http.Response, error){
output, err := xml.MarshalIndent(envelope, " ", " ")
if err != nil{
return nil, err
}
if conf.AuthType == "BasicAuth"{
if conf.Username == "" || conf.Passwd == "" {
// fmt.Errorf("AuthType BasicAuth needs Username and Passwd")
return nil, errors.New("AuthType BasicAuth needs Username and Passwd")
}
return conf.HttpBasicAuth(output)
}else if conf.AuthType == "CertAuth" {
return conf.HttpCertAuth(output)
}
return nil, errors.New(fmt.Sprintf("Invalid transport: %s", conf.AuthType))
}
func (conf *SoapRequest) GetHttpHeader () (map[string]string) {
header := make(map[string]string)
header["Content-Type"] = "application/soap+xml;charset=UTF-8"
header["User-Agent"] = "Go WinRM client"
return header
}
func (conf *SoapRequest) HttpCertAuth(data []byte) (*http.Response, error) {
protocol := strings.Split(conf.Endpoint, ":")
if protocol[0] != "http" && protocol[0] != "https"{
return nil, errors.New("Invalid protocol. Expected http or https")
}
header := conf.GetHttpHeader()
header["Authorization"] = "http://schemas.dmtf.org/wbem/wsman/1/wsman/secprofile/https/mutual"
if conf.HttpClient == nil{
conf.HttpClient = &http.Client{}
}
if protocol[0] != "https" {
return nil, errors.New("Ivalid protocol for this transport type")
}
cert, err := tls.LoadX509KeyPair(conf.CertAuth.Cert, conf.CertAuth.Key)
if err != nil {
return nil, err
}
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
Certificates: []tls.Certificate{
cert,
},
}
tr := &http.Transport{
TLSClientConfig: tlsConfig,
}
conf.HttpClient.Transport = tr
body := bytes.NewBuffer(data)
req, err := http.NewRequest("POST", conf.Endpoint, body)
req.ContentLength = int64(len(data))
for k, v := range header {
req.Header.Add(k, v)
}
resp, err := conf.HttpClient.Do(req)
if err != nil{
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("Remote host returned error status code: %d", resp.StatusCode))
}
//fmt.Printf("%v\n%v\n", resp, err)
return resp, err
}
func (conf *SoapRequest) HttpBasicAuth (data []byte) (*http.Response, error){
protocol := strings.Split(conf.Endpoint, ":")
if protocol[0] != "http" && protocol[0] != "https"{
return nil, errors.New("Invalid protocol. Expected http or https")
}
header := conf.GetHttpHeader()
if conf.HttpClient == nil{
conf.HttpClient = &http.Client{}
}
// Ignore SSL certificate errors
if protocol[0] == "https" {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: conf.HttpInsecure},
}
conf.HttpClient.Transport = tr
}
body := bytes.NewBuffer(data)
req, err := http.NewRequest("POST", conf.Endpoint, body)
req.ContentLength = int64(len(data))
req.SetBasicAuth(conf.Username, conf.Passwd)
for k, v := range header {
req.Header.Add(k, v)
}
resp, err := conf.HttpClient.Do(req)
if err != nil{
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("Remote host returned error status code: %d", resp.StatusCode))
}
return resp, err
}