srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/internal
diff options
context:
space:
mode:
Diffstat (limited to 'internal')
-rw-r--r--internal/ipc/ipc.go28
-rw-r--r--internal/ipc/server.go54
-rw-r--r--internal/proxy/proxy.go1
-rw-r--r--internal/proxy/repeat.go137
-rw-r--r--internal/store/store.go26
5 files changed, 220 insertions, 26 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) {
diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go
index 0a20382..fcf8c14 100644
--- a/internal/proxy/proxy.go
+++ b/internal/proxy/proxy.go
@@ -345,6 +345,7 @@ func (s *Server) record(started time.Time, duration time.Duration, scheme, host
ReqSize: len(reqRaw),
RespSize: len(respRaw),
Error: errMsg,
+ Source: "proxy",
})
}
}
diff --git a/internal/proxy/repeat.go b/internal/proxy/repeat.go
new file mode 100644
index 0000000..6373ee1
--- /dev/null
+++ b/internal/proxy/repeat.go
@@ -0,0 +1,137 @@
+package proxy
+
+import (
+ "bufio"
+ "bytes"
+ "context"
+ "crypto/tls"
+ "fmt"
+ "io"
+ "net"
+ "net/http"
+ "time"
+
+ "mitmux/internal/store"
+)
+
+// Repeat sends raw exactly as given - no framing correction, no header
+// injection - to scheme://host, and records the exchange to history
+// with Source "repeater". This is the raw-byte send/resend primitive:
+// unlike forward(), which round-trips a parsed *http.Request, Repeat
+// exists specifically so an edited, possibly malformed request (the
+// whole point of a Repeater tool) reaches the wire unmodified.
+//
+// Repeater only speaks HTTP/1.1: raw edited text has no equivalent in
+// HTTP/2's binary framing, so the connection is negotiated HTTP/1.1-only
+// rather than letting the server pick.
+func (s *Server) Repeat(ctx context.Context, scheme, host string, raw []byte) (*store.Entry, error) {
+ started := time.Now()
+ method, path := parseRequestLine(raw)
+
+ conn, err := dialForRepeat(ctx, scheme, host)
+ if err != nil {
+ return s.recordRepeat(started, time.Since(started), scheme, host, method, path, raw, nil, 0, err.Error())
+ }
+ defer conn.Close()
+
+ if _, err := conn.Write(raw); err != nil {
+ return s.recordRepeat(started, time.Since(started), scheme, host, method, path, raw, nil, 0, err.Error())
+ }
+
+ tee := newTeeConn(conn)
+ resp, err := http.ReadResponse(bufio.NewReader(tee), &http.Request{Method: method})
+ duration := time.Since(started)
+ if err != nil {
+ return s.recordRepeat(started, duration, scheme, host, method, path, raw, nil, 0, err.Error())
+ }
+ defer resp.Body.Close()
+ io.Copy(io.Discard, resp.Body)
+
+ return s.recordRepeat(started, duration, scheme, host, method, path, raw, tee.Take(), resp.StatusCode, "")
+}
+
+func (s *Server) recordRepeat(started time.Time, duration time.Duration, scheme, host, method, path string,
+ reqRaw, respRaw []byte, status int, errMsg string) (*store.Entry, error) {
+ e := &store.Entry{
+ StartedAt: started,
+ Duration: duration,
+ Method: method,
+ Scheme: scheme,
+ Host: host,
+ Path: path,
+ StatusCode: status,
+ RequestRaw: reqRaw,
+ ResponseRaw: respRaw,
+ RequestExact: true,
+ ResponseExact: respRaw != nil,
+ Error: errMsg,
+ Source: "repeater",
+ }
+ if s.store != nil {
+ id, err := s.store.Insert(e)
+ if err != nil {
+ return nil, fmt.Errorf("store repeater entry: %w", err)
+ }
+ e.ID = id
+ if s.OnEntry != nil {
+ s.OnEntry(store.Summary{
+ ID: id, StartedAt: e.StartedAt, Duration: e.Duration,
+ Method: e.Method, Scheme: e.Scheme, Host: e.Host, Path: e.Path,
+ StatusCode: e.StatusCode, ReqSize: len(reqRaw), RespSize: len(respRaw),
+ Error: errMsg, Source: e.Source,
+ })
+ }
+ }
+ return e, nil
+}
+
+// dialForRepeat connects to host for scheme, forcing HTTP/1.1 over ALPN
+// when TLS is involved (see Repeat's doc comment for why).
+func dialForRepeat(ctx context.Context, scheme, host string) (net.Conn, error) {
+ nd := &net.Dialer{Timeout: 10 * time.Second}
+ if scheme != "https" {
+ hostPort := host
+ if _, _, err := net.SplitHostPort(host); err != nil {
+ hostPort = net.JoinHostPort(host, "80")
+ }
+ return nd.DialContext(ctx, "tcp", hostPort)
+ }
+
+ hostname, hostPort := host, host
+ if h, _, err := net.SplitHostPort(host); err == nil {
+ hostname = h
+ } else {
+ hostPort = net.JoinHostPort(host, "443")
+ }
+ raw, err := nd.DialContext(ctx, "tcp", hostPort)
+ if err != nil {
+ return nil, err
+ }
+ conn := tls.Client(raw, &tls.Config{ServerName: hostname, NextProtos: []string{"http/1.1"}})
+ if err := conn.HandshakeContext(ctx); err != nil {
+ raw.Close()
+ return nil, err
+ }
+ return conn, nil
+}
+
+// parseRequestLine extracts the method and request-target from the
+// first line of a raw HTTP/1.1 request, without validating or parsing
+// anything else - used only to label the stored entry and to tell
+// http.ReadResponse whether this was a HEAD request (which changes
+// response body framing rules).
+func parseRequestLine(raw []byte) (method, path string) {
+ nl := bytes.IndexByte(raw, '\n')
+ if nl < 0 {
+ nl = len(raw)
+ }
+ line := bytes.TrimRight(raw[:nl], "\r\n")
+ fields := bytes.Fields(line)
+ if len(fields) > 0 {
+ method = string(fields[0])
+ }
+ if len(fields) > 1 {
+ path = string(fields[1])
+ }
+ return method, path
+}
diff --git a/internal/store/store.go b/internal/store/store.go
index c0219e4..88c9991 100644
--- a/internal/store/store.go
+++ b/internal/store/store.go
@@ -25,7 +25,8 @@ CREATE TABLE IF NOT EXISTS history (
response_raw BLOB,
request_exact INTEGER NOT NULL,
response_exact INTEGER NOT NULL,
- error TEXT NOT NULL DEFAULT ''
+ error TEXT NOT NULL DEFAULT '',
+ source TEXT NOT NULL DEFAULT 'proxy'
);
`
@@ -59,6 +60,9 @@ func Open(path string) (*Store, error) {
db.Close()
return nil, fmt.Errorf("create schema: %w", err)
}
+ // Added after the initial schema; ignore the "duplicate column" error
+ // on databases that already have it.
+ db.Exec("ALTER TABLE history ADD COLUMN source TEXT NOT NULL DEFAULT 'proxy'")
return &Store{db: db}, nil
}
@@ -82,6 +86,7 @@ type Entry struct {
RequestExact bool // true if RequestRaw is wire-exact, false if reconstructed (e.g. HTTP/2)
ResponseExact bool
Error string // network/transport error, if the request never got a response
+ Source string // "proxy" or "repeater"
}
// Summary is the lightweight metadata used for the history list view -
@@ -98,6 +103,7 @@ type Summary struct {
ReqSize int
RespSize int
Error string
+ Source string
}
// Insert stores e and returns its assigned ID.
@@ -106,13 +112,17 @@ func (s *Store) Insert(e *Entry) (int64, error) {
if e.StatusCode != 0 {
statusCode = e.StatusCode
}
+ source := e.Source
+ if source == "" {
+ source = "proxy"
+ }
res, err := s.db.Exec(
`INSERT INTO history
(started_at, duration_ms, method, scheme, host, path, status_code,
- request_raw, response_raw, request_exact, response_exact, error)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
+ request_raw, response_raw, request_exact, response_exact, error, source)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
e.StartedAt.UnixMilli(), e.Duration.Milliseconds(), e.Method, e.Scheme, e.Host, e.Path,
- statusCode, e.RequestRaw, e.ResponseRaw, boolToInt(e.RequestExact), boolToInt(e.ResponseExact), e.Error,
+ statusCode, e.RequestRaw, e.ResponseRaw, boolToInt(e.RequestExact), boolToInt(e.ResponseExact), e.Error, source,
)
if err != nil {
return 0, fmt.Errorf("insert history entry: %w", err)
@@ -131,7 +141,7 @@ func (s *Store) List(limit int, beforeID int64) ([]Summary, error) {
}
rows, err := s.db.Query(
`SELECT id, started_at, duration_ms, method, scheme, host, path,
- COALESCE(status_code, 0), length(request_raw), COALESCE(length(response_raw), 0), error
+ COALESCE(status_code, 0), length(request_raw), COALESCE(length(response_raw), 0), error, source
FROM history WHERE id < ? ORDER BY id DESC LIMIT ?`,
beforeID, limit,
)
@@ -145,7 +155,7 @@ func (s *Store) List(limit int, beforeID int64) ([]Summary, error) {
var sum Summary
var startedAt, durationMs int64
if err := rows.Scan(&sum.ID, &startedAt, &durationMs, &sum.Method, &sum.Scheme, &sum.Host, &sum.Path,
- &sum.StatusCode, &sum.ReqSize, &sum.RespSize, &sum.Error); err != nil {
+ &sum.StatusCode, &sum.ReqSize, &sum.RespSize, &sum.Error, &sum.Source); err != nil {
return nil, fmt.Errorf("scan history row: %w", err)
}
sum.StartedAt = time.UnixMilli(startedAt)
@@ -160,7 +170,7 @@ func (s *Store) Get(id int64) (*Entry, error) {
row := s.db.QueryRow(
`SELECT id, started_at, duration_ms, method, scheme, host, path,
COALESCE(status_code, 0), request_raw, response_raw,
- request_exact, response_exact, error
+ request_exact, response_exact, error, source
FROM history WHERE id = ?`,
id,
)
@@ -168,7 +178,7 @@ func (s *Store) Get(id int64) (*Entry, error) {
var startedAt, durationMs int64
var reqExact, respExact int
if err := row.Scan(&e.ID, &startedAt, &durationMs, &e.Method, &e.Scheme, &e.Host, &e.Path,
- &e.StatusCode, &e.RequestRaw, &e.ResponseRaw, &reqExact, &respExact, &e.Error); err != nil {
+ &e.StatusCode, &e.RequestRaw, &e.ResponseRaw, &reqExact, &respExact, &e.Error, &e.Source); err != nil {
return nil, fmt.Errorf("get history entry %d: %w", id, err)
}
e.StartedAt = time.UnixMilli(startedAt)