* plugin/metadata: add metadata plugin * plugin/metadata: Add MD struct, refactor code, fix doc * plugin/metadata: simplify metadata key * plugin/metadata: improve setup_test * Support of metadata by rewrite plugin. Move calculated variables to metadata. * Move variables from metadata to pkg, add UTs, READMEs change, metadata small fixes * Add client port validation to variables_test * plugin/metadata: improve README * plugin/metadata: rename methods * plugin/metadata: Update Metadataer interface, update doc, cosmetic code changes * plugin/metadata: move colllisions check to OnStartup(). Fix default variables metadataer. * plugin/metadata: Fix comment for method setValue * plugin/metadata: change variables order to fix linter warning * plugin/metadata: rename Metadataer to Provider
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package rewrite
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
type classRule struct {
|
|
fromClass uint16
|
|
toClass uint16
|
|
NextAction string
|
|
}
|
|
|
|
// newClassRule creates a class matching rule
|
|
func newClassRule(nextAction string, args ...string) (Rule, error) {
|
|
var from, to uint16
|
|
var ok bool
|
|
if from, ok = dns.StringToClass[strings.ToUpper(args[0])]; !ok {
|
|
return nil, fmt.Errorf("invalid class %q", strings.ToUpper(args[0]))
|
|
}
|
|
if to, ok = dns.StringToClass[strings.ToUpper(args[1])]; !ok {
|
|
return nil, fmt.Errorf("invalid class %q", strings.ToUpper(args[1]))
|
|
}
|
|
return &classRule{from, to, nextAction}, nil
|
|
}
|
|
|
|
// Rewrite rewrites the the current request.
|
|
func (rule *classRule) Rewrite(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) Result {
|
|
if rule.fromClass > 0 && rule.toClass > 0 {
|
|
if r.Question[0].Qclass == rule.fromClass {
|
|
r.Question[0].Qclass = rule.toClass
|
|
return RewriteDone
|
|
}
|
|
}
|
|
return RewriteIgnored
|
|
}
|
|
|
|
// Mode returns the processing mode
|
|
func (rule *classRule) Mode() string {
|
|
return rule.NextAction
|
|
}
|
|
|
|
// GetResponseRule return a rule to rewrite the response with. Currently not implemented.
|
|
func (rule *classRule) GetResponseRule() ResponseRule {
|
|
return ResponseRule{}
|
|
}
|