Skip to content

Instantly share code, notes, and snippets.

@sam-heller
Last active August 3, 2022 09:05
Show Gist options
  • Save sam-heller/ae959698111508cce2f5fb32fb6fbd0f to your computer and use it in GitHub Desktop.
Save sam-heller/ae959698111508cce2f5fb32fb6fbd0f to your computer and use it in GitHub Desktop.

Revisions

  1. sam-heller revised this gist Mar 15, 2021. No changes.
  2. sam-heller created this gist Mar 9, 2021.
    43 changes: 43 additions & 0 deletions postman-cloudflare-migadu.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    //Parent Request to https://api.cloudflare.com/client/v4/zones/**zone_id**/dns_records?type=mx,txt,cname,srv
    const current_zone = ''; //Zone ID
    const verify_code = ''; //value from the hosted-email-verify TXT entry supplied by Migadu
    const host = ''; //Hostname
    const cloudflare_token = ''; //Cloudflare API Token
    const base_url = 'https://api.cloudflare.com/client/v4';
    const dns_records_url = base_url + '/zones/' + current_zone + '/dns_records/';
    request = {header: {"Authorization" : "Bearer " + cloudflare_token,"Content-Type" : "application/json"}};

    const updates = [
    {"name": "**host**","type": "MX","content": "aspmx1.migadu.com","priority": 10,"proxied": false},
    {"name": "**host**","type": "MX","content": "aspmx2.migadu.com","priority": 20,"proxied": false},
    {"name": "**host**","type": "TXT","content": "v=spf1 include:spf.migadu.com -all","proxied": false},
    {"name": "**host**","type": "TXT","content": "hosted-email-verify=**verify_code**"},
    {"name": "_dmarc.**host**","type": "TXT","content": "v=DMARC1; p=quarantine;"},
    {"name": "autoconfig.**host**","type": "CNAME", "content": "autoconfig.migadu.com","proxied": false},
    {"name": "key1._domainkey.**host**","type": "CNAME", "content": "key1.**host**._domainkey.migadu.com", "proxied": false},
    {"name": "key2._domainkey.**host**","type": "CNAME", "content": "key2.**host**._domainkey.migadu.com","proxied": false},
    {"name": "key3._domainkey.**host**","type": "CNAME", "content": "key3.**host**._domainkey.migadu.com","proxied": false},
    {"type": "SRV","data": {"service": "_autodiscover", "proto" : "_tcp","name": "**host**","target": "autodiscover.migadu.com","port": 443, "weight" : 1,"priority": 0}},
    {"type": "SRV","data": {"service": "_submissions","name": "**host**","port": 993,"priority": 0,"proto": "_tcp","target": "smtp.migadu.com","weight": 1}},
    {"type": "SRV","data": {"service": "_imaps","name": "**host**","port": 993,"priority": 0,"proto": "_tcp","target": "imap.migadu.com","weight": 1}}
    ];

    for (let r of pm.response.json().result){
    request.url = dns_records_url + r.id;
    request.method = 'DELETE';
    pm.sendRequest(request, (err, resp) => {console.log(err, resp);})
    }

    for (let rule of updates){
    if (rule.type == 'SRV'){
    rule.data.name = rule.data.name.replace('**host**', host);
    } else {
    request.url = dns_records_url;
    request.method = 'POST';
    rule.name = rule.name.replace('**host**', host);
    rule.content = rule.content.replace('**host**', host);
    rule.content = rule.content.replace('**verify_code**', verify_code);
    }
    request.body = {mode: 'raw', raw: JSON.stringify(rule)}
    pm.sendRequest(request, (err, resp) => {console.log(err, resp);})
    }