1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
package ipc
import (
"context"
"encoding/json"
"io"
"log"
"net"
"sync"
"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
subs map[chan store.Summary]struct{}
}
// NewHub creates an empty Hub.
func NewHub() *Hub {
return &Hub{subs: make(map[chan store.Summary]struct{})}
}
// Broadcast notifies all current subscribers of e. Slow subscribers
// drop entries rather than blocking the proxy.
func (h *Hub) Broadcast(e store.Summary) {
h.mu.Lock()
defer h.mu.Unlock()
for ch := range h.subs {
select {
case ch <- e:
default:
}
}
}
func (h *Hub) subscribe() chan store.Summary {
ch := make(chan store.Summary, 64)
h.mu.Lock()
h.subs[ch] = struct{}{}
h.mu.Unlock()
return ch
}
func (h *Hub) unsubscribe(ch chan store.Summary) {
h.mu.Lock()
delete(h.subs, ch)
h.mu.Unlock()
close(ch)
}
// Server serves the daemon side of the mitmux control protocol.
type Server struct {
db *store.Store
hub *Hub
repeater Repeater
}
// NewServer creates a control-protocol Server backed by db, broadcasting
// 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
// listener is closed).
func (s *Server) Serve(ln net.Listener) error {
for {
conn, err := ln.Accept()
if err != nil {
return err
}
go s.handleConn(conn)
}
}
func (s *Server) handleConn(conn net.Conn) {
defer conn.Close()
dec := json.NewDecoder(conn)
enc := json.NewEncoder(conn)
for {
var req Request
if err := dec.Decode(&req); err != nil {
return
}
switch req.Type {
case "list":
var entries []store.Summary
var err error
if req.Query != "" {
entries, err = s.db.Search(req.Query, req.Limit, req.BeforeID)
} else {
entries, err = s.db.List(req.Limit, req.BeforeID)
}
if err != nil {
enc.Encode(Response{Type: "error", Error: err.Error()})
continue
}
enc.Encode(Response{Type: "list", Entries: entries})
case "get":
e, err := s.db.Get(req.ID)
if err != nil {
enc.Encode(Response{Type: "error", Error: err.Error()})
continue
}
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()
defer s.hub.unsubscribe(sub)
// The client never sends anything more on this connection,
// but reading it anyway is how we notice it went away when
// no new entry ever arrives to trigger a failed Encode -
// otherwise a subscriber that quits while the daemon is
// idle leaks its goroutine and channel indefinitely.
disconnected := make(chan struct{})
go func() {
io.Copy(io.Discard, conn)
close(disconnected)
}()
for {
select {
case e, ok := <-sub:
if !ok {
return
}
if err := enc.Encode(Response{Type: "new", New: &e}); err != nil {
return
}
case <-disconnected:
return
}
}
default:
enc.Encode(Response{Type: "error", Error: "unknown request type: " + req.Type})
}
}
}
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) {
return func(sum store.Summary) {
log.Printf("%s %s%s -> %d (%s)", sum.Method, sum.Host, sum.Path, sum.StatusCode, sum.Duration)
hub.Broadcast(sum)
}
}
|