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:
+29
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/apernet/OpenGFW/engine"
|
||||
"github.com/apernet/OpenGFW/modifier"
|
||||
"github.com/apernet/OpenGFW/ruleset"
|
||||
"github.com/apernet/OpenGFW/ruleset/builtins/geo"
|
||||
"github.com/apernet/OpenGFW/web"
|
||||
|
||||
"go.uber.org/zap"
|
||||
@@ -197,6 +198,7 @@ func startWebServer(ctx context.Context, config *cliConfig, rm *ruleManager) err
|
||||
Rules: rm,
|
||||
Meta: webMeta(),
|
||||
Info: func() web.Info { return webInfo(config, rm) },
|
||||
Geo: func() web.GeoData { return webGeoData(config) },
|
||||
Logf: func(format string, args ...interface{}) {
|
||||
logger.Info(fmt.Sprintf(format, args...))
|
||||
},
|
||||
@@ -266,6 +268,33 @@ func webInfo(config *cliConfig, rm *ruleManager) web.Info {
|
||||
}
|
||||
}
|
||||
|
||||
// webGeoData lists what the configured geo databases contain, so that the rule
|
||||
// builder can offer a picker instead of a free text field. Loading the files is
|
||||
// expensive, but the web server only calls this on demand and caches the result.
|
||||
func webGeoData(config *cliConfig) web.GeoData {
|
||||
matcher := geo.NewGeoMatcher(config.Ruleset.GeoSite, config.Ruleset.GeoIp)
|
||||
var data web.GeoData
|
||||
if entries, err := matcher.ListGeoIP(); err != nil {
|
||||
data.IPError = err.Error()
|
||||
} else {
|
||||
for _, e := range entries {
|
||||
data.IP = append(data.IP, web.GeoEntry{Code: e.Code, Count: e.CIDRs})
|
||||
}
|
||||
}
|
||||
if entries, err := matcher.ListGeoSite(); err != nil {
|
||||
data.SiteError = err.Error()
|
||||
} else {
|
||||
for _, e := range entries {
|
||||
data.Site = append(data.Site, web.GeoEntry{
|
||||
Code: e.Code,
|
||||
Count: e.Domains,
|
||||
Attributes: e.Attributes,
|
||||
})
|
||||
}
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// hubInfo converts engine stream info into the shape the hub understands.
|
||||
func hubInfo(info ruleset.StreamInfo) web.StreamInfo {
|
||||
return web.StreamInfo{
|
||||
|
||||
Reference in New Issue
Block a user