2
2
3
3
import com .browserup .bup .mitmproxy .addons .*;
4
4
import com .browserup .bup .mitmproxy .management .*;
5
+ import org .apache .commons .lang3 .SystemUtils ;
5
6
import org .awaitility .Awaitility ;
6
7
import org .awaitility .core .ConditionTimeoutException ;
8
+ import org .jetbrains .annotations .NotNull ;
7
9
import org .slf4j .Logger ;
8
10
import org .slf4j .LoggerFactory ;
9
11
import org .zeroturnaround .exec .ProcessExecutor ;
10
12
import org .zeroturnaround .exec .StartedProcess ;
11
13
import org .zeroturnaround .exec .stream .LogOutputStream ;
12
14
import org .zeroturnaround .exec .stream .slf4j .Slf4jStream ;
13
15
14
- import java .io .IOException ;
15
- import java .io .PipedInputStream ;
16
- import java .io .PipedOutputStream ;
17
16
import java .net .BindException ;
18
17
import java .net .InetSocketAddress ;
19
18
import java .util .ArrayList ;
23
22
24
23
public class MitmProxyProcessManager {
25
24
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 ();
26
28
27
29
public enum MitmProxyLoggingLevel {
28
30
error ,
@@ -66,6 +68,10 @@ public enum MitmProxyLoggingLevel {
66
68
67
69
private StringBuilder proxyLog = new StringBuilder ();
68
70
71
+ private static String getMitmproxyBinaryFileName () {
72
+ return SystemUtils .IS_OS_WINDOWS ? "mitmdump.exe" : "mitmdump" ;
73
+ }
74
+
69
75
public void start (int port ) {
70
76
start (port == 0 ? NetworkUtils .getFreePort () : port , defaultAddons ());
71
77
}
@@ -170,20 +176,7 @@ private void startProxyWithRetries(int port, List<AbstractAddon> addons, int ret
170
176
}
171
177
172
178
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 );
187
180
188
181
LOGGER .info ("Starting proxy using command: " + String .join (" " , command ));
189
182
@@ -193,6 +186,10 @@ private void startProxy(int port, List<AbstractAddon> addons) {
193
186
} catch (Exception ex ) {
194
187
throw new RuntimeException ("Couldn't start mitmproxy process" , ex );
195
188
}
189
+ waitForReady ();
190
+ }
191
+
192
+ private void waitForReady () {
196
193
try {
197
194
Awaitility .await ()
198
195
.atMost (5 , TimeUnit .SECONDS )
@@ -202,6 +199,33 @@ private void startProxy(int port, List<AbstractAddon> addons) {
202
199
}
203
200
}
204
201
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
+
205
229
private void handleHealthCheckFailure () {
206
230
LOGGER .error ("MitmProxy might not started properly, healthcheck failed for port: " + this .proxyPort );
207
231
if (startedProcess == null ) return ;
@@ -251,8 +275,13 @@ private void updateCommandWithAddOns(List<AbstractAddon> addons, List<String> co
251
275
}
252
276
253
277
private void updateCommandWithLogLevel (List <String > command ) {
278
+ MitmProxyLoggingLevel logLevel = getMitmProxyLoggingLevel ();
254
279
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
+ }
256
285
}
257
286
258
287
private void updateCommandWithUpstreamProxy (List <String > command ) {
0 commit comments