Mikrotik PPTP VPN with LAN IP Access

Free Cloudflare DDNS for Mikrotik

Free Cloudflare DDNS for Mikrotik, Mikrotik Public IP automatically update to Cloudflare custom domain.



Create Cloudflare Free Account 
Add your domain to Cloudflare 

Create Sub Domain name
server.karimzi.com

Copy Global Key
4d985f1df737dfabf836e528af433fb15dd84 (Example)

Create Domain API Token
JNlYqLOxsS2QUUX3F-FcchFcFUKcakpS6W-TQUjK 
(Example)

Verify API
curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \
     -H "Authorization: Bearer <API Token>" \
     -H "Content-Type:application/json"

Copy Zone ID
8a37eeeb82acd5273fdf508fbf27a054

Generate Record ID

 curl -X GET "https://api.cloudflare.com/client/v4/zones/8a37eeeb82acd5273fdf508fbf27a054/dns_records?name=test.mamayaan.com" \
     -H "X-Auth-Email: <Cloudflare Account Email>" \
    -H "X-Auth-Key: <Global Key>" \
     -H "Content-Type: application/json"


Input Script into Mikrotik



# Cloudflare Dynamic DNS update script
# Required policy: read, write, test, policy
# Add this script to scheduler
# Install DigiCert root CA or disable check-certificate
# Configuration ---------------------------------------------------------------------

:local TOKEN "-----------------"
:local ZONEID "-----------------------"
:local RECORDID "--------------"
:local RECORDNAME "-------------"
:local WANIF "-----------------"

#------------------------------------------------------------------------------------

:global IP4NEW
:global IP4CUR

:local url "https://api.cloudflare.com/client/v4/zones/$ZONEID/dns_records/$RECORDID/"

:if ([/interface get $WANIF value-name=running]) do={
# Get the current public IP
    :local requestip [tool fetch url="https://ipv4.icanhazip.com" mode=https check-certificate=no output=user as-value]
    :set IP4NEW [:pick ($requestip->"data") 0 ([:len ($requestip->"data")]-1)]
# Check if IP has changed
    :if ($IP4NEW != $IP4CUR) do={
        :log info "CF-DDNS: Public IP changed to $IP4NEW, updating"
        :local cfapi [/tool fetch http-method=put mode=https url=$url check-certificate=no output=user as-value \
            http-header-field="Authorization: Bearer $TOKEN,Content-Type: application/json" \
            http-data="{\"type\":\"A\",\"name\":\"$RECORDNAME\",\"content\":\"$IP4NEW\",\"ttl\":120,\"proxied\":false}"]
        :set IP4CUR $IP4NEW
        :log info "CF-DDNS: Host $RECORDNAME updated with IP $IP4CUR"
    }  else={
        :log info "CF-DDNS: Previous IP $IP4NEW not changed, quitting"
    }
} else={
    :log info "CF-DDNS: $WANIF is not currently running, quitting"
}


Add Scheduler in Mikrotik
/system script run <script name>

Video : https://www.youtube.com/watch?v=aphMbK3duqo





Comments