Skip to content

Commit 0869052

Browse files
authored
CLOUDFLAREAPI: CF_SINGLE_REDIRECT improvements: fix bugs, log translated redirects (#3051)
1 parent 1d348de commit 0869052

File tree

14 files changed

+309
-217
lines changed

14 files changed

+309
-217
lines changed

commands/types/dnscontrol.d.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,11 @@ declare function CAA(name: string, tag: "issue" | "issuewild" | "iodef", value:
472472
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;
473473

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

535540
/**
541+
* WARNING: Cloudflare is removing this feature and replacing it with a new
542+
* feature called "Dynamic Single Redirect". DNSControl will automatically
543+
* generate "Dynamic Single Redirects" for a limited number of use cases. See
544+
* [`CLOUDFLAREAPI`](../provider/cloudflareapi.md) for details.
545+
*
536546
* `CF_TEMP_REDIRECT` uses Cloudflare-specific features ("Forwarding URL" Page
537547
* Rules) to generate a HTTP 302 temporary redirect.
538548
*
@@ -1810,7 +1820,7 @@ declare function LOC_BUILDER_STR(opts: { label?: string; str: string; alt?: numb
18101820
*
18111821
* ```javascript
18121822
* D("example.com", REG_MY_PROVIDER, DnsProvider(DSP_MY_PROVIDER),
1813-
* M365_BUILDER({
1823+
* M365_BUILDER("example.com", {
18141824
* initialDomain: "example.onmicrosoft.com",
18151825
* }),
18161826
* END);
@@ -1822,7 +1832,7 @@ declare function LOC_BUILDER_STR(opts: { label?: string; str: string; alt?: numb
18221832
*
18231833
* ```javascript
18241834
* D("example.com", REG_MY_PROVIDER, DnsProvider(DSP_MY_PROVIDER),
1825-
* M365_BUILDER({
1835+
* M365_BUILDER("example.com", {
18261836
* label: "test",
18271837
* mx: false,
18281838
* autodiscover: false,

documentation/language-reference/domain-modifiers/CF_REDIRECT.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ parameter_types:
1111
"modifiers...": RecordModifier[]
1212
---
1313

14+
{% hint style="warning" %}
15+
WARNING: Cloudflare is removing this feature and replacing it with a new
16+
feature called "Dynamic Single Redirect". DNSControl will automatically
17+
generate "Dynamic Single Redirects" for a limited number of use cases. See
18+
[`CLOUDFLAREAPI`](../provider/cloudflareapi.md) for details.
19+
{% endhint %}
20+
1421
`CF_REDIRECT` uses Cloudflare-specific features ("Forwarding URL" Page Rules) to
1522
generate a HTTP 301 permanent redirect.
1623

documentation/language-reference/domain-modifiers/CF_TEMP_REDIRECT.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ parameter_types:
1111
"modifiers...": RecordModifier[]
1212
---
1313

14+
{% hint style="warning" %}
15+
**WARNING**: Cloudflare is removing this feature and replacing it with a new
16+
feature called "Dynamic Single Redirect". DNSControl will automatically
17+
generate "Dynamic Single Redirects" for a limited number of use cases. See
18+
[`CLOUDFLAREAPI`](../provider/cloudflareapi.md) for details.
19+
{% endhint %}
20+
1421
`CF_TEMP_REDIRECT` uses Cloudflare-specific features ("Forwarding URL" Page
1522
Rules) to generate a HTTP 302 temporary redirect.
1623

documentation/provider/cloudflareapi.md

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ Enable it using:
211211

212212
```javascript
213213
var DSP_CLOUDFLARE = NewDnsProvider("cloudflare", {
214-
"manage_redirects": true
214+
"manage_redirects": true,
215+
"transcode_log": "transcode.log",
215216
});
216217
```
217218

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

234-
235-
Conversion mode:
235+
### Conversion mode:
236236

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

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

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

282+
### Converting to CF_SINGLE_REDIRECT permanently
283+
284+
DNSControl will help convert `CF_REDIRECT`/`CF_TEMP_REDIRECT` statements into
285+
`CF_SINGLE_REDIRECT` statements. You might choose to do this if you do not want
286+
to rely on the automatic translation, or if you want to edit the results of the
287+
translation.
288+
289+
DNSControl will generate a file of the translated statements if you specify
290+
a filename using the `transcode_log` meta option.
291+
292+
```javascript
293+
var DSP_CLOUDFLARE = NewDnsProvider("cloudflare", {
294+
"manage_single_redirects": true,
295+
"transcode_log": "transcode.log",
296+
});
297+
```
298+
299+
After running `dnscontrol preview` the contents will look something like this:
300+
301+
{% code title="transcode.log" %}
302+
```text
303+
D("example.com", ...
304+
CF_SINGLE_REDIRECT("1,302,https://example.com/*,https://replacement.example.com/$1",
305+
302,
306+
'http.host eq "example.com"',
307+
'concat("https://replacement.example.com", http.request.uri.path)'
308+
),
309+
CF_SINGLE_REDIRECT("2,302,https://img.example.com/*,https://replacement.example.com/$1",
310+
302,
311+
'http.host eq "img.example.com"',
312+
'concat("https://replacement.example.com", http.request.uri.path)'
313+
),
314+
CF_SINGLE_REDIRECT("3,302,https://i.example.com/*,https://replacement.example.com/$1",
315+
302,
316+
'http.host eq "i.example.com"',
317+
'concat("https://replacement.example.com", http.request.uri.path)'
318+
),
319+
D("otherdomain.com", ...
320+
CF_SINGLE_REDIRECT("1,301,https://one.otherdomain.com/,https://www.google.com/",
321+
301,
322+
'http.host eq "one.otherdomain.com" and http.request.uri.path eq "/"',
323+
'concat("https://www.google.com/", "")'
324+
),
325+
```
326+
{% endcode %}
327+
328+
Copying the statements to the proper place in `dnsconfig.js` is manual.
329+
282330

283331
## Redirects
284332
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:

integrationTest/integration_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ func cfSingleRedirectEnabled() bool {
503503
}
504504

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

19521953
// CLOUDFLAREAPI: PROXY

models/record.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,18 @@ type CloudflareSingleRedirectConfig struct {
154154
//
155155
Code uint16 `json:"code,omitempty"` // 301 or 302
156156
// PR == PageRule
157-
PRDisplay string `json:"pr_display,omitempty"` // How is this displayed to the user
158157
PRWhen string `json:"pr_when,omitempty"`
159158
PRThen string `json:"pr_then,omitempty"`
160159
PRPriority int `json:"pr_priority,omitempty"` // Really an identifier for the rule.
160+
PRDisplay string `json:"pr_display,omitempty"` // How is this displayed to the user (SetTarget) for CF_REDIRECT/CF_TEMP_REDIRECT
161161
//
162162
// SR == SingleRedirect
163-
SRDisplay string `json:"sr_display,omitempty"` // How is this displayed to the user
163+
SRName string `json:"sr_name,omitempty"` // How is this displayed to the user
164164
SRWhen string `json:"sr_when,omitempty"`
165165
SRThen string `json:"sr_then,omitempty"`
166166
SRRRulesetID string `json:"sr_rulesetid,omitempty"`
167167
SRRRulesetRuleID string `json:"sr_rulesetruleid,omitempty"`
168+
SRDisplay string `json:"sr_display,omitempty"` // How is this displayed to the user (SetTarget) for CF_SINGLE_REDIRECT
168169
}
169170

170171
// MarshalJSON marshals RecordConfig.

pkg/rtypes/postprocess.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func PostProcess(domains []*models.DomainConfig) error {
3838

3939
case "CLOUDFLAREAPI_SINGLE_REDIRECT":
4040
err = cfsingleredirect.FromRaw(rec, rawRec.Args)
41+
rec.SetLabel("@", dc.Name)
4142

4243
default:
4344
err = fmt.Errorf("unknown rawrec type=%q", rawRec.Type)

0 commit comments

Comments
 (0)