Skip to content

Commit

Permalink
added addon for pre-configured docker proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabhg71 committed Apr 3, 2023
1 parent 5c4c665 commit b2cfd6f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
64 changes: 64 additions & 0 deletions mitmproxy/addons/docker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import logging
import shutil
import subprocess
import tempfile
import json
import os
from mitmproxy import command
from mitmproxy import ctx
from mitmproxy.log import ALERT


class Docker:

@command.command("docker.enable")
def enable(self) -> None:
# def start(self) -> None:
"""
Enable docker proxy to started [Make sure to run `docker.disable` when done]
Rebuild container after enabling this option
"""
if not shutil.which("docker"):
logging.log(
ALERT, "Docker not installed or not avaialble in path"
)
return

config = {}
config_path = os.path.join(os.path.expanduser('~'),".docker","config.json")
with open(config_path,"r") as file:
config = json.load(file)
config['proxies'] = {
"default" : {
"httpProxy": 'http://host.docker.internal:{}/'.format(ctx.options.listen_port or "8080"),
"httpsProxy": 'https://host.docker.internal:{}/'.format(ctx.options.listen_port or "8080"),
}
}
logging.debug(config)

with open(config_path,"w") as file:
json.dump(config,file)

logging.log(ALERT,"Enabled proxy [updated:~/.docker/config.json]")

@command.command("docker.disable")
def disable(self) -> None:
# def start(self) -> None:
"""
Disable docker proxy [Rebuild container after disabling this option]
"""
if not shutil.which("docker"):
logging.log(
ALERT, "Docker not installed or not avaialble in path"
)
return

config = {}
config_path = os.path.join(os.path.expanduser('~'),".docker","config.json")
with open(config_path,"r") as file:
config = json.load(file)
config['proxies'] = {}
logging.debug(config)

with open(config_path,"w") as file:
json.dump(config,file)
7 changes: 7 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import subprocess
from shutil import which

l = subprocess.Popen(["docker","run","mitm"])

if (which("docker")):
print(l)

0 comments on commit b2cfd6f

Please sign in to comment.