55using BGPLite . Api . Entities ;
66using BGPLite . Configuration ;
77using BGPLite . Protocol ;
8+ using BGPLite . Providers ;
89using BGPLite . Routing ;
910using BGPLite . Server ;
1011using Microsoft . Extensions . Hosting ;
@@ -19,6 +20,7 @@ public sealed class ManagementApi : IHostedService, IDisposable
1920 private readonly AppConfig _config ;
2021 private readonly BgpMetrics _metrics ;
2122 private readonly IPrefixService ? _prefixService ;
23+ private readonly IPrefixSourceService ? _prefixSources ;
2224 private readonly ISessionManager ? _sessionManager ;
2325 private readonly ILogger < ManagementApi > _logger ;
2426 private readonly int _port ;
@@ -33,13 +35,15 @@ public ManagementApi(
3335 BgpMetrics metrics ,
3436 ILogger < ManagementApi > logger ,
3537 IPrefixService ? prefixService = null ,
38+ IPrefixSourceService ? prefixSources = null ,
3639 ISessionManager ? sessionManager = null )
3740 {
3841 _store = store ;
3942 _routeTable = routeTable ;
4043 _config = config ;
4144 _metrics = metrics ;
4245 _prefixService = prefixService ;
46+ _prefixSources = prefixSources ;
4347 _sessionManager = sessionManager ;
4448 _logger = logger ;
4549 _port = config . ApiPort ;
@@ -267,7 +271,7 @@ private ApiResponse HandleGetMe(HttpListenerContext ctx)
267271 lists = subscriptions ,
268272 customPrefixes ,
269273 customAsns ,
270- communities = communities . Select ( CommunityToString ) ,
274+ communities = communities . Select ( CommunityCodec . Format ) ,
271275 allRoutes = communities . Count == 0
272276 }
273277 } ) ;
@@ -357,7 +361,7 @@ private ApiResponse HandleGetPeer(string peerId)
357361 lists = subscriptions ,
358362 customPrefixes ,
359363 customAsns ,
360- communities = communities . Select ( CommunityToString ) ,
364+ communities = communities . Select ( CommunityCodec . Format ) ,
361365 allRoutes = communities . Count == 0
362366 } ) ;
363367 }
@@ -521,6 +525,26 @@ private async Task<ApiResponse> HandleGetAsnListsAsync()
521525 } ) ;
522526 }
523527
528+ // Append configured PrefixSources (file/http) alongside the legacy RipeStat ASN-lists,
529+ // reusing the same response shape. "Kind" is intentionally not exposed.
530+ if ( _prefixSources is not null )
531+ {
532+ var seen = lists . Select ( l => l . Name ) . ToHashSet ( ) ;
533+ foreach ( var ( source , prefixes ) in await _prefixSources . LoadAllAsync ( ) )
534+ {
535+ if ( ! seen . Add ( source . Name ) ) continue ; // skip names already present (e.g. shared "ru")
536+ result . Add ( new
537+ {
538+ id = source . Name ,
539+ Name = source . Name ,
540+ Description = source . Description ,
541+ Country = ( string ? ) null ,
542+ prefixCount = prefixes . Count ,
543+ type = "list"
544+ } ) ;
545+ }
546+ }
547+
524548 return ApiResponse . Ok ( result ) ;
525549 }
526550
@@ -548,7 +572,7 @@ private ApiResponse HandleGetRoutes()
548572 ? [ ( community : 0u , route : r ) ]
549573 : r . Communities . Select ( c => ( community : c , route : r ) ) )
550574 . GroupBy ( x => x . community )
551- . ToDictionary ( g => g . Key == 0 ? "default" : CommunityToString ( g . Key ) , g => g . Count ( ) ) ;
575+ . ToDictionary ( g => g . Key == 0 ? "default" : CommunityCodec . Format ( g . Key ) , g => g . Count ( ) ) ;
552576
553577 return ApiResponse . Ok ( new { total = routes . Count , byCommunity } ) ;
554578 }
@@ -602,19 +626,6 @@ private static string GetClientIp(HttpListenerContext ctx)
602626 return ctx . Request . RemoteEndPoint ? . Address . ToString ( ) ?? "unknown" ;
603627 }
604628
605- private static uint ParseCommunity ( string community )
606- {
607- var colon = community . IndexOf ( ':' ) ;
608- var asn = uint . Parse ( community [ ..colon ] ) ;
609- var value = uint . Parse ( community [ ( colon + 1 ) ..] ) ;
610- return ( asn << 16 ) | ( value & 0xFFFF ) ;
611- }
612-
613- private static string CommunityToString ( uint community )
614- {
615- return $ "{ community >> 16 } :{ community & 0xFFFF } ";
616- }
617-
618629 private static async Task WriteResponse ( HttpListenerContext ctx , ApiResponse response )
619630 {
620631 ctx . Response . StatusCode = response . StatusCode ;
0 commit comments