Skip to content

Commit

Permalink
neaten up a little. pass in the interceptor function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonahss committed Apr 11, 2019
1 parent 987fda7 commit 87aabf4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
8 changes: 2 additions & 6 deletions src/main/java/MitmproxyJava.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ public class MitmproxyJava {
private MitmproxyServer server;
public static final int WEBSOCKET_PORT = 8765;

public MitmproxyJava(Function<InterceptedMessage, Boolean> messageInterceptor) throws URISyntaxException {
// InterceptedMessage message = new InterceptedMessage();
// message.message = "hi from message";
// messageInterceptor.apply(message);

server = new MitmproxyServer(new InetSocketAddress("localhost", WEBSOCKET_PORT));
public MitmproxyJava(Function<InterceptedMessage, InterceptedMessage> messageInterceptor) throws URISyntaxException {
server = new MitmproxyServer(new InetSocketAddress("localhost", WEBSOCKET_PORT), messageInterceptor);
server.run();
System.out.println("you started me");
}
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/MitmproxyServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.function.Function;

public class MitmproxyServer extends WebSocketServer {

public MitmproxyServer(InetSocketAddress address) {
private Function<InterceptedMessage, InterceptedMessage> interceptor;

public MitmproxyServer(InetSocketAddress address, Function<InterceptedMessage, InterceptedMessage> interceptor) {
super(address);
this.interceptor = interceptor;
}

@Override
Expand All @@ -35,15 +39,26 @@ public void onMessage(WebSocket conn, String message) {
public void onMessage( WebSocket conn, ByteBuffer message ) {
System.out.println("received ByteBuffer from " + conn.getRemoteSocketAddress());
InterceptedMessage intercepted = null;
InterceptedMessage modifiedMessage = null;

try {
intercepted = new InterceptedMessage(message);
} catch (IOException e) {
System.out.println("Could not parse message");
e.printStackTrace();
}
System.out.println(intercepted.requestURL.toString());

modifiedMessage = interceptor.apply(intercepted);

// if the supplied interceptor function does not return a message, assume no changes were intended and just
// complete the request
if (modifiedMessage == null) {
modifiedMessage = intercepted;
}

try {
conn.send(intercepted.serializedResponseToMitmproxy());
conn.send(modifiedMessage.serializedResponseToMitmproxy());
} catch (JsonProcessingException e) {
System.out.println("Could not encode response to mitmproxy");
e.printStackTrace();
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/MitmproxyJavaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ public class MitmproxyJavaTest {
public void ConstructorTest() throws URISyntaxException, IOException, InterruptedException {
MitmproxyJava proxy = new MitmproxyJava((InterceptedMessage m) -> {
System.out.println(m.requestURL.toString());
return new Boolean(true);
return m;
});
System.out.println("advanced in test");
proxy.stop();
}

//todo test not modifying (by not returning)

//todo test modifying a response
}

0 comments on commit 87aabf4

Please sign in to comment.