|
@ -6,6 +6,7 @@ from sqlalchemy.orm import Session |
|
|
from starlette.responses import JSONResponse |
|
|
from starlette.responses import JSONResponse |
|
|
|
|
|
|
|
|
import const |
|
|
import const |
|
|
|
|
|
import models |
|
|
import schemas |
|
|
import schemas |
|
|
import middleware |
|
|
import middleware |
|
|
import db.wireguard |
|
|
import db.wireguard |
|
@ -33,6 +34,7 @@ def add_interface( |
|
|
): |
|
|
): |
|
|
server.post_up = server.post_up if server.post_up != "" else const.DEFAULT_POST_UP |
|
|
server.post_up = server.post_up if server.post_up != "" else const.DEFAULT_POST_UP |
|
|
server.post_down = server.post_up if server.post_up != "" else const.DEFAULT_POST_DOWN |
|
|
server.post_down = server.post_up if server.post_up != "" else const.DEFAULT_POST_DOWN |
|
|
|
|
|
peers = server.peers if server.peers else [] |
|
|
|
|
|
|
|
|
# Public/Private key |
|
|
# Public/Private key |
|
|
try: |
|
|
try: |
|
@ -40,12 +42,30 @@ def add_interface( |
|
|
if server.filter_query(sess).count() != 0: |
|
|
if server.filter_query(sess).count() != 0: |
|
|
raise HTTPException(status_code=400, detail="The server interface %s already exists in the database" % server.interface) |
|
|
raise HTTPException(status_code=400, detail="The server interface %s already exists in the database" % server.interface) |
|
|
|
|
|
|
|
|
keys = script.wireguard.generate_keys() |
|
|
if not server.private_key: |
|
|
server.private_key = keys["private_key"] |
|
|
keys = script.wireguard.generate_keys() |
|
|
server.public_key = keys["public_key"] |
|
|
server.private_key = keys["private_key"] |
|
|
server.configuration = script.wireguard.generate_config(server) |
|
|
server.public_key = keys["public_key"] |
|
|
|
|
|
|
|
|
|
|
|
server.configuration = script.wireguard.generate_config(server) |
|
|
|
|
|
server.peers = [] |
|
|
server.sync(sess) |
|
|
server.sync(sess) |
|
|
|
|
|
|
|
|
|
|
|
if len(peers) > 0: |
|
|
|
|
|
server.from_db(sess) |
|
|
|
|
|
|
|
|
|
|
|
for schemaPeer in peers: |
|
|
|
|
|
schemaPeer.server_id = server.id |
|
|
|
|
|
schemaPeer.configuration = script.wireguard.generate_config(dict( |
|
|
|
|
|
peer=schemaPeer, |
|
|
|
|
|
server=server |
|
|
|
|
|
)) |
|
|
|
|
|
dbPeer = models.WGPeer(**schemaPeer.dict()) |
|
|
|
|
|
sess.add(dbPeer) |
|
|
|
|
|
sess.commit() |
|
|
|
|
|
|
|
|
|
|
|
server.from_db(sess) |
|
|
|
|
|
|
|
|
except ValueError as e: |
|
|
except ValueError as e: |
|
|
raise HTTPException(status_code=400, detail=str(e)) |
|
|
raise HTTPException(status_code=400, detail=str(e)) |
|
|
|
|
|
|
|
|