srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/internal/store/store.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/store/store.go')
-rw-r--r--internal/store/store.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/internal/store/store.go b/internal/store/store.go
index c0cdea5..befd5a6 100644
--- a/internal/store/store.go
+++ b/internal/store/store.go
@@ -16,6 +16,7 @@ import (
_ "modernc.org/sqlite"
"mitmux/internal/rules"
+ "mitmux/internal/scope"
)
const schema = `
@@ -55,6 +56,13 @@ CREATE TABLE IF NOT EXISTS rules (
is_regex INTEGER NOT NULL DEFAULT 0,
position INTEGER NOT NULL DEFAULT 0
);
+
+CREATE TABLE IF NOT EXISTS scope_rules (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ enabled INTEGER NOT NULL DEFAULT 1,
+ pattern TEXT NOT NULL,
+ is_regex INTEGER NOT NULL DEFAULT 0
+);
`
// Store is a handle to the history database. Safe for concurrent use.
@@ -560,6 +568,57 @@ func (s *Store) SetRuleEnabled(id int64, enabled bool) error {
return nil
}
+// ListScopeRules returns every scope rule, including disabled ones (the
+// scope management view needs to show and let you re-enable those too).
+func (s *Store) ListScopeRules() ([]scope.Rule, error) {
+ rows, err := s.db.Query(`SELECT id, enabled, pattern, is_regex FROM scope_rules ORDER BY id`)
+ if err != nil {
+ return nil, fmt.Errorf("list scope rules: %w", err)
+ }
+ defer rows.Close()
+
+ var out []scope.Rule
+ for rows.Next() {
+ var r scope.Rule
+ var enabled, isRegex int
+ if err := rows.Scan(&r.ID, &enabled, &r.Pattern, &isRegex); err != nil {
+ return nil, fmt.Errorf("scan scope rule row: %w", err)
+ }
+ r.Enabled = enabled != 0
+ r.IsRegex = isRegex != 0
+ out = append(out, r)
+ }
+ return out, rows.Err()
+}
+
+// AddScopeRule stores r and returns its assigned ID.
+func (s *Store) AddScopeRule(r scope.Rule) (int64, error) {
+ res, err := s.db.Exec(
+ `INSERT INTO scope_rules (enabled, pattern, is_regex) VALUES (?, ?, ?)`,
+ boolToInt(r.Enabled), r.Pattern, boolToInt(r.IsRegex),
+ )
+ if err != nil {
+ return 0, fmt.Errorf("add scope rule: %w", err)
+ }
+ return res.LastInsertId()
+}
+
+// SetScopeRuleEnabled toggles a scope rule without touching its pattern.
+func (s *Store) SetScopeRuleEnabled(id int64, enabled bool) error {
+ if _, err := s.db.Exec(`UPDATE scope_rules SET enabled = ? WHERE id = ?`, boolToInt(enabled), id); err != nil {
+ return fmt.Errorf("set scope rule %d enabled: %w", id, err)
+ }
+ return nil
+}
+
+// DeleteScopeRule removes a scope rule.
+func (s *Store) DeleteScopeRule(id int64) error {
+ if _, err := s.db.Exec(`DELETE FROM scope_rules WHERE id = ?`, id); err != nil {
+ return fmt.Errorf("delete scope rule %d: %w", id, err)
+ }
+ return nil
+}
+
// DeleteEntry removes a single history entry and its search index row.
func (s *Store) DeleteEntry(id int64) error {
tx, err := s.db.Begin()