diff options
Diffstat (limited to 'internal/ipc/server.go')
| -rw-r--r-- | internal/ipc/server.go | 54 |
1 files changed, 39 insertions, 15 deletions
diff --git a/internal/ipc/server.go b/internal/ipc/server.go index 11ba033..e3b2a29 100644 --- a/internal/ipc/server.go +++ b/internal/ipc/server.go @@ -1,6 +1,7 @@ package ipc import ( + "context" "encoding/json" "log" "net" @@ -9,6 +10,12 @@ import ( "mitmux/internal/store" ) +// Repeater sends raw bytes to scheme://host exactly as given and +// records the exchange to history - implemented by *proxy.Server. +type Repeater interface { + Repeat(ctx context.Context, scheme, host string, raw []byte) (*store.Entry, error) +} + // Hub fans out newly captured history entries to subscribed clients. type Hub struct { mu sync.Mutex @@ -50,14 +57,15 @@ func (h *Hub) unsubscribe(ch chan store.Summary) { // Server serves the daemon side of the mitmux control protocol. type Server struct { - db *store.Store - hub *Hub + db *store.Store + hub *Hub + repeater Repeater } // NewServer creates a control-protocol Server backed by db, broadcasting -// through hub. -func NewServer(db *store.Store, hub *Hub) *Server { - return &Server{db: db, hub: hub} +// through hub and sending Repeater requests through rep. +func NewServer(db *store.Store, hub *Hub, rep Repeater) *Server { + return &Server{db: db, hub: hub, repeater: rep} } // Serve accepts connections on ln until it returns an error (e.g. the @@ -98,16 +106,19 @@ func (s *Server) handleConn(conn net.Conn) { enc.Encode(Response{Type: "error", Error: err.Error()}) continue } - enc.Encode(Response{Type: "get", Detail: &EntryDetail{ - Summary: store.Summary{ - ID: e.ID, StartedAt: e.StartedAt, Duration: e.Duration, - Method: e.Method, Scheme: e.Scheme, Host: e.Host, Path: e.Path, - StatusCode: e.StatusCode, ReqSize: len(e.RequestRaw), RespSize: len(e.ResponseRaw), - Error: e.Error, - }, - RequestRaw: e.RequestRaw, ResponseRaw: e.ResponseRaw, - RequestExact: e.RequestExact, ResponseExact: e.ResponseExact, - }}) + enc.Encode(Response{Type: "get", Detail: detailFromEntry(e)}) + + case "repeat": + if s.repeater == nil { + enc.Encode(Response{Type: "error", Error: "repeater not available"}) + continue + } + e, err := s.repeater.Repeat(context.Background(), req.Scheme, req.Host, req.Raw) + if err != nil { + enc.Encode(Response{Type: "error", Error: err.Error()}) + continue + } + enc.Encode(Response{Type: "repeat", Detail: detailFromEntry(e)}) case "subscribe": sub := s.hub.subscribe() @@ -126,6 +137,19 @@ func (s *Server) handleConn(conn net.Conn) { } } +func detailFromEntry(e *store.Entry) *EntryDetail { + return &EntryDetail{ + Summary: store.Summary{ + ID: e.ID, StartedAt: e.StartedAt, Duration: e.Duration, + Method: e.Method, Scheme: e.Scheme, Host: e.Host, Path: e.Path, + StatusCode: e.StatusCode, ReqSize: len(e.RequestRaw), RespSize: len(e.ResponseRaw), + Error: e.Error, Source: e.Source, + }, + RequestRaw: e.RequestRaw, ResponseRaw: e.ResponseRaw, + RequestExact: e.RequestExact, ResponseExact: e.ResponseExact, + } +} + // LogAndBroadcast is a convenience OnEntry callback: logs the entry and // broadcasts it through hub. func LogAndBroadcast(hub *Hub) func(store.Summary) { |