Skip to content

Commit

Permalink
CLOUDFLAREAPI: CF_SINGLE_REDIRECT improvements: fix bugs, log transla…
Browse files Browse the repository at this point in the history
…ted redirects (#3051)
  • Loading branch information
tlimoncelli authored Jul 18, 2024
1 parent 1d348de commit 0869052
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 217 deletions.
14 changes: 12 additions & 2 deletions commands/types/dnscontrol.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ declare function CAA(name: string, tag: "issue" | "issuewild" | "iodef", value:
declare function CAA_BUILDER(opts: { label?: string; iodef: string; iodef_critical?: boolean; issue: string[]; issue_critical?: boolean; issuewild: string[]; issuewild_critical?: boolean; ttl?: Duration }): DomainModifier;

/**
* WARNING: Cloudflare is removing this feature and replacing it with a new
* feature called "Dynamic Single Redirect". DNSControl will automatically
* generate "Dynamic Single Redirects" for a limited number of use cases. See
* [`CLOUDFLAREAPI`](../provider/cloudflareapi.md) for details.
*
* `CF_REDIRECT` uses Cloudflare-specific features ("Forwarding URL" Page Rules) to
* generate a HTTP 301 permanent redirect.
*
Expand Down Expand Up @@ -533,6 +538,11 @@ declare function CF_REDIRECT(source: string, destination: string, ...modifiers:
declare function CF_SINGLE_REDIRECT(name: string, code: number, when: string, then: string, ...modifiers: RecordModifier[]): DomainModifier;

/**
* WARNING: Cloudflare is removing this feature and replacing it with a new
* feature called "Dynamic Single Redirect". DNSControl will automatically
* generate "Dynamic Single Redirects" for a limited number of use cases. See
* [`CLOUDFLAREAPI`](../provider/cloudflareapi.md) for details.
*
* `CF_TEMP_REDIRECT` uses Cloudflare-specific features ("Forwarding URL" Page
* Rules) to generate a HTTP 302 temporary redirect.
*
Expand Down Expand Up @@ -1810,7 +1820,7 @@ declare function LOC_BUILDER_STR(opts: { label?: string; str: string; alt?: numb
*
* ```javascript
* D("example.com", REG_MY_PROVIDER, DnsProvider(DSP_MY_PROVIDER),
* M365_BUILDER({
* M365_BUILDER("example.com", {
* initialDomain: "example.onmicrosoft.com",
* }),
* END);
Expand All @@ -1822,7 +1832,7 @@ declare function LOC_BUILDER_STR(opts: { label?: string; str: string; alt?: numb
*
* ```javascript
* D("example.com", REG_MY_PROVIDER, DnsProvider(DSP_MY_PROVIDER),
* M365_BUILDER({
* M365_BUILDER("example.com", {
* label: "test",
* mx: false,
* autodiscover: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ parameter_types:
"modifiers...": RecordModifier[]
---

{% hint style="warning" %}
WARNING: Cloudflare is removing this feature and replacing it with a new
feature called "Dynamic Single Redirect". DNSControl will automatically
generate "Dynamic Single Redirects" for a limited number of use cases. See
[`CLOUDFLAREAPI`](../provider/cloudflareapi.md) for details.
{% endhint %}

`CF_REDIRECT` uses Cloudflare-specific features ("Forwarding URL" Page Rules) to
generate a HTTP 301 permanent redirect.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ parameter_types:
"modifiers...": RecordModifier[]
---

{% hint style="warning" %}
**WARNING**: Cloudflare is removing this feature and replacing it with a new
feature called "Dynamic Single Redirect". DNSControl will automatically
generate "Dynamic Single Redirects" for a limited number of use cases. See
[`CLOUDFLAREAPI`](../provider/cloudflareapi.md) for details.
{% endhint %}

`CF_TEMP_REDIRECT` uses Cloudflare-specific features ("Forwarding URL" Page
Rules) to generate a HTTP 302 temporary redirect.

Expand Down
56 changes: 52 additions & 4 deletions documentation/provider/cloudflareapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ Enable it using:

```javascript
var DSP_CLOUDFLARE = NewDnsProvider("cloudflare", {
"manage_redirects": true
"manage_redirects": true,
"transcode_log": "transcode.log",
});
```

Expand All @@ -231,8 +232,7 @@ New-style redirects ("Single Redirect Rules") are a new feature of DNSControl
as of v4.12.0 and may have bugs. Please test carefully.
{% endhint %}


Conversion mode:
### Conversion mode:

DNSControl can convert from old-style redirects (Page Rules) to new-style
redirect (Single Redirects). To enable this mode, set both `manage_redirects`
Expand Down Expand Up @@ -268,7 +268,7 @@ via the CloudFlare control panel or wait for Cloudflare to remove support for th

{% hint style="warning" %}
Cloudflare's announcement says that they will convert old-style redirects (Page Rules) to new-style
redirect (Single Redirects) but they do not give a date for when this will happen. DNSControl
redirect (Single Redirects) but they do not give an exact date for when this will happen. DNSControl
will probably see these new redirects as foreign and delete them.

Therefore it is probably safer to do the conversion ahead of them.
Expand All @@ -279,6 +279,54 @@ than DNSControl's. However there's no way for DNSControl to manage them since t
If you have suggestions on how to handle this better please file a bug.
{% endhint %}

### Converting to CF_SINGLE_REDIRECT permanently

DNSControl will help convert `CF_REDIRECT`/`CF_TEMP_REDIRECT` statements into
`CF_SINGLE_REDIRECT` statements. You might choose to do this if you do not want
to rely on the automatic translation, or if you want to edit the results of the
translation.

DNSControl will generate a file of the translated statements if you specify
a filename using the `transcode_log` meta option.

```javascript
var DSP_CLOUDFLARE = NewDnsProvider("cloudflare", {
"manage_single_redirects": true,
"transcode_log": "transcode.log",
});
```

After running `dnscontrol preview` the contents will look something like this:

{% code title="transcode.log" %}
```text
D("example.com", ...
CF_SINGLE_REDIRECT("1,302,https://example.com/*,https://replacement.example.com/$1",
302,
'http.host eq "example.com"',
'concat("https://replacement.example.com", http.request.uri.path)'
),
CF_SINGLE_REDIRECT("2,302,https://img.example.com/*,https://replacement.example.com/$1",
302,
'http.host eq "img.example.com"',
'concat("https://replacement.example.com", http.request.uri.path)'
),
CF_SINGLE_REDIRECT("3,302,https://i.example.com/*,https://replacement.example.com/$1",
302,
'http.host eq "i.example.com"',
'concat("https://replacement.example.com", http.request.uri.path)'
),
D("otherdomain.com", ...
CF_SINGLE_REDIRECT("1,301,https://one.otherdomain.com/,https://www.google.com/",
301,
'http.host eq "one.otherdomain.com" and http.request.uri.path eq "/"',
'concat("https://www.google.com/", "")'
),
```
{% endcode %}

Copying the statements to the proper place in `dnsconfig.js` is manual.


## Redirects
The Cloudflare provider can manage "Forwarding URL" Page Rules (redirects) for your domains. Simply use the `CF_REDIRECT` and `CF_TEMP_REDIRECT` functions to make redirects:
Expand Down
3 changes: 2 additions & 1 deletion integrationTest/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ func cfSingleRedirectEnabled() bool {
}

func cfSingleRedirect(name string, code any, when, then string) *models.RecordConfig {
r := makeRec("@", name, "CLOUDFLAREAPI_SINGLE_REDIRECT")
r := makeRec("@", name, cfsingleredirect.SINGLEREDIRECT)
err := cfsingleredirect.FromRaw(r, []any{name, code, when, then})
if err != nil {
panic("Should not happen... cfSingleRedirect")
Expand Down Expand Up @@ -1947,6 +1947,7 @@ func makeTests() []*TestGroup {
tc("changecode", cfSingleRedirect(`name1`, `302`, `http.host eq "cnn.slackoverflow.com"`, `concat("https://www.cnn.com", http.request.uri.path)`)),
tc("changewhen", cfSingleRedirect(`name1`, `302`, `http.host eq "msnbc.slackoverflow.com"`, `concat("https://www.cnn.com", http.request.uri.path)`)),
tc("changethen", cfSingleRedirect(`name1`, `302`, `http.host eq "msnbc.slackoverflow.com"`, `concat("https://www.msnbc.com", http.request.uri.path)`)),
tc("changename", cfSingleRedirect(`name1bis`, `302`, `http.host eq "msnbc.slackoverflow.com"`, `concat("https://www.msnbc.com", http.request.uri.path)`)),
),

// CLOUDFLAREAPI: PROXY
Expand Down
5 changes: 3 additions & 2 deletions models/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,18 @@ type CloudflareSingleRedirectConfig struct {
//
Code uint16 `json:"code,omitempty"` // 301 or 302
// PR == PageRule
PRDisplay string `json:"pr_display,omitempty"` // How is this displayed to the user
PRWhen string `json:"pr_when,omitempty"`
PRThen string `json:"pr_then,omitempty"`
PRPriority int `json:"pr_priority,omitempty"` // Really an identifier for the rule.
PRDisplay string `json:"pr_display,omitempty"` // How is this displayed to the user (SetTarget) for CF_REDIRECT/CF_TEMP_REDIRECT
//
// SR == SingleRedirect
SRDisplay string `json:"sr_display,omitempty"` // How is this displayed to the user
SRName string `json:"sr_name,omitempty"` // How is this displayed to the user
SRWhen string `json:"sr_when,omitempty"`
SRThen string `json:"sr_then,omitempty"`
SRRRulesetID string `json:"sr_rulesetid,omitempty"`
SRRRulesetRuleID string `json:"sr_rulesetruleid,omitempty"`
SRDisplay string `json:"sr_display,omitempty"` // How is this displayed to the user (SetTarget) for CF_SINGLE_REDIRECT
}

// MarshalJSON marshals RecordConfig.
Expand Down
1 change: 1 addition & 0 deletions pkg/rtypes/postprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func PostProcess(domains []*models.DomainConfig) error {

case "CLOUDFLAREAPI_SINGLE_REDIRECT":
err = cfsingleredirect.FromRaw(rec, rawRec.Args)
rec.SetLabel("@", dc.Name)

default:
err = fmt.Errorf("unknown rawrec type=%q", rawRec.Type)
Expand Down
Loading

0 comments on commit 0869052

Please sign in to comment.