diff options
author | Mike Crute <mike@crute.us> | 2023-11-21 21:40:10 -0800 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2023-11-21 21:40:10 -0800 |
commit | ca74d3d137e13ea3668aab61276dea05ef772364 (patch) | |
tree | e2af80d289c8218220dcb418c3baca5adad30780 | |
parent | adb4b08da0000d0d7080d9015ce265c91f2b49b5 (diff) | |
download | dockerfiles-ca74d3d137e13ea3668aab61276dea05ef772364.tar.bz2 dockerfiles-ca74d3d137e13ea3668aab61276dea05ef772364.tar.xz dockerfiles-ca74d3d137e13ea3668aab61276dea05ef772364.zip |
Remove ddns
-rw-r--r-- | ddns/Dockerfile | 19 | ||||
-rw-r--r-- | ddns/Makefile | 15 | ||||
-rwxr-xr-x | ddns/ddns.py | 122 | ||||
-rw-r--r-- | ddns/uwsgi.ini | 10 |
4 files changed, 0 insertions, 166 deletions
diff --git a/ddns/Dockerfile b/ddns/Dockerfile deleted file mode 100644 index 0872f4d..0000000 --- a/ddns/Dockerfile +++ /dev/null | |||
@@ -1,19 +0,0 @@ | |||
1 | FROM alpine:latest | ||
2 | LABEL maintainer="Mike Crute <mike@crute.us>" | ||
3 | |||
4 | RUN \ | ||
5 | apk add --no-cache \ | ||
6 | py2-pip \ | ||
7 | uwsgi-python \ | ||
8 | && pip install \ | ||
9 | flask \ | ||
10 | boto3 \ | ||
11 | && apk del py2-pip \ | ||
12 | && addgroup -g 1002 -S ddns \ | ||
13 | && adduser -u 1002 -S -H -D -G ddns ddns | ||
14 | |||
15 | ADD uwsgi.ini /etc/uwsgi/ | ||
16 | ADD ddns.py /usr/share/ddns/ | ||
17 | |||
18 | STOPSIGNAL SIGINT | ||
19 | CMD [ "/usr/sbin/uwsgi", "--ini", "/etc/uwsgi/uwsgi.ini" ] | ||
diff --git a/ddns/Makefile b/ddns/Makefile deleted file mode 100644 index 1163601..0000000 --- a/ddns/Makefile +++ /dev/null | |||
@@ -1,15 +0,0 @@ | |||
1 | IMAGE=docker.crute.me/ddns:latest | ||
2 | |||
3 | all: | ||
4 | docker build -t $(IMAGE) . | ||
5 | |||
6 | all-no-cache: | ||
7 | docker build --no-cache -t $(IMAGE) . | ||
8 | |||
9 | run: | ||
10 | docker run -d \ | ||
11 | -p 9000:9000 \ | ||
12 | $(IMAGE) | ||
13 | |||
14 | publish: | ||
15 | docker push $(IMAGE) | ||
diff --git a/ddns/ddns.py b/ddns/ddns.py deleted file mode 100755 index 0bde4f7..0000000 --- a/ddns/ddns.py +++ /dev/null | |||
@@ -1,122 +0,0 @@ | |||
1 | #!/usr/bin/env python | ||
2 | |||
3 | import os | ||
4 | import json | ||
5 | import boto3 | ||
6 | import flask | ||
7 | import hashlib | ||
8 | import functools | ||
9 | |||
10 | # pip install flask boto3 | ||
11 | |||
12 | # This is purely for documentation purposes | ||
13 | __REQUIRED_IAM_POLICY__ = """ | ||
14 | { | ||
15 | "Version": "2012-10-17", | ||
16 | "Statement": [ | ||
17 | { | ||
18 | "Effect": "Allow", | ||
19 | "Action": "ssm:GetParameter", | ||
20 | "Resource": "arn:aws:ssm:us-west-2::parameter/DDNS_CLIENTS" | ||
21 | }, | ||
22 | { | ||
23 | "Effect": "Allow", | ||
24 | "Action": [ | ||
25 | "route53:ListHostedZones", | ||
26 | "route53:ChangeResourceRecordSets" | ||
27 | ], | ||
28 | "Resource": "*" | ||
29 | } | ||
30 | ] | ||
31 | } | ||
32 | """ | ||
33 | |||
34 | |||
35 | app = flask.Flask(__name__) | ||
36 | |||
37 | |||
38 | def returns_plain_text(f): | ||
39 | @functools.wraps(f) | ||
40 | def wrapper(*args, **kwargs): | ||
41 | return flask.Response(f(*args, **kwargs), content_type="text/plain") | ||
42 | |||
43 | return wrapper | ||
44 | |||
45 | |||
46 | def get_ip(): | ||
47 | if "X-Forwarded-For" in flask.request.headers: | ||
48 | return flask.request.headers["X-Forwarded-For"] | ||
49 | else: | ||
50 | return flask.request.remote_addr | ||
51 | |||
52 | |||
53 | def update_record(zone, record, ip): | ||
54 | client = boto3.client("route53") | ||
55 | zones = client.list_hosted_zones()["HostedZones"] | ||
56 | |||
57 | client.change_resource_record_sets( | ||
58 | HostedZoneId=[z["Id"] for z in zones if z["Name"] == zone][0], | ||
59 | ChangeBatch={ | ||
60 | "Changes": [{ | ||
61 | "Action": "UPSERT", | ||
62 | "ResourceRecordSet": { | ||
63 | "Name": ".".join((record, zone)), | ||
64 | "Type": "A", | ||
65 | "TTL": 60, | ||
66 | "ResourceRecords": [{ | ||
67 | "Value": ip, | ||
68 | }] | ||
69 | } | ||
70 | }] | ||
71 | } | ||
72 | ) | ||
73 | |||
74 | |||
75 | @app.errorhandler(404) | ||
76 | @app.errorhandler(405) | ||
77 | @app.errorhandler(500) | ||
78 | def handle_error(ex): | ||
79 | response = flask.Response("Error", content_type="text/plain") | ||
80 | response.status_code = getattr(ex, "code", 500) | ||
81 | return response | ||
82 | |||
83 | |||
84 | @app.route("/new-secret", methods=["GET"]) | ||
85 | @returns_plain_text | ||
86 | def new_secret(): | ||
87 | return hashlib.sha256(os.urandom(100)).hexdigest() | ||
88 | |||
89 | |||
90 | def get_client_config(client): | ||
91 | ssm = boto3.client("ssm") | ||
92 | clients = ssm.get_parameter(Name="DDNS_CLIENTS", WithDecryption=True) | ||
93 | config = json.loads(clients["Parameter"]["Value"]) | ||
94 | return config.get(client) | ||
95 | |||
96 | |||
97 | @app.route("/update", methods=["POST"]) | ||
98 | def update_ip(): | ||
99 | key = flask.request.form.get("key") | ||
100 | config = get_client_config(key) | ||
101 | |||
102 | if not config: | ||
103 | flask.abort(404) | ||
104 | |||
105 | resource, zone = config.split(".", 1) | ||
106 | |||
107 | try: | ||
108 | update_record(zone, resource, get_ip()) | ||
109 | return "OK" | ||
110 | except: | ||
111 | flask.abort(500) | ||
112 | |||
113 | |||
114 | @app.route("/", methods=["GET"]) | ||
115 | @returns_plain_text | ||
116 | def handle_home(): | ||
117 | return get_ip() | ||
118 | |||
119 | |||
120 | if __name__ == "__main__": | ||
121 | app.debug = True | ||
122 | app.run() | ||
diff --git a/ddns/uwsgi.ini b/ddns/uwsgi.ini deleted file mode 100644 index c0892b2..0000000 --- a/ddns/uwsgi.ini +++ /dev/null | |||
@@ -1,10 +0,0 @@ | |||
1 | [uwsgi] | ||
2 | master = true | ||
3 | socket = :9000 | ||
4 | uid = ddns | ||
5 | gid = ddns | ||
6 | plugin = python | ||
7 | chdir = /usr/share/ddns | ||
8 | workers = 2 | ||
9 | module = ddns:app | ||
10 | harakiri = 300 | ||