Exemple #1
0
def edit_peer(
        peer: schemas.WGPeer,
        sess: Session = Depends(middleware.get_db)
):
    # Retrieve server from db
    server: models.WGServer = db.wireguard.get_server_by_id(sess, peer.server_id)

    # Generate peer configuration
    peer.configuration = script.wireguard.generate_config(dict(
        peer=peer,
        server=server
    ))

    # Update database record for Peer
    sess.query(models.WGPeer)\
        .filter_by(id=peer.id)\
        .update(peer.dict(exclude={"id"}))

    # Generate server configuration
    server.configuration = script.wireguard.generate_config(server)
    sess.add(server)

    sess.commit()

    return dict(
        peer=peer,
        server_configuration=server.configuration
    )
Exemple #2
0
def edit_peer(peer: schemas.WGPeer,
              sess: Session = Depends(middleware.get_db)):
    server = schemas.WGServer(interface="")\
            .from_orm(sess.query(models.WGServer).filter_by(id=peer.server_id).one())

    peer.configuration = script.wireguard.generate_config(
        dict(peer=peer, server=server))
    peer.sync(sess)

    return peer
Exemple #3
0
def delete_peer(
        peer: schemas.WGPeer,
        sess: Session = Depends(middleware.get_db)
):
    peer.from_db(sess)  # Sync full object

    if not db.wireguard.peer_remove(sess, peer):
        raise HTTPException(400, detail="Were not able to delete peer %s (%s)" % (peer.name, peer.public_key))

    server = schemas.WGServer(interface=peer.server_id)
    if script.wireguard.is_running(server):
        script.wireguard.remove_peer(server, peer)

    return peer
Exemple #4
0
def peer_ip_address_set(sess: Session, peer: schemas.WGPeer) -> schemas.WGPeer:
    db_peer: models.WGPeer = peer_query_get_by_address(sess, peer.address,
                                                       peer.server).one()
    db_peer.address = peer.address
    sess.add(db_peer)
    sess.commit()
    return peer.from_orm(db_peer)
Exemple #5
0
def peer_remove(sess: Session, peer: schemas.WGPeer) -> bool:
    db_peers = peer.filter_query(sess).all()

    for db_peer in db_peers:
        sess.delete(db_peer)
        sess.commit()

    return True
Exemple #6
0
def peer_key_pair_generate(sess: Session, peer: schemas.WGPeer) -> schemas.WGPeer:
    db_peer: models.WGPeer = peer_query_get_by_address(sess, peer.address, peer.server).one()
    private_key, public_key = script.wireguard.generate_keys()
    db_peer.private_key = private_key
    db_peer.public_key = public_key

    sess.add(db_peer)
    sess.commit()

    return peer.from_orm(db_peer)
Exemple #7
0
def peer_edit(sess: Session, peer: schemas.WGPeer):
    # Retrieve server from db
    server: models.WGServer = get_server_by_id(sess, peer.server_id)

    # Generate peer configuration
    peer.configuration = script.wireguard.generate_config(dict(
        peer=peer,
        server=server
    ))

    # Update database record for Peer
    sess.query(models.WGPeer) \
        .filter_by(id=peer.id) \
        .update(peer.dict(exclude={"id"}))
    sess.commit()

    server_update_configuration(sess, server.id)

    return peer
Exemple #8
0
def peer_update(sess: Session, peer: schemas.WGPeer) -> schemas.WGPeer:
    db_peer: models.WGPeer = peer_query_get_by_address(sess, peer.address, peer.server).one()
    db_peer.address = peer.address
    db_peer.public_key = peer.public_key
    db_peer.private_key = peer.private_key
    db_peer.name = peer.name
    db_peer.dns = peer.dns
    db_peer.allowed_ips = peer.allowed_ips

    sess.add(db_peer)
    sess.commit()

    return peer.from_orm(db_peer)