1
- use crate :: extract :: RepeatQuery ;
2
- use crate :: http :: HttpError ;
3
- use crate :: token :: Protocol ;
4
- use crate :: DgwState ;
1
+ use std :: collections :: HashMap ;
2
+ use std :: net :: { IpAddr , Ipv4Addr , Ipv6Addr } ;
3
+ use std :: time :: Duration ;
4
+
5
5
use axum:: extract:: ws:: { Message , Utf8Bytes } ;
6
6
use axum:: extract:: WebSocketUpgrade ;
7
7
use axum:: response:: Response ;
@@ -16,9 +16,11 @@ use network_scanner::ping::PingEvent;
16
16
use network_scanner:: port_discovery:: TcpKnockEvent ;
17
17
use network_scanner:: scanner:: { self , DnsEvent , NetworkScannerParams , ScannerConfig , TcpKnockWithHost } ;
18
18
use serde:: { Deserialize , Serialize } ;
19
- use std:: fmt;
20
- use std:: net:: { IpAddr , Ipv4Addr , Ipv6Addr } ;
21
- use std:: time:: Duration ;
19
+
20
+ use crate :: extract:: RepeatQuery ;
21
+ use crate :: http:: HttpError ;
22
+ use crate :: token:: Protocol ;
23
+ use crate :: DgwState ;
22
24
23
25
pub fn make_router < S > ( state : DgwState ) -> Router < S > {
24
26
let router = Router :: new ( )
@@ -469,109 +471,77 @@ impl EventFilter {
469
471
tag = "Net" ,
470
472
path = "/jet/net/config" ,
471
473
responses(
472
- ( status = 200 , description = "Network interfaces" , body = [ Vec <NetworkInterface >] ) ,
474
+ ( status = 200 , description = "Network interfaces" , body = [ HashMap < String , Vec <InterfaceInfo > >] ) ,
473
475
( status = 400 , description = "Bad request" ) ,
474
476
( status = 401 , description = "Invalid or missing authorization token" ) ,
475
477
( status = 403 , description = "Insufficient permissions" ) ,
476
478
( status = 500 , description = "Unexpected server error" ) ,
477
479
) ,
478
480
security( ( "netscan_token" = [ ] ) ) ,
479
481
) ) ]
480
- pub async fn get_net_config ( _token : crate :: extract:: NetScanToken ) -> Result < Json < Vec < NetworkInterface > > , HttpError > {
481
- let interfaces = interfaces:: get_network_interfaces ( )
482
- . map_err ( HttpError :: internal ( ) . with_msg ( "failed to get network interfaces" ) . err ( ) ) ?
483
- . into_iter ( )
484
- . map ( NetworkInterface :: from)
485
- . collect ( ) ;
486
-
487
- Ok ( Json ( interfaces) )
488
- }
482
+ pub ( crate ) async fn get_net_config (
483
+ _token : crate :: extract:: NetScanToken ,
484
+ ) -> Result < Json < HashMap < String , Vec < InterfaceInfo > > > , HttpError > {
485
+ let net_ifaces = interfaces:: get_network_interfaces ( )
486
+ . map_err ( HttpError :: internal ( ) . with_msg ( "failed to get network interfaces" ) . err ( ) ) ?;
489
487
490
- #[ cfg_attr( feature = "openapi" , derive( utoipa:: ToSchema ) ) ]
491
- #[ derive( Debug , Clone , Serialize ) ]
492
- pub struct NetworkInterface {
493
- pub name : String ,
494
- #[ serde( rename = "addresses" ) ]
495
- pub addrs : Vec < Addr > ,
496
- #[ serde( skip_serializing_if = "Option::is_none" ) ]
497
- pub mac_addr : Option < String > ,
498
- pub index : u32 ,
499
- }
488
+ let mut interface_map = HashMap :: new ( ) ;
500
489
501
- /// Network interface address
502
- #[ cfg_attr( feature = "openapi" , derive( utoipa:: ToSchema ) ) ]
503
- #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , Serialize ) ]
504
- pub enum Addr {
505
- V4 ( V4IfAddr ) ,
506
- V6 ( V6IfAddr ) ,
507
- }
508
-
509
- #[ cfg_attr( feature = "openapi" , derive( utoipa:: ToSchema ) ) ]
510
- #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
511
- pub struct Netmask < T > ( pub T ) ;
490
+ for iface in net_ifaces {
491
+ let addresses: Vec < InterfaceInfo > = iface
492
+ . addr
493
+ . into_iter ( )
494
+ . map ( |addr| match addr {
495
+ interfaces:: Addr :: V4 ( addr) => InterfaceInfo {
496
+ address : IfAddress :: V4 {
497
+ address : addr. ip ,
498
+ broadcast : addr. broadcast ,
499
+ netmask : addr. netmask ,
500
+ } ,
501
+ mac : iface. mac_addr . clone ( ) ,
502
+ } ,
503
+ interfaces:: Addr :: V6 ( addr) => InterfaceInfo {
504
+ address : IfAddress :: V6 {
505
+ address : addr. ip ,
506
+ broadcast : addr. broadcast ,
507
+ netmask : addr. netmask ,
508
+ } ,
509
+ mac : iface. mac_addr . clone ( ) ,
510
+ } ,
511
+ } )
512
+ . collect ( ) ;
512
513
513
- impl < T : fmt:: Display > fmt:: Display for Netmask < T > {
514
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
515
- write ! ( f, "{}" , self . 0 )
514
+ interface_map. insert ( iface. name , addresses) ;
516
515
}
517
- }
518
516
519
- impl < T > Serialize for Netmask < T >
520
- where
521
- T : Serialize ,
522
- {
523
- fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
524
- where
525
- S : serde:: Serializer ,
526
- {
527
- self . 0 . serialize ( serializer)
528
- }
517
+ Ok ( Json ( interface_map) )
529
518
}
530
519
531
- #[ cfg_attr( feature = "openapi" , derive( utoipa:: ToSchema ) ) ]
532
- #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , Serialize ) ]
533
- pub struct V4IfAddr {
534
- pub ip : Ipv4Addr ,
535
- #[ serde( skip_serializing_if = "Option::is_none" ) ]
536
- pub broadcast : Option < Ipv4Addr > ,
520
+ #[ derive( Debug , Clone , Serialize ) ]
521
+ pub ( crate ) struct InterfaceInfo {
522
+ #[ serde( flatten) ]
523
+ address : IfAddress ,
537
524
#[ serde( skip_serializing_if = "Option::is_none" ) ]
538
- pub netmask : Option < Netmask < Ipv4Addr > > ,
525
+ mac : Option < String > ,
539
526
}
540
527
541
- #[ cfg_attr( feature = "openapi" , derive( utoipa:: ToSchema ) ) ]
542
528
#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , Serialize ) ]
543
- pub struct V6IfAddr {
544
- pub ip : Ipv6Addr ,
545
- #[ serde( skip_serializing_if = "Option::is_none" ) ]
546
- pub broadcast : Option < Ipv6Addr > ,
547
- #[ serde( skip_serializing_if = "Option::is_none" ) ]
548
- pub netmask : Option < Netmask < Ipv6Addr > > ,
549
- }
550
-
551
- impl From < interfaces:: NetworkInterface > for NetworkInterface {
552
- fn from ( iface : interfaces:: NetworkInterface ) -> Self {
553
- let addr = iface
554
- . addr
555
- . into_iter ( )
556
- . map ( |addr| match addr {
557
- interfaces:: Addr :: V4 ( v4) => Addr :: V4 ( V4IfAddr {
558
- ip : v4. ip ,
559
- broadcast : v4. broadcast ,
560
- netmask : v4. netmask . map ( Netmask ) ,
561
- } ) ,
562
- interfaces:: Addr :: V6 ( v6) => Addr :: V6 ( V6IfAddr {
563
- ip : v6. ip ,
564
- broadcast : v6. broadcast ,
565
- netmask : v6. netmask . map ( Netmask ) ,
566
- } ) ,
567
- } )
568
- . collect ( ) ;
569
-
570
- NetworkInterface {
571
- name : iface. name ,
572
- mac_addr : iface. mac_addr ,
573
- addrs : addr,
574
- index : iface. index ,
575
- }
576
- }
529
+ #[ serde( tag = "family" ) ]
530
+ enum IfAddress {
531
+ #[ serde( rename = "IPv4" ) ]
532
+ V4 {
533
+ address : Ipv4Addr ,
534
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
535
+ broadcast : Option < Ipv4Addr > ,
536
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
537
+ netmask : Option < Ipv4Addr > ,
538
+ } ,
539
+ #[ serde( rename = "IPv6" ) ]
540
+ V6 {
541
+ address : Ipv6Addr ,
542
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
543
+ broadcast : Option < Ipv6Addr > ,
544
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
545
+ netmask : Option < Ipv6Addr > ,
546
+ } ,
577
547
}
0 commit comments