소스 검색

Initial Commit

Joshua 8 년 전
커밋
3cbb4ed859
6개의 변경된 파일170개의 추가작업 그리고 0개의 파일을 삭제
  1. 5 0
      .dockerignore
  2. 17 0
      .gitattributes
  3. 45 0
      .gitignore
  4. 10 0
      Dockerfile
  5. 92 0
      cloudflare.py
  6. 1 0
      requirements.txt

+ 5 - 0
.dockerignore

@@ -0,0 +1,5 @@
+.git
+.gitignore
+.github
+.gitattributes
+README.md

+ 17 - 0
.gitattributes

@@ -0,0 +1,17 @@
+# Auto detect text files and perform LF normalization
+* text=auto
+
+# Custom for Visual Studio
+*.cs     diff=csharp
+
+# Standard to msysgit
+*.doc	 diff=astextplain
+*.DOC	 diff=astextplain
+*.docx diff=astextplain
+*.DOCX diff=astextplain
+*.dot  diff=astextplain
+*.DOT  diff=astextplain
+*.pdf  diff=astextplain
+*.PDF	 diff=astextplain
+*.rtf	 diff=astextplain
+*.RTF	 diff=astextplain

+ 45 - 0
.gitignore

@@ -0,0 +1,45 @@
+# Windows image file caches
+Thumbs.db
+ehthumbs.db
+
+# Folder config file
+Desktop.ini
+
+# Recycle Bin used on file shares
+$RECYCLE.BIN/
+
+# Windows Installer files
+*.cab
+*.msi
+*.msm
+*.msp
+
+# Windows shortcuts
+*.lnk
+
+# =========================
+# Operating System Files
+# =========================
+
+# OSX
+# =========================
+
+.DS_Store
+.AppleDouble
+.LSOverride
+
+# Thumbnails
+._*
+
+# Files that might appear on external disk
+.Spotlight-V100
+.Trashes
+
+# Directories potentially created on remote AFP share
+.AppleDB
+.AppleDesktop
+Network Trash Folder
+Temporary Items
+.apdisk
+
+.idea

+ 10 - 0
Dockerfile

@@ -0,0 +1,10 @@
+FROM python:3.6.2-alpine3.6
+
+ADD cloudflare.py /app
+ADD requirements.txt /app
+
+WORKDIR "/app"
+
+CMD [ "pip", "install", "requirements.txt" ]
+
+CMD [ "python", "cloudflare.py" ]

+ 92 - 0
cloudflare.py

@@ -0,0 +1,92 @@
+import json
+from os import environ, getenv
+from time import sleep
+
+from requests import get, put
+
+
+def get_request_header(email, api_key):
+    return {
+        "X-Auth-Email": email,
+        "X-Auth-Key": api_key,
+        "Content-Type": "application/json"
+    }
+
+
+def print_error(result):
+    for error in result["errors"]:
+        print(Exception(error))
+
+
+def get_zone_id(headers, host_name):
+    params = {"name": host_name}
+    response = get("https://api.cloudflare.com/client/v4/zones", params=params, headers=headers)
+    result = response.json()
+    if result["success"]:
+        return result["result"][0]["id"]
+    else:
+        print_error(result)
+        return None
+
+
+def get_record_id(headers, zone_id, host_name):
+    params = {"name": host_name}
+    response = get(f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records", params=params,
+                   headers=headers)
+    result = response.json()
+    if result["success"]:
+        return result["result"][0]["id"]
+    else:
+        print_error(result)
+        return None
+
+
+def update_dns(headers, zone_id, record_id, host_name, ip, ttl, proxy):
+    data = {"type": "A", "name": host_name, "content": ip, "ttl": ttl, "proxied": proxy}
+    response = put(f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{record_id}",
+                   data=json.dumps(data), headers=headers)
+    result = response.json()
+    if result["success"]:
+        return True
+    else:
+        print_error(result)
+        return False
+
+
+def get_ip():
+    return get(getenv("IP_ECHO", "http://ipecho.net/plain")).text  # http://icanhazip.com Also works
+
+
+def update(ip):
+    email = environ["EMAIL"]
+    api_key = environ["API"]
+    host_name = environ["HOST"]
+    headers = get_request_header(email, api_key)
+    zone_id = get_zone_id(headers, host_name)
+    if zone_id is None:
+        return
+    record_id = get_record_id(headers, zone_id, host_name)
+    if record_id is None:
+        return
+    ttl = int(getenv("TTL", "1"))
+    proxy = getenv("PROXY", "True").lower() == "true"
+    result = update_dns(headers, zone_id, record_id, host_name, ip, ttl, proxy)
+    if result:
+        print(f"Update Success:{host_name}({ip})")
+    else:
+        print(Exception(f"Update Fail:{host_name}({ip})"))
+
+
+def main():
+    ip = None
+    new_ip = get_ip()
+    while True:
+        if ip != new_ip:
+            update(new_ip)
+            ip = new_ip
+        sleep(int(getenv("WAIT", 300)))
+        new_ip = get_ip()
+
+
+if __name__ == "__main__":
+    main()

+ 1 - 0
requirements.txt

@@ -0,0 +1 @@
+requests==2.18.1