Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit 26714e2

Browse files
authored
Mitmdump binary management (#357)
1 parent 5ecf73d commit 26714e2

File tree

2 files changed

+84
-18
lines changed

2 files changed

+84
-18
lines changed

browserup-proxy-core/src/main/java/com/browserup/bup/mitmproxy/MitmProxyProcessManager.java

+47-18
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
import com.browserup.bup.mitmproxy.addons.*;
44
import com.browserup.bup.mitmproxy.management.*;
5+
import org.apache.commons.lang3.SystemUtils;
56
import org.awaitility.Awaitility;
67
import org.awaitility.core.ConditionTimeoutException;
8+
import org.jetbrains.annotations.NotNull;
79
import org.slf4j.Logger;
810
import org.slf4j.LoggerFactory;
911
import org.zeroturnaround.exec.ProcessExecutor;
1012
import org.zeroturnaround.exec.StartedProcess;
1113
import org.zeroturnaround.exec.stream.LogOutputStream;
1214
import org.zeroturnaround.exec.stream.slf4j.Slf4jStream;
1315

14-
import java.io.IOException;
15-
import java.io.PipedInputStream;
16-
import java.io.PipedOutputStream;
1716
import java.net.BindException;
1817
import java.net.InetSocketAddress;
1918
import java.util.ArrayList;
@@ -23,6 +22,9 @@
2322

2423
public class MitmProxyProcessManager {
2524
private static final Logger LOGGER = LoggerFactory.getLogger(MitmProxyProcessManager.class);
25+
private static final String MITMPROXY_BINARY_PATH_PROPERTY = "MITMPROXY_BINARY_PATH";
26+
private static final String MITMPROXY_HOME_PATH = System.getProperty("user.home") + "/.browserup-mitmproxy";
27+
private static final String MITMPROXY_DEFAULT_BINARY_PATH = MITMPROXY_HOME_PATH + "/" + getMitmproxyBinaryFileName();
2628

2729
public enum MitmProxyLoggingLevel {
2830
error,
@@ -66,6 +68,10 @@ public enum MitmProxyLoggingLevel {
6668

6769
private StringBuilder proxyLog = new StringBuilder();
6870

71+
private static String getMitmproxyBinaryFileName() {
72+
return SystemUtils.IS_OS_WINDOWS ? "mitmdump.exe" : "mitmdump";
73+
}
74+
6975
public void start(int port) {
7076
start(port == 0 ? NetworkUtils.getFreePort() : port, defaultAddons());
7177
}
@@ -170,20 +176,7 @@ private void startProxyWithRetries(int port, List<AbstractAddon> addons, int ret
170176
}
171177

172178
private void startProxy(int port, List<AbstractAddon> addons) {
173-
List<String> command = new ArrayList<String>() {{
174-
add("mitmdump");
175-
add("-p");
176-
add(String.valueOf(port));
177-
add("--set");
178-
add("flow_detail=3");
179-
}};
180-
if (trustAll) {
181-
command.add("--ssl-insecure");
182-
}
183-
184-
updateCommandWithUpstreamProxy(command);
185-
updateCommandWithLogLevel(command);
186-
updateCommandWithAddOns(addons, command);
179+
List<String> command = createCommand(port, addons);
187180

188181
LOGGER.info("Starting proxy using command: " + String.join(" ", command));
189182

@@ -193,6 +186,10 @@ private void startProxy(int port, List<AbstractAddon> addons) {
193186
} catch (Exception ex) {
194187
throw new RuntimeException("Couldn't start mitmproxy process", ex);
195188
}
189+
waitForReady();
190+
}
191+
192+
private void waitForReady() {
196193
try {
197194
Awaitility.await()
198195
.atMost(5, TimeUnit.SECONDS)
@@ -202,6 +199,33 @@ private void startProxy(int port, List<AbstractAddon> addons) {
202199
}
203200
}
204201

202+
@NotNull
203+
private ArrayList<String> createCommand(int port, List<AbstractAddon> addons) {
204+
ArrayList<String> command = new ArrayList<String>() {{
205+
add(getMitmproxyBinaryPath());
206+
add("-p");
207+
add(String.valueOf(port));
208+
add("--set");
209+
add("confdir=" + MITMPROXY_HOME_PATH);
210+
}};
211+
if (trustAll) {
212+
command.add("--ssl-insecure");
213+
}
214+
215+
updateCommandWithUpstreamProxy(command);
216+
updateCommandWithLogLevel(command);
217+
updateCommandWithAddOns(addons, command);
218+
return command;
219+
}
220+
221+
private String getMitmproxyBinaryPath() {
222+
String mitmproxyBinaryPathProperty = System.getProperty(MITMPROXY_BINARY_PATH_PROPERTY);
223+
if (mitmproxyBinaryPathProperty != null) {
224+
return mitmproxyBinaryPathProperty + "/" + getMitmproxyBinaryFileName();
225+
}
226+
return MITMPROXY_DEFAULT_BINARY_PATH;
227+
}
228+
205229
private void handleHealthCheckFailure() {
206230
LOGGER.error("MitmProxy might not started properly, healthcheck failed for port: " + this.proxyPort);
207231
if (startedProcess == null) return;
@@ -251,8 +275,13 @@ private void updateCommandWithAddOns(List<AbstractAddon> addons, List<String> co
251275
}
252276

253277
private void updateCommandWithLogLevel(List<String> command) {
278+
MitmProxyLoggingLevel logLevel = getMitmProxyLoggingLevel();
254279
command.add("--set");
255-
command.add("termlog_verbosity=" + getMitmProxyLoggingLevel());
280+
command.add("termlog_verbosity=" + logLevel);
281+
if (logLevel.equals(MitmProxyLoggingLevel.debug)) {
282+
command.add("--set");
283+
command.add("flow_detail=3");
284+
}
256285
}
257286

258287
private void updateCommandWithUpstreamProxy(List<String> command) {

browserup-proxy-dist/build.gradle

+37
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ plugins {
66
id 'application'
77
}
88

9+
application {
10+
applicationDefaultJvmArgs = ['-DMITMPROXY_BINARY_PATH=mitmproxy/']
11+
}
12+
913
uploadArchives {
1014
repositories {
1115
mavenDeployer {
@@ -83,6 +87,36 @@ dependencies {
8387
applicationName = 'browserup-proxy'
8488
mainClassName = 'com.browserup.bup.proxy.Main'
8589

90+
def mitmproxyLinuxURL = 'https://mitmproxy-linux.s3.us-east-2.amazonaws.com/mitmdump'.toURL()
91+
def mitmproxyWindowsURL = 'https://mitmproxy-windows.s3.us-east-2.amazonaws.com/mitmdump.exe'.toURL()
92+
93+
static void downloadFile(URL srcUrl, File dstFile) {
94+
dstFile.withOutputStream { out ->
95+
srcUrl.withInputStream { from ->
96+
out << from
97+
}
98+
}
99+
}
100+
101+
task downloadMitmproxyBinaries {
102+
File mitmproxyDir = file("$buildDir/mitmproxy")
103+
outputs.dir mitmproxyDir
104+
doLast {
105+
mitmproxyDir.mkdirs()
106+
println("Downloading mitmproxy dependencies...")
107+
def linuxBinaryFile = file("$buildDir/mitmproxy/mitmdump")
108+
def windowsBinaryFile = file("$buildDir/mitmproxy/mitmdump.exe")
109+
downloadFile(mitmproxyLinuxURL, linuxBinaryFile)
110+
downloadFile(mitmproxyWindowsURL, windowsBinaryFile)
111+
project.exec {
112+
commandLine('chmod', '+x', linuxBinaryFile.absolutePath)
113+
commandLine('chmod', '+x', windowsBinaryFile.absolutePath)
114+
}
115+
println("Downloaded mitmproxy dependencies.")
116+
mitmproxyDir
117+
}
118+
}
119+
86120
distributions {
87121
main {
88122
baseName = 'browserup-proxy'
@@ -92,6 +126,9 @@ distributions {
92126
into ('ssl') {
93127
from '../browserup-proxy-core/src/main/resources/sslSupport'
94128
}
129+
from(downloadMitmproxyBinaries) {
130+
into 'bin/mitmproxy'
131+
}
95132
}
96133
}
97134
}

0 commit comments

Comments
 (0)