srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/internal/ipc
diff options
context:
space:
mode:
Diffstat (limited to 'internal/ipc')
-rw-r--r--internal/ipc/ipc.go28
-rw-r--r--internal/ipc/server.go54
2 files changed, 64 insertions, 18 deletions
diff --git a/internal/ipc/ipc.go b/internal/ipc/ipc.go
index c9d92b4..b1a997a 100644
--- a/internal/ipc/ipc.go
+++ b/internal/ipc/ipc.go
@@ -15,17 +15,22 @@ import (
// Request is sent by a client to the daemon.
type Request struct {
- Type string `json:"type"` // "list", "get", or "subscribe"
+ Type string `json:"type"` // "list", "get", "subscribe", or "repeat"
Limit int `json:"limit,omitempty"`
BeforeID int64 `json:"before_id,omitempty"`
ID int64 `json:"id,omitempty"`
+
+ // For "repeat": send Raw to scheme://host exactly as given.
+ Scheme string `json:"scheme,omitempty"`
+ Host string `json:"host,omitempty"`
+ Raw []byte `json:"raw,omitempty"`
}
// Response is sent by the daemon to a client.
type Response struct {
- Type string `json:"type"` // "list", "get", "new", or "error"
+ Type string `json:"type"` // "list", "get", "new", "repeat", or "error"
Entries []store.Summary `json:"entries,omitempty"` // for "list"
- Detail *EntryDetail `json:"detail,omitempty"` // for "get"
+ Detail *EntryDetail `json:"detail,omitempty"` // for "get" and "repeat"
New *store.Summary `json:"new,omitempty"` // for "new" (subscribe push)
Error string `json:"error,omitempty"`
}
@@ -92,6 +97,23 @@ func (c *Client) Get(id int64) (*EntryDetail, error) {
return resp.Detail, nil
}
+// Repeat sends raw to scheme://host exactly as given (no re-serialization,
+// no header injection) and returns the resulting entry, including the raw
+// response bytes. The exchange is also recorded to history.
+func (c *Client) Repeat(scheme, host string, raw []byte) (*EntryDetail, error) {
+ if err := c.enc.Encode(Request{Type: "repeat", Scheme: scheme, Host: host, Raw: raw}); err != nil {
+ return nil, err
+ }
+ var resp Response
+ if err := c.dec.Decode(&resp); err != nil {
+ return nil, err
+ }
+ if resp.Type == "error" {
+ return nil, errors.New(resp.Error)
+ }
+ return resp.Detail, nil
+}
+
// Subscribe opens a dedicated connection that streams newly captured
// history entries as they happen. The returned channel is closed when
// the connection ends; call the returned close func to stop early.
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) {