11use base64:: Engine ;
2+
23use warp:: http:: Response ;
34
45use defguard_wireguard_rs:: net:: IpAddrMask ;
@@ -17,6 +18,8 @@ use pony::state::storage::connection::ApiOp;
1718use pony:: utils;
1819use pony:: xray_op:: clash:: generate_clash_config;
1920use pony:: xray_op:: clash:: generate_proxy_config;
21+ use pony:: zmq:: message:: Action ;
22+ use pony:: zmq:: message:: Message ;
2023use pony:: zmq:: publisher:: Publisher as ZmqPublisher ;
2124use pony:: Conn as Connection ;
2225use 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
453573pub async fn put_connection_handler < N , C > (
0 commit comments