Giter VIP home page Giter VIP logo

cloudflareddns's Introduction

cloudflareddns

Build Status PyPI version Buy Me a Coffee

A tiny command line utility for implementing DDNS with Cloudflare.

  • Supports virtually any server that is capable of running Python
  • Synology DiskStations supported
  • Quick to install using yum/dnf or pip

Synopsis

Update DNS A record for foo.example.com to 1.2.3.4

cloudflareddns --hostname foo.example.com --ip 1.2.3.4

Likewise, for an AAAA record:

cloudflareddns --hostname foo.example.com --ip 2001:0db8:85a3:0000:0000:8a2e:0370:7334

Get all options by simply running cloudflareddns -h:

usage: cloudflareddns [-h] [--email EMAIL] [--key KEY] [--hostname HOSTNAME]
                      [--ip IP] [--ttl TTL] [--verbose] [--version]

Update DDNS in Cloudflare.

optional arguments:
  -h, --help           show this help message and exit
  --email EMAIL        Cloudflare account email (omit if using API tokens)
  --key KEY            Cloudflare API key or token
  --hostname HOSTNAME  Hostname to set IP for
  --ip IP              The IP address
  --ttl TTL            TTL in seconds
  --verbose
  --version            show program's version number and exit

When invoked without any options, cloudflareddns will try to point the FQDN (aka <hostname>.<domain-name> of the machine it runs on to its public IP address (auto-detected).

Install and use with Synology DiskStations

You can configure a Synology DiskStation with CloudFlare DDNS.

It's worth noting that if your Synology DSM is recent enough, you can simply use Synology's own DDNS service, then create a CNAME record at your domain that points to it. The downside to this solution, however, is extra DNS lookup required to resolve domain to IP.

Alternative solution is to use cloudflaredns which ships with the necessary CLI interface for Synology compatibility: cloudflareddns-syno.

Step 1. Access Synology via SSH

  • Login to your DSM
  • Go to Control Panel > Terminal & SNMP > Enable SSH service
  • Use your client or commandline to access Synology. If you don't have any, I recommend you try out Putty for Windows.
  • Use your Synology admin account to connect.

Step 2. Install cloudflareddns

If you're not a lazy man, checkout instructions on installing using virtualenv for this step. For quick setup instead:

curl https://bootstrap.pypa.io/get-pip.py | python
pip install cloudflareddns

Run the following command to add new DDNS provider:

cat >> /etc/ddns_provider.conf << 'EOF'
[USER_Cloudflare]
        modulepath=/bin/cloudflareddns-syno
        queryurl=https://www.cloudflare.com/
EOF

Step 3. Get Cloudflare parameters

It is recommended to use a Cloudflare API token. Check the wiki page for instructions on how to get an API token with the most secure permissions.

Alternatively, you can get Cloudflare global API key in your account settings.

Step 4. Setup DDNS

  • Login to your DSM
  • Go to Control Panel > External Access > DDNS > Add
  • Select Cloudflare as service provider
  • Enter your domain as hostname
  • If using token authentication: enter x in the Username/Email, and API token as Password/Key. The requirement to put x is due to Synology GUI's constraints not allowing for an empty field
  • If using global API key: enter your Cloudflare account as Username/Email, and API key as Password/Key

Installation for CentOS/RHEL 7, 8

sudo yum -y install https://extras.getpagespeed.com/release-latest.rpm
sudo yum install cloudflareddns

Installation for other systems

Installing with pip is easiest:

pip install cloudflareddns

Usage in Python scripts

from cloudflareddns import cloudflareddns
hostname = 'foo.example.com'
ip = '1.2.3.4'
if cloudflareddns.updateRecord(hostname, ip):
  print('Record is OK')
  ...

Requires using environment variables (see tips below).

Specifying Cloudflare credentials

In non-Synology systems, you can store Cloudflare credentials in either environment variables or a configuration file.

Via configuration file

Create ~/.cloudflare/cloudflare.cfg and put:

[CloudFlare]
email = [email protected] # Do not set if using an API Token
token = xxxxxxxxxxxxxxxxxxxxxxxxxxx

Via environment variables

You can put your Cloudflare credentials into the ~/.bashrc file:

export CF_API_EMAIL="[email protected]" # Do not set if using an API Token
export CF_API_KEY="xxxxxx"

Don't forget to source ~/.bashrc if you have just put credentials in there. The cloudflareddns will pick those up, so no need to pass --email or --key every time.

cloudflareddns's People

Contributors

dvershinin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cloudflareddns's Issues

Unexpected keyword argument 'cfEmail'

On my Synology NAS, when I run cloudflareddns-syno from the command line I get an error. Note that "api-token, hostname, and IP" are replaced with the values for my setup.

/bin/cloudflareddns-syno x api-token hostname IP

I receive the following error:

Traceback (most recent call last):
  File "/bin/cloudflareddns-syno", line 8, in <module>
    sys.exit(syno())
  File "/usr/lib/python2.7/site-packages/cloudflareddns/cloudflareddns.py", line 229, in syno
    cfKey=sys.argv[4]
TypeError: update() got an unexpected keyword argument 'cfEmail'

And the DSM GUI fails with an unknown error.

When I use the same variables and call cloudflareddns, the DDNS updates as expected.

/bin/cloudflareddns --key api-token --hostname hostname --verbose

DEBUG: Verbose output.
DEBUG: Getting public IP from an external service
DEBUG: Starting new HTTPS connection (1): ident.me:443
DEBUG: https://ident.me:443 "GET / HTTP/1.1" 200 14
DEBUG: Updating foo to IP
DEBUG: Zone domain of hostname is foo
DEBUG: Starting new HTTPS connection (1): api.cloudflare.com:443
DEBUG: https://api.cloudflare.com:443 "GET /client/v4/zones?name=foo HTTP/1.1" 200 None
DEBUG: Zone ID is xxx
DEBUG: https://api.cloudflare.com:443 "GET /client/v4/zones/xxx/dns_records?type=A&name=foo&match=all HTTP/1.1" 200 None
INFO: UNCHANGED: foo == IP

Any ideas or debugging tips?

Permissions error when running "curl https://bootstrap.pypa.io/get-pip.py | python" on synology NAS

This is what I see when I enter "curl https://bootstrap.pypa.io/get-pip.py | python"
curl https://bootstrap.pypa.io/get-pip.py | python
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1825k 100 1825k 0 0 5668k 0 --:--:-- --:--:-- --:--:-- 5668k
/usr/local/lib/python2.7/site-packages/cffi/model.py:526: UserWarning: 'point_conversion_form_t' has no values explicitly defined; next version will refuse to guess which integer type it is meant to be (unsigned/signed, int/long)
% self._get_c_name())
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Collecting pip
Using cached pip-20.1.1-py2.py3-none-any.whl (1.5 MB)
Collecting wheel
Using cached wheel-0.34.2-py2.py3-none-any.whl (26 kB)
Installing collected packages: pip, wheel
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/site-packages/pip-20.1.1.dist-info'
Check the permissions.

I am also noob :)

DDNS Update Failed

I successfully install your module in the lazier way without virtualenv and I did found cloudflareddns under /bin. However, when I tried to add a Cloudflare ddns in GUI, it failed and show message "Failed to connect to the server. Please check the network connection of the server.". Anyway, I tried to manually execute this script with --email and other parameters, it succeeded smoothly. So I set it as schedule task to run every hour. Could you help me figure out what's going wrong?

Set Proxied to its current value or disabled by default

Could you set proxied as

  • disabled by default if creating a new record
  • its current value if updating a existing record

right now you force Proxied=true, which breaks any current configuration where a service would not be proxied through Cloudflare.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.