22
33import com .browserup .bup .mitmproxy .addons .*;
44import com .browserup .bup .mitmproxy .management .*;
5+ import org .apache .commons .lang3 .SystemUtils ;
56import org .awaitility .Awaitility ;
67import org .awaitility .core .ConditionTimeoutException ;
8+ import org .jetbrains .annotations .NotNull ;
79import org .slf4j .Logger ;
810import org .slf4j .LoggerFactory ;
911import org .zeroturnaround .exec .ProcessExecutor ;
1012import org .zeroturnaround .exec .StartedProcess ;
1113import org .zeroturnaround .exec .stream .LogOutputStream ;
1214import org .zeroturnaround .exec .stream .slf4j .Slf4jStream ;
1315
14- import java .io .IOException ;
15- import java .io .PipedInputStream ;
16- import java .io .PipedOutputStream ;
1716import java .net .BindException ;
1817import java .net .InetSocketAddress ;
1918import java .util .ArrayList ;
2322
2423public 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 ) {
0 commit comments