diff --git a/wg_dashboard_backend/main.py b/wg_dashboard_backend/main.py index 00a0fce..a876841 100644 --- a/wg_dashboard_backend/main.py +++ b/wg_dashboard_backend/main.py @@ -37,7 +37,7 @@ _db: Session = SessionLocal() if not database_exists(engine.url): ADMIN_USERNAME = os.getenv("ADMIN_USERNAME") if not ADMIN_USERNAME: - raise RuntimeError("Database does not exist and no ADMIN_USER is set") + raise RuntimeError("Database does not exist and no ADMIN_USERNAME is set") ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD") diff --git a/wg_dashboard_backend/migrations/versions/004_create_server_subnet.py b/wg_dashboard_backend/migrations/versions/004_create_server_subnet.py new file mode 100644 index 0000000..539a81d --- /dev/null +++ b/wg_dashboard_backend/migrations/versions/004_create_server_subnet.py @@ -0,0 +1,21 @@ +from sqlalchemy import * +from migrate import * + + +def upgrade(migrate_engine): + try: + meta = MetaData(bind=migrate_engine) + server = Table('server', meta, autoload=True) + subnet = Column('subnet', Text) + subnet.create(server) + except: + pass + + +def downgrade(migrate_engine): + try: + meta = MetaData(bind=migrate_engine) + server = Table('server', meta, autoload=True) + server.c.subnet.drop() + except: + pass diff --git a/wg_dashboard_backend/models.py b/wg_dashboard_backend/models.py index 0e4a07d..0b615d5 100644 --- a/wg_dashboard_backend/models.py +++ b/wg_dashboard_backend/models.py @@ -21,6 +21,7 @@ class WGServer(Base): id = Column(Integer, primary_key=True, index=True) interface = Column(sqlalchemy.String, unique=True, index=True) + subnet = Column(sqlalchemy.Integer, nullable=False) address = Column(sqlalchemy.String, unique=True) listen_port = Column(sqlalchemy.String, unique=True) private_key = Column(sqlalchemy.String) diff --git a/wg_dashboard_backend/routers/v1/peer.py b/wg_dashboard_backend/routers/v1/peer.py index c81aa08..c49c903 100644 --- a/wg_dashboard_backend/routers/v1/peer.py +++ b/wg_dashboard_backend/routers/v1/peer.py @@ -21,7 +21,7 @@ def add_peer( server = schemas.WGServer(interface=peer_add.server_interface).from_db(sess) peer = schemas.WGPeer(server_id=server.id) - address_space = set(ipaddress.ip_network(server.address, strict=False).hosts()) + address_space = set(ipaddress.ip_network(f"{server.address}/{server.subnet}", strict=False).hosts()) occupied_space = set() # Try add server IP to list. @@ -41,7 +41,7 @@ def add_peer( address_space -= occupied_space # Select first available address - peer.address = str(list(sorted(address_space)).pop(0)) + "/32" + peer.address = str(list(sorted(address_space)).pop(0)) # Private public key generation keys = script.wireguard.generate_keys() diff --git a/wg_dashboard_backend/schemas.py b/wg_dashboard_backend/schemas.py index 6a53614..d98c08a 100644 --- a/wg_dashboard_backend/schemas.py +++ b/wg_dashboard_backend/schemas.py @@ -144,6 +144,7 @@ class PSK(GenericModel): class WGServer(GenericModel): id: int = None address: str = None + subnet: int = None interface: str listen_port: int = None endpoint: str = None diff --git a/wg_dashboard_backend/templates/peer.j2 b/wg_dashboard_backend/templates/peer.j2 index c02656a..5639bff 100644 --- a/wg_dashboard_backend/templates/peer.j2 +++ b/wg_dashboard_backend/templates/peer.j2 @@ -1,5 +1,5 @@ [Interface] -Address = {{ data.peer.address.replace("/32", "/24") }} +Address = {{ data.peer.address }}/{{ data.server.subnet }} PrivateKey = {{ data.peer.private_key }} DNS = {{ data.peer.dns }} diff --git a/wg_dashboard_backend/templates/server.j2 b/wg_dashboard_backend/templates/server.j2 index e4f50dd..4f1c83c 100644 --- a/wg_dashboard_backend/templates/server.j2 +++ b/wg_dashboard_backend/templates/server.j2 @@ -1,5 +1,5 @@ [Interface] -Address = {{ data.address }} +Address = {{ data.address }}/{{ data.subnet }} ListenPort = {{ data.listen_port }} PrivateKey = {{ data.private_key }} @@ -13,5 +13,5 @@ PublicKey = {{ peer.public_key }} {%- if peer.shared_key %} PresharedKey = {{ peer.shared_key }} {%- endif %} -AllowedIPs = {{ peer.address }} +AllowedIPs = {{ peer.address }}/32 {% endfor %} diff --git a/wg_dashboard_frontend/src/app/interfaces/server.ts b/wg_dashboard_frontend/src/app/interfaces/server.ts index fb0d536..5c28a10 100644 --- a/wg_dashboard_frontend/src/app/interfaces/server.ts +++ b/wg_dashboard_frontend/src/app/interfaces/server.ts @@ -12,5 +12,6 @@ export interface Server { post_up: string; post_down: string; configuration: string; + subnet: number; peers: Peer[]; } diff --git a/wg_dashboard_frontend/src/app/page/dashboard/add-server/add-server.component.html b/wg_dashboard_frontend/src/app/page/dashboard/add-server/add-server.component.html index a30a80a..e32498a 100644 --- a/wg_dashboard_frontend/src/app/page/dashboard/add-server/add-server.component.html +++ b/wg_dashboard_frontend/src/app/page/dashboard/add-server/add-server.component.html @@ -29,10 +29,21 @@