feat(web): visual rule builder with geo, CIDR and wildcard pickers

The rule editor no longer requires writing expr by hand. Conditions are rows of
field + operator + values joined with AND or OR, each row negatable, and the
generated expression is shown live and validated by the engine before a rule is
accepted. Raw expression and YAML editing are still available.

Fields cover the connection (protocol, source/destination IP and port), domains
(TLS SNI, QUIC SNI, DNS query name), HTTP (host, path, method, User-Agent) and
protocol detection. Operators cover CIDR membership, GeoIP countries, GeoSite
categories, port ranges, wildcards, substrings and regular expressions.
Multiple values in a row are OR-ed, so one row holds a whole domain or country
list.

Wildcards compile to the cheapest expression that matches them: *.x.com
becomes endsWith, x.* startsWith, *ad* contains, and only a star in the
middle falls back to a regular expression. Values are validated as they are
typed, including a hint when a star is used with an operator that would match
it literally.

Country and category pickers are backed by the databases the engine actually
loaded, via a new GET /api/v1/geo endpoint (cached, loaded on demand) built on
new listing methods in the geo package. Country names and flags come from
Intl.DisplayNames, so no name table is shipped. Note that the v2geo format has
no AS numbers; the provider groups it does contain (cloudflare, google,
telegram, ...) are listed alongside the countries.

Opening an existing rule parses its expression back into conditions. Anything
the builder cannot represent opens in the expression editor with a warning
rather than being rewritten. ruleset/expr_test.go pins the canonical
expressions the builder generates and compiles them with the real engine, and
the devserver now uses the real ruleset compiler so the same errors show up
during frontend work.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
mei
2026-07-27 08:48:56 +08:00
co-authored by Claude Opus 5
parent 5a7722d1d2
commit f7ad3aac95
52 changed files with 2191 additions and 216 deletions
+34
View File
@@ -8,9 +8,14 @@ import (
"net/http"
"path"
"strings"
"sync"
"time"
)
// geoCacheTTL is how long a loaded geo database listing is reused. Parsing the
// files is expensive, and their content only changes when they are updated.
const geoCacheTTL = 10 * time.Minute
var (
errBadCredentials = errors.New("invalid password")
errTooManyAttempts = errors.New("too many failed attempts, try again later")
@@ -88,6 +93,27 @@ type AnalyzerInfo struct {
Proto string `json:"proto"`
}
// GeoData is the content of the configured GeoIP/GeoSite databases, used by the
// rule builder so that the user can pick a country or a site category from a
// list instead of typing a code.
type GeoData struct {
// IP lists the geoip() keys: country codes plus, depending on the
// database, provider groups such as "cloudflare" or "telegram".
IP []GeoEntry `json:"ip"`
// Site lists the geosite() keys along with their attributes.
Site []GeoEntry `json:"site"`
// IPError / SiteError describe why a database could not be loaded.
IPError string `json:"ipError,omitempty"`
SiteError string `json:"siteError,omitempty"`
}
// GeoEntry is a single key of a geo database.
type GeoEntry struct {
Code string `json:"code"`
Count int `json:"count"`
Attributes []string `json:"attributes,omitempty"`
}
// Config is the web server configuration.
type Config struct {
// Listen is the address to listen on, e.g. ":8080".
@@ -102,6 +128,9 @@ type Config struct {
Rules RuleManager
Meta MetaInfo
Info func() Info
// Geo loads the GeoIP/GeoSite databases. It is called at most once every
// geoCacheTTL, and only when the rule builder asks for the data. Optional.
Geo func() GeoData
// Logf is used for the few messages the server produces. Optional.
Logf func(format string, args ...interface{})
@@ -113,6 +142,10 @@ type Server struct {
auth *authenticator
mux *http.ServeMux
static http.Handler
geoMu sync.Mutex
geoCache *GeoData
geoLoaded time.Time
}
// NewServer creates a new web UI server.
@@ -158,6 +191,7 @@ func (s *Server) routes() {
s.mux.HandleFunc("/api/v1/live", s.guard(s.handleLive, false))
s.mux.HandleFunc("/api/v1/rules", s.guard(s.handleRules, false))
s.mux.HandleFunc("/api/v1/rules/validate", s.guard(s.handleRulesValidate, true))
s.mux.HandleFunc("/api/v1/geo", s.guard(s.handleGeo, false))
s.mux.HandleFunc("/", s.handleStatic)
}