From 7ca9b5016e66329dcb514bfde2acebb6d42d0e08 Mon Sep 17 00:00:00 2001 From: redxef Date: Sun, 28 Nov 2021 02:53:29 +0100 Subject: [PATCH] Add makefile and simpler startup. --- .gitignore | 4 ++-- Dockerfile | 5 +---- Makefile | 22 ++++++++++++++++++++++ configuration.env | 1 - docker-compose.yml | 3 ++- start | 7 ------- start-openvpn.sh | 8 ++++++-- start.tmpl | 17 +++++++++++++++++ 8 files changed, 50 insertions(+), 17 deletions(-) create mode 100644 Makefile delete mode 100644 configuration.env delete mode 100755 start create mode 100755 start.tmpl diff --git a/.gitignore b/.gitignore index cbb59e6..d2d50a9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -config.ovpn -auth.txt +start +build-docker diff --git a/Dockerfile b/Dockerfile index 20ea080..3ab71e5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,6 @@ FROM alpine:latest RUN apk add bash openvpn openresolv aria2 -ADD https://raw.githubusercontent.com/alfredopalhares/openvpn-update-resolv-conf/master/update-resolv-conf.sh \ - /etc/openvpn/update-resolv-conf COPY start-openvpn.sh /usr/local/bin/start-openvpn.sh -RUN chmod +x /etc/openvpn/update-resolv-conf \ - && chmod +x /usr/local/bin/start-openvpn.sh +RUN chmod +x /usr/local/bin/start-openvpn.sh ENTRYPOINT [ "start-openvpn.sh" ] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5988b02 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +START_SCRIPT := start +START_SCRIPT_SRC := start.tmpl +INSTALLATION_NAME := openvpn-aria2 +DOCKERFILE := Dockerfile +TAG = openvpn-aria2 + +build: build-docker $(START_SCRIPT) + +build-docker: $(DOCKERFILE) start-openvpn.sh + docker build --tag=$(TAG) . + echo "This is a marker for the docker build." > $@ + +$(START_SCRIPT): $(START_SCRIPT_SRC) + sed -e "s/{TAG}/$(TAG)/g" -e "s/{INSTALLATION_NAME}/$(INSTALLATION_NAME)/g" $< > $@ + chmod +x $@ + + +clean: + $(RM) build-docker $(START_SCRIPT) + +install: + install -D --mode=755 $(START_SCRIPT) /usr/local/bin/$(INSTALLATION_NAME) diff --git a/configuration.env b/configuration.env deleted file mode 100644 index 9ab6271..0000000 --- a/configuration.env +++ /dev/null @@ -1 +0,0 @@ -OVPN_CONFIGURATION_FILE=/opt/config.ovpn diff --git a/docker-compose.yml b/docker-compose.yml index a2a228f..8890e61 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: '3.3' +version: '2.0' services: openvpn: restart: never @@ -7,6 +7,7 @@ services: dockerfile: Dockerfile volumes: - ./config.ovpn:/opt/config.ovpn + - ./ovpn_config:/opt/ovpn_config - ./auth.txt:/opt/auth.txt - /etc/passwd:/etc/passwd cap_add: diff --git a/start b/start deleted file mode 100755 index 42bf981..0000000 --- a/start +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env sh - -docker-compose run \ - -e OVPN_USER="$USER" \ - -e OVPN_HOME="$HOME" \ - -v "$HOME:$HOME" \ - openvpn sh diff --git a/start-openvpn.sh b/start-openvpn.sh index 5e5bc7c..bfd6597 100644 --- a/start-openvpn.sh +++ b/start-openvpn.sh @@ -17,7 +17,7 @@ connect_vpn() { } get_ip() { - nsout="$(nslookup myip.opendns.com resolver1.opendns.com)" + nsout="$(nslookup -timeout=1 myip.opendns.com resolver1.opendns.com)" if [ "$?" != 0 ]; then echo "WARN: Failed to run nslookup" >&2 fi @@ -35,12 +35,16 @@ switch_user() { own_ip="$(get_ip)" echo "OWN IP: $own_ip" +if [ "$own_ip" = "" ]; then + echo "ERROR: Failed to lookup own ip, aborting" >&2 + exit 1 +fi change_hostname create_net_dev connect_vpn for _ in {1..50}; do vpn_ip="$(get_ip)" - if [ "$vpn_ip" = "$own_ip" ]; then + if [ "$vpn_ip" = "$own_ip" ] || [ "$vpn_ip" = "" ]; then sleep .1 else break diff --git a/start.tmpl b/start.tmpl new file mode 100755 index 0000000..e7fb788 --- /dev/null +++ b/start.tmpl @@ -0,0 +1,17 @@ +#!/usr/bin/env sh + +INSTALLATION_NAME='{INSTALLATION_NAME}' +TAG='{TAG}' + + +source "/etc/$INSTALLATION_NAME.conf" + + +docker run -it --cap-add NET_ADMIN \ + -v /etc/passwd:/etc/passwd \ + -v /etc/openvpn/client:/etc/openvpn/client \ + -v "$HOME:$HOME" \ + -e OVPN_USER="$USER" \ + -e OVPN_HOME="$HOME" \ + -e OVPN_CONFIGURATION_FILE="/etc/openvpn/client/$OVPN_CONFIGURATION_FILE" \ + "$TAG" sh