gapmind/src/services/routing/bgp.service.ts
2026-09-11 23:59:05 -04:00

32 lines
880 B
TypeScript

import { Injectable } from '@nestjs/common';
import { HttpService } from '../http.service';
export interface BgpRoute {
asn?: string;
prefix?: string;
holder?: string;
announced: boolean;
source: string;
}
@Injectable()
export class BgpService {
constructor(private readonly http: HttpService) {}
async lookup(ip: string): Promise<BgpRoute | undefined> {
const response = await this.http.json<{
data?: { asns?: string[]; prefix?: string; holder?: string };
}>(
`https://stat.ripe.net/data/network-info/data.json?resource=${encodeURIComponent(ip)}`,
);
const data = response.data;
if (!data?.prefix && !data?.asns?.length) return undefined;
return {
asn: data.asns?.[0],
prefix: data.prefix,
holder: data.holder,
announced: Boolean(data.asns?.length),
source: 'RIPEstat network-info',
};
}
}