diff options
Diffstat (limited to 'internal/ipc/ipc.go')
| -rw-r--r-- | internal/ipc/ipc.go | 73 |
1 files changed, 67 insertions, 6 deletions
diff --git a/internal/ipc/ipc.go b/internal/ipc/ipc.go index c2be080..2abce89 100644 --- a/internal/ipc/ipc.go +++ b/internal/ipc/ipc.go @@ -13,6 +13,7 @@ import ( "time" "mitmux/internal/rules" + "mitmux/internal/scope" "mitmux/internal/store" ) @@ -55,6 +56,14 @@ type Request struct { RuleID int64 `json:"rule_id,omitempty"` RuleEnabled bool `json:"rule_enabled,omitempty"` + // For "scope_add": the new rule (always an add - scope rules are + // simple enough that edit-in-place isn't worth a separate update + // path; delete and re-add covers it). For "scope_delete"/ + // "scope_toggle": ScopeRuleID (and RuleEnabled for toggle) identify + // the target. + ScopeRule *scope.Rule `json:"scope_rule,omitempty"` + ScopeRuleID int64 `json:"scope_rule_id,omitempty"` + // For "set_flagged" and "delete_entry": ID identifies the history // entry. "clear_history" needs no fields at all. Flagged bool `json:"flagged,omitempty"` @@ -62,12 +71,13 @@ type Request struct { // Response is sent by the daemon to a client. type Response struct { - Type string `json:"type"` // "list", "get", "new", "repeat", "rules", "intrude_result", "intrude_done", "status", "flagged", "deleted", "cleared", or "error" - Entries []store.Summary `json:"entries,omitempty"` // for "list" - Detail *EntryDetail `json:"detail,omitempty"` // for "get" and "repeat" - New *store.Summary `json:"new,omitempty"` // for "new" (subscribe push) - Rules []rules.Rule `json:"rules,omitempty"` // for "rules" - Status *StatusMsg `json:"status,omitempty"` // for "status" + Type string `json:"type"` // "list", "get", "new", "repeat", "rules", "scope_rules", "intrude_result", "intrude_done", "status", "flagged", "deleted", "cleared", or "error" + Entries []store.Summary `json:"entries,omitempty"` // for "list" + Detail *EntryDetail `json:"detail,omitempty"` // for "get" and "repeat" + New *store.Summary `json:"new,omitempty"` // for "new" (subscribe push) + Rules []rules.Rule `json:"rules,omitempty"` // for "rules" + ScopeRules []scope.Rule `json:"scope_rules,omitempty"` // for "scope_rules" + Status *StatusMsg `json:"status,omitempty"` // for "status" // For "intrude_result": one completed attack request. IntrudeResult *IntrudeResultMsg `json:"intrude_result,omitempty"` @@ -326,6 +336,57 @@ func (c *Client) rulesRoundTrip(req Request) ([]rules.Rule, error) { return resp.Rules, nil } +// ListScopeRules returns every scope rule, enabled or not. +func (c *Client) ListScopeRules() ([]scope.Rule, error) { + c.mu.Lock() + defer c.mu.Unlock() + return c.scopeRoundTrip(Request{Type: "scope_list"}) +} + +// AddScopeRule adds r and returns its assigned ID. +func (c *Client) AddScopeRule(r scope.Rule) (int64, error) { + c.mu.Lock() + defer c.mu.Unlock() + saved, err := c.scopeRoundTrip(Request{Type: "scope_add", ScopeRule: &r}) + if err != nil { + return 0, err + } + if len(saved) == 0 { + return 0, errors.New("scope_add: daemon returned no rule") + } + return saved[0].ID, nil +} + +// DeleteScopeRule removes a scope rule. +func (c *Client) DeleteScopeRule(id int64) error { + c.mu.Lock() + defer c.mu.Unlock() + _, err := c.scopeRoundTrip(Request{Type: "scope_delete", ScopeRuleID: id}) + return err +} + +// SetScopeRuleEnabled toggles a scope rule without touching its pattern. +func (c *Client) SetScopeRuleEnabled(id int64, enabled bool) error { + c.mu.Lock() + defer c.mu.Unlock() + _, err := c.scopeRoundTrip(Request{Type: "scope_toggle", ScopeRuleID: id, RuleEnabled: enabled}) + return err +} + +func (c *Client) scopeRoundTrip(req Request) ([]scope.Rule, error) { + if err := c.enc.Encode(req); 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.ScopeRules, 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. |