forked from mhewedy/ews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ews.go
133 lines (113 loc) · 2.71 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
package ews
import (
"bytes"
"crypto/tls"
"fmt"
"github.com/Azure/go-ntlmssp"
"io/ioutil"
"net/http"
"net/http/httputil"
)
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="Exchange2013_SP1" />
</soap:Header>
<soap:Body>
`
soapEnd = `
</soap:Body></soap:Envelope>`
)
type Config struct {
Dump bool
NTLM bool
SkipTLS bool
}
type Client interface {
SendAndReceive(body []byte) ([]byte, error)
GetEWSAddr() string
GetUsername() string
}
type client struct {
EWSAddr 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()
logRequest(c, req)
req.SetBasicAuth(c.Username, c.Password)
req.Header.Set("Content-Type", "text/xml")
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
applyConfig(c.config, client)
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(config *Config, client *http.Client) {
if config.NTLM {
client.Transport = ntlmssp.Negotiator{}
}
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))
}
}