Skip to content

Commit

Permalink
Merge pull request #14 from nhymxu/choose-service-public-ip
Browse files Browse the repository at this point in the history
support custom public ip service
  • Loading branch information
nhymxu authored Nov 20, 2023
2 parents 65be08f + 0591ec5 commit b04e37a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ Dynamic DNS record update utility for CloudFlare DNS service. Python implement
### Download & Setup

#### Download
Download code from Release page.
Download code from Release page.

Link here: [https://github.com/nhymxu/cf-dns-update-python/releases/latest](https://github.com/nhymxu/cf-dns-update-python/releases/latest)

Or
Or

clone repo using git:

Expand Down Expand Up @@ -51,7 +51,7 @@ CF_API_TOKEN = token_key_here
2. Copy `Zone ID`
3. Edit like sample

Notes:
Notes:
- `@` will be same as `base_domain`
- To proxy a record, include it in both `records` and `proxied_records`

Expand All @@ -72,3 +72,23 @@ And add this line to end of file
```

This script will run each 15 minutes.

### Service get public IP

If you want custom what service to get public IP, you can config in `config.ini` file

Sample

```ini
[common]
CHECK_IP_SERVICE = xxx
```

where xxx is service name listed below

| Name | Endpoint |
| ---- | -------- |
| amazonaws | https://checkip.amazonaws.com |
| ifconfig.me | https://ifconfig.me/ip |
| icanhazip | https://icanhazip.com/ |
| ipecho | https://ipecho.net/plain |
27 changes: 18 additions & 9 deletions cf-dns-update.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

CF_API_TOKEN = ''
IS_DRYRUN = False
PUBLIC_IP_SERVICE = 'amazonaws'


def make_request(method="GET", url="", request_body=None):
Expand Down Expand Up @@ -57,14 +58,17 @@ def get_local_ip():
:return: string
"""

'''
Other domain can using to get IP:
ifconfig.me
icanhazip.com
ipecho.net/plain
ifconfig.co
'''
endpoint = "https://checkip.amazonaws.com/"
svc_list = {
'amazonaws': 'https://checkip.amazonaws.com/',
'ifconfig.me': 'https://ifconfig.me/ip',
'icanhazip.com': 'https://icanhazip.com/',
'ipecho': 'https://ipecho.net/plain',
}

if PUBLIC_IP_SERVICE not in svc_list:
raise ValueError("Unknown service")

endpoint = svc_list[PUBLIC_IP_SERVICE]

return make_request(url=endpoint).strip().decode('utf-8')

Expand Down Expand Up @@ -171,6 +175,7 @@ def get_config(config_path='config.ini'):
:return:
"""
global CF_API_TOKEN
global PUBLIC_IP_SERVICE

if not path.exists(config_path):
raise RuntimeError("config file not found")
Expand All @@ -186,6 +191,9 @@ def get_config(config_path='config.ini'):

CF_API_TOKEN = config['common']['CF_API_TOKEN']

if config['common'].get('CHECK_IP_SERVICE'):
PUBLIC_IP_SERVICE = config['common']['CHECK_IP_SERVICE']

config_sections = config.sections()
config_sections.remove("common")

Expand Down Expand Up @@ -235,7 +243,8 @@ def main(args):

public_ip = get_local_ip()
print("")
print("--- Public IP: {}".format(public_ip))
print("--- [{}] Public IP: {}".format(PUBLIC_IP_SERVICE, public_ip))

if not IS_DRYRUN and public_ip == get_old_ip():
print("Skip update")
exit()
Expand Down

0 comments on commit b04e37a

Please sign in to comment.