Преглед на файлове

Use Bash instead of Python

Joshua преди 8 години
родител
ревизия
3e5dc6802a
променени са 4 файла, в които са добавени 36 реда и са изтрити 99 реда
  1. 7 6
      Dockerfile
  2. 0 92
      cloudflare.py
  3. 29 0
      cloudflare.sh
  4. 0 1
      requirements.txt

+ 7 - 6
Dockerfile

@@ -1,10 +1,11 @@
-FROM python:3.6.2-alpine3.6
+FROM alpine:3.6
+MAINTAINER JoshuaAvalon
 
-ADD cloudflare.py /app
-ADD requirements.txt /app
+RUN apk add --update curl && \
+    rm -rf /var/cache/apk/*
 
-WORKDIR "/app"
+ADD cloudflare.sh /app
 
-CMD [ "pip", "install", "requirements.txt" ]
+WORKDIR "/app"
 
-CMD [ "python", "cloudflare.py" ]
+CMD [ "sh", "cloudflare.sh" ]

+ 0 - 92
cloudflare.py

@@ -1,92 +0,0 @@
-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()

+ 29 - 0
cloudflare.sh

@@ -0,0 +1,29 @@
+#!/bin/bash
+
+update_cloudflare() {
+    zone_id=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$ZONE" -H "X-Auth-Email: $EMAIL" -H "X-Auth-Key: $API" -H "Content-Type: application/json" | perl -nle'print $& if m{(?<="id":")[^"]*}' | head -1 )
+    record_id=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?name=$ZONE" -H "X-Auth-Email: $EMAIL" -H "X-Auth-Key: $API" -H "Content-Type: application/json"  | perl -nle'print $& if m{(?<="id":")[^"]*}')
+    update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$record_id" -H "X-Auth-Email: $EMAIL" -H "X-Auth-Key: $API" -H "Content-Type: application/json" --data "{\"id\":\"$zone_id\",\"type\":\"A\",\"name\":\"$ZONE\",\"content\":\"$ip\",\"ttl\":$TTL,\"proxied\":$PROXY}")
+
+    if [[ ${update} == *"\"success\":false"* ]]; then
+        echo "API UPDATE FAILED. DUMPING RESULTS:\n$update"
+        exit 1
+    else
+        echo "IP changed to: $ip"
+    fi
+}
+
+# http://icanhazip.com Also works
+ip=$(curl -s http://ipecho.net/plain)
+update_cloudflare
+
+
+while sleep $WAIT; do
+    new_ip=$(curl -s http://ipecho.net/plain)
+    if [[ "${ip}" == "$new_ip" ]]; then
+        echo "Same ip: $ip"
+    else
+        ip="$new_ip"
+        update_cloudflare
+    fi
+done

+ 0 - 1
requirements.txt

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