Skip to content

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Dec 25, 2024
1 parent 89dfabe commit 20739f5
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 59 deletions.
2 changes: 1 addition & 1 deletion rules/provider/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
case "inline":
return newInlineProvider(name, behavior, schema.Payload, parse), nil
return NewInlineProvider(name, behavior, schema.Payload, parse), nil
default:
return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type)
}
Expand Down
126 changes: 68 additions & 58 deletions rules/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,6 @@ func SetTunnel(t P.Tunnel) {
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 {
/**
key: Domain or IP Cidr
Expand All @@ -48,6 +33,17 @@ type RulePayload struct {
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 {
Behavior() P.RuleBehavior
Match(metadata *C.Metadata) bool
Expand All @@ -66,53 +62,57 @@ type mrsRuleStrategy interface {
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
}

func (rp *ruleSetProvider) Initial() error {
_, err := rp.Fetcher.Initial()
return err
func (bp *baseProvider) Behavior() P.RuleBehavior {
return bp.behavior
}

func (rp *ruleSetProvider) Update() error {
_, _, err := rp.Fetcher.Update()
return err
func (bp *baseProvider) Count() int {
return bp.strategy.Count()
}

func (rp *ruleSetProviderBase) Behavior() P.RuleBehavior {
return rp.behavior
func (bp *baseProvider) Match(metadata *C.Metadata) bool {
return bp.strategy != nil && bp.strategy.Match(metadata)
}

func (rp *ruleSetProviderBase) Count() int {
return rp.strategy.Count()
func (bp *baseProvider) ShouldResolveIP() bool {
return bp.strategy.ShouldResolveIP()
}

func (rp *ruleSetProviderBase) Match(metadata *C.Metadata) bool {
return rp.strategy != nil && rp.strategy.Match(metadata)
func (bp *baseProvider) ShouldFindProcess() bool {
return bp.strategy.ShouldFindProcess()
}

func (rp *ruleSetProviderBase) ShouldResolveIP() bool {
return rp.strategy.ShouldResolveIP()
func (bp *baseProvider) Strategy() any {
return bp.strategy
}

func (rp *ruleSetProviderBase) ShouldFindProcess() bool {
return rp.strategy.ShouldFindProcess()
type ruleSetProvider struct {
baseProvider
*resource.Fetcher[ruleStrategy]
format P.RuleFormat
}

func (rp *ruleSetProviderBase) Strategy() any {
return rp.strategy
type RuleSetProvider struct {
*ruleSetProvider
}

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) Initial() error {
_, err := rp.Fetcher.Initial()
return err
}

func (rp *ruleSetProvider) Update() error {
_, _, err := rp.Fetcher.Update()
return err
}

func (rp *ruleSetProvider) MarshalJSON() ([]byte, error) {
Expand All @@ -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 {
rp := &ruleSetProvider{
ruleSetProviderBase: ruleSetProviderBase{
baseProvider: baseProvider{
behavior: behavior,
},
format: format,
Expand Down Expand Up @@ -285,11 +285,15 @@ func rulesParseInline(rs []string, strategy ruleStrategy) ruleStrategy {
return strategy
}

type InlineProvider struct {
*inlineProvider
}

type inlineProvider struct {
ruleSetProviderBase
name string
updateTime time.Time
payload []string
baseProvider
name string
updateAt time.Time
payload []string
}

func (i *inlineProvider) Name() string {
Expand All @@ -302,7 +306,7 @@ func (i *inlineProvider) Initial() error {

func (i *inlineProvider) Update() error {
// make api update happy
i.updateTime = time.Now()
i.updateAt = time.Now()
return nil
}

Expand All @@ -318,21 +322,27 @@ func (i *inlineProvider) MarshalJSON() ([]byte, error) {
RuleCount: i.strategy.Count(),
Type: i.Type().String(),
VehicleType: i.VehicleType().String(),
UpdatedAt: i.updateTime,
UpdatedAt: i.updateAt,
Payload: i.payload,
})
}

func newInlineProvider(name string, behavior P.RuleBehavior, payload []string, parse common.ParseRuleFunc) P.RuleProvider {
rp := &inlineProvider{
ruleSetProviderBase: ruleSetProviderBase{
func NewInlineProvider(name string, behavior P.RuleBehavior, payload []string, parse common.ParseRuleFunc) P.RuleProvider {
ip := &inlineProvider{
baseProvider: baseProvider{
behavior: behavior,
strategy: newStrategy(behavior, parse),
},
payload: payload,
name: name,
updateTime: time.Now(),
payload: payload,
name: name,
updateAt: time.Now(),
}
rp.strategy = rulesParseInline(payload, rp.strategy)
return rp
ip.strategy = rulesParseInline(payload, ip.strategy)

wrapper := &InlineProvider{
ip,
}

//runtime.SetFinalizer(wrapper, (*InlineProvider).Close)
return wrapper
}

0 comments on commit 20739f5

Please sign in to comment.