2016-03-18 20:57:35 +00:00
|
|
|
package rewrite
|
|
|
|
|
|
|
|
|
|
import (
|
2017-03-06 16:32:17 -05:00
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
"github.com/coredns/coredns/plugin"
|
2017-03-06 16:32:17 -05:00
|
|
|
|
2016-03-18 20:57:35 +00:00
|
|
|
"github.com/miekg/dns"
|
2017-03-06 16:32:17 -05:00
|
|
|
|
2016-03-19 07:18:57 +00:00
|
|
|
"golang.org/x/net/context"
|
2016-03-18 20:57:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Result is the result of a rewrite
|
|
|
|
|
type Result int
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// RewriteIgnored is returned when rewrite is not done on request.
|
|
|
|
|
RewriteIgnored Result = iota
|
|
|
|
|
// RewriteDone is returned when rewrite is done on request.
|
|
|
|
|
RewriteDone
|
|
|
|
|
// RewriteStatus is returned when rewrite is not needed and status code should be set
|
|
|
|
|
// for the request.
|
|
|
|
|
RewriteStatus
|
|
|
|
|
)
|
|
|
|
|
|
2017-09-20 13:06:53 -07:00
|
|
|
// These are defined processing mode.
|
|
|
|
|
const (
|
|
|
|
|
// Processing should stop after completing this rule
|
|
|
|
|
Stop = "stop"
|
|
|
|
|
// Processing should continue to next rule
|
|
|
|
|
Continue = "continue"
|
|
|
|
|
)
|
|
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
// Rewrite is plugin to rewrite requests internally before being handled.
|
2016-03-18 20:57:35 +00:00
|
|
|
type Rewrite struct {
|
2017-09-14 09:36:06 +01:00
|
|
|
Next plugin.Handler
|
2016-04-07 17:42:35 +01:00
|
|
|
Rules []Rule
|
|
|
|
|
noRevert bool
|
2016-03-18 20:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
// ServeDNS implements the plugin.Handler interface.
|
2016-03-19 07:18:57 +00:00
|
|
|
func (rw Rewrite) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
2016-04-21 22:02:26 +01:00
|
|
|
wr := NewResponseReverter(w, r)
|
2016-03-18 20:57:35 +00:00
|
|
|
for _, rule := range rw.Rules {
|
2017-08-24 09:34:07 -07:00
|
|
|
switch result := rule.Rewrite(w, r); result {
|
2016-03-18 20:57:35 +00:00
|
|
|
case RewriteDone:
|
2017-09-20 13:06:53 -07:00
|
|
|
if rule.Mode() == Stop {
|
|
|
|
|
if rw.noRevert {
|
|
|
|
|
return plugin.NextOrFailure(rw.Name(), rw.Next, ctx, w, r)
|
|
|
|
|
}
|
|
|
|
|
return plugin.NextOrFailure(rw.Name(), rw.Next, ctx, wr, r)
|
2016-04-07 17:42:35 +01:00
|
|
|
}
|
2016-03-18 20:57:35 +00:00
|
|
|
case RewriteIgnored:
|
|
|
|
|
break
|
|
|
|
|
case RewriteStatus:
|
|
|
|
|
// only valid for complex rules.
|
|
|
|
|
// if cRule, ok := rule.(*ComplexRule); ok && cRule.Status != 0 {
|
|
|
|
|
// return cRule.Status, nil
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-14 09:36:06 +01:00
|
|
|
return plugin.NextOrFailure(rw.Name(), rw.Next, ctx, w, r)
|
2016-03-18 20:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-27 11:48:37 +00:00
|
|
|
// Name implements the Handler interface.
|
2016-10-26 10:01:52 +01:00
|
|
|
func (rw Rewrite) Name() string { return "rewrite" }
|
|
|
|
|
|
2017-03-06 16:32:17 -05:00
|
|
|
// Rule describes a rewrite rule.
|
2016-03-18 20:57:35 +00:00
|
|
|
type Rule interface {
|
2017-03-06 16:32:17 -05:00
|
|
|
// Rewrite rewrites the current request.
|
2017-08-24 09:34:07 -07:00
|
|
|
Rewrite(dns.ResponseWriter, *dns.Msg) Result
|
2017-09-20 13:06:53 -07:00
|
|
|
// Mode returns the processing mode stop or continue
|
|
|
|
|
Mode() string
|
2017-03-06 16:32:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newRule(args ...string) (Rule, error) {
|
|
|
|
|
if len(args) == 0 {
|
2017-06-14 09:37:10 -07:00
|
|
|
return nil, fmt.Errorf("no rule type specified for rewrite")
|
2017-03-06 16:32:17 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-20 13:06:53 -07:00
|
|
|
arg0 := strings.ToLower(args[0])
|
|
|
|
|
var ruleType string
|
|
|
|
|
var expectNumArgs, startArg int
|
|
|
|
|
mode := Stop
|
|
|
|
|
switch arg0 {
|
|
|
|
|
case Continue:
|
|
|
|
|
mode = arg0
|
|
|
|
|
ruleType = strings.ToLower(args[1])
|
|
|
|
|
expectNumArgs = len(args) - 1
|
|
|
|
|
startArg = 2
|
|
|
|
|
case Stop:
|
|
|
|
|
ruleType = strings.ToLower(args[1])
|
|
|
|
|
expectNumArgs = len(args) - 1
|
|
|
|
|
startArg = 2
|
|
|
|
|
default:
|
2017-09-22 16:13:04 -07:00
|
|
|
// for backward compatibility
|
2017-09-20 13:06:53 -07:00
|
|
|
ruleType = arg0
|
|
|
|
|
expectNumArgs = len(args)
|
|
|
|
|
startArg = 1
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-13 11:31:19 -05:00
|
|
|
if ruleType != "edns0" && ruleType != "name" && expectNumArgs != 3 {
|
2017-03-06 16:32:17 -05:00
|
|
|
return nil, fmt.Errorf("%s rules must have exactly two arguments", ruleType)
|
|
|
|
|
}
|
|
|
|
|
switch ruleType {
|
|
|
|
|
case "name":
|
2017-12-13 11:31:19 -05:00
|
|
|
return newNameRule(mode, args[startArg:]...)
|
2017-03-06 16:32:17 -05:00
|
|
|
case "class":
|
2017-12-14 13:25:36 -05:00
|
|
|
return newClassRule(mode, args[startArg:]...)
|
2017-03-06 16:32:17 -05:00
|
|
|
case "type":
|
2017-12-14 13:25:36 -05:00
|
|
|
return newTypeRule(mode, args[startArg:]...)
|
2017-03-06 16:32:17 -05:00
|
|
|
case "edns0":
|
2017-09-20 13:06:53 -07:00
|
|
|
return newEdns0Rule(mode, args[startArg:]...)
|
2017-03-06 16:32:17 -05:00
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("invalid rule type %q", args[0])
|
|
|
|
|
}
|
2016-03-18 20:57:35 +00:00
|
|
|
}
|