chore: code cleanup

This commit is contained in:
wwqgtxx 2024-12-25 10:31:40 +08:00
parent 89dfabe9b3
commit 20739f5db7
2 changed files with 77 additions and 67 deletions

View File

@ -58,7 +58,7 @@ func ParseRuleProvider(name string, mapping map[string]any, parse common.ParseRu
} }
vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, nil, resource.DefaultHttpTimeout, schema.SizeLimit) vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, nil, resource.DefaultHttpTimeout, schema.SizeLimit)
case "inline": case "inline":
return newInlineProvider(name, behavior, schema.Payload, parse), nil return NewInlineProvider(name, behavior, schema.Payload, parse), nil
default: default:
return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type) return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type)
} }

View File

@ -24,21 +24,6 @@ func SetTunnel(t P.Tunnel) {
tunnel = t tunnel = t
} }
type ruleSetProvider struct {
ruleSetProviderBase
*resource.Fetcher[ruleStrategy]
format P.RuleFormat
}
type ruleSetProviderBase struct {
behavior P.RuleBehavior
strategy ruleStrategy
}
type RuleSetProvider struct {
*ruleSetProvider
}
type RulePayload struct { type RulePayload struct {
/** /**
key: Domain or IP Cidr key: Domain or IP Cidr
@ -48,6 +33,17 @@ type RulePayload struct {
Rules []string `yaml:"rules"` Rules []string `yaml:"rules"`
} }
type providerForApi struct {
Behavior string `json:"behavior"`
Format string `json:"format"`
Name string `json:"name"`
RuleCount int `json:"ruleCount"`
Type string `json:"type"`
VehicleType string `json:"vehicleType"`
UpdatedAt time.Time `json:"updatedAt"`
Payload []string `json:"payload,omitempty"`
}
type ruleStrategy interface { type ruleStrategy interface {
Behavior() P.RuleBehavior Behavior() P.RuleBehavior
Match(metadata *C.Metadata) bool Match(metadata *C.Metadata) bool
@ -66,10 +62,49 @@ type mrsRuleStrategy interface {
DumpMrs(f func(key string) bool) DumpMrs(f func(key string) bool)
} }
func (rp *ruleSetProviderBase) Type() P.ProviderType { type baseProvider struct {
behavior P.RuleBehavior
strategy ruleStrategy
}
func (bp *baseProvider) Type() P.ProviderType {
return P.Rule return P.Rule
} }
func (bp *baseProvider) Behavior() P.RuleBehavior {
return bp.behavior
}
func (bp *baseProvider) Count() int {
return bp.strategy.Count()
}
func (bp *baseProvider) Match(metadata *C.Metadata) bool {
return bp.strategy != nil && bp.strategy.Match(metadata)
}
func (bp *baseProvider) ShouldResolveIP() bool {
return bp.strategy.ShouldResolveIP()
}
func (bp *baseProvider) ShouldFindProcess() bool {
return bp.strategy.ShouldFindProcess()
}
func (bp *baseProvider) Strategy() any {
return bp.strategy
}
type ruleSetProvider struct {
baseProvider
*resource.Fetcher[ruleStrategy]
format P.RuleFormat
}
type RuleSetProvider struct {
*ruleSetProvider
}
func (rp *ruleSetProvider) Initial() error { func (rp *ruleSetProvider) Initial() error {
_, err := rp.Fetcher.Initial() _, err := rp.Fetcher.Initial()
return err return err
@ -80,41 +115,6 @@ func (rp *ruleSetProvider) Update() error {
return err return err
} }
func (rp *ruleSetProviderBase) Behavior() P.RuleBehavior {
return rp.behavior
}
func (rp *ruleSetProviderBase) Count() int {
return rp.strategy.Count()
}
func (rp *ruleSetProviderBase) Match(metadata *C.Metadata) bool {
return rp.strategy != nil && rp.strategy.Match(metadata)
}
func (rp *ruleSetProviderBase) ShouldResolveIP() bool {
return rp.strategy.ShouldResolveIP()
}
func (rp *ruleSetProviderBase) ShouldFindProcess() bool {
return rp.strategy.ShouldFindProcess()
}
func (rp *ruleSetProviderBase) Strategy() any {
return rp.strategy
}
type providerForApi struct {
Behavior string `json:"behavior"`
Format string `json:"format"`
Name string `json:"name"`
RuleCount int `json:"ruleCount"`
Type string `json:"type"`
VehicleType string `json:"vehicleType"`
UpdatedAt time.Time `json:"updatedAt"`
Payload []string `json:"payload,omitempty"`
}
func (rp *ruleSetProvider) MarshalJSON() ([]byte, error) { func (rp *ruleSetProvider) MarshalJSON() ([]byte, error) {
return json.Marshal( return json.Marshal(
providerForApi{ providerForApi{
@ -135,7 +135,7 @@ func (rp *RuleSetProvider) Close() error {
func NewRuleSetProvider(name string, behavior P.RuleBehavior, format P.RuleFormat, interval time.Duration, vehicle P.Vehicle, parse common.ParseRuleFunc) P.RuleProvider { func NewRuleSetProvider(name string, behavior P.RuleBehavior, format P.RuleFormat, interval time.Duration, vehicle P.Vehicle, parse common.ParseRuleFunc) P.RuleProvider {
rp := &ruleSetProvider{ rp := &ruleSetProvider{
ruleSetProviderBase: ruleSetProviderBase{ baseProvider: baseProvider{
behavior: behavior, behavior: behavior,
}, },
format: format, format: format,
@ -285,11 +285,15 @@ func rulesParseInline(rs []string, strategy ruleStrategy) ruleStrategy {
return strategy return strategy
} }
type InlineProvider struct {
*inlineProvider
}
type inlineProvider struct { type inlineProvider struct {
ruleSetProviderBase baseProvider
name string name string
updateTime time.Time updateAt time.Time
payload []string payload []string
} }
func (i *inlineProvider) Name() string { func (i *inlineProvider) Name() string {
@ -302,7 +306,7 @@ func (i *inlineProvider) Initial() error {
func (i *inlineProvider) Update() error { func (i *inlineProvider) Update() error {
// make api update happy // make api update happy
i.updateTime = time.Now() i.updateAt = time.Now()
return nil return nil
} }
@ -318,21 +322,27 @@ func (i *inlineProvider) MarshalJSON() ([]byte, error) {
RuleCount: i.strategy.Count(), RuleCount: i.strategy.Count(),
Type: i.Type().String(), Type: i.Type().String(),
VehicleType: i.VehicleType().String(), VehicleType: i.VehicleType().String(),
UpdatedAt: i.updateTime, UpdatedAt: i.updateAt,
Payload: i.payload, Payload: i.payload,
}) })
} }
func newInlineProvider(name string, behavior P.RuleBehavior, payload []string, parse common.ParseRuleFunc) P.RuleProvider { func NewInlineProvider(name string, behavior P.RuleBehavior, payload []string, parse common.ParseRuleFunc) P.RuleProvider {
rp := &inlineProvider{ ip := &inlineProvider{
ruleSetProviderBase: ruleSetProviderBase{ baseProvider: baseProvider{
behavior: behavior, behavior: behavior,
strategy: newStrategy(behavior, parse), strategy: newStrategy(behavior, parse),
}, },
payload: payload, payload: payload,
name: name, name: name,
updateTime: time.Now(), updateAt: time.Now(),
} }
rp.strategy = rulesParseInline(payload, rp.strategy) ip.strategy = rulesParseInline(payload, ip.strategy)
return rp
wrapper := &InlineProvider{
ip,
}
//runtime.SetFinalizer(wrapper, (*InlineProvider).Close)
return wrapper
} }