forked from adamfisk/LittleProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MitmProxyTest.java
170 lines (153 loc) · 7.49 KB
/
MitmProxyTest.java
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
package org.littleshoot.proxy;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import org.littleshoot.proxy.extras.SelfSignedMitmManager;
import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
/**
* Tests just a single basic proxy running as a man in the middle.
*/
public class MitmProxyTest extends BaseProxyTest {
private Set<HttpMethod> requestPreMethodsSeen = new HashSet<HttpMethod>();
private Set<HttpMethod> requestPostMethodsSeen = new HashSet<HttpMethod>();
private StringBuilder responsePreBody = new StringBuilder();
private StringBuilder responsePostBody = new StringBuilder();
private Set<HttpMethod> responsePreOriginalRequestMethodsSeen = new HashSet<HttpMethod>();
private Set<HttpMethod> responsePostOriginalRequestMethodsSeen = new HashSet<HttpMethod>();
@Override
protected void setUp() {
this.proxyServer = bootstrapProxy()
.withPort(0)
// Include a ChainedProxyManager to make sure that MITM setting
// overrides this
.withChainProxyManager(new ChainedProxyManager() {
@Override
public void lookupChainedProxies(HttpRequest httpRequest,
Queue<ChainedProxy> chainedProxies) {
}
})
.withManInTheMiddle(new SelfSignedMitmManager())
.withFiltersSource(new HttpFiltersSourceAdapter() {
@Override
public HttpFilters filterRequest(HttpRequest originalRequest) {
return new HttpFiltersAdapter(originalRequest) {
@Override
public HttpResponse clientToProxyRequest(
HttpObject httpObject) {
if (httpObject instanceof HttpRequest) {
requestPreMethodsSeen
.add(((HttpRequest) httpObject)
.getMethod());
}
return null;
}
@Override
public HttpResponse proxyToServerRequest(
HttpObject httpObject) {
if (httpObject instanceof HttpRequest) {
requestPostMethodsSeen
.add(((HttpRequest) httpObject)
.getMethod());
}
return null;
}
@Override
public HttpObject serverToProxyResponse(
HttpObject httpObject) {
if (httpObject instanceof HttpResponse) {
responsePreOriginalRequestMethodsSeen
.add(originalRequest.getMethod());
} else if (httpObject instanceof HttpContent) {
responsePreBody.append(((HttpContent) httpObject)
.content().toString(
Charset.forName("UTF-8")));
}
return httpObject;
}
@Override
public HttpObject proxyToClientResponse(
HttpObject httpObject) {
if (httpObject instanceof HttpResponse) {
responsePostOriginalRequestMethodsSeen
.add(originalRequest.getMethod());
} else if (httpObject instanceof HttpContent) {
responsePostBody.append(((HttpContent) httpObject)
.content().toString(
Charset.forName("UTF-8")));
}
return httpObject;
}
};
}
})
.start();
}
@Override
protected boolean isMITM() {
return true;
}
@Override
public void testSimpleGetRequest() throws Exception {
super.testSimpleGetRequest();
assertMethodSeenInRequestFilters(HttpMethod.GET);
assertMethodSeenInResponseFilters(HttpMethod.GET);
assertResponseFromFiltersMatchesActualResponse();
}
@Override
public void testSimpleGetRequestOverHTTPS() throws Exception {
super.testSimpleGetRequestOverHTTPS();
assertMethodSeenInRequestFilters(HttpMethod.CONNECT);
assertMethodSeenInRequestFilters(HttpMethod.GET);
assertMethodSeenInResponseFilters(HttpMethod.GET);
assertResponseFromFiltersMatchesActualResponse();
}
@Override
public void testSimplePostRequest() throws Exception {
super.testSimplePostRequest();
assertMethodSeenInRequestFilters(HttpMethod.POST);
assertMethodSeenInResponseFilters(HttpMethod.POST);
assertResponseFromFiltersMatchesActualResponse();
}
@Override
public void testSimplePostRequestOverHTTPS() throws Exception {
super.testSimplePostRequestOverHTTPS();
assertMethodSeenInRequestFilters(HttpMethod.CONNECT);
assertMethodSeenInRequestFilters(HttpMethod.POST);
assertMethodSeenInResponseFilters(HttpMethod.POST);
assertResponseFromFiltersMatchesActualResponse();
}
private void assertMethodSeenInRequestFilters(HttpMethod method) {
assertThat(method
+ " should have been seen in clientToProxyRequest filter",
requestPreMethodsSeen, hasItem(method));
assertThat(method
+ " should have been seen in proxyToServerRequest filter",
requestPostMethodsSeen, hasItem(method));
}
private void assertMethodSeenInResponseFilters(HttpMethod method) {
assertThat(
method
+ " should have been seen as the original requests's method in serverToProxyResponse filter",
responsePreOriginalRequestMethodsSeen, hasItem(method));
assertThat(
method
+ " should have been seen as the original requests's method in proxyToClientResponse filter",
responsePostOriginalRequestMethodsSeen, hasItem(method));
}
private void assertResponseFromFiltersMatchesActualResponse() {
assertEquals(
"Data received through HttpFilters.serverToProxyResponse should match response",
lastResponse, responsePreBody.toString());
assertEquals(
"Data received through HttpFilters.proxyToClientResponse should match response",
lastResponse, responsePostBody.toString());
}
}