forked from mhewedy/ews
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathews.go
183 lines (159 loc) · 4.06 KB
/
ews.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
package ews
import (
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"strconv"
httpntlm "github.com/vadimi/go-http-ntlm/v2"
)
const (
soapStart = `<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<t:RequestServerVersion Version="Exchange2010_SP2" />
</soap:Header>
<soap:Body>
`
soapEnd = `
</soap:Body></soap:Envelope>`
)
type Config struct {
Dump bool
NTLM bool
SkipTLS bool
Http2 bool
}
// DefaultConfig sets the default configuration
// use http1.1 instead of http2
func GetDefaultConfig() Config {
c := Config{}
c.Dump = true
c.NTLM = true
c.SkipTLS = true
c.Http2 = false
return c
}
type Client interface {
SendAndReceive(body []byte) ([]byte, error)
GetEWSAddr() string
GetUsername() string
}
type client struct {
EWSAddr string
Domain string
Username string
Password string
config *Config
}
func (c *client) GetEWSAddr() string {
return c.EWSAddr
}
func (c *client) GetUsername() string {
return c.Username
}
func NewClient(ewsAddr, username, password string, config *Config) Client {
return &client{
EWSAddr: ewsAddr,
Username: username,
Password: password,
config: config,
}
}
func (c *client) SendAndReceive(body []byte) ([]byte, error) {
bb := []byte(soapStart)
bb = append(bb, body...)
bb = append(bb, soapEnd...)
req, err := http.NewRequest("POST", c.EWSAddr, bytes.NewReader(bb))
if err != nil {
return nil, err
}
defer req.Body.Close()
req.Header.Set("Content-Type", "text/xml; charset=UTF-8")
req.Header.Set("User-Agent", "ExchangeServicesClient/0.0.0.0")
req.Header.Set("Accept", "text/xml")
req.Header.Set("Keep-Alive", "300")
req.Header.Set("Connection", "Keep-Alive")
req.Header.Set("Content-Length", strconv.Itoa(len(bb)))
logRequest(c, req)
req.SetBasicAuth(c.Username, c.Password)
var client *http.Client
if c.config.Http2 {
client = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
} else {
client = &http.Client{
Transport: &http.Transport{
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
}
applyConfig(client, c)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
logResponse(c, resp)
if resp.StatusCode != http.StatusOK {
return nil, NewError(resp)
}
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return respBytes, err
}
func applyConfig(client *http.Client, c *client) {
config := c.config
if config.NTLM {
client.Transport = &httpntlm.NtlmTransport{
Domain: c.Domain,
User: c.Username,
Password: c.Password,
// Configure RoundTripper if necessary, otherwise DefaultTransport is used
RoundTripper: &http.Transport{
// provide tls config
TLSClientConfig: &tls.Config{},
// other properties RoundTripper, see http.DefaultTransport
},
}
}
// if config.Https && config.NTLM {
// xprt := client.Transport
// xprt.TLSNextProto = make(map[string]func(authority string, c *tls.Conn) http.RoundTripper)
// client.Transport = &xprt
// }
if config.SkipTLS {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
}
func logRequest(c *client, req *http.Request) {
if c.config != nil && c.config.Dump {
dump, err := httputil.DumpRequestOut(req, true)
if err != nil {
fmt.Println(err)
}
fmt.Printf("Request:\n%v\n----\n", string(dump))
}
}
func logResponse(c *client, resp *http.Response) {
if c.config != nil && c.config.Dump {
dump, err := httputil.DumpResponse(resp, true)
if err != nil {
fmt.Println(err)
}
fmt.Printf("Response:\n%v\n----\n", string(dump))
}
}