Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add health-check for wireguard #697

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion stowaway/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ RUN \
libqrencode \
net-tools \
openresolv \
python3 \
perl && \
echo "**** clean up ****" && \
apk del --no-network build-dependencies && \
Expand Down Expand Up @@ -185,4 +186,5 @@ RUN sed -i 's,listen 80;,listen 8080;,' /etc/nginx/conf.d/default.co
COPY /root /

# ports and volumes
EXPOSE 51820/udp
EXPOSE 51820/udp
EXPOSE 8000/tcp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/with-contenv bash

/etc/s6-overlay/s6-rc.d/svc-wireguard-health-check/wg-health-check.py --device=wg0 --port=8000
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
longrun
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from http.server import HTTPServer, BaseHTTPRequestHandler
from optparse import OptionParser
from os import popen

class HealthCheck(BaseHTTPRequestHandler):

def do_GET(self):
if check(self.server.device):
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.end_headers()
self.wfile.write(b"healthy\n")
else:
self.send_error(404)

def do_HEAD(self):
self.do_GET()

def check(device):
return popen("ip link show %s up " % device).read() != ""

def test(device):
if check(device):
print("%s up" % device)
else:
print("%s down" % device)

def main(port, device):
server = HTTPServer(('', port), HealthCheck)
server.device = device
server.serve_forever()

def opts():
parser = OptionParser(
description="HTTP server that sends 204 response when device is up.")
parser.add_option("-d", "--device", dest="device", default="wg0",
help="device name to check (default wg0)")
parser.add_option("-p", "--port", dest="port", default=8080, type="int",
help="port on which to listen (default 8080)")
parser.add_option("-t", "--test", action="store_true", dest="test", default=False,
help="show status and exit")
return parser.parse_args()[0]

if __name__ == "__main__":
options = opts()
if options.test:
test(options.device)
else:
main(options.port, options.device)