Skip to content

Commit

Permalink
Basic proof of concept for mitm addons
Browse files Browse the repository at this point in the history
  • Loading branch information
kegsay committed Nov 30, 2023
1 parent 2cd3642 commit 955b514
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
js-sdk/node_modules
js-sdk/dist
internal/api/dist
__pycache__
16 changes: 14 additions & 2 deletions internal/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/url"
"os"
"os/exec"
"path/filepath"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -88,7 +89,15 @@ func RunNewDeployment(t *testing.T, shouldTCPDump bool) *SlidingSyncDeployment {
deployment := complement.Deploy(t, 2)
networkName := deployment.Network()

// make a reverse proxy.
workingDir, err := os.Getwd()
if err != nil {
t.Fatalf("failed to find working directory: %s", err)
}

// Make the mitmproxy and hardcode CONTAINER PORTS for hs1/hs2. HOST PORTS are still dynamically allocated.
// By running this container on the same network as the homeservers, we can leverage DNS hence hs1/hs2 URLs.
// We also need to preload addons into the proxy, so we bind mount the addons directory. This also allows
// test authors to easily add custom addons.
hs1ExposedPort := "3000/tcp"
hs2ExposedPort := "3001/tcp"
mitmproxyContainer, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{
Expand All @@ -97,13 +106,16 @@ func RunNewDeployment(t *testing.T, shouldTCPDump bool) *SlidingSyncDeployment {
ExposedPorts: []string{hs1ExposedPort, hs2ExposedPort},
Env: map[string]string{},
Cmd: []string{
"mitmdump", "--mode", "reverse:http://hs1:8008@3000", "--mode", "reverse:http://hs2:8008@3001",
"mitmdump", "--mode", "reverse:http://hs1:8008@3000", "--mode", "reverse:http://hs2:8008@3001", "-s", "/addons/__init__.py",
},
// WaitingFor: wait.ForLog("listening"),
Networks: []string{networkName},
NetworkAliases: map[string][]string{
networkName: {"mitmproxy"},
},
Mounts: testcontainers.Mounts(
testcontainers.BindMount(filepath.Join(workingDir, "addons"), "/addons"),
),
},
Started: true,
})
Expand Down
4 changes: 4 additions & 0 deletions tests/addons/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from add_header import AddHeader


addons = [AddHeader()]
9 changes: 9 additions & 0 deletions tests/addons/add_header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddHeader:
def __init__(self):
self.num = 0

def response(self, flow):
self.num = self.num + 1
print("got response num", self.num)
flow.response.headers["count"] = str(self.num)

0 comments on commit 955b514

Please sign in to comment.