rewriteng

package module
v0.0.0-...-3ead7ab Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 13, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

README

rewriteng

Name

rewriteng - New generation rewrite plugin.

Description

The rewriteng plugin rewrites queries and responses

Syntax

rewriteng CLASS RR-TYPE TYPE FROM-DOMAIN TO-DOMAIN {
    answer [name|data|both] exact|prefix|suffix|substring|regex|fullregex|noop FROM TO
    additional [name|data|both] exact|prefix|suffix|substring|regex|fullregex|noop FROM TO
    authority [name|data|both] exact|prefix|suffix|substring|regex|fullregex|noop FROM TO
}
  • CLASS the query class (usually IN or ANY).
  • RR-TYPE the query type (A, PTR, ... can be ANY to match all types).
  • TYPE the match type, exact|prefix|suffix|substring|regex|fullregex|noop, triggers re-write:
  • FROM-DOMAIN the domain to rewrite
  • TO-DOMAIN the domain to rewrite to
Rules

The rule syntax is as follows:

rule-type [rr-part] match-type FROM TO
rule-type

The following rule-types are supported:

  • answer: rewrites answers, atleast one answer is required multiple rules are allowed
  • additional: rewrites the additional section, additional rules are optional and multiple rules are allowed
  • authority: rewrites the authority section, authority rules are optional and multiple rules are allowed
rr-part

The following rr-parts are supported:

  • name: rewrites the name part
  • data: rewrites the data part
  • both: rewrites both the name and data parts

If the rr-part is omitted, the name rr-part is assumed.

match-type

The match-type is used to match the from, and the following are supported:

  • exact: on exact match of the part
  • substring: on a partial match of the part
  • prefix: when the part begins with the matching string
  • suffix: when the part ends with the matching string
  • regex: when the part matches a regular expression and matched regexes are used in the TO
  • fullregex: when the part matches a regular expression and matched regexes are not used in the TO
  • noop: when the part ends with the matching string, this is a place holder that does not make any modifications

Examples

The following rewrites queries to x.example.com to x.yahoo.com, it also rewrites the authority and the additional sections.

.:5300 {
    log
    bind 127.0.0.1
    forward . 192.168.1.2
    rewriteng IN ANY suffix example.com yahoo.com {
        answer regex (.*)\.yahoo\.com {1}.example.com
        answer data regex (.*)\.yahoo\.com {1}.example.com
        authority suffix yahoo.com. example.com.
        authority data substring yahoo. example.
        additional suffix yahoo.com. example.com.
        additional data substring 68.142.254.15 192.168.1.2
        additional data substring 68.180.130.15 192.168.1.2
    }
}

The normal output without rewriting is as follows:

$ dig www.yahoo.com

; <<>> DiG 9.8.3-P1 <<>> www.yahoo.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 27720
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 4, ADDITIONAL: 2

;; QUESTION SECTION:
;www.yahoo.com.			IN	A

;; ANSWER SECTION:
www.yahoo.com.		1800	IN	CNAME	atsv2-fp-shed.wg1.b.yahoo.com.
atsv2-fp-shed.wg1.b.yahoo.com. 60 IN	A	87.248.98.7
atsv2-fp-shed.wg1.b.yahoo.com. 60 IN	A	87.248.98.8

;; AUTHORITY SECTION:
wg1.b.yahoo.com.	87717	IN	NS	yf3.a1.b.yahoo.net.
wg1.b.yahoo.com.	87717	IN	NS	yf2.yahoo.com.
wg1.b.yahoo.com.	87717	IN	NS	yf4.a1.b.yahoo.net.
wg1.b.yahoo.com.	87717	IN	NS	yf1.yahoo.com.

;; ADDITIONAL SECTION:
yf1.yahoo.com.		1317	IN	A	68.142.254.15
yf2.yahoo.com.		1317	IN	A	68.180.130.15

;; Query time: 22 msec
;; SERVER: 192.168.1.2#53(192.168.1.2)
;; WHEN: Thu Apr 18 09:43:21 2019
;; MSG SIZE  rcvd: 215

The rewrite of www.example.com to www.yahoo.com outputs the following:

$ dig www.example.com @127.0.0.1 -p 5300

; <<>> DiG 9.8.3-P1 <<>> www.example.com @127.0.0.1 -p 5300
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12839
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 4, ADDITIONAL: 2

;; QUESTION SECTION:
;www.example.com.		IN	A

;; ANSWER SECTION:
www.example.com.	1662	IN	CNAME	atsv2-fp-shed.wg1.b.example.com.
atsv2-fp-shed.wg1.b.example.com. 60 IN	A	87.248.98.7

;; AUTHORITY SECTION:
wg1.b.example.com.	87579	IN	NS	yf2.example.com.
wg1.b.example.com.	87579	IN	NS	yf1.example.com.
wg1.b.example.com.	87579	IN	NS	yf3.a1.b.example.net.
wg1.b.example.com.	87579	IN	NS	yf4.a1.b.example.net.

;; ADDITIONAL SECTION:
yf1.example.com.	1179	IN	A	192.168.1.2
yf2.example.com.	1179	IN	A	192.168.1.2

;; Query time: 10 msec
;; SERVER: 127.0.0.1#5300(127.0.0.1)
;; WHEN: Thu Apr 18 09:45:39 2019
;; MSG SIZE  rcvd: 396

Also See

See the original rewrite plugin it was used as the basis for this plugin.

Documentation

Index

Constants

View Source
const (
	// ExactMatch matches only on exact match of the part
	ExactMatch = "exact"
	// PrefixMatch matches when the part begins with the matching string
	PrefixMatch = "prefix"
	// SuffixMatch matches when the part ends with the matching string
	SuffixMatch = "suffix"
	// SubstringMatch matches on partial match of the part
	SubstringMatch = "substring"
	// RegexMatch matches when the part matches a regular expression and the regex is used in the rewrite
	RegexMatch = "regex"
	// FullRegexMatch matchs when the part matches a regular expression and the regex is not used in the rewrite
	FullRegexMatch = "fullregex"
	// NoOpMatch placeholder that does nothing
	NoOpMatch = "noop"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ResponseRewriter

type ResponseRewriter struct {
	dns.ResponseWriter

	Rules []*nameRule
	// contains filtered or unexported fields
}

ResponseRewriter rewrites answers, additional and authority sections

func NewResponseRewriter

func NewResponseRewriter(w dns.ResponseWriter, r *dns.Msg) *ResponseRewriter

NewResponseRewriter returns a pointer to a new ResponseRewriter.

func (*ResponseRewriter) WriteMsg

func (r *ResponseRewriter) WriteMsg(res *dns.Msg) error

WriteMsg records the status code and calls the underlying ResponseWriter's WriteMsg method.

type Result

type Result int

Result is the result of a rewrite

const (
	// RewriteIgnored is returned when rewrite is not done on request.
	RewriteIgnored Result = iota
	// RewriteDone is returned when rewrite is done on request.
	RewriteDone
)

type RewriteNG

type RewriteNG struct {
	Next  plugin.Handler
	Rules []*nameRule
}

RewriteNG implements the rewriteng plugin

func (RewriteNG) Name

func (h RewriteNG) Name() string

Name implements the Handler interface.

func (RewriteNG) ServeDNS

func (h RewriteNG) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error)

ServeDNS implements the plugin.Handler interface.

type Rule

type Rule interface {
	Rewrite(ctx context.Context, state request.Request) Result
	Sub(n string) string
	RRPart() string
}

Rule describes a rewrite rule.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL