lightning-terminal/rules/interfaces.go

81 lines
2.8 KiB
Go
Raw Normal View History

2022-07-08 14:03:29 +02:00
package rules
import (
"context"
"encoding/json"
"github.com/lightninglabs/lightning-terminal/firewalldb"
"github.com/lightninglabs/lightning-terminal/litrpc"
"github.com/lightninglabs/lightning-terminal/session"
2022-07-08 14:03:29 +02:00
"google.golang.org/protobuf/proto"
)
// Manager is the interface that any firewall rule managers will need to
// implement. A rule Manager is used to construct a rule Enforcer or rule
// Values.
type Manager interface {
// NewEnforcer constructs a new rule enforcer using the passed values
// and config.
2025-01-13 07:02:42 +02:00
NewEnforcer(ctx context.Context, cfg Config, values Values) (Enforcer,
error)
2022-07-08 14:03:29 +02:00
// NewValueFromProto converts the given proto value into a Value object.
NewValueFromProto(p *litrpc.RuleValue) (Values, error)
// EmptyValue returns a new Values instance of the type that this
// Manager handles.
EmptyValue() Values
// Stop cleans up the resources held by the manager.
Stop() error
}
// Enforcer is the interface that any firewall rule enforcer must implement.
// An enforcer accepts, rejects, and possible alters an RPC proto message for a
// specific URI.
type Enforcer interface {
// HandleRequest checks the validity of a request and possibly edits it.
HandleRequest(ctx context.Context, uri string,
protoMsg proto.Message) (proto.Message, error)
// HandleResponse handles and possibly alters a response.
HandleResponse(ctx context.Context, uri string,
protoMsg proto.Message) (proto.Message, error)
// HandleErrorResponse handles and possibly alters a response error.
HandleErrorResponse(ctx context.Context, uri string, err error) (error,
error)
}
// Values represents the static values that encompass the settings of the rule.
type Values interface {
// RuleName returns the name of the rule that these values are to be
// used with.
RuleName() string
// VerifySane checks that the rules values are valid given the allowed
// minimum and maximum values.
VerifySane(minVal, maxVal Values) error
// ToProto converts the rule Values to the litrpc counterpart.
ToProto() *litrpc.RuleValue
// RealToPseudo converts the rule Values to a new one that uses pseudo
// keys, channel IDs, channel points etc. It returns a map of any new
// real to pseudo strings that should be persisted that it did not find
// in the given PrivacyMapReader.
RealToPseudo(ctx context.Context, db firewalldb.PrivacyMapReader,
flags session.PrivacyFlags) (Values, map[string]string, error)
2022-07-08 14:03:29 +02:00
// PseudoToReal attempts to convert any appropriate pseudo fields in
// the rule Values to their corresponding real values. It uses the
// passed PrivacyMapDB to find the real values.
PseudoToReal(ctx context.Context, db firewalldb.PrivacyMapDB,
flags session.PrivacyFlags) (Values, error)
2022-07-08 14:03:29 +02:00
}
// Marshal converts the rule Values to a json byte slice.
func Marshal(v Values) ([]byte, error) {
return json.Marshal(v)
}