Skip to content

Commit 630f031

Browse files
committed
Fix WG keys, add DELETE connection method
1 parent 539d81a commit 630f031

5 files changed

Lines changed: 137 additions & 5 deletions

File tree

dev/dev.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,6 @@ ALTER COLUMN password DROP NOT NULL;
9696

9797
ALTER TABLE inbounds ADD COLUMN dns INET[];
9898

99+
ALTER TABLE connections DROP CONSTRAINT connections_user_id_fkey;
100+
99101

src/bin/api/core/http/handlers/connection.rs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use base64::Engine;
2+
23
use warp::http::Response;
34

45
use defguard_wireguard_rs::net::IpAddrMask;
@@ -17,6 +18,8 @@ use pony::state::storage::connection::ApiOp;
1718
use pony::utils;
1819
use pony::xray_op::clash::generate_clash_config;
1920
use pony::xray_op::clash::generate_proxy_config;
21+
use pony::zmq::message::Action;
22+
use pony::zmq::message::Message;
2023
use pony::zmq::publisher::Publisher as ZmqPublisher;
2124
use pony::Conn as Connection;
2225
use pony::ConnWithId;
@@ -448,6 +451,123 @@ where
448451
}
449452
}
450453

454+
/// Handler deletes connection
455+
// DELETE /connection?conn_id=
456+
pub async fn delete_connection_handler<N, C>(
457+
conn_param: ConnQueryParam,
458+
publisher: ZmqPublisher,
459+
state: SyncState<N, C>,
460+
) -> Result<impl warp::Reply, warp::Rejection>
461+
where
462+
N: NodeStorageOp + Sync + Send + Clone + 'static,
463+
C: ConnectionApiOp
464+
+ ConnectionBaseOp
465+
+ Sync
466+
+ Send
467+
+ Clone
468+
+ 'static
469+
+ From<Connection>
470+
+ PartialEq,
471+
Connection: From<C>,
472+
{
473+
let conn_id = conn_param.conn_id;
474+
let conn_opt = {
475+
let mem = state.memory.lock().await;
476+
mem.connections.get(&conn_id).cloned()
477+
};
478+
479+
let Some(conn) = conn_opt else {
480+
let response = ResponseMessage::<Option<uuid::Uuid>> {
481+
status: StatusCode::NOT_FOUND.as_u16(),
482+
message: format!("Connection {} not found", conn_id),
483+
response: None,
484+
};
485+
return Ok(warp::reply::with_status(
486+
warp::reply::json(&response),
487+
StatusCode::NOT_FOUND,
488+
));
489+
};
490+
491+
if conn.get_deleted() {
492+
let response = ResponseMessage::<Option<uuid::Uuid>> {
493+
status: StatusCode::NOT_FOUND.as_u16(),
494+
message: format!("Connection {} is deleted", conn_id),
495+
response: None,
496+
};
497+
return Ok(warp::reply::with_status(
498+
warp::reply::json(&response),
499+
StatusCode::NOT_FOUND,
500+
));
501+
}
502+
503+
match SyncOp::delete_connection(&state, &conn_id).await {
504+
Ok(StorageOperationStatus::Ok(id)) => {
505+
let msg = Message {
506+
action: Action::Delete,
507+
conn_id: conn_id,
508+
password: conn.get_password(),
509+
wg: conn.get_wireguard().cloned(),
510+
tag: conn.get_proto().proto(),
511+
};
512+
513+
if let Some(node_id) = conn.get_wireguard_node_id() {
514+
let _ = publisher.send(&node_id.to_string(), msg).await;
515+
} else {
516+
let _ = publisher.send(&conn.get_env(), msg).await;
517+
}
518+
519+
let response = ResponseMessage::<Option<uuid::Uuid>> {
520+
status: StatusCode::OK.as_u16(),
521+
message: format!("Connection {} has been deleted", id),
522+
response: Some(id),
523+
};
524+
Ok(warp::reply::with_status(
525+
warp::reply::json(&response),
526+
StatusCode::OK,
527+
))
528+
}
529+
530+
Ok(StorageOperationStatus::NotFound(id)) => {
531+
let response = ResponseMessage::<Option<uuid::Uuid>> {
532+
status: StatusCode::NOT_FOUND.as_u16(),
533+
message: format!("Connection {} not found", id),
534+
response: None,
535+
};
536+
Ok(warp::reply::with_status(
537+
warp::reply::json(&response),
538+
StatusCode::NOT_FOUND,
539+
))
540+
}
541+
542+
Ok(status) => {
543+
let response = ResponseMessage::<Option<uuid::Uuid>> {
544+
status: StatusCode::BAD_REQUEST.as_u16(),
545+
message: format!("Unsupported operation status: {}", status),
546+
response: None,
547+
};
548+
Ok(warp::reply::with_status(
549+
warp::reply::json(&response),
550+
StatusCode::BAD_REQUEST,
551+
))
552+
}
553+
554+
Err(err) => {
555+
let response = ResponseMessage::<Option<uuid::Uuid>> {
556+
status: StatusCode::INTERNAL_SERVER_ERROR.as_u16(),
557+
message: format!(
558+
"Internal error while deleting connection {}: {}",
559+
conn_id, err
560+
),
561+
response: None,
562+
};
563+
Ok(warp::reply::with_status(
564+
warp::reply::json(&response),
565+
StatusCode::INTERNAL_SERVER_ERROR,
566+
))
567+
}
568+
}
569+
}
570+
451571
/// Handler updates connection
452572
// PUT /connection
453573
pub async fn put_connection_handler<N, C>(

src/bin/api/core/http/routes.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ where
164164
.and(with_state(self.state.clone()))
165165
.and_then(create_connection_handler);
166166

167+
let delete_connection_route = warp::delete()
168+
.and(warp::path("connection"))
169+
.and(warp::path::end())
170+
.and(auth.clone())
171+
.and(warp::query::<ConnQueryParam>())
172+
.and(publisher(self.publisher.clone()))
173+
.and(with_state(self.state.clone()))
174+
.and_then(delete_connection_handler);
175+
167176
let put_connection_route = warp::put()
168177
.and(warp::path("connection"))
169178
.and(warp::path::end())
@@ -191,6 +200,7 @@ where
191200
.or(get_connection_route)
192201
.or(get_subscription_route)
193202
.or(post_connection_route)
203+
.or(delete_connection_route)
194204
.or(put_connection_route)
195205
.recover(rejection);
196206

src/bin/api/core/sync/tasks.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ where
4545
conn_id: &uuid::Uuid,
4646
conn: ConnUpdateRequest,
4747
) -> Result<OperationStatus>;
48-
async fn delete_connection(&self, conn_id: &uuid::Uuid) -> Result<()>;
48+
async fn delete_connection(&self, conn_id: &uuid::Uuid) -> Result<OperationStatus>;
4949
async fn update_node_status(
5050
&self,
5151
uuid: &uuid::Uuid,
@@ -215,17 +215,17 @@ where
215215
)),
216216
}
217217
}
218-
async fn delete_connection(&self, conn_id: &uuid::Uuid) -> Result<()> {
218+
async fn delete_connection(&self, conn_id: &uuid::Uuid) -> Result<OperationStatus> {
219219
let mut mem = self.memory.lock().await;
220220

221221
match mem.connections.delete(conn_id) {
222222
Ok(_) => {
223223
self.sync_tx
224224
.send(SyncTask::DeleteConn { conn_id: *conn_id })
225225
.await?;
226-
Ok(())
226+
Ok(OperationStatus::Ok(*conn_id))
227227
}
228-
Err(e) => Err(PonyError::Custom(e.to_string())),
228+
Err(_) => Ok(OperationStatus::NotFound(*conn_id)),
229229
}
230230
}
231231
async fn update_node_status(

src/config/wireguard.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct WireguardSettings {
1818

1919
impl WireguardSettings {
2020
pub fn new(config: &WgConfig) -> Self {
21-
let (privkey, pubkey) = match (config.pubkey.clone(), config.privkey.clone()) {
21+
let (privkey, pubkey) = match (config.privkey.clone(), config.pubkey.clone()) {
2222
(Some(privkey), Some(pubkey)) => (privkey, pubkey),
2323
_ => {
2424
let keys = WgKeys::default();

0 commit comments

Comments
 (0)