Upload 6 files
Browse files- Dockerfile +3 -1
- app.py +127 -0
- docker-compose.yml +4 -1
- requirements.txt +5 -0
- vpn.env +3 -0
Dockerfile
CHANGED
|
@@ -18,6 +18,7 @@ RUN set -x \
|
|
| 18 |
iproute2 libcap-ng libcurl libevent linux-pam musl nspr nss nss-tools openrc \
|
| 19 |
bison flex gcc make libc-dev bsd-compat-headers linux-pam-dev \
|
| 20 |
nss-dev libcap-ng-dev libevent-dev curl-dev nspr-dev \
|
|
|
|
| 21 |
&& cd /sbin \
|
| 22 |
&& for fn in iptables iptables-save iptables-restore; do ln -fs xtables-legacy-multi "$fn"; done \
|
| 23 |
&& cd /opt/src \
|
|
@@ -45,7 +46,8 @@ RUN wget -t 3 -T 30 -nv -O /opt/src/ikev2.sh https://github.com/hwdsl2/setup-ips
|
|
| 45 |
COPY ./run.sh /opt/src/run.sh
|
| 46 |
RUN chmod 755 /opt/src/run.sh
|
| 47 |
EXPOSE 500/udp 4500/udp
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
ARG BUILD_DATE
|
| 51 |
ARG VERSION
|
|
|
|
| 18 |
iproute2 libcap-ng libcurl libevent linux-pam musl nspr nss nss-tools openrc \
|
| 19 |
bison flex gcc make libc-dev bsd-compat-headers linux-pam-dev \
|
| 20 |
nss-dev libcap-ng-dev libevent-dev curl-dev nspr-dev \
|
| 21 |
+
python3 py3-pip \
|
| 22 |
&& cd /sbin \
|
| 23 |
&& for fn in iptables iptables-save iptables-restore; do ln -fs xtables-legacy-multi "$fn"; done \
|
| 24 |
&& cd /opt/src \
|
|
|
|
| 46 |
COPY ./run.sh /opt/src/run.sh
|
| 47 |
RUN chmod 755 /opt/src/run.sh
|
| 48 |
EXPOSE 500/udp 4500/udp
|
| 49 |
+
|
| 50 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
| 51 |
|
| 52 |
ARG BUILD_DATE
|
| 53 |
ARG VERSION
|
app.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
# Set up logging
|
| 7 |
+
logging.basicConfig(level=logging.INFO)
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
def run_docker_compose(command):
|
| 13 |
+
"""Run a docker-compose command and return the result"""
|
| 14 |
+
try:
|
| 15 |
+
result = subprocess.run(
|
| 16 |
+
f"docker-compose {command}",
|
| 17 |
+
shell=True,
|
| 18 |
+
capture_output=True,
|
| 19 |
+
text=True
|
| 20 |
+
)
|
| 21 |
+
if result.returncode != 0:
|
| 22 |
+
logger.error(f"Docker compose {command} failed: {result.stderr}")
|
| 23 |
+
raise Exception(result.stderr)
|
| 24 |
+
return result.stdout
|
| 25 |
+
except Exception as e:
|
| 26 |
+
logger.error(f"Error running docker-compose {command}: {e}")
|
| 27 |
+
raise
|
| 28 |
+
|
| 29 |
+
@app.on_event("startup")
|
| 30 |
+
async def startup_event():
|
| 31 |
+
try:
|
| 32 |
+
logger.info("Starting Docker Compose stack...")
|
| 33 |
+
# Build and start the containers
|
| 34 |
+
run_docker_compose("up --build -d")
|
| 35 |
+
logger.info("Docker Compose stack is up")
|
| 36 |
+
|
| 37 |
+
# Execute run.sh inside the container
|
| 38 |
+
logger.info("Executing run.sh inside the container...")
|
| 39 |
+
result = subprocess.run(
|
| 40 |
+
"docker-compose exec -T vpn /opt/src/run.sh",
|
| 41 |
+
shell=True,
|
| 42 |
+
capture_output=True,
|
| 43 |
+
text=True
|
| 44 |
+
)
|
| 45 |
+
if result.returncode == 0:
|
| 46 |
+
logger.info("run.sh executed successfully")
|
| 47 |
+
else:
|
| 48 |
+
logger.error(f"run.sh execution failed: {result.stderr}")
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
logger.error(f"Startup error: {e}")
|
| 52 |
+
raise
|
| 53 |
+
|
| 54 |
+
@app.on_event("shutdown")
|
| 55 |
+
async def shutdown_event():
|
| 56 |
+
try:
|
| 57 |
+
logger.info("Stopping Docker Compose stack...")
|
| 58 |
+
run_docker_compose("down")
|
| 59 |
+
logger.info("Docker Compose stack is down")
|
| 60 |
+
except Exception as e:
|
| 61 |
+
logger.error(f"Shutdown error: {e}")
|
| 62 |
+
|
| 63 |
+
@app.get("/")
|
| 64 |
+
async def root():
|
| 65 |
+
return {"message": "VPN Server is running"}
|
| 66 |
+
|
| 67 |
+
@app.get("/status")
|
| 68 |
+
async def status():
|
| 69 |
+
try:
|
| 70 |
+
result = subprocess.run(
|
| 71 |
+
"docker-compose ps --format json",
|
| 72 |
+
shell=True,
|
| 73 |
+
capture_output=True,
|
| 74 |
+
text=True
|
| 75 |
+
)
|
| 76 |
+
if result.returncode == 0:
|
| 77 |
+
return {"status": "running", "details": result.stdout}
|
| 78 |
+
return {"status": "not_running", "details": result.stderr}
|
| 79 |
+
except Exception as e:
|
| 80 |
+
logger.error(f"Status check error: {e}")
|
| 81 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 82 |
+
|
| 83 |
+
@app.get("/logs")
|
| 84 |
+
async def logs():
|
| 85 |
+
try:
|
| 86 |
+
return {"logs": run_docker_compose("logs")}
|
| 87 |
+
except Exception as e:
|
| 88 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 89 |
+
|
| 90 |
+
@app.post("/restart")
|
| 91 |
+
async def restart():
|
| 92 |
+
try:
|
| 93 |
+
run_docker_compose("restart")
|
| 94 |
+
return {"message": "Services restarted successfully"}
|
| 95 |
+
except Exception as e:
|
| 96 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 97 |
+
|
| 98 |
+
@app.get("/vpn-details")
|
| 99 |
+
async def vpn_details():
|
| 100 |
+
try:
|
| 101 |
+
# Get VPN connection details from the container
|
| 102 |
+
result = subprocess.run(
|
| 103 |
+
"docker-compose exec -T vpn cat /etc/ipsec.d/vpn-gen.env",
|
| 104 |
+
shell=True,
|
| 105 |
+
capture_output=True,
|
| 106 |
+
text=True
|
| 107 |
+
)
|
| 108 |
+
if result.returncode == 0:
|
| 109 |
+
return {"vpn_details": result.stdout}
|
| 110 |
+
|
| 111 |
+
# If vpn-gen.env doesn't exist, try getting details from container logs
|
| 112 |
+
logs_result = subprocess.run(
|
| 113 |
+
"docker-compose logs vpn | grep -A 4 'Connect to your new VPN with these details:'",
|
| 114 |
+
shell=True,
|
| 115 |
+
capture_output=True,
|
| 116 |
+
text=True
|
| 117 |
+
)
|
| 118 |
+
if logs_result.returncode == 0:
|
| 119 |
+
return {"vpn_details": logs_result.stdout}
|
| 120 |
+
|
| 121 |
+
return {"error": "Could not retrieve VPN details"}
|
| 122 |
+
except Exception as e:
|
| 123 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 124 |
+
|
| 125 |
+
if __name__ == "__main__":
|
| 126 |
+
import uvicorn
|
| 127 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
docker-compose.yml
CHANGED
|
@@ -5,13 +5,16 @@ volumes:
|
|
| 5 |
|
| 6 |
services:
|
| 7 |
vpn:
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
restart: always
|
| 10 |
env_file:
|
| 11 |
- ./vpn.env
|
| 12 |
ports:
|
| 13 |
- "500:500/udp"
|
| 14 |
- "4500:4500/udp"
|
|
|
|
| 15 |
privileged: true
|
| 16 |
hostname: ipsec-vpn-server
|
| 17 |
container_name: ipsec-vpn-server
|
|
|
|
| 5 |
|
| 6 |
services:
|
| 7 |
vpn:
|
| 8 |
+
build:
|
| 9 |
+
context: .
|
| 10 |
+
dockerfile: Dockerfile
|
| 11 |
restart: always
|
| 12 |
env_file:
|
| 13 |
- ./vpn.env
|
| 14 |
ports:
|
| 15 |
- "500:500/udp"
|
| 16 |
- "4500:4500/udp"
|
| 17 |
+
- "8000:8000"
|
| 18 |
privileged: true
|
| 19 |
hostname: ipsec-vpn-server
|
| 20 |
container_name: ipsec-vpn-server
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
python-multipart
|
| 4 |
+
docker
|
| 5 |
+
python-dotenv
|
vpn.env
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
VPN_IPSEC_PSK=your_ipsec_pre_shared_key
|
| 2 |
+
VPN_USER=your_vpn_username
|
| 3 |
+
VPN_PASSWORD=your_vpn_password
|