-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction_proxy_test.go
206 lines (187 loc) · 5.23 KB
/
transaction_proxy_test.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package msmtpd
import (
"fmt"
"net"
"net/smtp"
"strings"
"testing"
"github.com/vodolaz095/msmtpd/internal"
)
func TestProxyNotEnabled(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{
EnableProxyProtocol: false, // important
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
where := strings.Split(addr, ":")
err = internal.DoCommand(c.Text, 550, "PROXY TCP4 8.8.8.8 %s 443 %s", where[0], where[1])
if err != nil {
t.Errorf("sending proxy command enabled from the box - %s", err)
}
err = c.Hello("nobody.example.org")
if err != nil {
t.Errorf("sending helo command failed with %s", err)
}
err = c.Quit()
if err != nil {
t.Errorf("sending quit command failed with %s", err)
}
}
func TestProxyEnabledSuccess(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{
EnableProxyProtocol: true, // important
ConnectionCheckers: []ConnectionChecker{
func(tr *Transaction) error {
tr.LogInfo("Remote address for connection checker is %s", tr.Addr)
tr.SetFact("originalRemote", tr.Addr.String())
return nil
},
},
HeloCheckers: []HelloChecker{
func(tr *Transaction) error {
tr.LogInfo("Remote address for HELO is %s", tr.Addr)
if tr.Addr.String() != "8.8.8.8:443" {
t.Errorf("remote address is not set properly %s", tr.Addr)
}
original, found := tr.GetFact("originalRemote")
if !found {
t.Errorf("original address is not set")
}
if original == tr.Addr.String() {
t.Errorf("address not changed")
}
return nil
},
},
})
defer closer()
con, err := net.Dial("tcp", addr)
if err != nil {
t.Errorf("%s: error dialing %s", err, addr)
}
where := strings.Split(addr, ":")
_, err = fmt.Fprintf(con, "PROXY TCP4 8.8.8.8 %s 443 %s\r\n", where[0], where[1])
if err != nil {
t.Errorf("%s : while sending proxy command", err)
return
}
c, err := smtp.NewClient(con, "localhost")
if err != nil {
t.Errorf("Dial failed: %v", err)
}
err = c.Hello("nobody.example.org")
if err != nil {
t.Errorf("sending helo command failed with %s", err)
}
err = c.Quit()
if err != nil {
t.Errorf("sending quit command failed with %s", err)
}
}
func TestProxyEnabledMalformedProtocol(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{
EnableProxyProtocol: true, // important
})
defer closer()
con, err := net.Dial("tcp", addr)
if err != nil {
t.Errorf("%s: error dialing %s", err, addr)
}
where := strings.Split(addr, ":")
_, err = fmt.Fprintf(con, "PROXY UDP 8.8.8.8 %s port %s\r\n", where[0], where[1])
if err != nil {
t.Errorf("%s : while sending proxy command", err)
return
}
_, err = smtp.NewClient(con, "localhost")
if err != nil {
if err.Error() == "502 unable to decode proxy protocol - only TCP4/TCP6 is supported" {
t.Logf("proxy command failed with malformed protocol")
} else {
t.Errorf("%s : unexpected error", err)
}
} else {
t.Errorf("proxy error not thrown for malformed protocol")
}
}
func TestProxyEnabledMalformedPort(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{
EnableProxyProtocol: true, // important
})
defer closer()
con, err := net.Dial("tcp", addr)
if err != nil {
t.Errorf("%s: error dialing %s", err, addr)
}
where := strings.Split(addr, ":")
_, err = fmt.Fprintf(con, "PROXY TCP4 8.8.8.8 %s port %s\r\n", where[0], where[1])
if err != nil {
t.Errorf("%s : while sending proxy command", err)
return
}
_, err = smtp.NewClient(con, "localhost")
if err != nil {
if err.Error() == "502 malformed port in proxy command" {
t.Logf("proxy command failed with malformed port")
} else {
t.Errorf("%s : unexpected error", err)
}
} else {
t.Errorf("proxy error not thrown for malformed port")
}
}
func TestProxyEnabledMalformedAddress(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{
EnableProxyProtocol: true, // important
})
defer closer()
con, err := net.Dial("tcp", addr)
if err != nil {
t.Errorf("%s: error dialing %s", err, addr)
}
where := strings.Split(addr, ":")
_, err = fmt.Fprintf(con, "PROXY TCP4 339.69.72.11 %s 443 %s\r\n", where[0], where[1])
if err != nil {
t.Errorf("%s : while sending proxy command", err)
return
}
_, err = smtp.NewClient(con, "localhost")
if err != nil {
if err.Error() == "502 malformed network address" {
t.Logf("proxy command failed with malformed address")
} else {
t.Errorf("%s : unexpected error", err)
}
} else {
t.Errorf("proxy error not thrown for malformed address")
}
}
func TestProxyEnabledMalformedManyArguments(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{
EnableProxyProtocol: true, // important
})
defer closer()
con, err := net.Dial("tcp", addr)
if err != nil {
t.Errorf("%s: error dialing %s", err, addr)
}
where := strings.Split(addr, ":")
_, err = fmt.Fprintf(con, "PROXY TCP4 %s %s\r\n", where[0], where[1])
if err != nil {
t.Errorf("%s : while sending proxy command", err)
return
}
_, err = smtp.NewClient(con, "localhost")
if err != nil {
if err.Error() == "502 malformed proxy command" {
t.Logf("proxy command failed with malformed address")
} else {
t.Errorf("%s : unexpected error", err)
}
} else {
t.Errorf("proxy error not thrown for malformed address")
}
}